1 简单示例

  • 下载包

go get github.com/prometheus/client_golang

1 定义指标

// 1. Counter(计数器):用于累计计数(如请求总数)  
var (  
    // NewCounterVec 支持标签(维度)  
    httpRequestsTotal = prometheus.NewCounterVec(  
       prometheus.CounterOpts{  
          Name: "myapp_http_requests_total",      // 指标名称  
          Help: "Total number of HTTP requests.", // 帮助信息  
       },  
       []string{"method", "path"}, // 标签名称列表(此处定义两个标签)  
    )  
)  
  
// 2. Gauge(仪表盘):表示可以增减的瞬时值(如内存使用量)  
var (  
    memoryUsageBytes = prometheus.NewGauge(  
       prometheus.GaugeOpts{  
          Name: "myapp_memory_usage_bytes",  
          Help: "Current memory usage in bytes.",  
       },  
    )  
)  
  
// 3. Histogram(直方图):用于统计数值分布(如请求延迟)  
var (  
    httpRequestDuration = prometheus.NewHistogramVec(  
       prometheus.HistogramOpts{  
          Name:    "myapp_http_request_duration_seconds",  
          Help:    "HTTP request latency distribution in seconds.",  
          Buckets: []float64{0.05, 0.1, 0.5, 1.0}, // 自定义分桶范围  
       },  
       []string{"method"}, // 标签名称列表  
    )  
)  
  
// 4. Summary(摘要):用于计算分位数(客户端计算)  
var (  
    responseSizes = prometheus.NewSummaryVec(  
       prometheus.SummaryOpts{  
          Name:       "myapp_response_sizes_bytes",  
          Help:       "Response size distribution in bytes.",  
          Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01}, // 分位数配置(中位数和90分位)  
       },  
       []string{"status"}, // 标签名称列表  
    )  
)

2 注册指标

在完成指标的定义后,还需要注册他们

3 业务逻辑

在完成以上步骤后,我们还需要模拟一下业务逻辑给指标赋值,且启动HTTP服务并暴露指标。

4 完整代码

最后更新于