metrics

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 8 Imported by: 4

README

gf-metrics

gf metrics exporter for Prometheus.

Introduction

gf-metrics 为 Gf HTTP 服务定义了一些监控指标,开箱即用。下边是默认监控指标的详细描述:

Metric Type Description
gf_request_total Counter 服务接收到的请求总数
gf_request_uv Counter 服务接收到的 IP 总数
gf_uri_request_total Counter 每个 URI 接收到的服务请求数
gf_request_body_total Counter 服务接收到的请求量,单位: 字节
gf_response_body_total Counter 服务返回的请求量,单位: 字节
gf_request_duration Histogram 服务处理请求使用的时间
gf_slow_request_total Counter 服务接收到的慢请求计数

Grafana

Set the grafana directory for details.

grafana

Installation

$ go get github.com/jxo-me/gf-metrics

Usage

使用如下代码运行,访问:http://localhost:8080/metrics 即可看到暴露出来的监控指标

package main

import (
	"github.com/gogf/gf/v2/frame/g"

	"github.com/jxo/gf-metrics/metrics"
)

func main() {
	s := g.Server()
	s.SetAddr(":8080")
	// get global Monitor object
	m := metrics.GetMonitor()
	// +optional set metric path, default /metrics
	m.SetMetricPath("/metrics")
	// +optional set slow time, default 5s
	m.SetSlowTime(10)
	// +optional set request duration, default {0.1, 0.3, 1.2, 5, 10}
	// used to p95, p99
	m.SetDuration([]float64{0.1, 0.3, 1.2, 5, 10})
	// set middleware for gf
	m.Use(s)
	r := s.Group("")
	r.GET("/hello", func(r *ghttp.Request) { r.Response.Write("Hello") })
	s.Run()
}

Custom Metric

gf-metric 提供了自定义监控指标的使用方式

Gauge

使用 Gauge 类型监控指标,可以通过 3 种方法来修改监控值:SetGaugeValueIncAdd

首先,需要定义一个 Gauge 类型的监控指标:

gaugeMetric := &metrics.Metric{
    Type:        metrics.Gauge,
    Name:        "example_gauge_metric",
    Description: "an example of gauge type metric",
    Labels:      []string{"label1"},
}

// Add metric to global monitor object
_ = metrics.GetMonitor().AddMetric(gaugeMetric)

SetGaugeValue

SetGaugeValue 方法会直接设置监控指标的值

_ = metrics.GetMonitor().GetMetric("example_gauge_metric").SetGaugeValue([]string{"label_value1"}, 0.1)

Inc

Inc 方法会在监控指标值的基础上增加 1

_ = metrics.GetMonitor().GetMetric("example_gauge_metric").Inc([]string{"label_value1"})

Add

Add 方法会为监控指标增加传入的值

_ = metrics.GetMonitor().GetMetric("example_gauge_metric").Add([]string{"label_value1"}, 0.2)
Counter

Counter 类型的监控指标,可以使用 IncAdd 方法,但是不能使用 SetGaugeValue 方法

Histogram and Summary

对于 HistogramSummary 类型的监控指标,需要用 Observe 方法来设置监控值。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Metric

type Metric struct {
	Type        MetricType
	Name        string
	Description string
	Labels      []string
	Buckets     []float64
	Objectives  map[float64]float64
	// contains filtered or unexported fields
}

Metric defines a metric object. Users can use it to save metric data. Every metric should be globally unique by name.

func (*Metric) Add

func (m *Metric) Add(labelValues []string, value float64) error

Add adds the given value to the Metric object. Only for Counter/Gauge type metric.

func (*Metric) Inc

func (m *Metric) Inc(labelValues []string) error

Inc increases value for Counter/Gauge type metric, increments the counter by 1

func (*Metric) Observe

func (m *Metric) Observe(labelValues []string, value float64) error

Observe is used by Histogram and Summary type metric to add observations.

func (*Metric) SetGaugeValue

func (m *Metric) SetGaugeValue(labelValues []string, value float64) error

SetGaugeValue set data for Gauge type Metric.

type MetricType

type MetricType int
const (
	None MetricType = iota
	Counter
	Gauge
	Histogram
	Summary

	DefaultMetricPath = "/metrics"
	DefaultSlowTime   = int32(5)
)

type Monitor

type Monitor struct {
	// contains filtered or unexported fields
}

Monitor is an object that uses to set Gf server monitor.

func GetMonitor

func GetMonitor() *Monitor

GetMonitor used to get global Monitor object, this function returns a singleton object.

func (*Monitor) AddMetric

func (m *Monitor) AddMetric(metric *Metric) error

AddMetric add custom monitor metric.

func (*Monitor) ApiExpose

func (m *Monitor) ApiExpose(r *ghttp.Request)

ApiExpose adds metric path to a given router. The router can be different with the one passed to UseWithoutExposingEndpoint. This allows to expose metrics on different port.

func (*Monitor) GetMetric

func (m *Monitor) GetMetric(name string) *Metric

GetMetric used to get metric object by metric_name.

func (*Monitor) InitMetrics

func (m *Monitor) InitMetrics() *Monitor

InitMetrics used to init Gf metrics

func (*Monitor) MetricHandle

func (m *Monitor) MetricHandle(r *ghttp.Request, start time.Time)

func (*Monitor) Middleware

func (m *Monitor) Middleware(r *ghttp.Request)

Middleware as Gf monitor middleware.

func (*Monitor) SetDuration

func (m *Monitor) SetDuration(duration []float64)

SetDuration set reqDuration property. reqDuration is used to ginRequestDuration metric buckets.

func (*Monitor) SetMetricPath

func (m *Monitor) SetMetricPath(path string)

SetMetricPath set metricPath property. metricPath is used for Prometheus to get Gf server monitoring data.

func (*Monitor) SetMetricPrefix

func (m *Monitor) SetMetricPrefix(prefix string)

func (*Monitor) SetMetricSuffix

func (m *Monitor) SetMetricSuffix(suffix string)

func (*Monitor) SetSlowTime

func (m *Monitor) SetSlowTime(slowTime int32)

SetSlowTime set slowTime property. slowTime is used to determine whether the request is slow. For "gf_slow_request_total" metric.

func (*Monitor) Use

func (m *Monitor) Use(s *ghttp.Server)

Use set Gf metrics middleware

func (*Monitor) UseWithoutExposingEndpoint

func (m *Monitor) UseWithoutExposingEndpoint(s *ghttp.Server)

UseWithoutExposingEndpoint is used to add monitor interceptor to Gf router It can be called multiple times to intercept from multiple gin.IRoutes http path is not set, to do that use Expose function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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