@@ -3,7 +3,10 @@ package tracing
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "net/http"
8+ "os"
9+ "runtime"
710
811 "github.com/rs/zerolog"
912 otelContrib "go.opentelemetry.io/contrib/propagators/Jaeger"
@@ -22,26 +25,41 @@ const (
2225 service = "cloudflared"
2326 tracerInstrumentName = "origin"
2427
25- tracerContextName = "cf-trace-id"
26- tracerContextNameOverride = "uber-trace-id"
28+ TracerContextName = "cf-trace-id"
29+ TracerContextNameOverride = "uber-trace-id"
2730
2831 IntCloudflaredTracingHeader = "cf-int-cloudflared-tracing"
2932)
3033
3134var (
3235 CanonicalCloudflaredTracingHeader = http .CanonicalHeaderKey (IntCloudflaredTracingHeader )
33- Http2TransportAttribute = trace .WithAttributes (TransportAttributeKey .String ("http2" ))
34- QuicTransportAttribute = trace .WithAttributes (TransportAttributeKey .String ("quic" ))
36+ Http2TransportAttribute = trace .WithAttributes (transportAttributeKey .String ("http2" ))
37+ QuicTransportAttribute = trace .WithAttributes (transportAttributeKey .String ("quic" ))
38+ HostOSAttribute = semconv .HostTypeKey .String (runtime .GOOS )
39+ HostArchAttribute = semconv .HostArchKey .String (runtime .GOARCH )
3540
36- TransportAttributeKey = attribute .Key ("transport" )
37- TrafficAttributeKey = attribute .Key ("traffic" )
41+ otelVersionAttribute attribute.KeyValue
42+ hostnameAttribute attribute.KeyValue
43+ cloudflaredVersionAttribute attribute.KeyValue
44+ serviceAttribute = semconv .ServiceNameKey .String (service )
45+
46+ transportAttributeKey = attribute .Key ("transport" )
47+ otelVersionAttributeKey = attribute .Key ("jaeger.version" )
3848
3949 errNoopTracerProvider = errors .New ("noop tracer provider records no spans" )
4050)
4151
4252func init () {
4353 // Register the jaeger propagator globally.
4454 otel .SetTextMapPropagator (otelContrib.Jaeger {})
55+ otelVersionAttribute = otelVersionAttributeKey .String (fmt .Sprintf ("go-otel-%s" , otel .Version ()))
56+ if hostname , err := os .Hostname (); err == nil {
57+ hostnameAttribute = attribute .String ("hostname" , hostname )
58+ }
59+ }
60+
61+ func Init (version string ) {
62+ cloudflaredVersionAttribute = semconv .ProcessRuntimeVersionKey .String (version )
4563}
4664
4765type TracedRequest struct {
@@ -67,7 +85,12 @@ func NewTracedRequest(req *http.Request) *TracedRequest {
6785 // Record information about this application in a Resource.
6886 tracesdk .WithResource (resource .NewWithAttributes (
6987 semconv .SchemaURL ,
70- semconv .ServiceNameKey .String (service ),
88+ serviceAttribute ,
89+ otelVersionAttribute ,
90+ hostnameAttribute ,
91+ cloudflaredVersionAttribute ,
92+ HostOSAttribute ,
93+ HostArchAttribute ,
7194 )),
7295 )
7396
@@ -110,27 +133,28 @@ func EndWithStatus(span trace.Span, code codes.Code, status string) {
110133 span .End ()
111134}
112135
113- // extractTrace attempts to check for a cf-trace-id from a request header.
136+ // extractTrace attempts to check for a cf-trace-id from a request and return the
137+ // trace context with the provided http.Request.
114138func extractTrace (req * http.Request ) (context.Context , bool ) {
115139 // Only add tracing for requests with appropriately tagged headers
116- remoteTraces := req .Header .Values (tracerContextName )
140+ remoteTraces := req .Header .Values (TracerContextName )
117141 if len (remoteTraces ) <= 0 {
118142 // Strip the cf-trace-id header
119- req .Header .Del (tracerContextName )
143+ req .Header .Del (TracerContextName )
120144 return nil , false
121145 }
122146
123- traceHeader := make ( map [string ]string , 1 )
147+ traceHeader := map [string ]string {}
124148 for _ , t := range remoteTraces {
125149 // Override the 'cf-trace-id' as 'uber-trace-id' so the jaeger propagator can extract it.
126150 // Last entry wins if multiple provided
127- traceHeader [tracerContextNameOverride ] = t
151+ traceHeader [TracerContextNameOverride ] = t
128152 }
129153
130154 // Strip the cf-trace-id header
131- req .Header .Del (tracerContextName )
155+ req .Header .Del (TracerContextName )
132156
133- if traceHeader [tracerContextNameOverride ] == "" {
157+ if traceHeader [TracerContextNameOverride ] == "" {
134158 return nil , false
135159 }
136160 remoteCtx := otel .GetTextMapPropagator ().Extract (req .Context (), propagation .MapCarrier (traceHeader ))
0 commit comments