Skip to content

Commit

Permalink
Instrumented gRPC server with Prometheus exporter
Browse files Browse the repository at this point in the history
go-grpc-prometheus exports various metrics:
- grpc_server_started_total: Count of RPCs started on the server by
  method.
- grpc_server_handled_total: Count of RPCs completed on the server,
  regardless of success or failure.
- grpc_server_handling_seconds_*: Histograms or summaries (if histograms
  are enabled) for tracking RPC handling duration.
- grpc_server_msg_received_total: Number of messages received per RPC.
- grpc_server_msg_sent_total: Number of messages sent per RPC.

Issue: COSI-65
  • Loading branch information
anurag4DSB committed Dec 20, 2024
1 parent 7f9314b commit 68ab8cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.6
require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.47
github.com/aws/smithy-go v1.22.1
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.36.1
github.com/prometheus/client_golang v1.12.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
12 changes: 12 additions & 0 deletions pkg/grpcfactory/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"net/url"

grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
"k8s.io/klog/v2"
cosi "sigs.k8s.io/container-object-storage-interface-spec"
Expand Down Expand Up @@ -49,9 +50,20 @@ func (s *COSIProvisionerServer) Run(ctx context.Context) error {
return fmt.Errorf("failed to start server: %w", err)
}
defer listener.Close()

// Add Prometheus gRPC interceptors for unary and streaming RPCs
s.listenOpts = append(s.listenOpts,
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
)

server := grpc.NewServer(s.listenOpts...)
cosi.RegisterIdentityServer(server, s.identityServer)
cosi.RegisterProvisionerServer(server, s.provisionerServer)

grpc_prometheus.Register(server)
grpc_prometheus.EnableHandlingTimeHistogram()

errChan := make(chan error, 1)
go func() {
errChan <- server.Serve(listener)
Expand Down

0 comments on commit 68ab8cb

Please sign in to comment.