-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
otelfiber: migrate go version from
1.19 -> 1.20
, upgrade semiconv p…
…ackage version and fix deprecated usages...
- Loading branch information
Showing
11 changed files
with
188 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/trace" | ||
"net/http" | ||
) | ||
|
||
// SpanStatusFromHTTPStatusCodeAndSpanKind 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) { | ||
// This code block ignores the HTTP 306 status code. The 306 status code is no longer in use. | ||
if len(http.StatusText(code)) == 0 { | ||
return codes.Error, fmt.Sprintf("Invalid HTTP status code %d", code) | ||
} | ||
|
||
if (code >= http.StatusContinue && code < http.StatusBadRequest) || | ||
(spanKind == trace.SpanKindServer && isCode4xx(code)) { | ||
return codes.Unset, "" | ||
} | ||
return codes.Error, "" | ||
} | ||
|
||
func isCode4xx(code int) bool { | ||
return code >= http.StatusBadRequest && code <= http.StatusUnavailableForLegalReasons | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/otel/codes" | ||
oteltrace "go.opentelemetry.io/otel/trace" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestIsCode4xxIsNotValid(t *testing.T) { | ||
response := isCode4xx(http.StatusOK) | ||
|
||
assert.False(t, response) | ||
} | ||
|
||
func TestIsCode4xxIsValid(t *testing.T) { | ||
response := isCode4xx(http.StatusNotFound) | ||
|
||
assert.True(t, response) | ||
} | ||
|
||
func TestStatusErrorWithMessage(t *testing.T) { | ||
spanStatus, spanMessage := SpanStatusFromHTTPStatusCodeAndSpanKind(600, oteltrace.SpanKindClient) | ||
|
||
assert.Equal(t, codes.Error, spanStatus) | ||
assert.Equal(t, "Invalid HTTP status code 600", spanMessage) | ||
} | ||
|
||
func TestStatusErrorWithMessageForIgnoredHTTPCode(t *testing.T) { | ||
spanStatus, spanMessage := SpanStatusFromHTTPStatusCodeAndSpanKind(306, oteltrace.SpanKindClient) | ||
|
||
assert.Equal(t, codes.Error, spanStatus) | ||
assert.Equal(t, "Invalid HTTP status code 306", spanMessage) | ||
} | ||
|
||
func TestStatusErrorWhenHTTPCode5xx(t *testing.T) { | ||
spanStatus, spanMessage := SpanStatusFromHTTPStatusCodeAndSpanKind(http.StatusInternalServerError, oteltrace.SpanKindServer) | ||
|
||
assert.Equal(t, codes.Error, spanStatus) | ||
assert.Equal(t, "", spanMessage) | ||
} | ||
|
||
func TestStatusUnsetWhenServerSpanAndBadRequest(t *testing.T) { | ||
spanStatus, spanMessage := SpanStatusFromHTTPStatusCodeAndSpanKind(http.StatusBadRequest, oteltrace.SpanKindServer) | ||
|
||
assert.Equal(t, codes.Unset, spanStatus) | ||
assert.Equal(t, "", spanMessage) | ||
} | ||
|
||
func TestStatusUnset(t *testing.T) { | ||
spanStatus, spanMessage := SpanStatusFromHTTPStatusCodeAndSpanKind(http.StatusOK, oteltrace.SpanKindClient) | ||
|
||
assert.Equal(t, codes.Unset, spanStatus) | ||
assert.Equal(t, "", spanMessage) | ||
} |
Oops, something went wrong.