diff --git a/docs/prometheus-metrics.md b/docs/prometheus-metrics.md index 942ac77..352bd65 100644 --- a/docs/prometheus-metrics.md +++ b/docs/prometheus-metrics.md @@ -45,3 +45,6 @@ The following metrics are available: | `signaling_mcu_no_backend_available_total` | Counter | 0.4.0 | Total number of publishing requests where no backend was available | `type` | | `signaling_room_sessions` | Gauge | 0.4.0 | The current number of sessions in a room | `backend`, `room`, `clienttype` | | `signaling_server_messages_total` | Counter | 0.4.0 | The total number of signaling messages | `type` | +| `signaling_grpc_clients` | Gauge | 1.0.0 | The current number of GRPC clients | | +| `signaling_grpc_client_calls_total` | Counter | 1.0.0 | The total number of GRPC client calls | `method` | +| `signaling_grpc_server_calls_total` | Counter | 1.0.0 | The total number of GRPC server calls | `method` | diff --git a/grpc_client.go b/grpc_client.go index 6cd1173..49190f4 100644 --- a/grpc_client.go +++ b/grpc_client.go @@ -37,6 +37,10 @@ import ( status "google.golang.org/grpc/status" ) +func init() { + RegisterGrpcClientStats() +} + type grpcClientImpl struct { RpcMcuClient RpcSessionsClient @@ -76,6 +80,7 @@ func (c *GrpcClient) Close() error { } func (c *GrpcClient) LookupSessionId(ctx context.Context, roomSessionId string) (string, error) { + statsGrpcClientCalls.WithLabelValues("LookupSessionId").Inc() // TODO: Remove debug logging log.Printf("Lookup room session %s on %s", roomSessionId, c.Target()) response, err := c.impl.LookupSessionId(ctx, &LookupSessionIdRequest{ @@ -96,6 +101,7 @@ func (c *GrpcClient) LookupSessionId(ctx context.Context, roomSessionId string) } func (c *GrpcClient) IsSessionInCall(ctx context.Context, sessionId string, room *Room) (bool, error) { + statsGrpcClientCalls.WithLabelValues("IsSessionInCall").Inc() // TODO: Remove debug logging log.Printf("Check if session %s is in call %s on %s", sessionId, room.Id(), c.Target()) response, err := c.impl.IsSessionInCall(ctx, &IsSessionInCallRequest{ @@ -113,6 +119,7 @@ func (c *GrpcClient) IsSessionInCall(ctx context.Context, sessionId string, room } func (c *GrpcClient) GetPublisherId(ctx context.Context, sessionId string, streamType string) (string, string, net.IP, error) { + statsGrpcClientCalls.WithLabelValues("GetPublisherId").Inc() // TODO: Remove debug logging log.Printf("Get %s publisher id %s on %s", streamType, sessionId, c.Target()) response, err := c.impl.GetPublisherId(ctx, &GetPublisherIdRequest{ @@ -209,6 +216,7 @@ func (c *GrpcClients) load(config *goconf.ConfigFile) error { c.clients = clients c.clientsMap = clientsMap + statsGrpcClients.Set(float64(len(clients))) return nil } diff --git a/grpc_server.go b/grpc_server.go index ec2e454..d2ba2fb 100644 --- a/grpc_server.go +++ b/grpc_server.go @@ -35,6 +35,10 @@ import ( status "google.golang.org/grpc/status" ) +func init() { + RegisterGrpcServerStats() +} + type GrpcServer struct { UnimplementedRpcMcuServer UnimplementedRpcSessionsServer @@ -92,6 +96,7 @@ func (s *GrpcServer) Close() { } func (s *GrpcServer) LookupSessionId(ctx context.Context, request *LookupSessionIdRequest) (*LookupSessionIdReply, error) { + statsGrpcServerCalls.WithLabelValues("LookupSessionId").Inc() // TODO: Remove debug logging log.Printf("Lookup session id for room session id %s", request.RoomSessionId) sid, err := s.hub.roomSessions.GetSessionId(request.RoomSessionId) @@ -107,6 +112,7 @@ func (s *GrpcServer) LookupSessionId(ctx context.Context, request *LookupSession } func (s *GrpcServer) IsSessionInCall(ctx context.Context, request *IsSessionInCallRequest) (*IsSessionInCallReply, error) { + statsGrpcServerCalls.WithLabelValues("IsSessionInCall").Inc() // TODO: Remove debug logging log.Printf("Check if session %s is in call %s on %s", request.SessionId, request.RoomId, request.BackendUrl) session := s.hub.GetSessionByPublicId(request.SessionId) @@ -127,6 +133,7 @@ func (s *GrpcServer) IsSessionInCall(ctx context.Context, request *IsSessionInCa } func (s *GrpcServer) GetPublisherId(ctx context.Context, request *GetPublisherIdRequest) (*GetPublisherIdReply, error) { + statsGrpcServerCalls.WithLabelValues("GetPublisherId").Inc() // TODO: Remove debug logging log.Printf("Get %s publisher id for session %s", request.StreamType, request.SessionId) session := s.hub.GetSessionByPublicId(request.SessionId) diff --git a/grpc_stats_prometheus.go b/grpc_stats_prometheus.go new file mode 100644 index 0000000..fa37bdc --- /dev/null +++ b/grpc_stats_prometheus.go @@ -0,0 +1,65 @@ +/** + * Standalone signaling server for the Nextcloud Spreed app. + * Copyright (C) 2022 struktur AG + * + * @author Joachim Bauch + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package signaling + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + statsGrpcClients = prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: "signaling", + Subsystem: "grpc", + Name: "clients", + Help: "The current number of GRPC clients", + }) + statsGrpcClientCalls = prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: "signaling", + Subsystem: "grpc", + Name: "client_calls_total", + Help: "The total number of GRPC client calls", + }, []string{"method"}) + + grpcClientStats = []prometheus.Collector{ + statsGrpcClients, + statsGrpcClientCalls, + } + + statsGrpcServerCalls = prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: "signaling", + Subsystem: "grpc", + Name: "server_calls_total", + Help: "The total number of GRPC server calls", + }, []string{"method"}) + + grpcServerStats = []prometheus.Collector{ + statsGrpcServerCalls, + } +) + +func RegisterGrpcClientStats() { + registerAll(grpcClientStats...) +} + +func RegisterGrpcServerStats() { + registerAll(grpcServerStats...) +}