lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
var traceparentPattern = regexp.MustCompile(`^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$`)
|
||||
|
||||
// ValidTraceparent reports whether a broker-carried traceparent matches the
|
||||
// closed schema before it is trusted as a parent context.
|
||||
func ValidTraceparent(value string) bool {
|
||||
return value == "" || (len(value) <= 512 && traceparentPattern.MatchString(value))
|
||||
}
|
||||
|
||||
// Inject serializes the current span context for a durable outbox payload.
|
||||
func Inject(ctx context.Context) (traceparent, tracestate string) {
|
||||
carrier := propagation.MapCarrier{}
|
||||
otel.GetTextMapPropagator().Inject(ctx, carrier)
|
||||
tracestate = carrier["tracestate"]
|
||||
if len(tracestate) > 512 {
|
||||
tracestate = ""
|
||||
}
|
||||
return carrier["traceparent"], tracestate
|
||||
}
|
||||
|
||||
// Extract restores a parent context from a broker payload. Invalid values are
|
||||
// ignored; the caller still gets a usable context.
|
||||
func Extract(ctx context.Context, traceparent, tracestate string) context.Context {
|
||||
if !ValidTraceparent(traceparent) || len(tracestate) > 512 {
|
||||
return ctx
|
||||
}
|
||||
carrier := propagation.MapCarrier{"traceparent": traceparent}
|
||||
if tracestate != "" {
|
||||
carrier["tracestate"] = tracestate
|
||||
}
|
||||
return otel.GetTextMapPropagator().Extract(ctx, carrier)
|
||||
}
|
||||
|
||||
// StartPublishSpan starts the producer span for an outbox publication. The
|
||||
// returned context carries the span that must be injected into the message.
|
||||
func StartPublishSpan(ctx context.Context, message domain.JobMessage) (context.Context, trace.Span) {
|
||||
parent := Extract(ctx, message.Traceparent, message.Tracestate)
|
||||
return otel.Tracer("messenger/queue").Start(parent, "job.publish",
|
||||
trace.WithSpanKind(trace.SpanKindProducer),
|
||||
trace.WithAttributes(
|
||||
attribute.String("messaging.system", "rabbitmq"),
|
||||
attribute.String("messaging.operation", "publish"),
|
||||
attribute.String("messaging.destination.name", message.Type),
|
||||
attribute.String("job.id", message.JobID.String()),
|
||||
attribute.String("job.type", message.Type),
|
||||
attribute.Int("job.attempt", message.Attempt),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// StartRequeueSpanForJob starts a per-job reaper/reconcile span. The durable
|
||||
// job keeps the original upload trace context, so a retry remains linked to
|
||||
// attempt 1 instead of the relay's batch-wide reaper span.
|
||||
func StartRequeueSpanForJob(ctx context.Context, job domain.Job) (context.Context, trace.Span) {
|
||||
parent := Extract(ctx, job.Traceparent, job.Tracestate)
|
||||
return otel.Tracer("messenger/relay").Start(parent, "job.requeue",
|
||||
trace.WithAttributes(
|
||||
attribute.String("job.id", job.ID.String()),
|
||||
attribute.String("job.type", domain.JobType),
|
||||
attribute.Int("job.attempt", job.Attempt),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// StartConsumerSpan starts the worker span as a child of the publish span.
|
||||
func StartConsumerSpan(ctx context.Context, message domain.JobMessage) (context.Context, trace.Span) {
|
||||
parent := Extract(ctx, message.Traceparent, message.Tracestate)
|
||||
return otel.Tracer("messenger/worker").Start(parent, "job.process",
|
||||
trace.WithSpanKind(trace.SpanKindConsumer),
|
||||
trace.WithAttributes(
|
||||
attribute.String("messaging.system", "rabbitmq"),
|
||||
attribute.String("messaging.operation", "process"),
|
||||
attribute.String("messaging.destination.name", message.Type),
|
||||
attribute.String("job.id", message.JobID.String()),
|
||||
attribute.String("job.type", message.Type),
|
||||
attribute.Int("job.attempt", message.Attempt),
|
||||
attribute.Int("pipeline.version", message.PipelineVersion),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// StartRequeueSpan covers the relay's lease reaper / reconciliation path.
|
||||
func StartRequeueSpan(ctx context.Context, operation string) (context.Context, trace.Span) {
|
||||
return otel.Tracer("messenger/relay").Start(ctx, "job."+operation,
|
||||
trace.WithAttributes(attribute.String("job.operation", operation)),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user