Skip to content

Commit

Permalink
feat: redo by reusing validateHTTPStatusCode
Browse files Browse the repository at this point in the history
  • Loading branch information
hanyuancheung committed Nov 3, 2021
1 parent 622ef2a commit 41f1514
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 228 deletions.
42 changes: 3 additions & 39 deletions semconv/v1.4.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ 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) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code)
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
Expand All @@ -273,43 +273,7 @@ func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
// 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) (codes.Code, bool) {
category := code / 100
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
}

// 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) {
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
Expand Down
27 changes: 9 additions & 18 deletions semconv/v1.4.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,35 +856,24 @@ func TestHTTPAttributesFromHTTPStatusCode(t *testing.T) {

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

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

func getExpectedCodeForHTTPCode(code int) codes.Code {
func getExpectedCodeForHTTPCode(code int, spanKind trace.SpanKind) codes.Code {
if http.StatusText(code) == "" {
return codes.Error
}
Expand All @@ -902,6 +891,8 @@ func getExpectedCodeForHTTPCode(code int) codes.Code {
category := code / 100
if category > 0 && category < 4 {
return codes.Unset
} else if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset
}
return codes.Error
}
Expand Down
42 changes: 3 additions & 39 deletions semconv/v1.5.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ 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) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code)
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
Expand All @@ -273,43 +273,7 @@ func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
// 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) (codes.Code, bool) {
category := code / 100
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
}

// 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) {
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
Expand Down
27 changes: 9 additions & 18 deletions semconv/v1.5.0/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,35 +856,24 @@ func TestHTTPAttributesFromHTTPStatusCode(t *testing.T) {

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

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

func getExpectedCodeForHTTPCode(code int) codes.Code {
func getExpectedCodeForHTTPCode(code int, spanKind trace.SpanKind) codes.Code {
if http.StatusText(code) == "" {
return codes.Error
}
Expand All @@ -902,6 +891,8 @@ func getExpectedCodeForHTTPCode(code int) codes.Code {
category := code / 100
if category > 0 && category < 4 {
return codes.Unset
} else if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset
}
return codes.Error
}
Expand Down
42 changes: 3 additions & 39 deletions semconv/v1.6.1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ 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) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code)
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
Expand All @@ -273,43 +273,7 @@ func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
// 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) (codes.Code, bool) {
category := code / 100
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
}

// 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) {
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
Expand Down
25 changes: 7 additions & 18 deletions semconv/v1.6.1/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,35 +855,24 @@ func TestHTTPAttributesFromHTTPStatusCode(t *testing.T) {

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

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

func getExpectedCodeForHTTPCode(code int) codes.Code {
func getExpectedCodeForHTTPCode(code int, spanKind trace.SpanKind) codes.Code {
if http.StatusText(code) == "" {
return codes.Error
}
Expand Down
42 changes: 3 additions & 39 deletions semconv/v1.7.0/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ 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) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code)
func SpanStatusFromHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, string) {
spanCode, valid := validateHTTPStatusCode(code, spanKind)
if !valid {
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
}
Expand All @@ -274,43 +274,7 @@ func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
// 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) (codes.Code, bool) {
category := code / 100
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
}

// 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) {
func validateHTTPStatusCode(code int, spanKind trace.SpanKind) (codes.Code, bool) {
category := code / 100
if spanKind == trace.SpanKindServer && category == 4 {
return codes.Unset, false
Expand Down

0 comments on commit 41f1514

Please sign in to comment.