GoWind 开源生态GoWind 开源生态
首页
框架
GoWind Admin
GoWind CMS
GoWind IM
GoWind UBA
GoWind IoT
GoWind Toolkit
GoWind Quant
GitHub
首页
框架
GoWind Admin
GoWind CMS
GoWind IM
GoWind UBA
GoWind IoT
GoWind Toolkit
GoWind Quant
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap CLI 工具
    • Bootstrap 实战示例
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

指标监控插件

go-wind-plugins 提供统一的指标采集和导出接口,支持 Prometheus、Datadog 等。

一、Metrics 接口

type Metrics interface {
    Counter(name string, tags ...Tag) Counter
    Gauge(name string, tags ...Tag) Gauge
    Histogram(name string, tags ...Tag) Histogram
    Timer(name string, tags ...Tag) Timer
}

二、适配器列表

适配器导入路径后端
Prometheusplugins/metrics/prometheusPrometheus + Grafana
Datadogplugins/metrics/datadogDatadog Agent
StatsDplugins/metrics/statsdStatsD / DogStatsD
OTLPplugins/metrics/otlpOpenTelemetry Collector

三、Prometheus

import _ "github.com/tx7do/go-wind-plugins/metrics/prometheus"

YAML 配置

metrics:
  prometheus:
    enabled: true
    path: /metrics          # 采集路径
    namespace: myapp        # 指标前缀
    subsystem: api
    labels:
      service: my-service
      env: production

自定义指标

import "github.com/tx7do/go-wind/metrics"

// Counter(计数器)
requestCount := metrics.Counter("requests_total",
    metrics.Tag("method", "GET"),
    metrics.Tag("path", "/api/users"),
)
requestCount.Inc()

// Gauge(瞬时值)
activeConnections := metrics.Gauge("active_connections")
activeConnections.Set(42)
activeConnections.Inc()
activeConnections.Dec()

// Histogram(直方图)
latency := metrics.Histogram("request_duration_seconds",
    metrics.Tag("path", "/api/users"),
)
latency.Observe(0.123)  // 记录耗时

// Timer(计时器)
timer := metrics.Timer("db_query_duration")
timer.Start()
// ... 执行数据库查询
timer.Stop()

暴露 /metrics 端点

Prometheus 插件自动在 HTTP Server 上注册 /metrics 路径:

// 无需额外代码
// Prometheus 插件 init() 自动注册 /metrics handler
// Prometheus Server 通过 http://localhost:8080/metrics 采集

Prometheus 采集配置

# prometheus.yml
scrape_configs:
  - job_name: 'my-service'
    scrape_interval: 15s
    static_configs:
      - targets: ['localhost:8080']

四、Datadog

import _ "github.com/tx7do/go-wind-plugins/metrics/datadog"

YAML 配置

metrics:
  datadog:
    enabled: true
    addr: "localhost:8125"       # DogStatsD 地址
    namespace: myapp
    tags:
      - "service:my-service"
      - "env:production"
    flush_interval: 10s

五、常用指标模式

5.1 HTTP 请求监控

func MetricsMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        start := time.Now()

        next.ServeHTTP(w, r)

        duration := time.Since(start).Seconds()
        status := strconv.Itoa(w.(*responseWriter).statusCode)

        metrics.Counter("http_requests_total",
            metrics.Tag("method", r.Method),
            metrics.Tag("path", r.URL.Path),
            metrics.Tag("status", status),
        ).Inc()

        metrics.Histogram("http_request_duration_seconds",
            metrics.Tag("method", r.Method),
            metrics.Tag("path", r.URL.Path),
        ).Observe(duration)
    })
}

5.2 业务指标

// 订单指标
metrics.Counter("orders_created_total",
    metrics.Tag("type", "vip"),
).Inc()

// 支付金额
metrics.Histogram("payment_amount",
    metrics.Tag("currency", "CNY"),
).Observe(99.9)

// 队列积压
metrics.Gauge("queue_size",
    metrics.Tag("queue", "order_events"),
).Set(float64(queue.Len()))

六、Grafana 面板

推荐 Grafana 面板配置:

面板PromQL类型
QPSrate(http_requests_total[5m])Graph
延迟 P99histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))Graph
错误率rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m])Graph
活跃连接active_connectionsGauge

相关文档

  • 链路追踪插件
  • 插件配置系统
  • 日志适配插件
Edit this page
Last Updated:: 6/21/26, 9:28 PM
Contributors: Bobo
Prev
限流插件
Next
AI 插件