CI / test (push) Failing after 43s
- Admin pprof is opt-in (AdminConfig.EnablePprof) - Client spans drop the URL query string; HostFunc bounds the host label - Config.HTTP/HTTPClient/GRPC configure the Observer's instrumentation - A panic after a partial HTTP response counts as 500 - GaugeFunc, CounterFunc and RegisterDBStats panic on duplicates - Abandoned client gRPC streams are recorded as Canceled - OTEL_EXPORTER_OTLP_PROTOCOL selects the protocol when unset - Health reports are cached for 1s; hung checks are not restarted - Environment is exported as deployment.environment.name Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
104 lines
2.9 KiB
Go
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" {
|
|
t.Errorf("redactURL = %q", got)
|
|
}
|
|
}
|