2022-10-22 13:29:39 +02:00
|
|
|
package telemetry
|
2022-10-21 16:24:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-05-19 11:42:25 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2024-05-27 08:39:18 +02:00
|
|
|
"time"
|
2023-05-19 11:42:25 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
"github.com/google/uuid"
|
2024-08-23 18:42:55 +02:00
|
|
|
"github.com/gorilla/mux"
|
2022-10-21 16:24:13 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-05-19 16:56:15 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2022-10-22 13:29:39 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2024-07-03 11:33:02 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/formatter"
|
|
|
|
nbContext "github.com/netbirdio/netbird/management/server/context"
|
2022-10-21 16:24:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
httpRequestCounterPrefix = "management.http.request.counter"
|
|
|
|
httpResponseCounterPrefix = "management.http.response.counter"
|
2023-05-19 11:42:25 +02:00
|
|
|
httpRequestDurationPrefix = "management.http.request.duration.ms"
|
2022-10-21 16:24:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// WrappedResponseWriter is a wrapper for http.ResponseWriter that allows the
|
|
|
|
// written HTTP status code to be captured for metrics reporting or logging purposes.
|
|
|
|
type WrappedResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
status int
|
|
|
|
wroteHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapResponseWriter wraps original http.ResponseWriter
|
|
|
|
func WrapResponseWriter(w http.ResponseWriter) *WrappedResponseWriter {
|
|
|
|
return &WrappedResponseWriter{ResponseWriter: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns response status
|
|
|
|
func (rw *WrappedResponseWriter) Status() int {
|
|
|
|
return rw.status
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader wraps http.ResponseWriter.WriteHeader method
|
|
|
|
func (rw *WrappedResponseWriter) WriteHeader(code int) {
|
|
|
|
if rw.wroteHeader {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.status = code
|
|
|
|
rw.ResponseWriter.WriteHeader(code)
|
|
|
|
rw.wroteHeader = true
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:50:21 +02:00
|
|
|
// HTTPMiddleware handler used to collect metrics of every request/response coming to the API.
|
2022-10-21 16:24:13 +02:00
|
|
|
// Also adds request tracing (logging).
|
2022-10-22 11:50:21 +02:00
|
|
|
type HTTPMiddleware struct {
|
2024-08-23 18:42:55 +02:00
|
|
|
ctx context.Context
|
2023-05-19 11:42:25 +02:00
|
|
|
// all HTTP requests by endpoint & method
|
2024-08-23 18:42:55 +02:00
|
|
|
httpRequestCounter metric.Int64Counter
|
2023-05-19 11:42:25 +02:00
|
|
|
// all HTTP responses by endpoint & method & status code
|
2024-08-23 18:42:55 +02:00
|
|
|
httpResponseCounter metric.Int64Counter
|
2022-10-22 10:07:13 +02:00
|
|
|
// all HTTP requests
|
2024-05-27 08:39:18 +02:00
|
|
|
totalHTTPRequestsCounter metric.Int64Counter
|
2022-10-22 10:07:13 +02:00
|
|
|
// all HTTP responses
|
2024-05-27 08:39:18 +02:00
|
|
|
totalHTTPResponseCounter metric.Int64Counter
|
2022-10-22 10:07:13 +02:00
|
|
|
// all HTTP responses by status code
|
2024-08-23 18:42:55 +02:00
|
|
|
totalHTTPResponseCodeCounter metric.Int64Counter
|
2023-05-19 11:42:25 +02:00
|
|
|
// all HTTP requests durations by endpoint and method
|
2024-08-23 18:42:55 +02:00
|
|
|
httpRequestDuration metric.Int64Histogram
|
2023-05-19 11:42:25 +02:00
|
|
|
// all HTTP requests durations
|
2024-05-27 08:39:18 +02:00
|
|
|
totalHTTPRequestDuration metric.Int64Histogram
|
2023-05-19 11:42:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetricsMiddleware creates a new HTTPMiddleware
|
|
|
|
func NewMetricsMiddleware(ctx context.Context, meter metric.Meter) (*HTTPMiddleware, error) {
|
2024-08-23 18:42:55 +02:00
|
|
|
httpRequestCounter, err := meter.Int64Counter(httpRequestCounterPrefix, metric.WithUnit("1"))
|
2023-05-19 11:42:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-27 08:39:18 +02:00
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
httpResponseCounter, err := meter.Int64Counter(httpResponseCounterPrefix, metric.WithUnit("1"))
|
2023-05-19 11:42:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
totalHTTPRequestsCounter, err := meter.Int64Counter(fmt.Sprintf("%s.total", httpRequestCounterPrefix), metric.WithUnit("1"))
|
2023-05-19 11:42:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
totalHTTPResponseCounter, err := meter.Int64Counter(fmt.Sprintf("%s.total", httpResponseCounterPrefix), metric.WithUnit("1"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-21 16:24:13 +02:00
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
totalHTTPResponseCodeCounter, err := meter.Int64Counter(fmt.Sprintf("%s.code.total", httpResponseCounterPrefix), metric.WithUnit("1"))
|
2022-10-21 16:24:13 +02:00
|
|
|
if err != nil {
|
2024-08-23 18:42:55 +02:00
|
|
|
return nil, err
|
2022-10-21 16:24:13 +02:00
|
|
|
}
|
2024-05-27 08:39:18 +02:00
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
httpRequestDuration, err := meter.Int64Histogram(httpRequestDurationPrefix, metric.WithUnit("milliseconds"))
|
2023-05-19 11:42:25 +02:00
|
|
|
if err != nil {
|
2024-08-23 18:42:55 +02:00
|
|
|
return nil, err
|
2023-05-19 11:42:25 +02:00
|
|
|
}
|
2022-10-22 10:07:13 +02:00
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
totalHTTPRequestDuration, err := meter.Int64Histogram(fmt.Sprintf("%s.total", httpRequestDurationPrefix), metric.WithUnit("milliseconds"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-10-21 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
return &HTTPMiddleware{
|
|
|
|
ctx: ctx,
|
|
|
|
httpRequestCounter: httpRequestCounter,
|
|
|
|
httpResponseCounter: httpResponseCounter,
|
|
|
|
httpRequestDuration: httpRequestDuration,
|
|
|
|
totalHTTPResponseCodeCounter: totalHTTPResponseCodeCounter,
|
|
|
|
totalHTTPRequestsCounter: totalHTTPRequestsCounter,
|
|
|
|
totalHTTPResponseCounter: totalHTTPResponseCounter,
|
|
|
|
totalHTTPRequestDuration: totalHTTPRequestDuration,
|
|
|
|
},
|
|
|
|
nil
|
2022-10-21 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
func replaceEndpointChars(endpoint string) string {
|
|
|
|
endpoint = strings.ReplaceAll(endpoint, "{", "")
|
|
|
|
endpoint = strings.ReplaceAll(endpoint, "}", "")
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
func getEndpointMetricAttr(r *http.Request) string {
|
|
|
|
var endpoint string
|
|
|
|
route := mux.CurrentRoute(r)
|
|
|
|
if route != nil {
|
|
|
|
pathTmpl, err := route.GetPathTemplate()
|
|
|
|
if err == nil {
|
|
|
|
endpoint = replaceEndpointChars(pathTmpl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return endpoint
|
2022-10-21 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handler logs every request and response and adds the, to metrics.
|
2022-10-22 11:50:21 +02:00
|
|
|
func (m *HTTPMiddleware) Handler(h http.Handler) http.Handler {
|
2022-10-21 16:24:13 +02:00
|
|
|
fn := func(rw http.ResponseWriter, r *http.Request) {
|
2023-05-19 11:42:25 +02:00
|
|
|
reqStart := time.Now()
|
2024-07-03 11:33:02 +02:00
|
|
|
|
|
|
|
//nolint
|
|
|
|
ctx := context.WithValue(r.Context(), formatter.ExecutionContextKey, formatter.HTTPSource)
|
|
|
|
|
|
|
|
reqID := uuid.New().String()
|
|
|
|
//nolint
|
|
|
|
ctx = context.WithValue(ctx, nbContext.RequestIDKey, reqID)
|
|
|
|
|
|
|
|
log.WithContext(ctx).Tracef("HTTP request %v: %v %v", reqID, r.Method, r.URL)
|
2022-10-21 16:24:13 +02:00
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
endpointAttr := attribute.String("endpoint", getEndpointMetricAttr(r))
|
|
|
|
methodAttr := attribute.String("method", r.Method)
|
2022-10-21 16:24:13 +02:00
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
m.httpRequestCounter.Add(m.ctx, 1, metric.WithAttributes(endpointAttr, methodAttr))
|
2022-10-22 10:07:13 +02:00
|
|
|
m.totalHTTPRequestsCounter.Add(m.ctx, 1)
|
2022-10-21 16:24:13 +02:00
|
|
|
|
|
|
|
w := WrapResponseWriter(rw)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
h.ServeHTTP(w, r.WithContext(ctx))
|
2022-10-21 16:24:13 +02:00
|
|
|
|
|
|
|
if w.Status() > 399 {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("HTTP response %v: %v %v status %v", reqID, r.Method, r.URL, w.Status())
|
2022-10-21 16:24:13 +02:00
|
|
|
} else {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Tracef("HTTP response %v: %v %v status %v", reqID, r.Method, r.URL, w.Status())
|
2022-10-21 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
statusCodeAttr := attribute.Int("code", w.Status())
|
2022-10-22 10:07:13 +02:00
|
|
|
|
2024-08-23 18:42:55 +02:00
|
|
|
m.httpResponseCounter.Add(m.ctx, 1, metric.WithAttributes(endpointAttr, methodAttr, statusCodeAttr))
|
2022-10-22 10:07:13 +02:00
|
|
|
m.totalHTTPResponseCounter.Add(m.ctx, 1)
|
2024-08-23 18:42:55 +02:00
|
|
|
m.totalHTTPResponseCodeCounter.Add(m.ctx, 1, metric.WithAttributes(statusCodeAttr))
|
2023-05-19 11:42:25 +02:00
|
|
|
|
|
|
|
reqTook := time.Since(reqStart)
|
2024-08-23 18:42:55 +02:00
|
|
|
m.httpRequestDuration.Record(m.ctx, reqTook.Milliseconds(), metric.WithAttributes(endpointAttr, methodAttr))
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("request %s %s took %d ms and finished with status %d", r.Method, r.URL.Path, reqTook.Milliseconds(), w.Status())
|
2023-05-22 16:26:36 +02:00
|
|
|
|
|
|
|
if w.Status() == 200 && (r.Method == http.MethodPut || r.Method == http.MethodPost || r.Method == http.MethodDelete) {
|
2024-05-27 08:39:18 +02:00
|
|
|
opts := metric.WithAttributeSet(attribute.NewSet(attribute.String("type", "write")))
|
|
|
|
m.totalHTTPRequestDuration.Record(m.ctx, reqTook.Milliseconds(), opts)
|
2023-05-22 16:26:36 +02:00
|
|
|
} else {
|
2024-05-27 08:39:18 +02:00
|
|
|
opts := metric.WithAttributeSet(attribute.NewSet(attribute.String("type", "read")))
|
|
|
|
m.totalHTTPRequestDuration.Record(m.ctx, reqTook.Milliseconds(), opts)
|
2023-05-22 16:26:36 +02:00
|
|
|
}
|
2023-05-19 11:42:25 +02:00
|
|
|
|
2022-10-21 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|