@@ -20,13 +20,14 @@ import (
2020 "fmt"
2121 "io"
2222 "net/http"
23+ "net/http/httptrace"
2324 "os"
2425 "time"
2526
2627 "github.com/gogo/protobuf/proto"
2728 "github.com/golang/snappy"
2829 "go.buf.build/protocolbuffers/go/prometheus/prometheus"
29- "go.opentelemetry.io/otel "
30+ "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace "
3031 "go.opentelemetry.io/otel/trace"
3132)
3233
@@ -36,6 +37,9 @@ const (
3637)
3738
3839type Client struct {
40+ tp trace.TracerProvider
41+ tracer trace.Tracer
42+
3943 client * http.Client
4044 urlString string
4145 userAgent string
@@ -46,6 +50,7 @@ type Client struct {
4650}
4751
4852func NewClient (
53+ tp trace.TracerProvider ,
4954 client * http.Client ,
5055 userAgent string ,
5156 timeout time.Duration ,
@@ -57,6 +62,9 @@ func NewClient(
5762 }
5863
5964 return & Client {
65+ tp : tp ,
66+ tracer : tp .Tracer ("parca/analytics" ),
67+
6068 client : client ,
6169
6270 urlString : analyticsURL ,
@@ -69,6 +77,9 @@ func NewClient(
6977}
7078
7179func (c * Client ) Send (ctx context.Context , wreq * prometheus.WriteRequest ) error {
80+ ctx , span := c .tracer .Start (ctx , "Send" , trace .WithSpanKind (trace .SpanKindClient ))
81+ defer span .End ()
82+
7283 c .pBuf .Reset ()
7384 err := c .pBuf .Marshal (wreq )
7485 if err != nil {
@@ -88,6 +99,9 @@ func (c *Client) Send(ctx context.Context, wreq *prometheus.WriteRequest) error
8899// Store sends a batch of samples to the HTTP endpoint, the request is the proto marshaled
89100// and encoded bytes from codec.go.
90101func (c * Client ) sendReq (ctx context.Context , req []byte ) error {
102+ ctx , span := c .tracer .Start (ctx , "sendReq" , trace .WithSpanKind (trace .SpanKindClient ))
103+ defer span .End ()
104+
91105 httpReq , err := http .NewRequest (http .MethodPost , c .urlString , bytes .NewReader (req ))
92106 if err != nil {
93107 // Errors from NewRequest are from unparsable URLs, so are not
@@ -99,11 +113,10 @@ func (c *Client) sendReq(ctx context.Context, req []byte) error {
99113 httpReq .Header .Set ("Content-Type" , "application/x-protobuf" )
100114 httpReq .Header .Set ("User-Agent" , c .userAgent )
101115 httpReq .Header .Set ("X-Prometheus-Remote-Write-Version" , "0.1.0" )
116+
102117 ctx , cancel := context .WithTimeout (ctx , c .timeout )
103118 defer cancel ()
104-
105- ctx , span := otel .Tracer ("" ).Start (ctx , "Remote Store" , trace .WithSpanKind (trace .SpanKindClient ))
106- defer span .End ()
119+ ctx = httptrace .WithClientTrace (ctx , otelhttptrace .NewClientTrace (ctx , otelhttptrace .WithTracerProvider (c .tp )))
107120
108121 httpResp , err := c .client .Do (httpReq .WithContext (ctx ))
109122 if err != nil {
0 commit comments