Skip to content

Commit

Permalink
Update 05-metrics.md (#222)
Browse files Browse the repository at this point in the history
* Update 05-metrics.md

upgrade to mention how to export metrics to prometheus in kratos >= 2.8.0

* Update 05-metrics.md

upgrade to mention how to export metrics to prometheus in kratos >= 2.8.0
  • Loading branch information
TeCHiScy authored Oct 21, 2024
1 parent 09ce03e commit 4e4b445
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 9 deletions.
97 changes: 92 additions & 5 deletions docs/component/middleware/05-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func WithRequests(c metrics.Counter) Option {

用于设置 metrics 中间件统计请求计数的 `Counter` 计数器。

### 使用方式
### 使用方式 (kratos < 2.8.0)

#### 使用 prometheus
```go
// Detailed reference https://github.com/go-kratos/examples/tree/main/metrics
// 详见 https://github.com/go-kratos/examples/tree/main/metrics

_metricSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "server",
Expand All @@ -62,8 +62,6 @@ _metricRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
}, []string{"kind", "operation", "code", "reason"})

prometheus.MustRegister(_metricSeconds, _metricRequests)

httpSrv.Handle("/metrics", promhttp.Handler())
```
#### Server 中使用 metrics

Expand Down Expand Up @@ -93,6 +91,7 @@ httpSrv := http.NewServer(
),
),
)
httpSrv.Handle("/metrics", promhttp.Handler())
```

#### Client 中使用 metrics
Expand Down Expand Up @@ -123,9 +122,97 @@ conn, err := http.NewClient(
)
```

### 使用方式 (kratos >= 2.8.0)
kratos 从 [v2.8.0](https://github.com/go-kratos/kratos/releases/tag/v2.8.0) 开始使用 otel.Metrics,需要用以下方法 export 数据到 prometheus。

#### 使用 prometheus
```go
import (
"github.com/go-kratos/kratos/v2/middleware/metrics"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
)

### References
// Detailed reference https://github.com/go-kratos/examples/tree/main/metrics
func init() {
exporter, err := prometheus.New()
if err != nil {
panic(err)
}
provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(exporter))
meter := provider.Meter(Name)

_metricRequests, err = metrics.DefaultRequestsCounter(meter, metrics.DefaultServerRequestsCounterName)
if err != nil {
panic(err)
}

_metricSeconds, err = metrics.DefaultSecondsHistogram(meter, metrics.DefaultServerSecondsHistogramName)
if err != nil {
panic(err)
}
}
```

#### Server 中使用 metrics
```go
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// grpc service
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
metrics.Server(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)

// http service
httpSrv := http.NewServer(
http.Address(":8000"),
http.Middleware(
metrics.Server(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)
httpSrv.Handle("/metrics", promhttp.Handler())
```

#### Client 中使用 metrics
```go
// grpc client
conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("127.0.0.1:9000"),
grpc.WithMiddleware(
metrics.Client(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)

// http client
conn, err := http.NewClient(
context.Background(),
http.WithEndpoint("127.0.0.1:8000"),
http.WithMiddleware(
metrics.Client(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)
```

### References
* https://prometheus.io/docs/concepts/metric_types/
* https://github.com/go-kratos/examples/tree/main/metrics
* https://pkg.go.dev/go.opentelemetry.io/otel/exporters/prometheus
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func WithRequests(c metrics.Counter) Option {

The `Counter` counter used to set the metrics middleware statistics request count.

### Usage
### Usage (kratos < 2.8.0)

#### Prometheus
```go
Expand All @@ -62,8 +62,6 @@ _metricRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
}, []string{"kind", "operation", "code", "reason"})

prometheus.MustRegister(_metricSeconds, _metricRequests)

httpSrv.Handle("/metrics", promhttp.Handler())
```
#### To configure metrics in servers

Expand Down Expand Up @@ -93,6 +91,7 @@ httpSrv := http.NewServer(
),
),
)
httpSrv.Handle("/metrics", promhttp.Handler())
```

#### To configure metrics in clients
Expand Down Expand Up @@ -123,9 +122,98 @@ conn, err := http.NewClient(
)
```

### Usage (kratos >= 2.8.0)

Since version [v2.8.0](https://github.com/go-kratos/kratos/releases/tag/v2.8.0), kratos uses otel.Metrics. Way to export metrics to prometheus is as follows:

### References
#### Prometheus
```go
import (
"github.com/go-kratos/kratos/v2/middleware/metrics"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
)

// Detailed reference https://github.com/go-kratos/examples/tree/main/metrics
func init() {
exporter, err := prometheus.New()
if err != nil {
panic(err)
}
provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(exporter))
meter := provider.Meter(Name)

_metricRequests, err = metrics.DefaultRequestsCounter(meter, metrics.DefaultServerRequestsCounterName)
if err != nil {
panic(err)
}

_metricSeconds, err = metrics.DefaultSecondsHistogram(meter, metrics.DefaultServerSecondsHistogramName)
if err != nil {
panic(err)
}
}
```

#### To configure metrics in servers
```go
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// grpc service
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
metrics.Server(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)

// http service
httpSrv := http.NewServer(
http.Address(":8000"),
http.Middleware(
metrics.Server(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)
httpSrv.Handle("/metrics", promhttp.Handler())
```

#### To configure metrics in clients
```go
// grpc client
conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("127.0.0.1:9000"),
grpc.WithMiddleware(
metrics.Client(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)

// http client
conn, err := http.NewClient(
context.Background(),
http.WithEndpoint("127.0.0.1:8000"),
http.WithMiddleware(
metrics.Client(
metrics.WithSeconds(_metricSeconds),
metrics.WithRequests(_metricRequests),
),
),
)
```

### References
* https://prometheus.io/docs/concepts/metric_types/
* https://github.com/go-kratos/examples/tree/main/metrics
* https://pkg.go.dev/go.opentelemetry.io/otel/exporters/prometheus

0 comments on commit 4e4b445

Please sign in to comment.