/
request_id.go
35 lines (28 loc) · 995 Bytes
/
request_id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)
}