forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
otellog "go.opentelemetry.io/otel/log"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Start records a real operation without copying error text, credentials or business data.
|
||||
func (r *Runtime) Start(ctx context.Context, name, kind string) (context.Context, func(error)) {
|
||||
spanKind := trace.SpanKindInternal
|
||||
switch kind {
|
||||
case "server":
|
||||
spanKind = trace.SpanKindServer
|
||||
case "producer":
|
||||
spanKind = trace.SpanKindProducer
|
||||
case "consumer":
|
||||
spanKind = trace.SpanKindConsumer
|
||||
case "client":
|
||||
spanKind = trace.SpanKindClient
|
||||
}
|
||||
ctx, span := r.tracer.Start(ctx, name, trace.WithSpanKind(spanKind))
|
||||
return ctx, func(err error) {
|
||||
outcome := "ok"
|
||||
if err != nil {
|
||||
outcome = "error"
|
||||
span.SetStatus(codes.Error, "operation failed")
|
||||
}
|
||||
r.event(ctx, name, outcome, "")
|
||||
span.End()
|
||||
}
|
||||
}
|
||||
|
||||
// Extract restores only W3C trace headers, never arbitrary baggage.
|
||||
func (r *Runtime) Extract(ctx context.Context, parent, state string) context.Context {
|
||||
return propagation.TraceContext{}.Extract(ctx, propagation.MapCarrier{"traceparent": parent, "tracestate": state})
|
||||
}
|
||||
|
||||
// Inject returns the bounded W3C carrier for a durable asynchronous handoff.
|
||||
func (r *Runtime) Inject(ctx context.Context) (string, string) {
|
||||
carrier := propagation.MapCarrier{}
|
||||
propagation.TraceContext{}.Inject(ctx, carrier)
|
||||
return carrier["traceparent"], carrier["tracestate"]
|
||||
}
|
||||
|
||||
// HTTP records one completed request with bounded attributes and a correlated log event.
|
||||
func (r *Runtime) HTTP(ctx context.Context, method, route string, status int, elapsed time.Duration, requestID string) {
|
||||
if len(route) > 128 || !strings.HasPrefix(route, "/") {
|
||||
route = "unmatched"
|
||||
}
|
||||
switch method {
|
||||
case "GET", "POST", "DELETE", "HEAD", "OPTIONS":
|
||||
default:
|
||||
method = "other"
|
||||
}
|
||||
class := "5xx"
|
||||
switch status / 100 {
|
||||
case 2:
|
||||
class = "2xx"
|
||||
case 3:
|
||||
class = "3xx"
|
||||
case 4:
|
||||
class = "4xx"
|
||||
}
|
||||
span := trace.SpanFromContext(ctx)
|
||||
span.SetName(method + " " + route)
|
||||
span.SetAttributes(attribute.String("http.route", route), attribute.String("http.request.method", method), attribute.Int("http.response.status_code", status))
|
||||
if status >= 500 {
|
||||
span.SetStatus(codes.Error, "server error")
|
||||
}
|
||||
r.recordHTTP(ctx, method, route, class, elapsed.Seconds())
|
||||
r.event(ctx, "http.request", class, requestID)
|
||||
}
|
||||
|
||||
// event emits only an allowlisted technical record; SDK attaches trace/span IDs from context.
|
||||
func (r *Runtime) event(ctx context.Context, event, outcome, requestID string) {
|
||||
var record otellog.Record
|
||||
record.SetTimestamp(time.Now())
|
||||
record.SetSeverity(otellog.SeverityInfo)
|
||||
record.SetBody(otellog.StringValue(event))
|
||||
record.AddAttributes(otellog.String("event", event), otellog.String("outcome", outcome))
|
||||
if requestID != "" {
|
||||
record.AddAttributes(otellog.String("request_id", requestID))
|
||||
}
|
||||
r.logger.Emit(ctx, record)
|
||||
}
|
||||
Reference in New Issue
Block a user