forked from mtusi2026/mtusi2026lab7
на оценку 4
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
apptelemetry "mtusi2026lab7/internal/telemetry"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type responseWriterInterceptor struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
}
|
||||
|
||||
func (w *responseWriterInterceptor) WriteHeader(code int) {
|
||||
w.statusCode = code
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func Observability(runtime *apptelemetry.Runtime, next http.Handler) (http.Handler, error) {
|
||||
requests, err := runtime.Meter.Int64Counter(
|
||||
"messenger.http.server.requests",
|
||||
metric.WithDescription("Number of completed HTTP requests"),
|
||||
metric.WithUnit("{request}"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create HTTP request counter: %w", err)
|
||||
}
|
||||
duration, err := runtime.Meter.Float64Histogram(
|
||||
"messenger.http.server.duration",
|
||||
metric.WithDescription("HTTP server request duration"),
|
||||
metric.WithUnit("s"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create HTTP duration histogram: %w", err)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/health/") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
start := time.Now()
|
||||
parent := otel.GetTextMapPropagator().Extract(
|
||||
r.Context(),
|
||||
propagation.HeaderCarrier(r.Header),
|
||||
)
|
||||
ctx, span := runtime.Tracer.Start(
|
||||
parent,
|
||||
r.Method+" HTTP request",
|
||||
trace.WithSpanKind(trace.SpanKindServer),
|
||||
trace.WithAttributes(attribute.String("http.request.method", r.Method)),
|
||||
)
|
||||
defer span.End()
|
||||
interceptor := &responseWriterInterceptor{
|
||||
ResponseWriter: w,
|
||||
statusCode: http.StatusOK,
|
||||
}
|
||||
observedRequest := r.WithContext(ctx)
|
||||
next.ServeHTTP(interceptor, observedRequest)
|
||||
|
||||
route := observedRequest.Pattern
|
||||
if route == "" {
|
||||
route = "unmatched"
|
||||
}
|
||||
statusClass := strconv.Itoa(interceptor.statusCode/100) + "xx"
|
||||
elapsed := time.Since(start)
|
||||
labels := metric.WithAttributes(
|
||||
attribute.String("method", r.Method),
|
||||
attribute.String("route", route),
|
||||
attribute.String("status_class", statusClass),
|
||||
)
|
||||
requests.Add(ctx, 1, labels)
|
||||
duration.Record(ctx, elapsed.Seconds(), labels)
|
||||
|
||||
span.SetName(r.Method + " " + route)
|
||||
span.SetAttributes(
|
||||
attribute.String("http.route", route),
|
||||
attribute.Int("http.response.status_code", interceptor.statusCode),
|
||||
)
|
||||
if interceptor.statusCode >= http.StatusInternalServerError {
|
||||
span.SetStatus(codes.Error, "server error")
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
|
||||
runtime.Logger.Info(ctx, "http_request_completed", apptelemetry.Fields{
|
||||
"method": r.Method,
|
||||
"route": route,
|
||||
"status_code": interceptor.statusCode,
|
||||
"status_class": statusClass,
|
||||
"duration_ms": float64(elapsed.Microseconds()) / 1000,
|
||||
"request_id": GetRequestID(ctx),
|
||||
})
|
||||
}), nil
|
||||
}
|
||||
Reference in New Issue
Block a user