Skip to content

Commit 7046036

Browse files
committed
feat: add a trace ID header
1 parent d0e9739 commit 7046036

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
1919
"go.opentelemetry.io/otel"
2020
"go.opentelemetry.io/otel/codes"
21+
"go.opentelemetry.io/otel/trace"
2122
"golang.org/x/sync/singleflight"
2223
)
2324

@@ -53,6 +54,13 @@ func main() {
5354
r.Use(p.Instrument())
5455
r.Use(otelgin.Middleware("sqlrunner"))
5556

57+
// Add a middleware to add the trace ID to the response header
58+
r.Use(func(c *gin.Context) {
59+
traceID := trace.SpanContextFromContext(c.Request.Context()).TraceID().String()
60+
c.Header("X-Trace-ID", traceID)
61+
c.Next()
62+
})
63+
5664
p.AddCustomCounter("query_requests_total", "The total number of SQL query requests.", []string{"code"})
5765
p.AddCustomHistogram("query_requests_duration_seconds", "The duration of each SQL query request.", []string{"code"})
5866

0 commit comments

Comments
 (0)