-
Notifications
You must be signed in to change notification settings - Fork 838
Additional GRPC stats #3036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Additional GRPC stats #3036
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b3c3528
More GRPC stats.
pstibrany 5990dce
Added test for grpc stats.
pstibrany e0b6321
Added test for streaming payload.
pstibrany 7239165
Removed default panic case.
pstibrany de18b6d
Don't use fmt.Println in tests.
pstibrany 8fb06b5
CHANGELOG.md
pstibrany 8226748
Move entry to correct version.
pstibrany 7262e56
Address review feedback.
pstibrany 2b85cbd
Added metric names to CHANGELOG entry.
pstibrany 68e57d7
Merge branch 'master' into grpc_stats
pracucci cb2842f
Merge branch 'master' into grpc_stats
pstibrany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,109 @@ | ||
| package stats | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| "google.golang.org/grpc/stats" | ||
| ) | ||
|
|
||
| func NewStatsHandler(r prometheus.Registerer) stats.Handler { | ||
| const MiB = 1024 * 1024 | ||
| messageSizeBuckets := []float64{1 * MiB, 2.5 * MiB, 5 * MiB, 10 * MiB, 25 * MiB, 50 * MiB, 100 * MiB, 250 * MiB} | ||
|
|
||
| return &grpcStatsHandler{ | ||
| connectedClients: promauto.With(r).NewGauge(prometheus.GaugeOpts{ | ||
| Name: "cortex_grpc_connected_clients", | ||
| Help: "Number of clients connected to gRPC server.", | ||
| }), | ||
|
|
||
| inflightRPC: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{ | ||
| Name: "cortex_grpc_inflight_requests", | ||
| Help: "Number of inflight gRPC calls.", | ||
| }, []string{"method"}), | ||
|
|
||
| methodErrors: promauto.With(r).NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "cortex_grpc_method_errors_total", | ||
| Help: "Number of errors returned by method.", | ||
| }, []string{"method"}), | ||
|
|
||
| receivedPayloadSize: promauto.With(r).NewHistogramVec(prometheus.HistogramOpts{ | ||
| Name: "cortex_grpc_received_payload_size_bytes", | ||
| Help: "Size of received gRPC messages as seen on the wire (eg. compressed, signed, encrypted).", | ||
| Buckets: messageSizeBuckets, | ||
| }, []string{"method"}), | ||
|
|
||
| sentPayloadSize: promauto.With(r).NewHistogramVec(prometheus.HistogramOpts{ | ||
| Name: "cortex_grpc_sent_payload_size_bytes", | ||
| Help: "Size of sent gRPC messages as seen on the wire (eg. compressed, signed, encrypted).", | ||
| Buckets: messageSizeBuckets, | ||
| }, []string{"method"}), | ||
| } | ||
| } | ||
|
|
||
| type grpcStatsHandler struct { | ||
| connectedClients prometheus.Gauge | ||
| inflightRPC *prometheus.GaugeVec | ||
| receivedPayloadSize *prometheus.HistogramVec | ||
| sentPayloadSize *prometheus.HistogramVec | ||
| methodErrors *prometheus.CounterVec | ||
| } | ||
|
|
||
| // Custom type to hide it from other packages. | ||
| type contextKey int | ||
|
|
||
| const ( | ||
| contextKeyMethodName contextKey = 1 | ||
| ) | ||
|
|
||
| func (g *grpcStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context { | ||
| return context.WithValue(ctx, contextKeyMethodName, info.FullMethodName) | ||
| } | ||
|
|
||
| func (g *grpcStatsHandler) HandleRPC(ctx context.Context, rpcStats stats.RPCStats) { | ||
| // We use full method name from context, because not all RPCStats structs have it. | ||
| fullMethodName, ok := ctx.Value(contextKeyMethodName).(string) | ||
pstibrany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| switch s := rpcStats.(type) { | ||
| case *stats.Begin: | ||
| g.inflightRPC.WithLabelValues(fullMethodName).Inc() | ||
| case *stats.End: | ||
| g.inflightRPC.WithLabelValues(fullMethodName).Dec() | ||
| if s.Error != nil { | ||
| g.methodErrors.WithLabelValues(fullMethodName).Inc() | ||
| } | ||
|
|
||
| case *stats.InHeader: | ||
| // Ignored. Cortex doesn't use headers. Furthermore WireLength seems to be incorrect for large headers -- it uses | ||
| // length of last frame (16K) even for headers in megabytes. | ||
| case *stats.InPayload: | ||
| g.receivedPayloadSize.WithLabelValues(fullMethodName).Observe(float64(s.WireLength)) | ||
| case *stats.InTrailer: | ||
| // Ignored. Cortex doesn't use trailers. | ||
|
|
||
| case *stats.OutHeader: | ||
| // Ignored. Cortex doesn't send headers, and since OutHeader doesn't have WireLength, we could only estimate it. | ||
| case *stats.OutPayload: | ||
| g.sentPayloadSize.WithLabelValues(fullMethodName).Observe(float64(s.WireLength)) | ||
| case *stats.OutTrailer: | ||
| // Ignored, Cortex doesn't use trailers. OutTrailer doesn't have valid WireLength (there is deperecated field, always set to 0). | ||
| } | ||
| } | ||
|
|
||
| func (g *grpcStatsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context { | ||
| return ctx | ||
| } | ||
|
|
||
| func (g *grpcStatsHandler) HandleConn(_ context.Context, connStats stats.ConnStats) { | ||
| switch connStats.(type) { | ||
| case *stats.ConnBegin: | ||
| g.connectedClients.Inc() | ||
|
|
||
| case *stats.ConnEnd: | ||
| g.connectedClients.Dec() | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion(non-blocking): This struct has a large potential for reuse in other projects. Would you mind making the
cortexprefix a configurable namespace?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for suggestion. I plan to drop this PR if/when weaveworks/common#196 gets merged. If weaveworks/common#196 doesn't get accepted, I will reconsider this feedback.