Extend tracing and metrics

Tracing: OTLP gRPC/HTTP exporters with TLS and headers, parent-based
sampling, W3C TraceContext/Baggage propagation, richer resource
attributes, no-export mode without an endpoint, Disabled flag, and
Trace/RecordError/TraceID helpers.

Metrics: idempotent registration, Sub, GaugeFunc, CounterFunc,
RegisterDBStats, OpenMetrics handler with trace exemplars.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
M.ShakhmatovandClaude Opus 5.5 committed 2026-10-30 00:18:43 +03:00
1 parent 362267470e
commit 9a8ebe987b
6 files changed
+570 -41

No files matched your search

+78
View File
@@ -1,11 +1,17 @@
package obsx
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func TestMetrics_Counter(t *testing.T) {
@@ -62,3 +68,75 @@ func scrape(t *testing.T, m *Metrics) string {
body, _ := io.ReadAll(w.Body)
return string(body)
}
func TestMetrics_Sub(t *testing.T) {
m := NewMetrics(MetricsConfig{Namespace: "app"})
m.Sub("db").Counter("queries_total", "Queries").WithLabelValues().Inc()
if !strings.Contains(scrape(t, m), "app_db_queries_total 1") {
t.Error("Sub factory must share the registry and namespace")
}
}
func TestMetrics_IdempotentRegistration(t *testing.T) {
m := NewMetrics(MetricsConfig{})
a := m.Counter("events_total", "Events", "kind")
b := m.Counter("events_total", "Events", "kind")
if a != b {
t.Error("registering an identical metric must return the existing collector")
}
defer func() {
if recover() == nil {
t.Error("conflicting registration must panic")
}
}()
m.Counter("events_total", "Events", "other_label")
}
func TestMetrics_Funcs(t *testing.T) {
m := NewMetrics(MetricsConfig{Namespace: "q"})
m.GaugeFunc("depth", "Queue depth", func() float64 { return 7 })
m.CounterFunc("processed_total", "Processed", func() float64 { return 3 })
body := scrape(t, m)
if !strings.Contains(body, "q_depth 7") || !strings.Contains(body, "q_processed_total 3") {
t.Errorf("func metrics missing:\n%s", body)
}
}
func TestMetrics_DBStats(t *testing.T) {
m := NewMetrics(MetricsConfig{})
// sql.OpenDB does not dial, so no real database is needed.
db := sql.OpenDB(nopConnector{})
defer db.Close()
m.RegisterDBStats(db, "main")
m.RegisterDBStats(db, "main") // idempotent
if !strings.Contains(scrape(t, m), `go_sql_max_open_connections{db_name="main"}`) {
t.Error("db stats missing")
}
}
func TestMetrics_Exemplars(t *testing.T) {
m := NewMetrics(MetricsConfig{})
h := m.Histogram("latency_seconds", "Latency", nil)
tp := sdktrace.NewTracerProvider()
ctx, span := tp.Tracer("t").Start(context.Background(), "op")
observe(ctx, h.WithLabelValues(), 0.2)
span.End()
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/metrics", nil)
r.Header.Set("Accept", "application/openmetrics-text; version=1.0.0")
m.Handler().ServeHTTP(w, r)
if !strings.Contains(w.Body.String(), `# {trace_id="`+span.SpanContext().TraceID().String()+`"} 0.2`) {
t.Errorf("exemplar missing from OpenMetrics output:\n%s", w.Body.String())
}
}
type nopConnector struct{}
func (nopConnector) Connect(context.Context) (driver.Conn, error) { return nil, errors.New("no db") }
func (nopConnector) Driver() driver.Driver { return nil }