mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 16:26:38 +00:00
Add basic signal metrics (#2107)
This commit is contained in:
124
signal/metrics/app.go
Normal file
124
signal/metrics/app.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
// AppMetrics holds all the application metrics
|
||||
type AppMetrics struct {
|
||||
metric.Meter
|
||||
|
||||
ActivePeers metric.Int64UpDownCounter
|
||||
PeerConnectionDuration metric.Int64Histogram
|
||||
|
||||
Registrations metric.Int64Counter
|
||||
Deregistrations metric.Int64Counter
|
||||
RegistrationFailures metric.Int64Counter
|
||||
RegistrationDelay metric.Float64Histogram
|
||||
|
||||
MessagesForwarded metric.Int64Counter
|
||||
MessageForwardFailures metric.Int64Counter
|
||||
MessageForwardLatency metric.Float64Histogram
|
||||
}
|
||||
|
||||
func NewAppMetrics(meter metric.Meter) (*AppMetrics, error) {
|
||||
activePeers, err := meter.Int64UpDownCounter("active_peers")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
peerConnectionDuration, err := meter.Int64Histogram("peer_connection_duration_seconds",
|
||||
metric.WithExplicitBucketBoundaries(getPeerConnectionDurationBucketBoundaries()...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
registrations, err := meter.Int64Counter("registrations_total")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deregistrations, err := meter.Int64Counter("deregistrations_total")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
registrationFailures, err := meter.Int64Counter("registration_failures_total")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
registrationDelay, err := meter.Float64Histogram("registration_delay_milliseconds",
|
||||
metric.WithExplicitBucketBoundaries(getStandardBucketBoundaries()...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messagesForwarded, err := meter.Int64Counter("messages_forwarded_total")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messageForwardFailures, err := meter.Int64Counter("message_forward_failures_total")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messageForwardLatency, err := meter.Float64Histogram("message_forward_latency_milliseconds",
|
||||
metric.WithExplicitBucketBoundaries(getStandardBucketBoundaries()...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &AppMetrics{
|
||||
Meter: meter,
|
||||
|
||||
ActivePeers: activePeers,
|
||||
PeerConnectionDuration: peerConnectionDuration,
|
||||
|
||||
Registrations: registrations,
|
||||
Deregistrations: deregistrations,
|
||||
RegistrationFailures: registrationFailures,
|
||||
RegistrationDelay: registrationDelay,
|
||||
|
||||
MessagesForwarded: messagesForwarded,
|
||||
MessageForwardFailures: messageForwardFailures,
|
||||
MessageForwardLatency: messageForwardLatency,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getStandardBucketBoundaries() []float64 {
|
||||
return []float64{
|
||||
0.1,
|
||||
0.5,
|
||||
1,
|
||||
5,
|
||||
10,
|
||||
50,
|
||||
100,
|
||||
500,
|
||||
1000,
|
||||
5000,
|
||||
10000,
|
||||
}
|
||||
}
|
||||
func getPeerConnectionDurationBucketBoundaries() []float64 {
|
||||
return []float64{
|
||||
1,
|
||||
60,
|
||||
// 10m
|
||||
600,
|
||||
// 1h
|
||||
3600,
|
||||
// 2h,
|
||||
7200,
|
||||
// 6h,
|
||||
21600,
|
||||
// 12h,
|
||||
43200,
|
||||
// 24h,
|
||||
86400,
|
||||
// 48h,
|
||||
172800,
|
||||
}
|
||||
}
|
||||
74
signal/metrics/metrics.go
Normal file
74
signal/metrics/metrics.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
prometheus2 "github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/exporters/prometheus"
|
||||
api "go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/sdk/metric"
|
||||
)
|
||||
|
||||
const defaultEndpoint = "/metrics"
|
||||
|
||||
// Metrics holds the metrics information and exposes it
|
||||
type Metrics struct {
|
||||
Meter api.Meter
|
||||
provider *metric.MeterProvider
|
||||
Endpoint string
|
||||
|
||||
*http.Server
|
||||
}
|
||||
|
||||
// NewServer initializes and returns a new Metrics instance
|
||||
func NewServer(port int, endpoint string) *Metrics {
|
||||
exporter, err := prometheus.New()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
provider := metric.NewMeterProvider(metric.WithReader(exporter))
|
||||
otel.SetMeterProvider(provider)
|
||||
|
||||
pkg := reflect.TypeOf(defaultEndpoint).PkgPath()
|
||||
meter := provider.Meter(pkg)
|
||||
|
||||
if endpoint == "" {
|
||||
endpoint = defaultEndpoint
|
||||
}
|
||||
|
||||
router := http.NewServeMux()
|
||||
router.Handle(endpoint, promhttp.HandlerFor(
|
||||
prometheus2.DefaultGatherer,
|
||||
promhttp.HandlerOpts{EnableOpenMetrics: true}))
|
||||
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Handler: router,
|
||||
}
|
||||
|
||||
return &Metrics{
|
||||
Meter: meter,
|
||||
provider: provider,
|
||||
Endpoint: endpoint,
|
||||
Server: server,
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown stops the metrics server
|
||||
func (m *Metrics) Shutdown(ctx context.Context) error {
|
||||
if err := m.Server.Shutdown(ctx); err != nil {
|
||||
return fmt.Errorf("http server: %w", err)
|
||||
}
|
||||
|
||||
if err := m.provider.Shutdown(ctx); err != nil {
|
||||
return fmt.Errorf("meter provider: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user