Debian Codegraph @

otel/internal/utils/mapotelstatus_test.go

raw
1 package utils_test
2
3 import (
4 "fmt"
5 "github.com/getsentry/sentry-go"
6 "github.com/getsentry/sentry-go/otel/internal/utils"
7 "go.opentelemetry.io/otel/attribute"
8 "go.opentelemetry.io/otel/codes"
9 "go.opentelemetry.io/otel/sdk/trace"
10 semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
11 oteltrace "go.opentelemetry.io/otel/trace"
12 "strconv"
13 "testing"
14 )
15
16 type mockReadOnlySpan struct {
17 trace.ReadOnlySpan
18 name string
19 status trace.Status
20 spanKind oteltrace.SpanKind
21 attributes []attribute.KeyValue
22 }
23
24 var _ trace.ReadOnlySpan = new(mockReadOnlySpan)
25
26 func (m *mockReadOnlySpan) Attributes() []attribute.KeyValue {
27 return m.attributes
28 }
29
30 func (m *mockReadOnlySpan) Status() trace.Status {
31 return m.status
32 }
33
34 func (m *mockReadOnlySpan) SpanKind() oteltrace.SpanKind {
35 return m.spanKind
36 }
37
38 func (m *mockReadOnlySpan) Name() string {
39 return m.name
40 }
41
42 func TestMapOtelStatus(t *testing.T) {
43 t.Run("Given no meaningful attributes to derive the status", func(t *testing.T) {
44 tests := []struct {
45 name string
46 span trace.ReadOnlySpan
47 want sentry.SpanStatus
48 }{
49 {
50 name: "Should return SpanStatusOk if given a Ok status",
51 span: &mockReadOnlySpan{
52 status: trace.Status{
53 Code: codes.Ok,
54 },
55 },
56 want: sentry.SpanStatusOK,
57 },
58 {
59 name: "Should return SpanStatusOk if given a Unset status",
60 span: &mockReadOnlySpan{
61 status: trace.Status{
62 Code: codes.Unset,
63 },
64 },
65 want: sentry.SpanStatusOK,
66 },
67 {
68 name: "Should return SpanStatusError if given a Error status",
69 span: &mockReadOnlySpan{
70 status: trace.Status{
71 Code: codes.Error,
72 },
73 },
74 want: sentry.SpanStatusInternalError,
75 },
76 {
77 name: "Should return SpanStatusUnknown if given an unknown status",
78 span: &mockReadOnlySpan{
79 status: trace.Status{
80 Code: 1337,
81 },
82 },
83 want: sentry.SpanStatusUnknown,
84 },
85 }
86 for _, tt := range tests {
87 t.Run(tt.name, func(t *testing.T) {
88 if got := utils.MapOtelStatus(tt.span); got != tt.want {
89 t.Errorf("MapOtelStatus() = %v, want %v", got, tt.want)
90 }
91 })
92 }
93 })
94
95 asInt := func(key attribute.Key, i int) (attribute.KeyValue, string) {
96 return key.Int(i), "as an int attribute"
97 }
98
99 asString := func(key attribute.Key, i int) (attribute.KeyValue, string) {
100 return key.String(strconv.Itoa(i)), "as a string attribute"
101 }
102
103 t.Run("Given a HTTP Status code", func(t *testing.T) {
104 tts := []struct {
105 code int
106 factory func(key attribute.Key, i int) (attribute.KeyValue, string)
107 want sentry.SpanStatus
108 }{
109 {400, asInt, sentry.SpanStatusFailedPrecondition},
110 {401, asInt, sentry.SpanStatusUnauthenticated},
111 {403, asInt, sentry.SpanStatusPermissionDenied},
112 {404, asInt, sentry.SpanStatusNotFound},
113 {409, asInt, sentry.SpanStatusAborted},
114 {429, asInt, sentry.SpanStatusResourceExhausted},
115 {499, asInt, sentry.SpanStatusCanceled},
116 {500, asInt, sentry.SpanStatusInternalError},
117 {501, asInt, sentry.SpanStatusUnimplemented},
118 {503, asInt, sentry.SpanStatusUnavailable},
119 {504, asInt, sentry.SpanStatusDeadlineExceeded},
120 {400, asString, sentry.SpanStatusFailedPrecondition},
121 {401, asString, sentry.SpanStatusUnauthenticated},
122 {403, asString, sentry.SpanStatusPermissionDenied},
123 {404, asString, sentry.SpanStatusNotFound},
124 {409, asString, sentry.SpanStatusAborted},
125 {429, asString, sentry.SpanStatusResourceExhausted},
126 {499, asString, sentry.SpanStatusCanceled},
127 {500, asString, sentry.SpanStatusInternalError},
128 {501, asString, sentry.SpanStatusUnimplemented},
129 {503, asString, sentry.SpanStatusUnavailable},
130 {504, asString, sentry.SpanStatusDeadlineExceeded},
131 }
132
133 for _, tt := range tts {
134 attr, how := tt.factory(semconv.HTTPStatusCodeKey, tt.code)
135 span := &mockReadOnlySpan{
136 attributes: []attribute.KeyValue{
137 attr,
138 },
139 }
140
141 name := fmt.Sprintf("Should return %s given the code %d %s", tt.want, tt.code, how)
142
143 t.Run(name, func(t *testing.T) {
144 if got := utils.MapOtelStatus(span); got != tt.want {
145 t.Errorf("MapOtelStatus() = %v, want %v", got, tt.want)
146 }
147 })
148 }
149 })
150
151 t.Run("Given a GRPC Status code", func(t *testing.T) {
152 tts := []struct {
153 code int
154 factory func(key attribute.Key, i int) (attribute.KeyValue, string)
155 want sentry.SpanStatus
156 }{
157 {1, asInt, sentry.SpanStatusCanceled},
158 {2, asInt, sentry.SpanStatusUnknown},
159 {3, asInt, sentry.SpanStatusInvalidArgument},
160 {4, asInt, sentry.SpanStatusDeadlineExceeded},
161 {5, asInt, sentry.SpanStatusNotFound},
162 {6, asInt, sentry.SpanStatusAlreadyExists},
163 {7, asInt, sentry.SpanStatusPermissionDenied},
164 {8, asInt, sentry.SpanStatusResourceExhausted},
165 {9, asInt, sentry.SpanStatusFailedPrecondition},
166 {10, asInt, sentry.SpanStatusAborted},
167 {11, asInt, sentry.SpanStatusOutOfRange},
168 {12, asInt, sentry.SpanStatusUnimplemented},
169 {13, asInt, sentry.SpanStatusInternalError},
170 {14, asInt, sentry.SpanStatusUnavailable},
171 {15, asInt, sentry.SpanStatusDataLoss},
172 {16, asInt, sentry.SpanStatusUnauthenticated},
173 {1, asString, sentry.SpanStatusCanceled},
174 {2, asString, sentry.SpanStatusUnknown},
175 {3, asString, sentry.SpanStatusInvalidArgument},
176 {4, asString, sentry.SpanStatusDeadlineExceeded},
177 {5, asString, sentry.SpanStatusNotFound},
178 {6, asString, sentry.SpanStatusAlreadyExists},
179 {7, asString, sentry.SpanStatusPermissionDenied},
180 {8, asString, sentry.SpanStatusResourceExhausted},
181 {9, asString, sentry.SpanStatusFailedPrecondition},
182 {10, asString, sentry.SpanStatusAborted},
183 {11, asString, sentry.SpanStatusOutOfRange},
184 {12, asString, sentry.SpanStatusUnimplemented},
185 {13, asString, sentry.SpanStatusInternalError},
186 {14, asString, sentry.SpanStatusUnavailable},
187 {15, asString, sentry.SpanStatusDataLoss},
188 {16, asString, sentry.SpanStatusUnauthenticated},
189 }
190
191 for _, tt := range tts {
192 attr, how := tt.factory(semconv.RPCGRPCStatusCodeKey, tt.code)
193 span := &mockReadOnlySpan{
194 attributes: []attribute.KeyValue{
195 attr,
196 },
197 }
198
199 name := fmt.Sprintf("Should return %s given the code %d %s", tt.want, tt.code, how)
200 t.Run(name, func(t *testing.T) {
201 if got := utils.MapOtelStatus(span); got != tt.want {
202 t.Errorf("MapOtelStatus() = %v, want %v", got, tt.want)
203 }
204 })
205 }
206 })
207 }