mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-06 06:07:11 +02:00
adding test
Signed-off-by: Vanshikav123 <vanshikav928@gmail.com>
This commit is contained in:
parent
4d5add3cc3
commit
97f288ad87
@ -822,9 +822,6 @@ func acceptEncodingHeader(enableCompression bool) string {
|
|||||||
var UserAgent = version.PrometheusUserAgent()
|
var UserAgent = version.PrometheusUserAgent()
|
||||||
|
|
||||||
func (s *targetScraper) scrape(ctx context.Context) (*http.Response, error) {
|
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 {
|
if s.req == nil {
|
||||||
req, err := http.NewRequest(http.MethodGet, s.URL().String(), nil)
|
req, err := http.NewRequest(http.MethodGet, s.URL().String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -837,11 +834,10 @@ func (s *targetScraper) scrape(ctx context.Context) (*http.Response, error) {
|
|||||||
|
|
||||||
s.req = req
|
s.req = req
|
||||||
}
|
}
|
||||||
|
ctx, span := otel.Tracer("").Start(ctx, "Scrape", trace.WithSpanKind(trace.SpanKindClient))
|
||||||
|
defer span.End()
|
||||||
req := s.req.WithContext(ctx)
|
req := s.req.WithContext(ctx)
|
||||||
|
|
||||||
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
|
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
|
||||||
|
|
||||||
return s.client.Do(req)
|
return s.client.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,9 @@ import (
|
|||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"github.com/prometheus/common/promslog"
|
"github.com/prometheus/common/promslog"
|
||||||
"github.com/stretchr/testify/require"
|
"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/config"
|
||||||
"github.com/prometheus/prometheus/discovery"
|
"github.com/prometheus/prometheus/discovery"
|
||||||
@ -3116,10 +3119,16 @@ func TestTargetScraperScrapeOK(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
protobufParsing bool
|
protobufParsing bool
|
||||||
allowUTF8 bool
|
allowUTF8 bool
|
||||||
qValuePattern = regexp.MustCompile(`q=([0-9]+(\.\d+)?)`)
|
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(
|
server := httptest.NewServer(
|
||||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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")
|
timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds")
|
||||||
require.Equal(t, expectedTimeout, timeout, "Expected scrape timeout header.")
|
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 {
|
if allowUTF8 {
|
||||||
w.Header().Set("Content-Type", `text/plain; version=1.0.0; escaping=allow-utf-8`)
|
w.Header().Set("Content-Type", `text/plain; version=1.0.0; escaping=allow-utf-8`)
|
||||||
} else {
|
} else {
|
||||||
@ -3162,7 +3176,6 @@ func TestTargetScraperScrapeOK(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
runTest := func(t *testing.T, acceptHeader string) {
|
runTest := func(t *testing.T, acceptHeader string) {
|
||||||
ts := &targetScraper{
|
ts := &targetScraper{
|
||||||
Target: &Target{
|
Target: &Target{
|
||||||
|
Loading…
Reference in New Issue
Block a user