-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement traceid.Transport & add example usage (#1)
- Loading branch information
1 parent
f15203d
commit 4bdfc81
Showing
7 changed files
with
166 additions
and
29 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,23 @@ | ||
package traceid | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
func Middleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
traceID := r.Header.Get(Header) | ||
if _, err := uuid.Parse(traceID); err != nil { | ||
traceID = uuidV7() | ||
} | ||
|
||
ctx = context.WithValue(ctx, ctxKey{}, traceID) | ||
w.Header().Set(Header, traceID) | ||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
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,47 @@ | ||
package traceid | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func Transport(next http.RoundTripper) http.RoundTripper { | ||
return RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) { | ||
r := cloneRequest(req) | ||
|
||
traceID, _ := r.Context().Value(ctxKey{}).(string) | ||
if traceID == "" { | ||
traceID = uuidV7() | ||
} | ||
r.Header.Set(http.CanonicalHeaderKey(Header), traceID) | ||
|
||
return next.RoundTrip(r) | ||
}) | ||
} | ||
|
||
// RoundTripFunc, similar to http.HandlerFunc, is an adapter | ||
// to allow the use of ordinary functions as http.RoundTrippers. | ||
type RoundTripFunc func(r *http.Request) (*http.Response, error) | ||
|
||
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return f(req) | ||
} | ||
|
||
// cloneRequest creates a shallow copy of a given request | ||
// to comply with stdlib's http.RoundTripper contract: | ||
// | ||
// RoundTrip should not modify the request, except for | ||
// consuming and closing the Request's Body. RoundTrip may | ||
// read fields of the request in a separate goroutine. Callers | ||
// should not mutate or reuse the request until the Response's | ||
// Body has been closed. | ||
func cloneRequest(orig *http.Request) *http.Request { | ||
clone := &http.Request{} | ||
*clone = *orig | ||
|
||
clone.Header = make(http.Header, len(orig.Header)) | ||
for key, value := range orig.Header { | ||
clone.Header[key] = append([]string{}, value...) | ||
} | ||
|
||
return clone | ||
} |