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>
181 lines
5.5 KiB
Go
181 lines
5.5 KiB
Go
package obsx
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"google.golang.org/grpc"
|
|
grpccodes "google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
healthpb "google.golang.org/grpc/health/grpc_health_v1"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/grpc/test/bufconn"
|
|
)
|
|
|
|
// startGRPC serves a grpc.health.v1 service backed by h over an in-memory
|
|
// connection, with both server and client instrumented by ic.
|
|
func startGRPC(t *testing.T, h *Health, ic *GRPCInterceptors) healthpb.HealthClient {
|
|
t.Helper()
|
|
return startGRPCServer(t, h.GRPCServer(), ic)
|
|
}
|
|
|
|
func startGRPCServer(t *testing.T, impl healthpb.HealthServer, ic *GRPCInterceptors) healthpb.HealthClient {
|
|
t.Helper()
|
|
ln := bufconn.Listen(1 << 20)
|
|
srv := grpc.NewServer(ic.ServerOptions()...)
|
|
healthpb.RegisterHealthServer(srv, impl)
|
|
go func() { _ = srv.Serve(ln) }()
|
|
t.Cleanup(srv.Stop)
|
|
|
|
opts := append([]grpc.DialOption{
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return ln.DialContext(ctx) }),
|
|
}, ic.DialOptions()...)
|
|
conn, err := grpc.NewClient("passthrough:///bufnet", opts...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
return healthpb.NewHealthClient(conn)
|
|
}
|
|
|
|
func TestGRPC_UnaryInstrumentation(t *testing.T) {
|
|
m := NewMetrics(MetricsConfig{})
|
|
tp, exp := newTestProvider(t)
|
|
var logs bytes.Buffer
|
|
logger, _ := NewLogger(LogConfig{Output: &logs})
|
|
|
|
h := NewHealth(HealthConfig{})
|
|
client := startGRPC(t, h, NewGRPCInterceptors(GRPCConfig{
|
|
Metrics: m,
|
|
Logger: logger,
|
|
TracerProvider: tp,
|
|
Propagator: testPropagator,
|
|
}))
|
|
|
|
resp, err := client.Check(context.Background(), &healthpb.HealthCheckRequest{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.Status != healthpb.HealthCheckResponse_SERVING {
|
|
t.Errorf("status = %v, want SERVING", resp.Status)
|
|
}
|
|
|
|
h.SetReady(false)
|
|
resp, err = client.Check(context.Background(), &healthpb.HealthCheckRequest{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.Status != healthpb.HealthCheckResponse_NOT_SERVING {
|
|
t.Errorf("status = %v, want NOT_SERVING", resp.Status)
|
|
}
|
|
|
|
body := scrape(t, m)
|
|
for _, want := range []string{
|
|
`grpc_server_requests_total{code="OK",method="Check",service="grpc.health.v1.Health"} 2`,
|
|
`grpc_client_requests_total{code="OK",method="Check",service="grpc.health.v1.Health"} 2`,
|
|
`grpc_server_request_duration_seconds_count{code="OK",method="Check",service="grpc.health.v1.Health"} 2`,
|
|
`grpc_server_requests_in_flight 0`,
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("metrics missing %q", want)
|
|
}
|
|
}
|
|
|
|
// Server span (ended first) must be a child of the client span.
|
|
spans := exp.GetSpans()
|
|
if len(spans) != 4 {
|
|
t.Fatalf("got %d spans, want 4", len(spans))
|
|
}
|
|
server, client0 := spans[0], spans[1]
|
|
if server.SpanKind != trace.SpanKindServer || client0.SpanKind != trace.SpanKindClient {
|
|
t.Fatalf("unexpected span kinds: %v, %v", server.SpanKind, client0.SpanKind)
|
|
}
|
|
if server.Name != "grpc.health.v1.Health/Check" {
|
|
t.Errorf("span name = %q", server.Name)
|
|
}
|
|
if server.Parent.SpanID() != client0.SpanContext.SpanID() {
|
|
t.Error("server span is not a child of the client span: metadata propagation failed")
|
|
}
|
|
|
|
if !strings.Contains(logs.String(), `"msg":"grpc request"`) || !strings.Contains(logs.String(), `"code":"OK"`) {
|
|
t.Errorf("missing gRPC access log: %s", logs.String())
|
|
}
|
|
}
|
|
|
|
func TestGRPC_StreamInstrumentation(t *testing.T) {
|
|
m := NewMetrics(MetricsConfig{})
|
|
tp, exp := newTestProvider(t)
|
|
client := startGRPC(t, NewHealth(HealthConfig{}), NewGRPCInterceptors(GRPCConfig{
|
|
Metrics: m,
|
|
TracerProvider: tp,
|
|
Propagator: testPropagator,
|
|
}))
|
|
|
|
stream, err := client.Watch(context.Background(), &healthpb.HealthCheckRequest{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = stream.Recv()
|
|
if status.Code(err) != grpccodes.Unimplemented {
|
|
t.Fatalf("Recv error = %v, want Unimplemented", err)
|
|
}
|
|
if _, err := stream.Recv(); err == nil || err == io.EOF {
|
|
t.Fatalf("second Recv error = %v", err)
|
|
}
|
|
|
|
body := scrape(t, m)
|
|
for _, want := range []string{
|
|
`grpc_server_requests_total{code="Unimplemented",method="Watch",service="grpc.health.v1.Health"} 1`,
|
|
`grpc_client_requests_total{code="Unimplemented",method="Watch",service="grpc.health.v1.Health"} 1`,
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("metrics missing %q\n%s", want, body)
|
|
}
|
|
}
|
|
|
|
spans := exp.GetSpans()
|
|
if len(spans) != 2 {
|
|
t.Fatalf("got %d spans, want 2", len(spans))
|
|
}
|
|
for _, s := range spans {
|
|
if s.Status.Code != codes.Error {
|
|
t.Errorf("span %q (%v) status = %v, want Error", s.Name, s.SpanKind, s.Status.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGRPC_Skip(t *testing.T) {
|
|
m := NewMetrics(MetricsConfig{})
|
|
client := startGRPC(t, NewHealth(HealthConfig{}), NewGRPCInterceptors(GRPCConfig{
|
|
Metrics: m,
|
|
Skip: func(string) bool { return true },
|
|
}))
|
|
if _, err := client.Check(context.Background(), &healthpb.HealthCheckRequest{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(scrape(t, m), "grpc_server_requests_total{") {
|
|
t.Error("skipped method was instrumented")
|
|
}
|
|
}
|
|
|
|
func TestSplitFullMethod(t *testing.T) {
|
|
tests := []struct{ in, service, method string }{
|
|
{"/pkg.Service/Method", "pkg.Service", "Method"},
|
|
{"/a.b.c.Svc/Do", "a.b.c.Svc", "Do"},
|
|
{"bad", "unknown", "bad"},
|
|
}
|
|
for _, tt := range tests {
|
|
s, m := splitFullMethod(tt.in)
|
|
if s != tt.service || m != tt.method {
|
|
t.Errorf("splitFullMethod(%q) = %q, %q", tt.in, s, m)
|
|
}
|
|
}
|
|
}
|