-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add metrics middleware * fmt: imports * dash: update metrics dashboard * rename to metrics
- Loading branch information
1 parent
7e6e83b
commit 7320f9e
Showing
6 changed files
with
69 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Layr-Labs/cerberus/internal/metrics" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type MetricsMiddleware struct { | ||
registry *prometheus.Registry | ||
recorder metrics.Recorder | ||
} | ||
|
||
func NewMetricsMiddleware( | ||
registry *prometheus.Registry, | ||
recorder metrics.Recorder, | ||
) *MetricsMiddleware { | ||
return &MetricsMiddleware{ | ||
registry: registry, | ||
recorder: recorder, | ||
} | ||
} | ||
|
||
// UnaryServerInterceptor returns a new unary server interceptor for metrics | ||
func (m *MetricsMiddleware) UnaryServerInterceptor() grpc.UnaryServerInterceptor { | ||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
recordDuration := m.recorder.RecordRPCServerRequest(info.FullMethod) | ||
|
||
// Handle request | ||
resp, err := handler(ctx, req) | ||
|
||
// Get status code | ||
code := status.Code(err) | ||
|
||
// Record response | ||
recordDuration(code.String()) | ||
|
||
return resp, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters