diff --git a/scrape/scrape.go b/scrape/scrape.go index 79ef69ae49..cf7d9c4e02 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -822,9 +822,6 @@ func acceptEncodingHeader(enableCompression bool) string { var UserAgent = version.PrometheusUserAgent() func (s *targetScraper) scrape(ctx context.Context) (*http.Response, error) { - ctx, span := otel.Tracer("").Start(ctx, "Scrape", trace.WithSpanKind(trace.SpanKindClient)) - defer span.End() - if s.req == nil { req, err := http.NewRequest(http.MethodGet, s.URL().String(), nil) if err != nil { @@ -837,11 +834,10 @@ func (s *targetScraper) scrape(ctx context.Context) (*http.Response, error) { s.req = req } - + ctx, span := otel.Tracer("").Start(ctx, "Scrape", trace.WithSpanKind(trace.SpanKindClient)) + defer span.End() req := s.req.WithContext(ctx) - otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) - return s.client.Do(req) } diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 270121c8c5..3e29fdbcc7 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -45,6 +45,9 @@ import ( "github.com/prometheus/common/model" "github.com/prometheus/common/promslog" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/propagation" + sdktrace "go.opentelemetry.io/otel/sdk/trace" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery" @@ -3116,10 +3119,16 @@ func TestTargetScraperScrapeOK(t *testing.T) { ) var ( - protobufParsing bool - allowUTF8 bool - qValuePattern = regexp.MustCompile(`q=([0-9]+(\.\d+)?)`) + protobufParsing bool + allowUTF8 bool + qValuePattern = regexp.MustCompile(`q=([0-9]+(\.\d+)?)`) + traceparentPattern = regexp.MustCompile(`^[0-9a-f]{2}-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$`) ) + tp := sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.AlwaysSample()), + ) + otel.SetTracerProvider(tp) + otel.SetTextMapPropagator(propagation.TraceContext{}) server := httptest.NewServer( http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -3148,6 +3157,11 @@ func TestTargetScraperScrapeOK(t *testing.T) { timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds") require.Equal(t, expectedTimeout, timeout, "Expected scrape timeout header.") + traceparent := r.Header.Get("traceparent") + require.NotEmpty(t, traceparent, "Expected traceparent header to be present") + require.Regexp(t, traceparentPattern, traceparent, + "Traceparent header must match W3C format, got %q", traceparent) + if allowUTF8 { w.Header().Set("Content-Type", `text/plain; version=1.0.0; escaping=allow-utf-8`) } else { @@ -3162,7 +3176,6 @@ func TestTargetScraperScrapeOK(t *testing.T) { if err != nil { panic(err) } - runTest := func(t *testing.T, acceptHeader string) { ts := &targetScraper{ Target: &Target{