Skip to content

Commit

Permalink
feat: move the judgement to SpanStatusFromHTTPStatusCodeAndSpanKind
Browse files Browse the repository at this point in the history
  • Loading branch information
hanyuancheung committed Nov 3, 2021
1 parent 45f5baf commit 36c5264
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 28 deletions.
11 changes: 6 additions & 5 deletions semconv/v1.4.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,22 @@ var validRangesPerCategory = map[int][]codeRange{
// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
spanCode, valid := validateHTTPStatusCode(code)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, ""
}
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.
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
func validateHTTPStatusCode(code int) (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
Expand Down
4 changes: 2 additions & 2 deletions semconv/v1.4.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,14 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
got, msg := SpanStatusFromHTTPStatusCode(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCode(code, trace.SpanKindClient)
_, valid := validateHTTPStatusCode(code)
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")
}
}
code, valid := validateHTTPStatusCode(400, trace.SpanKindServer)
code, valid := validateHTTPStatusCode(400)
if !valid {
assert.Equalf(t, codes.Unset, code, "message should be set if error cannot be inferred from code")
}
Expand Down
11 changes: 6 additions & 5 deletions semconv/v1.5.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,22 @@ var validRangesPerCategory = map[int][]codeRange{
// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
spanCode, valid := validateHTTPStatusCode(code)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, ""
}
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.
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
func validateHTTPStatusCode(code int) (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
Expand Down
4 changes: 2 additions & 2 deletions semconv/v1.5.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,14 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
got, msg := SpanStatusFromHTTPStatusCode(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCode(code, trace.SpanKindClient)
_, valid := validateHTTPStatusCode(code)
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")
}
}
code, valid := validateHTTPStatusCode(400, trace.SpanKindServer)
code, valid := validateHTTPStatusCode(400)
if !valid {
assert.Equalf(t, codes.Unset, code, "message should be set if error cannot be inferred from code")
}
Expand Down
11 changes: 6 additions & 5 deletions semconv/v1.6.1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,22 @@ var validRangesPerCategory = map[int][]codeRange{
// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
spanCode, valid := validateHTTPStatusCode(code)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, ""
}
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.
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
func validateHTTPStatusCode(code int) (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
Expand Down
4 changes: 2 additions & 2 deletions semconv/v1.6.1/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,14 +859,14 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
got, msg := SpanStatusFromHTTPStatusCode(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCode(code, trace.SpanKindClient)
_, valid := validateHTTPStatusCode(code)
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")
}
}
code, valid := validateHTTPStatusCode(400, trace.SpanKindServer)
code, valid := validateHTTPStatusCode(400)
if !valid {
assert.Equalf(t, codes.Unset, code, "message should be set if error cannot be inferred from code")
}
Expand Down
11 changes: 6 additions & 5 deletions semconv/v1.7.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,22 @@ var validRangesPerCategory = map[int][]codeRange{
// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by the OpenTelemetry specification for a span.
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
spanCode, valid := validateHTTPStatusCode(code)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, ""
}
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.
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
func validateHTTPStatusCode(code int) (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
Expand Down
4 changes: 2 additions & 2 deletions semconv/v1.7.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,14 @@ func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
got, msg := SpanStatusFromHTTPStatusCode(code, trace.SpanKindClient)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)

_, valid := validateHTTPStatusCode(code, trace.SpanKindClient)
_, valid := validateHTTPStatusCode(code)
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")
}
}
code, valid := validateHTTPStatusCode(400, trace.SpanKindServer)
code, valid := validateHTTPStatusCode(400)
if !valid {
assert.Equalf(t, codes.Unset, code, "message should be set if error cannot be inferred from code")
}
Expand Down

0 comments on commit 36c5264

Please sign in to comment.