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

Function handleHTTPGRPCRequestWithOpenTracing

middleware/http_tracing.go:211–253  ·  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

209}
210
211func handleHTTPGRPCRequestWithOpenTracing(ctx context.Context, req any, httpRequest *http.Request, router *mux.Router, handler grpc.UnaryHandler) (any, error) {
212 tracer := opentracing.GlobalTracer()
213 parentSpan := opentracing.SpanFromContext(ctx)
214
215 // extract relevant span & tag data from request
216 method := httpRequest.Method
217 routeName := getRouteName(router, httpRequest)
218 urlPath := httpRequest.URL.Path
219 userAgent := httpRequest.Header.Get("User-Agent")
220
221 // tag parent httpgrpc.HTTP/Handle server span, if it exists
222 if parentSpan != nil {
223 parentSpan.SetTag(string(ext.HTTPUrl), urlPath)
224 parentSpan.SetTag(string(ext.HTTPMethod), method)
225 parentSpan.SetTag("http.route", routeName)
226 parentSpan.SetTag("http.user_agent", userAgent)
227 }
228
229 // create and start child HTTP span
230 // mirroring opentracing-contrib/go-stdlib/nethttp.Middleware span name and tags
231 childSpanName := getOperationName(routeName, httpRequest)
232 startSpanOpts := []opentracing.StartSpanOption{
233 ext.SpanKindRPCServer,
234 opentracing.Tag{Key: string(ext.Component), Value: "net/http"},
235 opentracing.Tag{Key: string(ext.HTTPUrl), Value: urlPath},
236 opentracing.Tag{Key: string(ext.HTTPMethod), Value: method},
237 opentracing.Tag{Key: "http.route", Value: routeName},
238 opentracing.Tag{Key: "http.user_agent", Value: userAgent},
239 }
240 if parentSpan != nil {
241 startSpanOpts = append(
242 startSpanOpts,
243 opentracing.SpanReference{
244 Type: opentracing.ChildOfRef,
245 ReferencedContext: parentSpan.Context(),
246 })
247 }
248
249 childSpan := tracer.StartSpan(childSpanName, startSpanOpts...)
250 defer childSpan.Finish()
251 ctx = opentracing.ContextWithSpan(ctx, childSpan)
252 return handler(ctx, req)
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

Callers 1

Calls 7

getRouteNameFunction · 0.85
getOperationNameFunction · 0.85
StartSpanMethod · 0.80
GetMethod · 0.65
SetTagMethod · 0.45
ContextMethod · 0.45
FinishMethod · 0.45

Tested by

no test coverage detected