prome

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

Prometheus 快速接入封装

支持框架
  • gin 框架
封装说明

此包封装了 3 个默认指标

  • 接口请求量计数
  • 接口请求耗时 P99, P90, P70, P50
  • 应用业务异常计数

其中接口请求量计数和接口请求耗时指标通过中间件接入,应用业务异常计数需要在业务代码中进行调用。

gin框架接入

第一步:在入口函数初始化 Prometheus 对象, 传入一个参数,建议是应用名称。

注意: 初始化时应用应用名称只能是 [A-Za-z1-9_] 不能使用中划线 -

prome.NewPrometheus("gin_demo")

注册 gin 服务后添加中间件即可开启接口请求量计数和应用业务异常计数指标

r := gin.Default()
prome.Use(r)

业务服务报错指标需要在需要的位置自行添加计数埋点

// 业务报错计数指标
prome.PromeHandler().IncGinServiceErr(c,"testSrv")

一个 gin 完整接入示例:

package main

import (
	"github.com/gin-gonic/gin"
	"gitee.com/zhucheer/prome"
	"github.com/prometheus/client_golang/prometheus"
)

func main() {

	promeIns:=prome.NewPrometheus("gin_demo").
		RegisterCustomerMetrics("customer","customer metrics", prome.CounterVec, []string{"label1","label2"})

	r := gin.Default()
	promeIns.Use(r)
	r.GET("/ping", func(c *gin.Context) {
		// 自定义指标
		metrix:=prome.PromeHandler().PromeMetrics("customer").(*prometheus.CounterVec)
		metrix.WithLabelValues("srvname1","/ping").Inc()

		// 业务报错计数指标
		prome.PromeHandler().IncGinServiceErr(c,"testSrv")

		c.JSON(200, gin.H{
			"message": "pong",
		})
	})


	r.Run(":8001")
}

Documentation

Index

Constants

View Source
const (
	CounterVec   = "counter_vec"
	Counter      = "counter"
	GaugeVec     = "gauge_vec"
	Gauge        = "gauge"
	HistogramVec = "histogram_vec"
	Histogram    = "histogram"
	SummaryVec   = "summary_vec"
	Summary      = "summary"
)

Variables

This section is empty.

Functions

func NewMetric

func NewMetric(m *Metric, subsystem string) prometheus.Collector

NewMetric associates prometheus.Collector based on Metric.Type

func PrometheusHttpHandler

func PrometheusHttpHandler() http.Handler

PrometheusHandler metrics指标httpHandler

Types

type Metric

type Metric struct {
	MetricCollector prometheus.Collector
	ID              string
	Name            string
	Description     string
	Type            string
	Args            []string
}

Metric is a definition for the name, description, type, ID, and prometheus.Collector type (i.e. CounterVec, Summary, etc) of each metric

type PromeMiddleware

type PromeMiddleware struct {
	PromeHandler *Prometheus
}

func NewPromeMiddleware

func NewPromeMiddleware(p *Prometheus) *PromeMiddleware

func (PromeMiddleware) Func

Func implements Middleware interface.

type Prometheus

type Prometheus struct {
	MetricsList []*Metric
	MetricsPath string
	// contains filtered or unexported fields
}

Prometheus contains the metrics gathered by the instance and its path

func NewPrometheus

func NewPrometheus(subsystem string) *Prometheus

NewPrometheus generates a new set of metrics with a certain subsystem name

func PromeHandler

func PromeHandler() *Prometheus

func (*Prometheus) IncGinServiceErr

func (p *Prometheus) IncGinServiceErr(ctx *gin.Context, serviceName string)

服务异常报错 Gin 框架

[]string{"method", "srv_name", "handler"}}

func (*Prometheus) IncOrangeServiceErr

func (p *Prometheus) IncOrangeServiceErr(ctx *app.Context, serviceName string)

服务异常报错 orange框架

[]string{"method", "srv_name", "handler"}}

func (*Prometheus) MiddlewareGinHandlerFunc

func (p *Prometheus) MiddlewareGinHandlerFunc() gin.HandlerFunc

MiddlewareGinHandlerFunc HandlerFunc defines handler function for middleware

func (*Prometheus) PromeMetrics

func (p *Prometheus) PromeMetrics(name string) prometheus.Collector

func (*Prometheus) RegisterCustomerMetrics

func (p *Prometheus) RegisterCustomerMetrics(name string, Desc string, MetrixType string, Args []string) *Prometheus

func (*Prometheus) Use

func (p *Prometheus) Use(e *gin.Engine)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL