11package http
22
33import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "io"
78 "net"
89 "net/http"
10+ "net/http/httptrace"
911 "time"
1012
13+ "github.com/appclacks/cabourotte/healthcheck"
14+ "github.com/appclacks/cabourotte/tls"
1115 "github.com/pkg/errors"
1216 prom "github.com/prometheus/client_golang/prometheus"
17+ "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
18+ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
19+ "go.opentelemetry.io/otel"
20+ "go.opentelemetry.io/otel/attribute"
21+ "go.opentelemetry.io/otel/codes"
1322 "go.uber.org/zap"
1423 "gopkg.in/tomb.v2"
15-
16- "github.com/appclacks/cabourotte/healthcheck"
17- "github.com/appclacks/cabourotte/tls"
1824)
1925
2026// HTTPDiscovery the http discovery struct
@@ -57,8 +63,13 @@ func New(logger *zap.Logger, config *Configuration, checkComponent *healthcheck.
5763 Config : config ,
5864 URL : url ,
5965 Client : & http.Client {
60- Transport : transport ,
61- Timeout : time .Second * 5 ,
66+ Transport : otelhttp .NewTransport (
67+ transport ,
68+ otelhttp .WithClientTrace (func (ctx context.Context ) * httptrace.ClientTrace {
69+ return otelhttptrace .NewClientTrace (ctx )
70+ }),
71+ ),
72+ Timeout : time .Second * 5 ,
6273 CheckRedirect : func (req * http.Request , via []* http.Request ) error {
6374 return http .ErrUseLastResponse
6475 },
@@ -67,8 +78,8 @@ func New(logger *zap.Logger, config *Configuration, checkComponent *healthcheck.
6778 return & component , nil
6879}
6980
70- func (c * HTTPDiscovery ) request () error {
71- req , err := http .NewRequest ( "GET" , c .URL , nil )
81+ func (c * HTTPDiscovery ) request (ctx context. Context ) error {
82+ req , err := http .NewRequestWithContext ( ctx , "GET" , c .URL , nil )
7283 if err != nil {
7384 return errors .Wrapf (err , "HTTP discovery: fail to create request for %s" , c .URL )
7485 }
@@ -120,16 +131,26 @@ func (c *HTTPDiscovery) Start() error {
120131 for {
121132 select {
122133 case <- c .tick .C :
134+ tracer := otel .Tracer ("discovery" )
135+ ctx , span := tracer .Start (context .Background (), "discovery" )
136+ span .SetAttributes (attribute .String ("cabourotte.discovery.name" , c .Config .Name ))
137+ span .SetAttributes (attribute .String ("cabourotte.discovery.type" , "http" ))
123138 c .Logger .Debug (fmt .Sprintf ("HTTP discovery: polling %s" , c .URL ))
124139 start := time .Now ()
125140 status := "success"
126- err := c .request ()
141+ err := c .request (ctx )
127142 duration := time .Since (start )
128143 if err != nil {
144+ span .RecordError (err )
145+ span .SetStatus (codes .Error , "discovery failure" )
129146 status = "failure"
130147 msg := fmt .Sprintf ("HTTP discovery error: %s" , err .Error ())
131148 c .Logger .Error (msg )
149+ } else {
150+ span .SetStatus (codes .Ok , "discovery successful" )
132151 }
152+ span .SetAttributes (attribute .String ("cabourotte.discovery.status" , status ))
153+ span .End ()
133154 c .requestHistogram .With (prom.Labels {"name" : c .Config .Name }).Observe (duration .Seconds ())
134155 c .responseCounter .With (prom.Labels {"status" : status , "name" : c .Config .Name }).Inc ()
135156 case <- c .t .Dying ():
0 commit comments