feat: add OpenTelemetry instrumentation

This commit is contained in:
2026-11-01 17:06:32 +03:00
parent cea50c2cd6
commit 934dfefee7
49 changed files with 1113 additions and 286 deletions
+41 -70
View File
@@ -1,14 +1,15 @@
package httpapi
import (
"context"
"net/http"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
var (
@@ -17,80 +18,50 @@ var (
)
type metrics struct {
requests *prometheus.CounterVec
duration *prometheus.HistogramVec
inFlight prometheus.Gauge
replays prometheus.Counter
attachmentUploads *prometheus.CounterVec
attachmentDuration prometheus.Histogram
attachmentBytes prometheus.Counter
requests metric.Int64Counter
duration metric.Float64Histogram
inFlight metric.Int64UpDownCounter
replays metric.Int64Counter
attachmentUploads metric.Int64Counter
attachmentDuration metric.Float64Histogram
attachmentBytes metric.Int64Counter
handler http.Handler
}
func newMetrics() *metrics {
registry := prometheus.NewRegistry()
result := &metrics{
requests: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "messenger",
Subsystem: "http",
Name: "requests_total",
Help: "Completed HTTP requests.",
}, []string{"method", "route", "status_class"}),
duration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "messenger",
Subsystem: "http",
Name: "request_duration_seconds",
Help: "HTTP request duration in seconds.",
Buckets: requestDurationBuckets,
}, []string{"method", "route"}),
inFlight: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "messenger",
Subsystem: "http",
Name: "requests_in_flight",
Help: "HTTP requests currently being served.",
}),
replays: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "messenger",
Subsystem: "http",
Name: "idempotency_replays_total",
Help: "Message requests served from an idempotency record.",
}),
attachmentUploads: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "messenger",
Name: "attachment_uploads_total",
Help: "Completed attachment uploads by result.",
}, []string{"result"}),
attachmentDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "messenger",
Name: "attachment_upload_duration_seconds",
Help: "Attachment upload duration in seconds.",
Buckets: uploadDurationBuckets,
}),
attachmentBytes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "messenger",
Name: "attachment_upload_bytes_total",
Help: "Bytes accepted by successful attachment uploads.",
}),
func newMetrics(meter metric.Meter, handler http.Handler) *metrics {
if meter == nil {
meter = otel.Meter("messenger-api")
}
registry.MustRegister(
result.requests,
result.duration,
result.inFlight,
result.replays,
result.attachmentUploads,
result.attachmentDuration,
result.attachmentBytes,
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
if handler == nil {
handler = http.NotFoundHandler()
}
requests, _ := meter.Int64Counter("messenger_http_requests", metric.WithDescription("Completed HTTP requests."))
duration, _ := meter.Float64Histogram("messenger_http_request_duration",
metric.WithDescription("HTTP request duration."), metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(requestDurationBuckets...),
)
result.handler = promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
return result
inFlight, _ := meter.Int64UpDownCounter("messenger_http_requests_in_flight", metric.WithDescription("HTTP requests currently being served."))
replays, _ := meter.Int64Counter("messenger_http_idempotency_replays", metric.WithDescription("Message requests served from an idempotency record."))
attachmentUploads, _ := meter.Int64Counter("messenger_attachment_uploads", metric.WithDescription("Completed attachment uploads by result."))
attachmentDuration, _ := meter.Float64Histogram("messenger_attachment_upload_duration",
metric.WithDescription("Attachment upload duration."), metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(uploadDurationBuckets...),
)
attachmentBytes, _ := meter.Int64Counter("messenger_attachment_upload_bytes", metric.WithDescription("Bytes accepted by successful attachment uploads."), metric.WithUnit("By"))
return &metrics{
requests: requests, duration: duration, inFlight: inFlight, replays: replays,
attachmentUploads: attachmentUploads, attachmentDuration: attachmentDuration,
attachmentBytes: attachmentBytes, handler: handler,
}
}
func (m *metrics) observe(r *http.Request, status int, started time.Time) {
route := routeTemplate(r.URL.Path)
m.requests.WithLabelValues(r.Method, route, strconv.Itoa(status/100)+"xx").Inc()
m.duration.WithLabelValues(r.Method, route).Observe(time.Since(started).Seconds())
func (m *metrics) observe(ctx context.Context, r *http.Request, status int, started time.Time) {
attributes := metric.WithAttributes(
attribute.String("method", r.Method), attribute.String("route", routeTemplate(r.URL.Path)),
attribute.String("status_class", strconv.Itoa(status/100)+"xx"),
)
m.requests.Add(ctx, 1, attributes)
m.duration.Record(ctx, time.Since(started).Seconds(), attributes)
}
func routeTemplate(path string) string {