Django middleware: allow autogenerated span name override#757
Django middleware: allow autogenerated span name override#757mhindery wants to merge 3 commits intocensus-instrumentation:masterfrom
Conversation
|
Our usage is that we're using the package for tracing with a set of service in Python & Go, and sending the traces to StackDriver.
tracingWrapper := func(handler http.Handler) http.Handler {
incomingSpanNamer := func(req *http.Request) string {
return fmt.Sprintf("GoAPI.%s: %s", deployment, req.URL.Path)
}
return &ochttp.Handler{
Propagation: &propagation.HTTPFormat{},
Handler: handler,
FormatSpanName: incomingSpanNamer,
}
}I fully agree to have the default behavior not changed following the spec, but having a hook to allow customization like Go offers, when desired for somebody's use-case seems good. |
|
Thanks for the background. Now I understand the scenario. I think what you're looking for is something that can be done at exporter level (e.g. If Stackdriver wants to format the span names in a different way, the exporter |
|
If you start a span yourself with Having it 'hardcoded' to a view is very restrictive, especially since with the dynamic nature of python & django views; you can have several structures and hierarchies where the initial View name isn't necessarily what you want as a representation. A concrete example from one of the codebases I'm working with is # urls.py file
url(r'^api/v3/endpoint_cats/?', ApiView.as_view(input_generator=CatInputGenerator, <other_args_here>), name="apiv3_cats"),
url(r'^api/v3/endpoint_dogs/?', ApiView.as_view(input_generator=DogInputGenerator, <other_args_here>), name="apiv3_dogs"),
url(r'^api/v3/endpoint_birds/?', ApiView.as_view(input_generator=BirdsInputGenerator, <other_args_here>), name="apiv3_birds"),
# a lot of other url entries ...In this case |
songy23
left a comment
There was a problem hiding this comment.
This PR looks to me more of an enhancement to a specific framework instrumentation (Django in this case). In general I agree that hard-coded span names in the instrumentation could be restrictive. We can allow users to customize names, while default settings should still follow the HTTP specs. In terms of Stackdriver Trace I agree that adding a prefix would help with navigation. WDYT @c24t ?
|
Closing this in favor of development on the opentelemetry project: open-telemetry/opentelemetry-python#571 |


In the Django Middleware, a span is created with an autogenerated name. The name setting is currently part of the
process_view()function which also contains other logic for the middleware functionality, so it's not easy / safe to override this method for the purpose of customizing the span naming. With the Django new-style middleware in the future, there won't be aprocess_view()method alltogether (seeopencensus-python/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py
Line 123 in 63e2c97
This PR adds a separate span naming method to the middleware as a hook to allow end-user customization. It's sole purpose is to generate a span name, which gives people an easy way to customize the naming convention by subclassing the middleware and overriding this single method without impacting the other functioning of the middleware (compatible with both this style and new-style middleware).