GoWindGoWind
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
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 命令行集成
    • Bootstrap 实战示例
  • go-wind-flow 流程引擎

    • GoWind Flow 流程引擎
  • 教程

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

指标监控插件

go-wind-plugins 提供统一的指标采集和导出接口 metrics.Metrics,内置 Prometheus、OpenTelemetry、Datadog 三个插件。

一、Metrics 接口

type Metrics interface {
    // Counter increments a monotonically increasing counter by value.
    Counter(ctx context.Context, name string, value float64, labels map[string]string)

    // Histogram records an observation into a histogram distribution.
    Histogram(ctx context.Context, name string, value float64, labels map[string]string)

    // Gauge sets the current value of a gauge.
    Gauge(ctx context.Context, name string, value float64, labels map[string]string)
}

// Closer extends Metrics with graceful shutdown.
type Closer interface {
    Close() error
}

业务代码只依赖接口,具体实现(Prometheus / OTel / Datadog)按需注入。

二、适配器列表

适配器导入路径后端
Prometheusplugins/metrics/prometheusPrometheus client_golang
OpenTelemetryplugins/metrics/otelOTLP (gRPC/HTTP)
Datadogplugins/metrics/datadogDogStatsD

三、Prometheus

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

provider, _ := prometheus.New(
    prometheus.WithNamespace("myapp"),
    prometheus.WithSubsystem("api"),
    // prometheus.WithRegistry(customRegistry),  // 注入自定义 Registerer
)
// 或使用默认全局 Registry:
// provider, _ := prometheus.NewWithDefaultRegistry()

记录指标

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

labels := map[string]string{"method": "GET", "path": "/api/users"}

// Counter:计数器累加
provider.Counter(ctx, "http_requests_total", 1, labels)

// Histogram:记录耗时观测
provider.Histogram(ctx, "http_request_duration_seconds", 0.123, labels)

// Gauge:设置瞬时值
provider.Gauge(ctx, "active_connections", 42, nil)

暴露 /metrics 端点

Provider 基于 prometheus client_golang,把 /metrics handler 挂到 HTTP Server 或使用 promhttp 暴露即可,Prometheus Server 通过 http://localhost:8080/metrics 采集。

Prometheus 采集配置

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

YAML 配置

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

四、OpenTelemetry

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

provider, _ := otel.New(
    otel.WithEndpoint("localhost:4317"),
    otel.WithServiceName("my-service"),
    otel.WithExportInterval(30*time.Second),
    // otel.WithHTTP(),          // OTLP HTTP 导出
    // otel.WithInsecure(true),
)

YAML 配置

metrics:
  otel:
    endpoint: "localhost:4317"
    service_name: my-service
    export_interval: 30s

五、Datadog

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

provider, _ := datadog.New(
    datadog.WithAddress("localhost:8125"),   // DogStatsD 地址
    datadog.WithNamespace("myapp"),
    datadog.WithFlushPeriod(10*time.Second),
    datadog.WithSampleRate(1.0),
)
defer provider.Close()

YAML 配置

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

六、常用指标模式

6.1 HTTP 请求监控

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

        next.ServeHTTP(w, r)

        labels := map[string]string{
            "method": r.Method,
            "path":   r.URL.Path,
            "status": strconv.Itoa(w.(*responseWriter).statusCode),
        }
        m.Counter(r.Context(), "http_requests_total", 1, labels)
        m.Histogram(r.Context(), "http_request_duration_seconds",
            time.Since(start).Seconds(), labels)
    })
}

6.2 业务指标

// 订单指标
m.Counter(ctx, "orders_created_total", 1,
    map[string]string{"type": "vip"})

// 支付金额
m.Histogram(ctx, "payment_amount", 99.9,
    map[string]string{"currency": "CNY"})

// 队列积压
m.Gauge(ctx, "queue_size", float64(queue.Len()),
    map[string]string{"queue": "order_events"})

七、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
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
限流插件
Next
AI 插件