opencensus-go
opencensus-go copied to clipboard
proposal: trace.Span: add default attributes
Hi,
It would be very useful to set a default set of attributes that are applied to all traces. For example, we would like to be able to add availability zone, environment, etc... that go out on all traces. I don't believe there is a way to do this? Or I am missing it?
Thanks!
I think this should be supported in the exporter configuration. When you register the exporter you pass a map of <key, value> for attributes that will be added to all the Spans.
I'd really like to have this feature, so I added a possible implementation. Not sure if the exporter config is the right place to put this. One caveat in this implementation is that the exporter attributes override normal span attributes in case of key conflicts. (Not a big issue for my use case but maybe for others)
Once way to do this without adding the functionality to every exporter would be to implement a wrapper exporter. Perhaps we could add this wrapper to an exporters/exporterutil
package?
So something like:
enhancer := exporterutil.NewSpanEnhancer(myExporter)
enhancer.DefaultAttributes = append(wrapper.DefaultAttributes, trace.StringAttribute("xxx", "xxx"), ...)
enhancer.SpanNameTemplate = "{{.SpanKind}} {{.Attribute "http.route"}}"
enhancer.ComputedAttributes = func(sd *trace.SpanData) []trace.Attribute {
dur := sd.EndTime.Sub(sd.StartTime)
if dur > 5*time.Second {
return []trace.Attribute{ trace.BoolAttribute("slow", true) }
}
return nil
}
trace.RegisterExporter(enhancer)
(Threw in a bunch of other random ideas that a span "enhancer" might want to do before the Span is exported)