Skip to content

Commit 9e9529e

Browse files
authored
Merge branch 'main' into fixmemory
2 parents 85e3289 + 58f69f0 commit 9e9529e

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4141
- The `otel-collector` example now correctly flushes metric events prior to shutting down the exporter. (#1678)
4242
- Reduced excess memory usage by global `TracerProvider`. (#1687)
4343

44+
### Fixed
45+
- Do not set span status message in `SpanStatusFromHTTPStatusCode` if it can be inferred from `http.status_code`. (#1681)
46+
4447
## [0.18.0] - 2020-03-03
4548

4649
### Added

semconv/http.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -264,29 +264,34 @@ var validRangesPerCategory = map[int][]codeRange{
264264
// SpanStatusFromHTTPStatusCode generates a status code and a message
265265
// as specified by the OpenTelemetry specification for a span.
266266
func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
267-
spanCode, valid := func() (codes.Code, bool) {
268-
category := code / 100
269-
ranges, ok := validRangesPerCategory[category]
270-
if !ok {
271-
return codes.Error, false
272-
}
273-
ok = false
274-
for _, crange := range ranges {
275-
ok = crange.contains(code)
276-
if ok {
277-
break
278-
}
279-
}
280-
if !ok {
281-
return codes.Error, false
282-
}
283-
if category > 0 && category < 4 {
284-
return codes.Unset, true
285-
}
286-
return codes.Error, true
287-
}()
267+
spanCode, valid := validateHTTPStatusCode(code)
288268
if !valid {
289269
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
290270
}
291-
return spanCode, fmt.Sprintf("HTTP status code: %d", code)
271+
return spanCode, ""
272+
}
273+
274+
// Validates the HTTP status code and returns corresponding span status code.
275+
// If the `code` is not a valid HTTP status code, returns span status Error
276+
// and false.
277+
func validateHTTPStatusCode(code int) (codes.Code, bool) {
278+
category := code / 100
279+
ranges, ok := validRangesPerCategory[category]
280+
if !ok {
281+
return codes.Error, false
282+
}
283+
ok = false
284+
for _, crange := range ranges {
285+
ok = crange.contains(code)
286+
if ok {
287+
break
288+
}
289+
}
290+
if !ok {
291+
return codes.Error, false
292+
}
293+
if category > 0 && category < 4 {
294+
return codes.Unset, true
295+
}
296+
return codes.Error, true
292297
}

semconv/http_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,15 @@ func TestHTTPAttributesFromHTTPStatusCode(t *testing.T) {
696696
func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
697697
for code := 0; code < 1000; code++ {
698698
expected := getExpectedCodeForHTTPCode(code)
699-
got, _ := SpanStatusFromHTTPStatusCode(code)
699+
got, msg := SpanStatusFromHTTPStatusCode(code)
700700
assert.Equalf(t, expected, got, "%s vs %s", expected, got)
701+
702+
_, valid := validateHTTPStatusCode(code)
703+
if !valid {
704+
assert.NotEmpty(t, msg, "message should be set if error cannot be inferred from code")
705+
} else {
706+
assert.Empty(t, msg, "message should not be set if error can be inferred from code")
707+
}
701708
}
702709
}
703710

0 commit comments

Comments
 (0)