Debian Codegraph @

otel/internal/utils/spanattributes.go

raw
1 package utils
2
3 import (
4 "fmt"
5 "net/url"
6
7 "github.com/getsentry/sentry-go"
8 otelSdkTrace "go.opentelemetry.io/otel/sdk/trace"
9 semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
10 otelTrace "go.opentelemetry.io/otel/trace"
11 )
12
13 type SpanAttributes struct {
14 Op string
15 Description string
16 Source sentry.TransactionSource
17 }
18
19 func ParseSpanAttributes(s otelSdkTrace.ReadOnlySpan) SpanAttributes {
20 for _, attribute := range s.Attributes() {
21 if attribute.Key == semconv.HTTPMethodKey {
22 return descriptionForHttpMethod(s)
23 }
24 if attribute.Key == semconv.DBSystemKey {
25 return descriptionForDbSystem(s)
26 }
27 if attribute.Key == semconv.RPCSystemKey {
28 return SpanAttributes{
29 Op: "rpc",
30 Description: s.Name(),
31 Source: sentry.SourceRoute,
32 }
33 }
34 if attribute.Key == semconv.MessagingSystemKey {
35 return SpanAttributes{
36 Op: "messaging",
37 Description: s.Name(),
38 Source: sentry.SourceRoute,
39 }
40 }
41 // TODO(michi) Check if this works for AWS Lambda and such.
42 if attribute.Key == semconv.FaaSTriggerKey {
43 return SpanAttributes{
44 Op: attribute.Value.AsString(),
45 Description: s.Name(),
46 Source: sentry.SourceRoute,
47 }
48 }
49 }
50
51 return SpanAttributes{
52 Op: "", // becomes "default" in Relay
53 Description: s.Name(),
54 Source: sentry.SourceCustom,
55 }
56 }
57
58 func descriptionForDbSystem(s otelSdkTrace.ReadOnlySpan) SpanAttributes {
59 description := s.Name()
60 for _, attribute := range s.Attributes() {
61 if attribute.Key == semconv.DBStatementKey {
62 // TODO(michi)
63 // Note: The value may be sanitized to exclude sensitive information.
64 // See: https://pkg.go.dev/go.opentelemetry.io/otel/semconv/v1.12.0
65 description = attribute.Value.AsString()
66 break
67 }
68 }
69
70 return SpanAttributes{
71 Op: "db",
72 Description: description,
73 Source: sentry.SourceTask,
74 }
75 }
76
77 func descriptionForHttpMethod(s otelSdkTrace.ReadOnlySpan) SpanAttributes {
78 op := "http"
79 switch s.SpanKind() {
80 case otelTrace.SpanKindClient:
81 op = "http.client"
82 case otelTrace.SpanKindServer:
83 op = "http.server"
84 }
85
86 var httpTarget string
87 var httpRoute string
88 var httpMethod string
89 var httpUrl string
90
91 for _, attribute := range s.Attributes() {
92 switch attribute.Key {
93 case semconv.HTTPTargetKey:
94 httpTarget = attribute.Value.AsString()
95 case semconv.HTTPRouteKey:
96 httpRoute = attribute.Value.AsString()
97 case semconv.HTTPMethodKey:
98 httpMethod = attribute.Value.AsString()
99 case semconv.HTTPURLKey:
100 httpUrl = attribute.Value.AsString()
101 }
102 }
103
104 var httpPath string
105
106 // Prefer httpRoute if available
107 if httpRoute != "" {
108 httpPath = httpRoute
109 } else if httpTarget != "" {
110 if parsedUrl, err := url.Parse(httpTarget); err == nil {
111 // Do not include the query and fragment parts
112 httpPath = parsedUrl.Path
113 } else {
114 httpPath = httpTarget
115 }
116 } else if httpUrl != "" {
117 // This is normally the HTTP-client case
118 if parsedUrl, err := url.Parse(httpUrl); err == nil {
119 // Do not include the query and fragment parts
120 httpPath = fmt.Sprintf("%s://%s%s", parsedUrl.Scheme, parsedUrl.Host, parsedUrl.Path)
121 }
122 }
123
124 if httpPath == "" {
125 return SpanAttributes{
126 Op: op,
127 Description: s.Name(),
128 Source: sentry.SourceCustom,
129 }
130 }
131
132 // Ex. description="GET /api/users".
133 description := fmt.Sprintf("%s %s", httpMethod, httpPath)
134
135 var source sentry.TransactionSource
136 // If `httpPath` is a root path, then we can categorize the transaction source as route.
137 if httpRoute != "" || httpPath == "/" {
138 source = sentry.SourceRoute
139 } else {
140 source = sentry.SourceURL
141 }
142
143 return SpanAttributes{
144 Op: op,
145 Description: description,
146 Source: source,
147 }
148 }