MCPcopy
hub / github.com/grafana/dskit / handleHTTPGRPCRequestWithOTel

Function handleHTTPGRPCRequestWithOTel

middleware/http_tracing.go:255–289  ·  view source on GitHub ↗
(ctx context.Context, req any, httpRequest *http.Request, router *mux.Router, handler grpc.UnaryHandler)

Source from the content-addressed store, hash-verified

253}
254
255func handleHTTPGRPCRequestWithOTel(ctx context.Context, req any, httpRequest *http.Request, router *mux.Router, handler grpc.UnaryHandler) (any, error) {
256 // extract relevant span & tag data from request
257 method := httpRequest.Method
258 routeName := getRouteName(router, httpRequest)
259 urlPath := httpRequest.URL.Path
260 userAgent := httpRequest.Header.Get("User-Agent")
261
262 parentSpan := trace.SpanFromContext(ctx)
263 if parentSpan.SpanContext().IsValid() {
264 parentSpan.SetAttributes(
265 semconv.HTTPRequestMethodKey.String(method),
266 semconv.HTTPRouteKey.String(routeName),
267 attribute.String("url.path", urlPath),
268 semconv.UserAgentOriginal(userAgent),
269 )
270 }
271 // create and start child HTTP span and set span name and attributes
272 childSpanName := getOperationName(routeName, httpRequest)
273
274 startSpanOpts := []trace.SpanStartOption{
275 trace.WithSpanKind(trace.SpanKindServer),
276 trace.WithAttributes(
277 semconv.HTTPRequestMethodKey.String(method),
278 semconv.HTTPRouteKey.String(routeName),
279 semconv.UserAgentOriginal(userAgent),
280 attribute.String("url.path", urlPath),
281 ),
282 }
283
284 var childSpan trace.Span
285 ctx, childSpan = tracer.Start(ctx, childSpanName, startSpanOpts...)
286 defer childSpan.End()
287
288 return handler(ctx, req)
289}
290
291func httpOperationName(r *http.Request) string {
292 routeName := ExtractRouteName(r.Context())

Callers 1

Calls 6

getRouteNameFunction · 0.85
getOperationNameFunction · 0.85
SpanContextMethod · 0.80
GetMethod · 0.65
StringMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected