Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from pace/improved-logging
Improved logging
- Loading branch information
Showing
12 changed files
with
176 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2020/05/18 by Vincent Landgraf | ||
|
||
package transport | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/pace/bricks/maintenance/log" | ||
) | ||
|
||
// RequestIDRoundTripper implements a chainable round tripper for setting the Request-Source header | ||
type RequestIDRoundTripper struct { | ||
transport http.RoundTripper | ||
SourceName string | ||
} | ||
|
||
// Transport returns the RoundTripper to make HTTP requests | ||
func (l *RequestIDRoundTripper) Transport() http.RoundTripper { | ||
return l.transport | ||
} | ||
|
||
// SetTransport sets the RoundTripper to make HTTP requests | ||
func (l *RequestIDRoundTripper) SetTransport(rt http.RoundTripper) { | ||
l.transport = rt | ||
} | ||
|
||
// RoundTrip executes a single HTTP transaction via Transport() | ||
func (l *RequestIDRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
ctx := req.Context() | ||
if reqID := log.RequestIDFromContext(ctx); reqID != "" { | ||
req.Header.Set("Request-Id", reqID) | ||
} | ||
return l.Transport().RoundTrip(req) | ||
} |
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,53 @@ | ||
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2020/05/18 by Vincent Landgraf | ||
|
||
package transport | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/pace/bricks/maintenance/log" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRequestIDRoundTripper(t *testing.T) { | ||
rt := RequestIDRoundTripper{} | ||
rt.SetTransport(&transportWithResponse{}) | ||
|
||
t.Run("without req_id", func(t *testing.T) { | ||
req := httptest.NewRequest("GET", "/foo", nil) | ||
_, err := rt.RoundTrip(req) | ||
assert.NoError(t, err) | ||
assert.Empty(t, req.Header["Request-Id"]) | ||
}) | ||
|
||
t.Run("with req_id", func(t *testing.T) { | ||
ID := "bqprir5mp1o6vaipufsg" | ||
|
||
r := mux.NewRouter() | ||
r.Use(log.Handler()) | ||
r.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { | ||
require.Equal(t, []string{ID}, r.Header["Request-Id"]) | ||
require.Equal(t, ID, log.RequestID(r)) | ||
require.Equal(t, ID, log.RequestIDFromContext(r.Context())) | ||
|
||
r1 := httptest.NewRequest("GET", "/foo", nil) | ||
r1 = r1.WithContext(r.Context()) | ||
|
||
_, err := rt.RoundTrip(r1) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []string{ID}, r1.Header["Request-Id"]) | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
rec := httptest.NewRecorder() | ||
req := httptest.NewRequest("GET", "/foo", nil) | ||
req.Header.Set("Request-Id", ID) | ||
r.ServeHTTP(rec, req) | ||
assert.Equal(t, http.StatusNoContent, rec.Code) | ||
}) | ||
} |
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,16 @@ | ||
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2020/05/18 by Vincent Landgraf | ||
|
||
package transport | ||
|
||
import "net/http" | ||
|
||
type transportWithResponse struct { | ||
statusCode int | ||
} | ||
|
||
func (t *transportWithResponse) RoundTrip(req *http.Request) (*http.Response, error) { | ||
resp := &http.Response{StatusCode: t.statusCode} | ||
|
||
return resp, nil | ||
} |
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