Files
obsx/httpclient_test.go
T
M.ShakhmatovandClaude Opus 5.5 8398e81bc7 Add HTTP and gRPC instrumentation
HTTP server middleware, HTTP client transport and gRPC server/client
interceptors (unary and stream) providing spans, context propagation,
RED metrics with uniform names and access logs. Labels are kept
low-cardinality (route templates, normalized methods).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 16:39:38 +03:00

104 lines
2.9 KiB
Go

package obsx
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
func TestHTTPTransport_PropagationAndMetrics(t *testing.T) {
m := NewMetrics(MetricsConfig{})
tp, exp := newTestProvider(t)
var gotTraceparent string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotTraceparent = r.Header.Get("traceparent")
if r.URL.Path == "/missing" {
w.WriteHeader(http.StatusNotFound)
}
}))
defer srv.Close()
client := &http.Client{Transport: NewHTTPTransport(nil, HTTPClientConfig{
Metrics: m,
TracerProvider: tp,
Propagator: testPropagator,
})}
req, _ := http.NewRequest("GET", srv.URL+"/ok", nil)
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if req.Header.Get("traceparent") != "" {
t.Error("transport modified the caller's request headers")
}
resp, err = client.Get(srv.URL + "/missing")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
spans := exp.GetSpans()
if len(spans) != 2 {
t.Fatalf("got %d spans, want 2", len(spans))
}
if spans[0].SpanKind != trace.SpanKindClient {
t.Errorf("span kind = %v, want client", spans[0].SpanKind)
}
sc := spans[1].SpanContext
if !strings.Contains(gotTraceparent, sc.TraceID().String()) || !strings.Contains(gotTraceparent, sc.SpanID().String()) {
t.Errorf("traceparent %q does not reference client span %s/%s", gotTraceparent, sc.TraceID(), sc.SpanID())
}
host := strings.TrimPrefix(srv.URL, "http://")
body := scrape(t, m)
for _, want := range []string{
`http_client_requests_total{code="200",host="` + host + `",method="GET"} 1`,
`http_client_requests_total{code="404",host="` + host + `",method="GET"} 1`,
`http_client_request_duration_seconds_count{code="200",host="` + host + `",method="GET"} 1`,
} {
if !strings.Contains(body, want) {
t.Errorf("metrics missing %q", want)
}
}
}
func TestHTTPTransport_Error(t *testing.T) {
m := NewMetrics(MetricsConfig{})
tp, exp := newTestProvider(t)
client := &http.Client{Transport: NewHTTPTransport(nil, HTTPClientConfig{Metrics: m, TracerProvider: tp, Propagator: propagation.TraceContext{}})}
srv := httptest.NewServer(http.NotFoundHandler())
addr := srv.URL
srv.Close() // connection refused
if _, err := client.Get(addr); err == nil {
t.Fatal("expected error")
}
if !strings.Contains(scrape(t, m), `code="error"`) {
t.Error("transport error not counted with code=error")
}
if spans := exp.GetSpans(); len(spans) != 1 || len(spans[0].Events) == 0 {
t.Error("transport error not recorded on span")
}
}
func TestRedactURL(t *testing.T) {
u, _ := url.Parse("https://user:secret@example.com/path?q=1")
got := redactURL(&http.Request{URL: u})
if strings.Contains(got, "secret") || strings.Contains(got, "user") {
t.Errorf("credentials not redacted: %s", got)
}
if got != "https://example.com/path?q=1" {
t.Errorf("redactURL = %q", got)
}
}