@@ -6,30 +6,31 @@ import (
6
6
"github.com/felixge/httpsnoop"
7
7
"go.inout.gg/foundations/http/httpmiddleware"
8
8
"go.inout.gg/foundations/must"
9
+ "go.opentelemetry.io/otel"
9
10
"go.opentelemetry.io/otel/attribute"
10
11
"go.opentelemetry.io/otel/metric"
11
12
)
12
13
13
- // Middleware returns a middleware that captures metrics for incoming HTTP requests.
14
- func Middleware (p metric.MeterProvider ) httpmiddleware.Middleware {
15
- meter := p .Meter ("foundations:httpmetrics" )
16
- requestDurationHisto := must .Must (
17
- meter .Int64Histogram (
18
- "request_duration_ms" ,
19
- metric .WithDescription ("The incoming request duration in milliseconds." ),
20
- metric .WithUnit ("ms" ),
21
- metric .WithExplicitBucketBoundaries (1 , 5 , 10 , 25 , 50 , 100 , 200 , 500 , 1_000 , 5_000 , 10_000 , 30_000 , 60_000 ),
22
- ),
23
- )
24
- responseBodySizeHisto := must .Must (
25
- meter .Int64Histogram (
26
- "response_body_size_bytes" ,
27
- metric .WithDescription ("The outgoing response body size in bytes." ),
28
- metric .WithUnit ("bytes" ),
29
- metric .WithExplicitBucketBoundaries (1 , 10 , 100 , 1_000 , 10_000 , 100_000 , 1_000_000 , 10_000_000 ),
30
- ),
31
- )
14
+ var (
15
+ provider = otel .GetMeterProvider ()
16
+ meter = provider .Meter ("foundations:httpmetrics" )
17
+
18
+ requestDurationHisto = must .Must (meter .Int64Histogram (
19
+ "request_duration_ms" ,
20
+ metric .WithDescription ("The incoming request duration in milliseconds." ),
21
+ metric .WithUnit ("ms" ),
22
+ metric .WithExplicitBucketBoundaries (1 , 5 , 10 , 25 , 50 , 100 , 200 , 500 , 1_000 , 5_000 , 10_000 , 30_000 , 60_000 ),
23
+ ))
24
+ responseBodySizeHisto = must .Must (meter .Int64Histogram (
25
+ "response_body_size_bytes" ,
26
+ metric .WithDescription ("The outgoing response body size in bytes." ),
27
+ metric .WithUnit ("bytes" ),
28
+ metric .WithExplicitBucketBoundaries (1 , 10 , 100 , 1_000 , 10_000 , 100_000 , 1_000_000 , 10_000_000 ),
29
+ ))
30
+ )
32
31
32
+ // Middleware returns a middleware that captures metrics for incoming HTTP requests.
33
+ func Middleware () httpmiddleware.Middleware {
33
34
return httpmiddleware .MiddlewareFunc (func (next http.Handler ) http.Handler {
34
35
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
35
36
ctx := r .Context ()
0 commit comments