Skip to content

Commit fd9de64

Browse files
xuerbujiawuhongyu
authored and
wuhongyu
committed
feat Modify the mapping of HTTP codes to OTel codes.
1 parent 97bb76c commit fd9de64

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

pkg/inst-api-semconv/instrumenter/http/http_status_code_extractor.go

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func (h HttpClientSpanStatusExtractor[REQUEST, RESPONSE]) Extract(span trace.Spa
3434
} else {
3535
span.SetStatus(codes.Error, invalidHttpStatusCode)
3636
}
37+
} else if statusCode >= 200 && statusCode < 300 {
38+
span.SetStatus(codes.Ok, "success")
3739
}
3840
}
3941

@@ -50,5 +52,7 @@ func (h HttpServerSpanStatusExtractor[REQUEST, RESPONSE]) Extract(span trace.Spa
5052
} else {
5153
span.SetStatus(codes.Error, invalidHttpStatusCode)
5254
}
55+
} else if statusCode >= 200 && statusCode < 300 {
56+
span.SetStatus(codes.Ok, "success")
5357
}
5458
}

pkg/inst-api-semconv/instrumenter/http/http_status_code_extractor_test.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,23 @@ func TestHttpClientSpanStatusExtractor200(t *testing.T) {
108108
u := codes.Code(0)
109109
span := testSpan{status: &u}
110110
c.Extract(span, nil, nil, nil)
111-
if *span.status != codes.Unset {
112-
panic("span status should be unset!")
111+
if *span.status != codes.Ok {
112+
panic("span status should be ok!")
113+
}
114+
}
115+
func TestHttpClientSpanStatusExtractor201(t *testing.T) {
116+
c := HttpClientSpanStatusExtractor[any, any]{
117+
Getter: customizedNetHttpAttrsGetter{
118+
code: 201,
119+
},
120+
}
121+
u := codes.Code(0)
122+
span := testSpan{status: &u}
123+
c.Extract(span, nil, nil, nil)
124+
if *span.status != codes.Ok {
125+
panic("span status should be ok!")
113126
}
114127
}
115-
116128
func TestHttpServerSpanStatusExtractor500(t *testing.T) {
117129
c := HttpServerSpanStatusExtractor[any, any]{
118130
Getter: customizedNetHttpAttrsGetter{
@@ -150,7 +162,20 @@ func TestHttpServerSpanStatusExtractor200(t *testing.T) {
150162
u := codes.Code(0)
151163
span := testSpan{status: &u}
152164
c.Extract(span, nil, nil, nil)
153-
if *span.status != codes.Unset {
154-
panic("span status should be unset!")
165+
if *span.status != codes.Ok {
166+
panic("span status should be ok!")
167+
}
168+
}
169+
func TestHttpServerSpanStatusExtractor201(t *testing.T) {
170+
c := HttpClientSpanStatusExtractor[any, any]{
171+
Getter: customizedNetHttpAttrsGetter{
172+
code: 201,
173+
},
174+
}
175+
u := codes.Code(0)
176+
span := testSpan{status: &u}
177+
c.Extract(span, nil, nil, nil)
178+
if *span.status != codes.Ok {
179+
panic("span status should be ok!")
155180
}
156181
}

0 commit comments

Comments
 (0)