Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new function to exclude span kind 4xx for status code #2339

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The following interface types simply moved from `metric` to `metric/sdkapi`: `Descriptor`, `MeterImpl`, `InstrumentImpl`, `SyncImpl`, `BoundSyncImpl`, `AsyncImpl`, `AsyncRunner`, `AsyncSingleRunner`, and `AsyncBatchRunner`
- The following struct types moved and are replaced with type aliases, since they are exposed to the user: `Observation`, `Measurement`.
- The No-op implementations of sync and async instruments are no longer exported, new functions `sdkapi.NewNoopAsyncInstrument()` and `sdkapi.NewNoopSyncInstrument()` are provided instead. (#2271)
- Add new function to exclude span kind 4xx for status code. (#2296)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

### Added

Expand Down
1 change: 0 additions & 1 deletion internal/tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/itchyny/go-flags v1.5.0 h1:Z5q2ist2sfDjDlExVPBrMqlsEDxDR2h4zuOElB0OEYI=
github.com/itchyny/go-flags v1.5.0/go.mod h1:lenkYuCobuxLBAd/HGFE4LRoW8D3B6iXRQfWYJ+MNbA=
github.com/itchyny/gojq v0.12.5 h1:6SJ1BQ1VAwJAlIvLSIZmqHP/RUEq3qfVWvsRxrqhsD0=
github.com/itchyny/gojq v0.12.5/go.mod h1:3e1hZXv+Kwvdp6V9HXpVrvddiHVApi5EDZwS+zLFeiE=
Expand Down
40 changes: 40 additions & 0 deletions semconv/v1.4.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

// HTTP scheme attributes.
Expand Down Expand Up @@ -293,3 +294,42 @@ func validateHTTPStatusCode(code int) (codes.Code, bool) {
}
return codes.Error, true
}

// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
// Exclude 4xx for SERVER to set the appropriate status.
func SpanStatusFromHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCodeAndSpanKind(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
return spanCode, ""
}

// Validates the HTTP status code and returns corresponding span status code.
// If the `code` is not a valid HTTP status code, returns span status Error
// and false. Exclude 4xx for SERVER to set the appropriate status.
func validateHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
}
ranges, ok := validRangesPerCategory[category]
if !ok {
return codes.Error, false
}
ok = false
for _, crange := range ranges {
ok = crange.contains(code)
if ok {
break
}
}
if !ok {
return codes.Error, false
}
if category > 0 && category < 4 {
return codes.Unset, true
}
return codes.Error, true
}
17 changes: 17 additions & 0 deletions semconv/v1.4.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"testing"

"go.opentelemetry.io/otel/trace"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -867,6 +869,21 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
}
}

func TestSpanStatusFromHTTPStatusCodeAndSpanKind(t *testing.T) {
for code := 0; code < 1000; code++ {
expected := getExpectedCodeForHTTPCode(code)
got, msg := SpanStatusFromHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
if !valid {
assert.NotEmpty(t, msg, "message should be set if error cannot be inferred from code")
} else {
assert.Empty(t, msg, "message should not be set if error can be inferred from code")
}
}
}

func getExpectedCodeForHTTPCode(code int) codes.Code {
if http.StatusText(code) == "" {
return codes.Error
Expand Down
40 changes: 40 additions & 0 deletions semconv/v1.5.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

// HTTP scheme attributes.
Expand Down Expand Up @@ -293,3 +294,42 @@ func validateHTTPStatusCode(code int) (codes.Code, bool) {
}
return codes.Error, true
}

// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
// Exclude 4xx for SERVER to set the appropriate status.
func SpanStatusFromHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCodeAndSpanKind(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
return spanCode, ""
}

// Validates the HTTP status code and returns corresponding span status code.
// If the `code` is not a valid HTTP status code, returns span status Error
// and false. Exclude 4xx for SERVER to set the appropriate status.
func validateHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
}
ranges, ok := validRangesPerCategory[category]
if !ok {
return codes.Error, false
}
ok = false
for _, crange := range ranges {
ok = crange.contains(code)
if ok {
break
}
}
if !ok {
return codes.Error, false
}
if category > 0 && category < 4 {
return codes.Unset, true
}
return codes.Error, true
}
17 changes: 17 additions & 0 deletions semconv/v1.5.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
Expand Down Expand Up @@ -867,6 +869,21 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
}
}

func TestSpanStatusFromHTTPStatusCodeAndSpanKind(t *testing.T) {
for code := 0; code < 1000; code++ {
expected := getExpectedCodeForHTTPCode(code)
got, msg := SpanStatusFromHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
if !valid {
assert.NotEmpty(t, msg, "message should be set if error cannot be inferred from code")
} else {
assert.Empty(t, msg, "message should not be set if error can be inferred from code")
}
}
}

func getExpectedCodeForHTTPCode(code int) codes.Code {
if http.StatusText(code) == "" {
return codes.Error
Expand Down
40 changes: 40 additions & 0 deletions semconv/v1.6.1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

// HTTP scheme attributes.
Expand Down Expand Up @@ -293,3 +294,42 @@ func validateHTTPStatusCode(code int) (codes.Code, bool) {
}
return codes.Error, true
}

// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
// Exclude 4xx for SERVER to set the appropriate status.
func SpanStatusFromHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCodeAndSpanKind(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
return spanCode, ""
}

// Validates the HTTP status code and returns corresponding span status code.
// If the `code` is not a valid HTTP status code, returns span status Error
// and false. Exclude 4xx for SERVER to set the appropriate status.
func validateHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
}
ranges, ok := validRangesPerCategory[category]
if !ok {
return codes.Error, false
}
ok = false
for _, crange := range ranges {
ok = crange.contains(code)
if ok {
break
}
}
if !ok {
return codes.Error, false
}
if category > 0 && category < 4 {
return codes.Unset, true
}
return codes.Error, true
}
16 changes: 16 additions & 0 deletions semconv/v1.6.1/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

type tlsOption int
Expand Down Expand Up @@ -867,6 +868,21 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
}
}

func TestSpanStatusFromHTTPStatusCodeAndSpanKind(t *testing.T) {
for code := 0; code < 1000; code++ {
expected := getExpectedCodeForHTTPCode(code)
got, msg := SpanStatusFromHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
if !valid {
assert.NotEmpty(t, msg, "message should be set if error cannot be inferred from code")
} else {
assert.Empty(t, msg, "message should not be set if error can be inferred from code")
}
}
}

func getExpectedCodeForHTTPCode(code int) codes.Code {
if http.StatusText(code) == "" {
return codes.Error
Expand Down
41 changes: 41 additions & 0 deletions semconv/v1.7.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"strconv"
"strings"

"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
Expand Down Expand Up @@ -293,3 +295,42 @@ func validateHTTPStatusCode(code int) (codes.Code, bool) {
}
return codes.Error, true
}

// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
// Exclude 4xx for SERVER to set the appropriate status.
func SpanStatusFromHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCodeAndSpanKind(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
return spanCode, ""
}

// Validates the HTTP status code and returns corresponding span status code.
// If the `code` is not a valid HTTP status code, returns span status Error
// and false. Exclude 4xx for SERVER to set the appropriate status.
func validateHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
}
ranges, ok := validRangesPerCategory[category]
if !ok {
return codes.Error, false
}
ok = false
for _, crange := range ranges {
ok = crange.contains(code)
if ok {
break
}
}
if !ok {
return codes.Error, false
}
if category > 0 && category < 4 {
return codes.Unset, true
}
return codes.Error, true
pellared marked this conversation as resolved.
Show resolved Hide resolved
}
17 changes: 17 additions & 0 deletions semconv/v1.7.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"testing"

"go.opentelemetry.io/otel/trace"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -867,6 +869,21 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
}
}

func TestSpanStatusFromHTTPStatusCodeAndSpanKind(t *testing.T) {
for code := 0; code < 1000; code++ {
expected := getExpectedCodeForHTTPCode(code)
got, msg := SpanStatusFromHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient)
if !valid {
assert.NotEmpty(t, msg, "message should be set if error cannot be inferred from code")
} else {
assert.Empty(t, msg, "message should not be set if error can be inferred from code")
}
}
}

func getExpectedCodeForHTTPCode(code int) codes.Code {
if http.StatusText(code) == "" {
return codes.Error
Expand Down