From 7c595e84934ae846d53dc0264b41614467c176f4 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Tue, 16 Jul 2024 15:36:51 +0200 Subject: [PATCH] Add get_registration_delay_milliseconds metric (#2275) --- signal/README.md | 3 +++ signal/metrics/app.go | 8 ++++++++ signal/server/signal.go | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/signal/README.md b/signal/README.md index dd2d761ad..2da47283e 100644 --- a/signal/README.md +++ b/signal/README.md @@ -90,6 +90,9 @@ The Signal Server exposes the following metrics in Prometheus format: - **registration_delay_milliseconds**: A Histogram metric that measures the time it took to register a peer in milliseconds. +- **get_registration_delay_milliseconds**: A Histogram metric that measures the time + it took to get a peer registration in + milliseconds. - **messages_forwarded_total**: A Counter metric that counts the total number of messages forwarded between peers. - **message_forward_failures_total**: A Counter metric that counts the total diff --git a/signal/metrics/app.go b/signal/metrics/app.go index fb882a5d4..f8be88be7 100644 --- a/signal/metrics/app.go +++ b/signal/metrics/app.go @@ -15,6 +15,7 @@ type AppMetrics struct { Deregistrations metric.Int64Counter RegistrationFailures metric.Int64Counter RegistrationDelay metric.Float64Histogram + GetRegistrationDelay metric.Float64Histogram MessagesForwarded metric.Int64Counter MessageForwardFailures metric.Int64Counter @@ -54,6 +55,12 @@ func NewAppMetrics(meter metric.Meter) (*AppMetrics, error) { return nil, err } + getRegistrationDelay, err := meter.Float64Histogram("get_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 @@ -80,6 +87,7 @@ func NewAppMetrics(meter metric.Meter) (*AppMetrics, error) { Deregistrations: deregistrations, RegistrationFailures: registrationFailures, RegistrationDelay: registrationDelay, + GetRegistrationDelay: getRegistrationDelay, MessagesForwarded: messagesForwarded, MessageForwardFailures: messageForwardFailures, diff --git a/signal/server/signal.go b/signal/server/signal.go index 02c49c31d..4ececafff 100644 --- a/signal/server/signal.go +++ b/signal/server/signal.go @@ -30,6 +30,10 @@ const ( labelErrorMissingId = "missing_id" labelErrorMissingMeta = "missing_meta" labelErrorFailedHeader = "failed_header" + + labelRegistrionStatus = "status" + labelRegistrationFound = "found" + labelRegistrationNotFound = "not_found" ) // Server an instance of a Signal server @@ -63,7 +67,10 @@ func (s *Server) Send(ctx context.Context, msg *proto.EncryptedMessage) (*proto. return nil, fmt.Errorf("peer %s is not registered", msg.Key) } + getRegistrationStart := time.Now() + if dstPeer, found := s.registry.Get(msg.RemoteKey); found { + s.metrics.GetRegistrationDelay.Record(ctx, float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeMessage), attribute.String(labelRegistrionStatus, labelRegistrationFound))) start := time.Now() //forward the message to the target peer if err := dstPeer.Stream.Send(msg); err != nil { @@ -76,6 +83,7 @@ func (s *Server) Send(ctx context.Context, msg *proto.EncryptedMessage) (*proto. s.metrics.MessagesForwarded.Add(context.Background(), 1) } } else { + s.metrics.GetRegistrationDelay.Record(ctx, float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeMessage), attribute.String(labelRegistrionStatus, labelRegistrationNotFound))) log.Debugf("message from peer [%s] can't be forwarded to peer [%s] because destination peer is not connected", msg.Key, msg.RemoteKey) //todo respond to the sender? @@ -125,8 +133,11 @@ func (s *Server) ConnectStream(stream proto.SignalExchange_ConnectStreamServer) log.Debugf("received a new message from peer [%s] to peer [%s]", p.Id, msg.RemoteKey) + getRegistrationStart := time.Now() + // lookup the target peer where the message is going to if dstPeer, found := s.registry.Get(msg.RemoteKey); found { + s.metrics.GetRegistrationDelay.Record(stream.Context(), float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream), attribute.String(labelRegistrionStatus, labelRegistrationFound))) start := time.Now() //forward the message to the target peer if err := dstPeer.Stream.Send(msg); err != nil { @@ -139,10 +150,10 @@ func (s *Server) ConnectStream(stream proto.SignalExchange_ConnectStreamServer) s.metrics.MessagesForwarded.Add(stream.Context(), 1) } } else { + s.metrics.GetRegistrationDelay.Record(stream.Context(), float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream), attribute.String(labelRegistrionStatus, labelRegistrationNotFound))) + s.metrics.MessageForwardFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelType, labelTypeNotConnected))) log.Debugf("message from peer [%s] can't be forwarded to peer [%s] because destination peer is not connected", p.Id, msg.RemoteKey) //todo respond to the sender? - - s.metrics.MessageForwardFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelType, labelTypeNotConnected))) } } <-stream.Context().Done()