forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
collectorlogs "go.opentelemetry.io/proto/otlp/collector/logs/v1"
|
||||
collectormetrics "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
|
||||
collectortrace "go.opentelemetry.io/proto/otlp/collector/trace/v1"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/observation"
|
||||
)
|
||||
|
||||
type capture struct {
|
||||
mu sync.Mutex
|
||||
traces []*collectortrace.ExportTraceServiceRequest
|
||||
logs []*collectorlogs.ExportLogsServiceRequest
|
||||
metrics []*collectormetrics.ExportMetricsServiceRequest
|
||||
}
|
||||
|
||||
// serve captures actual OTLP requests without relying on SDK implementation internals.
|
||||
func (c *capture) serve(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "read failed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
switch r.URL.Path {
|
||||
case "/v1/traces":
|
||||
message := &collectortrace.ExportTraceServiceRequest{}
|
||||
if err := proto.Unmarshal(data, message); err != nil {
|
||||
http.Error(w, "invalid OTLP", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
c.traces = append(c.traces, message)
|
||||
case "/v1/logs":
|
||||
message := &collectorlogs.ExportLogsServiceRequest{}
|
||||
if err := proto.Unmarshal(data, message); err != nil {
|
||||
http.Error(w, "invalid OTLP", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
c.logs = append(c.logs, message)
|
||||
case "/v1/metrics":
|
||||
message := &collectormetrics.ExportMetricsServiceRequest{}
|
||||
if err := proto.Unmarshal(data, message); err != nil {
|
||||
http.Error(w, "invalid OTLP", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
c.metrics = append(c.metrics, message)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/x-protobuf")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
type snapshotFixture struct {
|
||||
err error
|
||||
}
|
||||
|
||||
// Snapshot supplies safe bounded saturation data for the exporter integration test.
|
||||
func (s snapshotFixture) Snapshot(context.Context) (observation.Snapshot, error) {
|
||||
return observation.Snapshot{QueueDepth: 2, OldestSeconds: 12, PoolUsed: 1}, s.err
|
||||
}
|
||||
|
||||
// TestOTLPSignalsShareContext verifies real HTTP export, parentage, metrics and structured-log correlation.
|
||||
func TestOTLPSignalsShareContext(t *testing.T) {
|
||||
capture := &capture{}
|
||||
server := httptest.NewServer(http.HandlerFunc(capture.serve))
|
||||
defer server.Close()
|
||||
r, err := New(t.Context(), &config.Telemetry{Endpoint: server.URL, Service: "messenger-worker", Version: "test", Instance: "synthetic", SampleRatio: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.ObserveSaturation(snapshotFixture{}, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, finish := r.Start(t.Context(), "http.request", "server")
|
||||
r.HTTP(ctx, "POST", "/api/v1/chats/:chat_id/attachments", 202, time.Millisecond, "synthetic-request")
|
||||
parent, state := r.Inject(ctx)
|
||||
producer, endProducer := r.Start(r.Extract(t.Context(), parent, state), "attachment.publish", "producer")
|
||||
parent, state = r.Inject(producer)
|
||||
consumer, endConsumer := r.Start(r.Extract(t.Context(), parent, state), "attachment.process", "consumer")
|
||||
r.JobStarted()
|
||||
r.JobFinished(consumer, time.Millisecond, nil)
|
||||
r.Ready(consumer, time.Now().Add(-time.Second))
|
||||
query := r.TraceQueryStart(consumer, nil, pgx.TraceQueryStartData{SQL: "secret SQL that must not be exported"})
|
||||
r.TraceQueryEnd(query, nil, pgx.TraceQueryEndData{Err: errors.New("secret database password")})
|
||||
acquire := r.TraceAcquireStart(consumer, nil, pgxpool.TraceAcquireStartData{})
|
||||
r.TraceAcquireEnd(acquire, nil, pgxpool.TraceAcquireEndData{})
|
||||
endConsumer(nil)
|
||||
endProducer(nil)
|
||||
finish(nil)
|
||||
if err := r.Shutdown(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
capture.mu.Lock()
|
||||
defer capture.mu.Unlock()
|
||||
if len(capture.traces) == 0 || len(capture.logs) == 0 || len(capture.metrics) == 0 {
|
||||
t.Fatal("one OTLP signal is absent")
|
||||
}
|
||||
spans := 0
|
||||
correlatedLogs := 0
|
||||
for _, request := range capture.traces {
|
||||
if strings.Contains(request.String(), "secret") {
|
||||
t.Fatal("trace leaked dependency payload")
|
||||
}
|
||||
for _, resource := range request.ResourceSpans {
|
||||
for _, scope := range resource.ScopeSpans {
|
||||
spans += len(scope.Spans)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, request := range capture.logs {
|
||||
if strings.Contains(request.String(), "secret") {
|
||||
t.Fatal("log leaked dependency payload")
|
||||
}
|
||||
for _, resource := range request.ResourceLogs {
|
||||
for _, scope := range resource.ScopeLogs {
|
||||
for _, record := range scope.LogRecords {
|
||||
if len(record.TraceId) == 16 && len(record.SpanId) == 8 {
|
||||
correlatedLogs++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if spans < 4 || correlatedLogs < 4 {
|
||||
t.Fatal("causal operations or correlated logs missing")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHTTPBoundedDimensions covers status classes and rejects arbitrary method labels.
|
||||
func TestHTTPBoundedDimensions(t *testing.T) {
|
||||
r, err := New(t.Context(), &config.Telemetry{Service: "api", SampleRatio: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, finish := r.Start(t.Context(), "request", "internal")
|
||||
for _, status := range []int{200, 302, 401, 503} {
|
||||
r.HTTP(ctx, "custom-method", "bad-route", status, time.Millisecond, "")
|
||||
}
|
||||
r.JobStarted()
|
||||
r.JobFinished(ctx, time.Millisecond, errors.New("safe error"))
|
||||
r.TraceQueryEnd(ctx, nil, pgx.TraceQueryEndData{})
|
||||
finish(errors.New("safe error"))
|
||||
if err := r.ObserveSaturation(snapshotFixture{err: errors.New("unavailable")}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.Shutdown(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnavailableBackendDoesNotBlockBusinessWork bounds exporter failures independently of requests.
|
||||
func TestUnavailableBackendDoesNotBlockBusinessWork(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusServiceUnavailable) }))
|
||||
defer server.Close()
|
||||
r, err := New(t.Context(), &config.Telemetry{Endpoint: server.URL, Service: "api", SampleRatio: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Now()
|
||||
for i := 0; i < 2000; i++ {
|
||||
ctx, end := r.Start(t.Context(), "safe-operation", "client")
|
||||
r.HTTP(ctx, "GET", "/api/v1/chats", 200, time.Millisecond, "synthetic")
|
||||
end(nil)
|
||||
}
|
||||
if time.Since(start) > 2*time.Second {
|
||||
t.Fatal("business path blocked on unavailable exporter")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
if err := r.Shutdown(ctx); err == nil {
|
||||
t.Fatal("backend outage was not visible at exporter shutdown")
|
||||
}
|
||||
}
|
||||
|
||||
// TestExporterInvalidPort rejects an unusable exporter URL without starting the application.
|
||||
func TestExporterInvalidPort(t *testing.T) {
|
||||
runtime, err := New(t.Context(), &config.Telemetry{Endpoint: "http://localhost:invalid", SampleRatio: 1})
|
||||
if err == nil {
|
||||
_ = runtime.Shutdown(t.Context())
|
||||
t.Fatal("invalid exporter port accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMissingRuntimeConfiguration returns an error instead of dereferencing a missing section.
|
||||
func TestMissingRuntimeConfiguration(t *testing.T) {
|
||||
if _, err := New(t.Context(), nil); err == nil {
|
||||
t.Fatal("missing configuration accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestInvalidRuntimeSampling prevents an SDK sampler panic for out-of-range ratios.
|
||||
func TestInvalidRuntimeSampling(t *testing.T) {
|
||||
if _, err := New(t.Context(), &config.Telemetry{SampleRatio: -1}); err == nil {
|
||||
t.Fatal("invalid sampling accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user