Skip to content

Commit

Permalink
Use constants for HTTP status codes (#3189)
Browse files Browse the repository at this point in the history
Co-authored-by: Chester Cheung <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2023
1 parent 26eb21e commit 67c2b13
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAppendMiddlewares(t *testing.T) {
expectedStatusCode int
}{
"invalidChangeBatchError": {
responseStatus: 500,
responseStatus: http.StatusInternalServerError,
responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?>
<InvalidChangeBatch xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
<Messages>
Expand All @@ -55,11 +55,11 @@ func TestAppendMiddlewares(t *testing.T) {
expectedRegion: "us-east-1",
expectedError: codes.Error,
expectedRequestID: "b25f48e8-84fd-11e6-80d9-574e0c4664cb",
expectedStatusCode: 500,
expectedStatusCode: http.StatusInternalServerError,
},

"standardRestXMLError": {
responseStatus: 404,
responseStatus: http.StatusNotFound,
responseBody: []byte(`<?xml version="1.0"?>
<ErrorResponse xmlns="http://route53.amazonaws.com/doc/2016-09-07/">
<Error>
Expand All @@ -73,11 +73,11 @@ func TestAppendMiddlewares(t *testing.T) {
expectedRegion: "us-west-1",
expectedError: codes.Error,
expectedRequestID: "1234567890A",
expectedStatusCode: 404,
expectedStatusCode: http.StatusNotFound,
},

"Success response": {
responseStatus: 200,
responseStatus: http.StatusOK,
responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?>
<ChangeResourceRecordSetsResponse>
<ChangeInfo>
Expand All @@ -86,7 +86,7 @@ func TestAppendMiddlewares(t *testing.T) {
</ChangeInfo>
</ChangeResourceRecordSetsResponse>`),
expectedRegion: "us-west-2",
expectedStatusCode: 200,
expectedStatusCode: http.StatusOK,
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func TestDynamodbTags(t *testing.T) {
expectedStatusCode int
expectedError codes.Code
}{
responseStatus: 200,
responseStatus: http.StatusOK,
expectedRegion: "us-west-2",
expectedStatusCode: 200,
expectedStatusCode: http.StatusOK,
}

server := httptest.NewServer(http.HandlerFunc(
Expand Down Expand Up @@ -113,9 +113,9 @@ func TestDynamodbTagsCustomSetter(t *testing.T) {
expectedStatusCode int
expectedError codes.Code
}{
responseStatus: 200,
responseStatus: http.StatusOK,
expectedRegion: "us-west-2",
expectedStatusCode: 200,
expectedStatusCode: http.StatusOK,
}

server := httptest.NewServer(http.HandlerFunc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (u userResource) WebService() *restful.WebService {
ws.Route(ws.GET("/{user-id}").To(u.getUser).
Param(ws.PathParameter("user-id", "identifier of the user").DataType("integer").DefaultValue("1")).
Writes(user{}). // on the response
Returns(200, "OK", user{}).
Returns(404, "Not Found", nil))
Returns(http.StatusOK, "OK", user{}).
Returns(http.StatusNotFound, "Not Found", nil))
return ws
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestChildSpanFromGlobalTracer(t *testing.T) {
}
ws := &restful.WebService{}
ws.Route(ws.GET("/user/{id}").To(handlerFunc).
Returns(200, "OK", nil).
Returns(404, "Not Found", nil))
Returns(http.StatusOK, "OK", nil).
Returns(http.StatusNotFound, "Not Found", nil))
container := restful.NewContainer()
container.Filter(otelrestful.OTelFilter("my-service"))
container.Add(ws)
Expand Down Expand Up @@ -178,9 +178,9 @@ func TestSpanStatus(t *testing.T) {
httpStatusCode int
wantSpanStatus codes.Code
}{
{200, codes.Unset},
{400, codes.Unset},
{500, codes.Error},
{http.StatusOK, codes.Unset},
{http.StatusBadRequest, codes.Unset},
{http.StatusInternalServerError, codes.Error},
}
for _, tc := range testCases {
t.Run(strconv.Itoa(tc.httpStatusCode), func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func TestSpanStatus(t *testing.T) {
httpStatusCode int
wantSpanStatus codes.Code
}{
{200, codes.Unset},
{400, codes.Unset},
{500, codes.Error},
{http.StatusOK, codes.Unset},
{http.StatusBadRequest, codes.Unset},
{http.StatusInternalServerError, codes.Error},
}
for _, tc := range testCases {
t.Run(strconv.Itoa(tc.httpStatusCode), func(t *testing.T) {
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestHTML(t *testing.T) {

// a handler with an error and make the requests
router.GET("/hello", func(c *gin.Context) {
otelgin.HTML(c, 200, "hello", "world")
otelgin.HTML(c, http.StatusOK, "hello", "world")
})
r := httptest.NewRequest("GET", "/hello", nil)
w := httptest.NewRecorder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"testing"

"github.com/labstack/echo/v4"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestGetSpanNotInstrumented(t *testing.T) {
span := trace.SpanFromContext(c.Request().Context())
ok := !span.SpanContext().IsValid()
assert.True(t, ok)
return c.String(200, "ok")
return c.String(http.StatusOK, "ok")
})
r := httptest.NewRequest("GET", "/ping", nil)
w := httptest.NewRecorder()
Expand Down Expand Up @@ -89,7 +88,7 @@ func TestPropagationWithGlobalPropagators(t *testing.T) {
span := trace.SpanFromContext(c.Request().Context())
assert.Equal(t, sc.TraceID(), span.SpanContext().TraceID())
assert.Equal(t, sc.SpanID(), span.SpanContext().SpanID())
return c.NoContent(200)
return c.NoContent(http.StatusOK)
})

router.ServeHTTP(w, r)
Expand Down Expand Up @@ -120,7 +119,7 @@ func TestPropagationWithCustomPropagators(t *testing.T) {
span := trace.SpanFromContext(c.Request().Context())
assert.Equal(t, sc.TraceID(), span.SpanContext().TraceID())
assert.Equal(t, sc.SpanID(), span.SpanContext().SpanID())
return c.NoContent(200)
return c.NoContent(http.StatusOK)
})

router.ServeHTTP(w, r)
Expand All @@ -141,7 +140,7 @@ func TestSkipper(t *testing.T) {
span := trace.SpanFromContext(c.Request().Context())
assert.False(t, span.SpanContext().HasSpanID())
assert.False(t, span.SpanContext().HasTraceID())
return c.NoContent(200)
return c.NoContent(http.StatusOK)
})

router.ServeHTTP(w, r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestChildSpanFromGlobalTracer(t *testing.T) {
router := echo.New()
router.Use(otelecho.Middleware("foobar"))
router.GET("/user/:id", func(c echo.Context) error {
return c.NoContent(200)
return c.NoContent(http.StatusOK)
})

r := httptest.NewRequest("GET", "/user/123", nil)
Expand All @@ -63,7 +63,7 @@ func TestChildSpanFromCustomTracer(t *testing.T) {
router := echo.New()
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider)))
router.GET("/user/:id", func(c echo.Context) error {
return c.NoContent(200)
return c.NoContent(http.StatusOK)
})

r := httptest.NewRequest("GET", "/user/123", nil)
Expand All @@ -82,7 +82,7 @@ func TestTrace200(t *testing.T) {
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider)))
router.GET("/user/:id", func(c echo.Context) error {
id := c.Param("id")
return c.String(200, id)
return c.String(http.StatusOK, id)
})

r := httptest.NewRequest("GET", "/user/123", nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func TestSpanStatus(t *testing.T) {
httpStatusCode int
wantSpanStatus codes.Code
}{
{200, codes.Unset},
{400, codes.Unset},
{500, codes.Error},
{http.StatusOK, codes.Unset},
{http.StatusBadRequest, codes.Unset},
{http.StatusInternalServerError, codes.Error},
}
for _, tc := range testCases {
t.Run(strconv.Itoa(tc.httpStatusCode), func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/net/http/otelhttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
record: writeRecordFunc,
ctx: ctx,
props: h.propagators,
statusCode: 200, // default status code in case the Handler doesn't write anything
statusCode: http.StatusOK, // default status code in case the Handler doesn't write anything
}

// Wrap w to use our ResponseWriter methods while also exposing
Expand Down
8 changes: 4 additions & 4 deletions instrumentation/net/http/otelhttp/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestHandler(t *testing.T) {
}), "test_handler",
)
},
expectedStatusCode: 200,
expectedStatusCode: http.StatusOK,
},
{
name: "succeeds",
Expand All @@ -59,7 +59,7 @@ func TestHandler(t *testing.T) {
)
},
requestBody: strings.NewReader("hello world"),
expectedStatusCode: 200,
expectedStatusCode: http.StatusOK,
},
{
name: "succeeds with a nil body",
Expand All @@ -70,7 +70,7 @@ func TestHandler(t *testing.T) {
}), "test_handler",
)
},
expectedStatusCode: 200,
expectedStatusCode: http.StatusOK,
},
{
name: "succeeds with an http.NoBody",
Expand All @@ -82,7 +82,7 @@ func TestHandler(t *testing.T) {
)
},
requestBody: http.NoBody,
expectedStatusCode: 200,
expectedStatusCode: http.StatusOK,
},
}
for _, tc := range testCases {
Expand Down
18 changes: 9 additions & 9 deletions instrumentation/net/http/otelhttp/test/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestHandlerEmittedAttributes(t *testing.T) {
w.WriteHeader(http.StatusOK)
},
attributes: []attribute.KeyValue{
attribute.Int("http.status_code", 200),
attribute.Int("http.status_code", http.StatusOK),
},
},
{
Expand All @@ -167,15 +167,15 @@ func TestHandlerEmittedAttributes(t *testing.T) {
w.WriteHeader(http.StatusBadRequest)
},
attributes: []attribute.KeyValue{
attribute.Int("http.status_code", 400),
attribute.Int("http.status_code", http.StatusBadRequest),
},
},
{
name: "With an empty handler",
handler: func(w http.ResponseWriter, r *http.Request) {
},
attributes: []attribute.KeyValue{
attribute.Int("http.status_code", 200),
attribute.Int("http.status_code", http.StatusOK),
},
},
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestHandlerRequestWithTraceContext(t *testing.T) {
r = r.WithContext(ctx)

h.ServeHTTP(rr, r)
assert.Equal(t, 200, rr.Result().StatusCode)
assert.Equal(t, http.StatusOK, rr.Result().StatusCode)

span.End()

Expand Down Expand Up @@ -270,7 +270,7 @@ func TestWithPublicEndpoint(t *testing.T) {

rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
assert.Equal(t, 200, rr.Result().StatusCode)
assert.Equal(t, http.StatusOK, rr.Result().StatusCode)

// Recorded span should be linked with an incoming span context.
assert.NoError(t, spanRecorder.ForceFlush(ctx))
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestWithPublicEndpointFn(t *testing.T) {

rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
assert.Equal(t, 200, rr.Result().StatusCode)
assert.Equal(t, http.StatusOK, rr.Result().StatusCode)

// Recorded span should be linked with an incoming span context.
assert.NoError(t, spanRecorder.ForceFlush(ctx))
Expand All @@ -369,9 +369,9 @@ func TestSpanStatus(t *testing.T) {
httpStatusCode int
wantSpanStatus codes.Code
}{
{200, codes.Unset},
{400, codes.Unset},
{500, codes.Error},
{http.StatusOK, codes.Unset},
{http.StatusBadRequest, codes.Unset},
{http.StatusInternalServerError, codes.Error},
}
for _, tc := range testCases {
t.Run(strconv.Itoa(tc.httpStatusCode), func(t *testing.T) {
Expand Down

0 comments on commit 67c2b13

Please sign in to comment.