-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
42 lines (36 loc) · 991 Bytes
/
client.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
36
37
38
39
40
41
42
package core
import (
"github.com/armory-io/go-commons/opentelemetry"
"github.com/hashicorp/go-cleanhttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/propagation"
"net/http"
)
type (
Parameters struct {
Tracing opentelemetry.Configuration `optional:"true"`
}
)
// NewRoundTripper creates an http.RoundTripper that propagates OpenTelemetry trace headers.
func NewRoundTripper(params Parameters) http.RoundTripper {
base := cleanhttp.DefaultTransport()
if params.Tracing.Push.Enabled {
return otelhttp.NewTransport(
base,
otelhttp.WithPropagators(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
),
),
)
}
return base
}
// NewHTTPClient creates an http.Client that propagates OpenTelemetry trace headers.
func NewHTTPClient(params Parameters) *http.Client {
rt := NewRoundTripper(params)
c := cleanhttp.DefaultClient()
c.Transport = rt
return c
}