GoWindGoWind
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
  • Introduction

    • GoWind Framework
    • Framework Architecture
  • go-wind Core

    • go-wind Core Framework
    • App Lifecycle Management
    • Context Propagation
    • Transport Abstraction
    • Log Facade Interface
  • go-wind-plugins

    • go-wind-plugins Plugin Overview
    • Plugin Configuration System
    • Plugin Registration Mechanism (SPI)
    • Logging Adapter Plugins
    • Transport Protocol Plugins
    • Message Middleware Plugins
    • Encoding Plugins
    • Security and Authentication Plugins
    • Distributed Tracing Plugins
    • Cache Plugins
    • Object Storage Plugins (OSS)
    • Rate Limiting Plugins
    • Metrics Plugins
    • AI Plugins
    • Workflow Plugins
    • Database and Cache Plugins
    • Bootstrap Integration and Gateway Plugins
    • Compression and Serialization Plugins
    • Template Rendering and Validation Plugins
  • go-wind-bootstrap

    • go-wind-bootstrap: The Declarative Bootstrap
    • The Bootstrap Configuration System
    • The Bootstrap SPI Mechanism
    • Declarative Middleware Orchestration
    • Bootstrap Command-Line Integration
    • Bootstrap Practical Examples
  • go-wind-flow Workflow Engine

    • GoWind Flow Process Engine
  • Tutorials

    • Quick Start Tutorial
    • Custom Plugin Development Tutorial
    • Multi-Protocol Listening Tutorial
    • Framework Migration Guide

Metrics Plugins

go-wind-plugins provides the unified metrics collection and export interface metrics.Metrics, with three built-in plugins: Prometheus, OpenTelemetry, and Datadog.

1. The Metrics Interface

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
}

Business code depends only on the interface; concrete implementations (Prometheus / OTel / Datadog) are injected as needed.

2. Adapter List

AdapterImport PathBackend
Prometheusplugins/metrics/prometheusPrometheus client_golang
OpenTelemetryplugins/metrics/otelOTLP (gRPC/HTTP)
Datadogplugins/metrics/datadogDogStatsD

3. Prometheus

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

provider, _ := prometheus.New(
    prometheus.WithNamespace("myapp"),
    prometheus.WithSubsystem("api"),
    // prometheus.WithRegistry(customRegistry),  // inject a custom Registerer
)
// Or use the default global registry:
// provider, _ := prometheus.NewWithDefaultRegistry()

Recording Metrics

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

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

// Counter: accumulate a counter
provider.Counter(ctx, "http_requests_total", 1, labels)

// Histogram: record a duration observation
provider.Histogram(ctx, "http_request_duration_seconds", 0.123, labels)

// Gauge: set an instantaneous value
provider.Gauge(ctx, "active_connections", 42, nil)

Exposing the /metrics Endpoint

The provider is based on prometheus client_golang; mount the /metrics handler onto an HTTP Server or expose it via promhttp, and the Prometheus Server scrapes it at http://localhost:8080/metrics.

Prometheus Scraping Configuration

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

YAML Configuration

metrics:
  prometheus:
    enabled: true
    path: /metrics          # scrape path
    namespace: myapp        # metric prefix
    subsystem: api
    labels:
      service: my-service
      env: production

4. 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 export
    // otel.WithInsecure(true),
)

YAML Configuration

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

5. Datadog

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

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

YAML Configuration

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

6. Common Metric Patterns

6.1 HTTP Request Monitoring

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 Business Metrics

// Order metrics
m.Counter(ctx, "orders_created_total", 1,
    map[string]string{"type": "vip"})

// Payment amount
m.Histogram(ctx, "payment_amount", 99.9,
    map[string]string{"currency": "CNY"})

// Queue backlog
m.Gauge(ctx, "queue_size", float64(queue.Len()),
    map[string]string{"queue": "order_events"})

7. Grafana Dashboards

Recommended Grafana dashboard configuration:

PanelPromQLType
QPSrate(http_requests_total[5m])Graph
P99 latencyhistogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))Graph
Error raterate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m])Graph
Active connectionsactive_connectionsGauge

Related Documents

  • Distributed Tracing Plugins
  • Plugin Configuration System
  • Logging Adapter Plugins
Edit this page
Last Updated:: 11/3/26, 2:14 PM
Contributors: Bobo
Prev
Rate Limiting Plugins
Next
AI Plugins