Skeleton contracts / contracts (pull_request) Blocked by required conditions
Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"gavrilenko-sxdees/lab7/internal/telemetry"
|
|
)
|
|
|
|
type statusWriter struct {
|
|
http.ResponseWriter
|
|
status int
|
|
}
|
|
|
|
func (w *statusWriter) Unwrap() http.ResponseWriter {
|
|
return w.ResponseWriter
|
|
}
|
|
|
|
func (w *statusWriter) WriteHeader(status int) {
|
|
w.status = status
|
|
w.ResponseWriter.WriteHeader(status)
|
|
}
|
|
|
|
func Telemetry() func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
ctx, span := telemetry.Start(telemetry.Incoming(r), "http.request", trace.SpanKindServer)
|
|
defer span.End()
|
|
|
|
writer := &statusWriter{ResponseWriter: w, status: http.StatusOK}
|
|
next.ServeHTTP(writer, r.WithContext(ctx))
|
|
|
|
route := telemetry.RoutePattern(r)
|
|
if route == "/health/live" || route == "/health/ready" {
|
|
return
|
|
}
|
|
telemetry.HTTP(ctx, r.Method, route, writer.status, time.Since(start).Seconds())
|
|
if tid := telemetry.TraceID(ctx); tid != "" && tid != "00000000000000000000000000000000" {
|
|
w.Header().Set("X-Trace-Id", tid)
|
|
}
|
|
})
|
|
}
|
|
} |