2022-10-22 15:06:54 +02:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-19 11:42:25 +02:00
|
|
|
"time"
|
|
|
|
|
2022-10-22 15:06:54 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GRPCMetrics are gRPC server metrics
|
|
|
|
type GRPCMetrics struct {
|
|
|
|
meter metric.Meter
|
2024-05-27 08:39:18 +02:00
|
|
|
syncRequestsCounter metric.Int64Counter
|
|
|
|
loginRequestsCounter metric.Int64Counter
|
|
|
|
getKeyRequestsCounter metric.Int64Counter
|
|
|
|
activeStreamsGauge metric.Int64ObservableGauge
|
|
|
|
syncRequestDuration metric.Int64Histogram
|
|
|
|
loginRequestDuration metric.Int64Histogram
|
|
|
|
channelQueueLength metric.Int64Histogram
|
2022-10-22 15:06:54 +02:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGRPCMetrics creates new GRPCMetrics struct and registers common metrics of the gRPC server
|
|
|
|
func NewGRPCMetrics(ctx context.Context, meter metric.Meter) (*GRPCMetrics, error) {
|
2024-05-27 08:39:18 +02:00
|
|
|
syncRequestsCounter, err := meter.Int64Counter("management.grpc.sync.request.counter", metric.WithUnit("1"))
|
2022-10-22 15:06:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-27 08:39:18 +02:00
|
|
|
|
|
|
|
loginRequestsCounter, err := meter.Int64Counter("management.grpc.login.request.counter", metric.WithUnit("1"))
|
2022-10-22 15:06:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-27 08:39:18 +02:00
|
|
|
|
|
|
|
getKeyRequestsCounter, err := meter.Int64Counter("management.grpc.key.request.counter", metric.WithUnit("1"))
|
2022-10-22 15:06:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
activeStreamsGauge, err := meter.Int64ObservableGauge("management.grpc.connected.streams", metric.WithUnit("1"))
|
2022-10-22 15:06:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
syncRequestDuration, err := meter.Int64Histogram("management.grpc.sync.request.duration.ms", metric.WithUnit("milliseconds"))
|
2023-05-19 11:42:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
loginRequestDuration, err := meter.Int64Histogram("management.grpc.login.request.duration.ms", metric.WithUnit("milliseconds"))
|
2023-05-19 11:42:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-25 10:23:43 +02:00
|
|
|
// We use histogram here as we have multiple channel at the same time and we want to see a slice at any given time
|
|
|
|
// Then we should be able to extract min, manx, mean and the percentiles.
|
|
|
|
// TODO(yury): This needs custom bucketing as we are interested in the values from 0 to server.channelBufferSize (100)
|
2024-05-27 08:39:18 +02:00
|
|
|
channelQueue, err := meter.Int64Histogram(
|
2023-09-25 10:23:43 +02:00
|
|
|
"management.grpc.updatechannel.queue",
|
2024-05-27 08:39:18 +02:00
|
|
|
metric.WithDescription("Number of update messages in the channel queue"),
|
|
|
|
metric.WithUnit("length"),
|
2023-09-25 10:23:43 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:06:54 +02:00
|
|
|
return &GRPCMetrics{
|
|
|
|
meter: meter,
|
|
|
|
syncRequestsCounter: syncRequestsCounter,
|
|
|
|
loginRequestsCounter: loginRequestsCounter,
|
|
|
|
getKeyRequestsCounter: getKeyRequestsCounter,
|
|
|
|
activeStreamsGauge: activeStreamsGauge,
|
2023-05-19 11:42:25 +02:00
|
|
|
syncRequestDuration: syncRequestDuration,
|
|
|
|
loginRequestDuration: loginRequestDuration,
|
2023-09-25 10:23:43 +02:00
|
|
|
channelQueueLength: channelQueue,
|
2022-10-22 15:06:54 +02:00
|
|
|
ctx: ctx,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountSyncRequest counts the number of gRPC sync requests coming to the gRPC API
|
|
|
|
func (grpcMetrics *GRPCMetrics) CountSyncRequest() {
|
|
|
|
grpcMetrics.syncRequestsCounter.Add(grpcMetrics.ctx, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountGetKeyRequest counts the number of gRPC get server key requests coming to the gRPC API
|
|
|
|
func (grpcMetrics *GRPCMetrics) CountGetKeyRequest() {
|
|
|
|
grpcMetrics.getKeyRequestsCounter.Add(grpcMetrics.ctx, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountLoginRequest counts the number of gRPC login requests coming to the gRPC API
|
|
|
|
func (grpcMetrics *GRPCMetrics) CountLoginRequest() {
|
|
|
|
grpcMetrics.loginRequestsCounter.Add(grpcMetrics.ctx, 1)
|
|
|
|
}
|
|
|
|
|
2023-05-19 11:42:25 +02:00
|
|
|
// CountLoginRequestDuration counts the duration of the login gRPC requests
|
|
|
|
func (grpcMetrics *GRPCMetrics) CountLoginRequestDuration(duration time.Duration) {
|
|
|
|
grpcMetrics.loginRequestDuration.Record(grpcMetrics.ctx, duration.Milliseconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountSyncRequestDuration counts the duration of the sync gRPC requests
|
|
|
|
func (grpcMetrics *GRPCMetrics) CountSyncRequestDuration(duration time.Duration) {
|
|
|
|
grpcMetrics.syncRequestDuration.Record(grpcMetrics.ctx, duration.Milliseconds())
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:06:54 +02:00
|
|
|
// RegisterConnectedStreams registers a function that collects number of active streams and feeds it to the metrics gauge.
|
|
|
|
func (grpcMetrics *GRPCMetrics) RegisterConnectedStreams(producer func() int64) error {
|
2024-05-27 08:39:18 +02:00
|
|
|
_, err := grpcMetrics.meter.RegisterCallback(
|
|
|
|
func(ctx context.Context, observer metric.Observer) error {
|
|
|
|
observer.ObserveInt64(grpcMetrics.activeStreamsGauge, producer())
|
|
|
|
return nil
|
2022-10-22 15:06:54 +02:00
|
|
|
},
|
2024-05-27 08:39:18 +02:00
|
|
|
grpcMetrics.activeStreamsGauge,
|
2022-10-22 15:06:54 +02:00
|
|
|
)
|
2024-05-27 08:39:18 +02:00
|
|
|
return err
|
2022-10-22 15:06:54 +02:00
|
|
|
}
|
2023-09-25 10:23:43 +02:00
|
|
|
|
|
|
|
// UpdateChannelQueueLength update the histogram that keep distribution of the update messages channel queue
|
2023-11-10 16:33:13 +01:00
|
|
|
func (metrics *GRPCMetrics) UpdateChannelQueueLength(length int) {
|
|
|
|
metrics.channelQueueLength.Record(metrics.ctx, int64(length))
|
2023-09-25 10:23:43 +02:00
|
|
|
}
|