Skip to content

Commit 2bcbcab

Browse files
authored
stats/opentelemetry: Add usage of metrics registry (#7410)
1 parent 64adc81 commit 2bcbcab

File tree

9 files changed

+508
-41
lines changed

9 files changed

+508
-41
lines changed

experimental/stats/metricregistry.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ import (
2323
"testing"
2424

2525
"google.golang.org/grpc/grpclog"
26+
"google.golang.org/grpc/internal"
2627
)
2728

29+
func init() {
30+
internal.SnapshotMetricRegistryForTesting = snapshotMetricsRegistryForTesting
31+
}
32+
2833
var logger = grpclog.Component("metrics-registry")
2934

3035
// DefaultMetrics are the default metrics registered through global metrics
@@ -54,6 +59,10 @@ type MetricDescriptor struct {
5459
// The type of metric. This is set by the metric registry, and not intended
5560
// to be set by a component registering a metric.
5661
Type MetricType
62+
// Bounds are the bounds of this metric. This only applies to histogram
63+
// metrics. If unset or set with length 0, stats handlers will fall back to
64+
// default bounds.
65+
Bounds []float64
5766
}
5867

5968
// MetricType is the type of metric.

internal/internal.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ var (
215215

216216
// SetConnectedAddress sets the connected address for a SubConnState.
217217
SetConnectedAddress any // func(scs *SubConnState, addr resolver.Address)
218+
219+
// SnapshotMetricRegistryForTesting snapshots the global data of the metric
220+
// registry. Registers a cleanup function on the provided testing.T that
221+
// sets the metric registry to its original state. Only called in testing
222+
// functions.
223+
SnapshotMetricRegistryForTesting any // func(t *testing.T)
218224
)
219225

220226
// HealthChecker defines the signature of the client-side LB channel health

stats/opentelemetry/client_metrics.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
)
3434

3535
type clientStatsHandler struct {
36-
options Options
37-
36+
estats.MetricsRecorder
37+
options Options
3838
clientMetrics clientMetrics
3939
}
4040

@@ -52,14 +52,20 @@ func (h *clientStatsHandler) initializeMetrics() {
5252

5353
metrics := h.options.MetricsOptions.Metrics
5454
if metrics == nil {
55-
metrics = DefaultMetrics
55+
metrics = DefaultMetrics()
5656
}
5757

5858
h.clientMetrics.attemptStarted = createInt64Counter(metrics.Metrics(), "grpc.client.attempt.started", meter, otelmetric.WithUnit("attempt"), otelmetric.WithDescription("Number of client call attempts started."))
5959
h.clientMetrics.attemptDuration = createFloat64Histogram(metrics.Metrics(), "grpc.client.attempt.duration", meter, otelmetric.WithUnit("s"), otelmetric.WithDescription("End-to-end time taken to complete a client call attempt."), otelmetric.WithExplicitBucketBoundaries(DefaultLatencyBounds...))
6060
h.clientMetrics.attemptSentTotalCompressedMessageSize = createInt64Histogram(metrics.Metrics(), "grpc.client.attempt.sent_total_compressed_message_size", meter, otelmetric.WithUnit("By"), otelmetric.WithDescription("Compressed message bytes sent per client call attempt."), otelmetric.WithExplicitBucketBoundaries(DefaultSizeBounds...))
6161
h.clientMetrics.attemptRcvdTotalCompressedMessageSize = createInt64Histogram(metrics.Metrics(), "grpc.client.attempt.rcvd_total_compressed_message_size", meter, otelmetric.WithUnit("By"), otelmetric.WithDescription("Compressed message bytes received per call attempt."), otelmetric.WithExplicitBucketBoundaries(DefaultSizeBounds...))
6262
h.clientMetrics.callDuration = createFloat64Histogram(metrics.Metrics(), "grpc.client.call.duration", meter, otelmetric.WithUnit("s"), otelmetric.WithDescription("Time taken by gRPC to complete an RPC from application's perspective."), otelmetric.WithExplicitBucketBoundaries(DefaultLatencyBounds...))
63+
64+
rm := &registryMetrics{
65+
optionalLabels: h.options.MetricsOptions.OptionalLabels,
66+
}
67+
h.MetricsRecorder = rm
68+
rm.registerMetrics(metrics, meter)
6369
}
6470

6571
func (h *clientStatsHandler) unaryInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {

stats/opentelemetry/csm/observability_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
206206
serverOptionWithCSMPluginOption(opentelemetry.Options{
207207
MetricsOptions: opentelemetry.MetricsOptions{
208208
MeterProvider: provider,
209-
Metrics: opentelemetry.DefaultMetrics,
209+
Metrics: opentelemetry.DefaultMetrics(),
210210
}}, po),
211211
}
212212
dopts := []grpc.DialOption{dialOptionWithCSMPluginOption(opentelemetry.Options{
213213
MetricsOptions: opentelemetry.MetricsOptions{
214214
MeterProvider: provider,
215-
Metrics: opentelemetry.DefaultMetrics,
215+
Metrics: opentelemetry.DefaultMetrics(),
216216
OptionalLabels: []string{"csm.service_name", "csm.service_namespace_name"},
217217
},
218218
}, po)}
@@ -368,13 +368,13 @@ func (s) TestCSMPluginOptionStreaming(t *testing.T) {
368368
serverOptionWithCSMPluginOption(opentelemetry.Options{
369369
MetricsOptions: opentelemetry.MetricsOptions{
370370
MeterProvider: provider,
371-
Metrics: opentelemetry.DefaultMetrics,
371+
Metrics: opentelemetry.DefaultMetrics(),
372372
}}, po),
373373
}
374374
dopts := []grpc.DialOption{dialOptionWithCSMPluginOption(opentelemetry.Options{
375375
MetricsOptions: opentelemetry.MetricsOptions{
376376
MeterProvider: provider,
377-
Metrics: opentelemetry.DefaultMetrics,
377+
Metrics: opentelemetry.DefaultMetrics(),
378378
OptionalLabels: []string{"csm.service_name", "csm.service_namespace_name"},
379379
},
380380
}, po)}
@@ -460,7 +460,7 @@ func (s) TestXDSLabels(t *testing.T) {
460460
dopts := []grpc.DialOption{dialOptionWithCSMPluginOption(opentelemetry.Options{
461461
MetricsOptions: opentelemetry.MetricsOptions{
462462
MeterProvider: provider,
463-
Metrics: opentelemetry.DefaultMetrics,
463+
Metrics: opentelemetry.DefaultMetrics(),
464464
OptionalLabels: []string{"csm.service_name", "csm.service_namespace_name"},
465465
},
466466
}, po), grpc.WithUnaryInterceptor(unaryInterceptorAttachXDSLabels)}

stats/opentelemetry/e2e_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ func Test(t *testing.T) {
5252
// component and the server.
5353
func setup(t *testing.T, methodAttributeFilter func(string) bool) (*metric.ManualReader, *stubserver.StubServer) {
5454
reader := metric.NewManualReader()
55-
provider := metric.NewMeterProvider(
56-
metric.WithReader(reader),
57-
)
55+
provider := metric.NewMeterProvider(metric.WithReader(reader))
5856
ss := &stubserver.StubServer{
5957
UnaryCallF: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
6058
return &testpb.SimpleResponse{Payload: &testpb.Payload{
@@ -74,12 +72,12 @@ func setup(t *testing.T, methodAttributeFilter func(string) bool) (*metric.Manua
7472
if err := ss.Start([]grpc.ServerOption{opentelemetry.ServerOption(opentelemetry.Options{
7573
MetricsOptions: opentelemetry.MetricsOptions{
7674
MeterProvider: provider,
77-
Metrics: opentelemetry.DefaultMetrics,
75+
Metrics: opentelemetry.DefaultMetrics(),
7876
MethodAttributeFilter: methodAttributeFilter,
7977
}})}, opentelemetry.DialOption(opentelemetry.Options{
8078
MetricsOptions: opentelemetry.MetricsOptions{
8179
MeterProvider: provider,
82-
Metrics: opentelemetry.DefaultMetrics,
80+
Metrics: opentelemetry.DefaultMetrics(),
8381
},
8482
})); err != nil {
8583
t.Fatalf("Error starting endpoint server: %v", err)

stats/opentelemetry/example_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package opentelemetry_test
1919
import (
2020
"google.golang.org/grpc"
2121
"google.golang.org/grpc/credentials/insecure"
22-
stats2 "google.golang.org/grpc/experimental/stats"
22+
estats "google.golang.org/grpc/experimental/stats"
2323
"google.golang.org/grpc/stats/opentelemetry"
2424

2525
"go.opentelemetry.io/otel/sdk/metric"
@@ -53,7 +53,7 @@ func Example_dialOption() {
5353
opts := opentelemetry.Options{
5454
MetricsOptions: opentelemetry.MetricsOptions{
5555
MeterProvider: provider,
56-
Metrics: opentelemetry.DefaultMetrics, // equivalent to unset - distinct from empty
56+
Metrics: opentelemetry.DefaultMetrics(), // equivalent to unset - distinct from empty
5757
},
5858
}
5959
do := opentelemetry.DialOption(opts)
@@ -88,7 +88,7 @@ func ExampleMetrics_excludeSome() {
8888
// To exclude specific metrics, initialize Options as follows:
8989
opts := opentelemetry.Options{
9090
MetricsOptions: opentelemetry.MetricsOptions{
91-
Metrics: opentelemetry.DefaultMetrics.Remove(opentelemetry.ClientAttemptDuration, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSize),
91+
Metrics: opentelemetry.DefaultMetrics().Remove(opentelemetry.ClientAttemptDuration, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSize),
9292
},
9393
}
9494
do := opentelemetry.DialOption(opts)
@@ -103,7 +103,7 @@ func ExampleMetrics_disableAll() {
103103
// To disable all metrics, initialize Options as follows:
104104
opts := opentelemetry.Options{
105105
MetricsOptions: opentelemetry.MetricsOptions{
106-
Metrics: stats2.NewMetrics(), // Distinct to nil, which creates default metrics. This empty set creates no metrics.
106+
Metrics: estats.NewMetrics(), // Distinct to nil, which creates default metrics. This empty set creates no metrics.
107107
},
108108
}
109109
do := opentelemetry.DialOption(opts)
@@ -118,7 +118,7 @@ func ExampleMetrics_enableSome() {
118118
// To only create specific metrics, initialize Options as follows:
119119
opts := opentelemetry.Options{
120120
MetricsOptions: opentelemetry.MetricsOptions{
121-
Metrics: stats2.NewMetrics(opentelemetry.ClientAttemptDuration, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSize), // only create these metrics
121+
Metrics: estats.NewMetrics(opentelemetry.ClientAttemptDuration, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSize), // only create these metrics
122122
},
123123
}
124124
do := opentelemetry.DialOption(opts)

0 commit comments

Comments
 (0)