Skip to content

Commit 7dcdbf1

Browse files
committed
fix histogram metric boundaries
1 parent 6185db6 commit 7dcdbf1

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

example/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log"
77
"net/http"
88
"os"
9-
"time"
109

1110
"github.com/caarlos0/env/v11"
1211
"github.com/hasura/gotel"
@@ -40,7 +39,9 @@ func main() {
4039
defer ts.Shutdown(context.TODO())
4140

4241
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43-
time.Sleep(5 * time.Second)
42+
_, span := ts.Tracer.Start(r.Context(), "hello")
43+
defer span.End()
44+
4445
w.Write([]byte(fmt.Sprintf("%s %s", r.Method, r.URL.Path)))
4546
})
4647

middleware.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ func NewTracingMiddleware(
5050
"http.server.request.duration",
5151
metric.WithDescription("Duration of HTTP server requests"),
5252
metric.WithUnit("s"),
53+
metric.WithExplicitBucketBoundaries(
54+
0.005,
55+
0.01,
56+
0.025,
57+
0.05,
58+
0.075,
59+
0.1,
60+
0.25,
61+
0.5,
62+
0.75,
63+
1,
64+
2.5,
65+
5,
66+
7.5,
67+
10,
68+
),
5369
)
5470
if err != nil {
5571
panic(fmt.Errorf("failed to create http.server.request.duration metric: %w", err))
@@ -133,8 +149,10 @@ func (tm *tracingMiddleware) ServeHTTP( //nolint:gocognit,cyclop,funlen,maintidx
133149
}
134150

135151
requestID := getRequestID(r)
136-
logger := tm.Exporters.Logger.With(slog.String("request_id", requestID))
137-
httpLogger := logger.With(slog.String("type", "http-log"))
152+
logger := tm.Exporters.Logger.With(
153+
slog.String("request_id", requestID),
154+
slog.String("type", "http-log"),
155+
)
138156
isDebug := logger.Enabled(ctx, slog.LevelDebug)
139157

140158
if tm.Options.CustomAttributesFunc != nil {
@@ -253,7 +271,7 @@ func (tm *tracingMiddleware) ServeHTTP( //nolint:gocognit,cyclop,funlen,maintidx
253271

254272
if statusCode >= http.StatusBadRequest {
255273
span.SetStatus(codes.Error, description)
256-
httpLogger.LogAttrs(ctx, slog.LevelError, description, logAttrs...)
274+
logger.LogAttrs(ctx, slog.LevelError, description, logAttrs...)
257275

258276
return
259277
}
@@ -273,7 +291,7 @@ func (tm *tracingMiddleware) ServeHTTP( //nolint:gocognit,cyclop,funlen,maintidx
273291
logger.LogAttrs(ctx, successLevel, http.StatusText(statusCode), logAttrs...)
274292
}
275293

276-
if isDebug && r.Body != nil &&
294+
if isDebug && r.Body != nil && r.Body != http.NoBody &&
277295
otelutils.IsContentTypeDebuggable(r.Header.Get(contentTypeHeader)) {
278296
bodyStr, err := debugRequestBody(ww, r, logger)
279297
if err != nil {

0 commit comments

Comments
 (0)