Files
obsx/export_test.go
T
M.ShakhmatovandClaude Fable 5.2 c92fd9826b
CI / test (push) Failing after 43s
Harden instrumentation, health checks and the admin server
- 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>
2026-11-15 16:56:39 +03:00

107 lines
2.9 KiB
Go

package obsx
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"net"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"go.opentelemetry.io/otel"
coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type collector struct {
coltracepb.UnimplementedTraceServiceServer
got chan *coltracepb.ExportTraceServiceRequest
}
func (c *collector) Export(_ context.Context, r *coltracepb.ExportTraceServiceRequest) (*coltracepb.ExportTraceServiceResponse, error) {
c.got <- r
return &coltracepb.ExportTraceServiceResponse{}, nil
}
// End to end: spans reach a real OTLP gRPC collector, over plaintext, TLS
// with the standard certificate variable, and TLS with TracerConfig.TLS.
func TestSetupTracer_Exports(t *testing.T) {
for _, name := range []string{"insecure", "tls", "custom-tls"} {
insecure := name == "insecure"
t.Run(name, func(t *testing.T) {
var tlsCfg *tls.Config
prev := otel.GetTracerProvider()
t.Cleanup(func() { otel.SetTracerProvider(prev) })
var opts []grpc.ServerOption
if !insecure {
// Borrow httptest's self-signed certificate (valid for 127.0.0.1).
ts := httptest.NewTLSServer(nil)
defer ts.Close()
opts = append(opts, grpc.Creds(credentials.NewServerTLSFromCert(&ts.TLS.Certificates[0])))
if name == "custom-tls" {
pool := x509.NewCertPool()
pool.AddCert(ts.Certificate())
tlsCfg = &tls.Config{RootCAs: pool}
} else {
ca := filepath.Join(t.TempDir(), "ca.pem")
pemBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ts.Certificate().Raw})
if err := os.WriteFile(ca, pemBytes, 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("OTEL_EXPORTER_OTLP_CERTIFICATE", ca)
}
}
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
c := &collector{got: make(chan *coltracepb.ExportTraceServiceRequest, 1)}
srv := grpc.NewServer(opts...)
coltracepb.RegisterTraceServiceServer(srv, c)
go srv.Serve(lis)
defer srv.Stop()
ctx := context.Background()
shutdown, err := SetupTracer(ctx, TracerConfig{
ServiceName: "export-test",
Endpoint: lis.Addr().String(),
Insecure: insecure,
TLS: tlsCfg,
})
if err != nil {
t.Fatal(err)
}
_, span := StartSpan(ctx, "exported")
span.End()
if err := shutdown(ctx); err != nil { // flushes
t.Fatalf("shutdown: %v", err)
}
select {
case r := <-c.got:
rs := r.ResourceSpans[0]
if n := rs.ScopeSpans[0].Spans[0].Name; n != "exported" {
t.Errorf("span name = %q", n)
}
var svc string
for _, a := range rs.Resource.Attributes {
if a.Key == "service.name" {
svc = a.Value.GetStringValue()
}
}
if svc != "export-test" {
t.Errorf("service.name = %q", svc)
}
default:
t.Fatal("collector received no spans")
}
})
}
}