Skip to content

Commit

Permalink
fix(middleware): http middlewares order (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan C. Yamacho H authored Jun 22, 2023
1 parent ac44c91 commit a912344
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
8 changes: 4 additions & 4 deletions middleware/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
func HTTP(serviceName, handlerName, path string, log logger.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := otel.HTTPMiddleware(serviceName, handlerName, path)(next)
h = logmiddleware.HTTPRequestLogger([]string{})(h)
h := logmiddleware.HTTPRequestLogger([]string{})(next)
h = otel.HTTPMiddleware(serviceName, handlerName, path)(h)
h = logmiddleware.HTTPAddLogger(log)(h)
h = HTTPTrackingID(h)

Expand All @@ -30,8 +30,8 @@ func HTTP(serviceName, handlerName, path string, log logger.Logger) func(http.Ha
func HTTPWithBodyFilter(serviceName, handlerName, path string, filterKeys []string, log logger.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := otel.HTTPMiddleware(serviceName, handlerName, path)(next)
h = logmiddleware.HTTPRequestLogger([]string{})(h)
h := logmiddleware.HTTPRequestLogger([]string{})(next)
h = otel.HTTPMiddleware(serviceName, handlerName, path)(h)
h = logmiddleware.HTTPAddLogger(log)(h)
h = HTTPTrackingID(h)
h = logmiddleware.HTTPAddBodyFilters(filterKeys)(h)
Expand Down
5 changes: 4 additions & 1 deletion middleware/http_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func ExampleHTTP() {
// "path": "/foo",
// "request_id": "tracking_id_ExampleHTTP",
// "timestamp": "2009-11-10T23:00:00.000Z",
// "trace_id": "00000000000000000000000000000000",
// "tracking_id": "tracking_id_ExampleHTTP",
// "user_agent": "",
// "verb": "GET"
Expand Down Expand Up @@ -137,6 +138,7 @@ func ExampleHTTP_onlyRequestIDHeader() {
// "path": "/foo",
// "request_id": "ExampleHTTP_onlyRequestIDHeader",
// "timestamp": "2009-11-10T23:00:00.000Z",
// "trace_id": "00000000000000000000000000000000",
// "tracking_id": "ExampleHTTP_onlyRequestIDHeader",
// "user_agent": "",
// "verb": "GET"
Expand Down Expand Up @@ -203,6 +205,7 @@ func ExampleHTTP_trackingIDAndRequestIDHeaders() {
// "path": "/foo",
// "request_id": "ExampleHTTP_trackingIDAndRequestIDHeaders",
// "timestamp": "2009-11-10T23:00:00.000Z",
// "trace_id": "00000000000000000000000000000000",
// "tracking_id": "ExampleHTTP_trackingIDAndRequestIDHeaders",
// "user_agent": "",
// "verb": "GET"
Expand Down Expand Up @@ -236,7 +239,7 @@ func TestHTTPWithBodyFilter(t *testing.T) {
filterKeys: []string{},
log: logger.Logger{},
},
want: `{"level":"info","application":"TestHTTPRequestLogger","host":"example.com","ip":"192.0.2.1","params":"","path":"/with_body","request_id":"tracking_id_ExampleHTTP","tracking_id":"tracking_id_ExampleHTTP","user_agent":"","verb":"POST","http_status":200,"duration_ms":0,"body":{"hello":"world"},"timestamp":"2009-11-10T23:00:00.000Z","message":"POST /with_body"}` + "\n",
want: `{"level":"info","application":"TestHTTPRequestLogger","trace_id":"00000000000000000000000000000000","host":"example.com","ip":"192.0.2.1","params":"","path":"/with_body","request_id":"tracking_id_ExampleHTTP","tracking_id":"tracking_id_ExampleHTTP","user_agent":"","verb":"POST","http_status":200,"duration_ms":0,"body":{"hello":"world"},"timestamp":"2009-11-10T23:00:00.000Z","message":"POST /with_body"}` + "\n",
},
}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module-sets:
modules:
- github.com/blacklane/go-libs/logger
middleware:
version: v0.3.8
version: v0.3.9
modules:
- github.com/blacklane/go-libs/middleware
otel:
Expand Down

0 comments on commit a912344

Please sign in to comment.