Skip to content

Commit 3c0586a

Browse files
zasweqdfawley
andauthored
stats/opentelemetry: Cleanup OpenTelemetry API's before stabilization (#7874)
Co-authored-by: Doug Fawley <dfawley@google.com>
1 parent 4c07bca commit 3c0586a

File tree

8 files changed

+156
-142
lines changed

8 files changed

+156
-142
lines changed

experimental/stats/metricregistry.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

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

2829
func init() {
@@ -34,15 +35,15 @@ var logger = grpclog.Component("metrics-registry")
3435
// DefaultMetrics are the default metrics registered through global metrics
3536
// registry. This is written to at initialization time only, and is read only
3637
// after initialization.
37-
var DefaultMetrics = NewMetrics()
38+
var DefaultMetrics = stats.NewMetricSet()
3839

3940
// MetricDescriptor is the data for a registered metric.
4041
type MetricDescriptor struct {
4142
// The name of this metric. This name must be unique across the whole binary
4243
// (including any per call metrics). See
4344
// https://github.com/grpc/proposal/blob/master/A79-non-per-call-metrics-architecture.md#metric-instrument-naming-conventions
4445
// for metric naming conventions.
45-
Name Metric
46+
Name string
4647
// The description of this metric.
4748
Description string
4849
// The unit (e.g. entries, seconds) of this metric.
@@ -154,27 +155,27 @@ func (h *Int64GaugeHandle) Record(recorder MetricsRecorder, incr int64, labels .
154155
}
155156

156157
// registeredMetrics are the registered metric descriptor names.
157-
var registeredMetrics = make(map[Metric]bool)
158+
var registeredMetrics = make(map[string]bool)
158159

159160
// metricsRegistry contains all of the registered metrics.
160161
//
161162
// This is written to only at init time, and read only after that.
162-
var metricsRegistry = make(map[Metric]*MetricDescriptor)
163+
var metricsRegistry = make(map[string]*MetricDescriptor)
163164

164165
// DescriptorForMetric returns the MetricDescriptor from the global registry.
165166
//
166167
// Returns nil if MetricDescriptor not present.
167-
func DescriptorForMetric(metric Metric) *MetricDescriptor {
168-
return metricsRegistry[metric]
168+
func DescriptorForMetric(metricName string) *MetricDescriptor {
169+
return metricsRegistry[metricName]
169170
}
170171

171-
func registerMetric(name Metric, def bool) {
172-
if registeredMetrics[name] {
173-
logger.Fatalf("metric %v already registered", name)
172+
func registerMetric(metricName string, def bool) {
173+
if registeredMetrics[metricName] {
174+
logger.Fatalf("metric %v already registered", metricName)
174175
}
175-
registeredMetrics[name] = true
176+
registeredMetrics[metricName] = true
176177
if def {
177-
DefaultMetrics = DefaultMetrics.Add(name)
178+
DefaultMetrics = DefaultMetrics.Add(metricName)
178179
}
179180
}
180181

@@ -256,8 +257,8 @@ func snapshotMetricsRegistryForTesting() func() {
256257
oldRegisteredMetrics := registeredMetrics
257258
oldMetricsRegistry := metricsRegistry
258259

259-
registeredMetrics = make(map[Metric]bool)
260-
metricsRegistry = make(map[Metric]*MetricDescriptor)
260+
registeredMetrics = make(map[string]bool)
261+
metricsRegistry = make(map[string]*MetricDescriptor)
261262
maps.Copy(registeredMetrics, registeredMetrics)
262263
maps.Copy(metricsRegistry, metricsRegistry)
263264

experimental/stats/metrics.go

-75
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
// Package stats contains experimental metrics/stats API's.
2020
package stats
2121

22-
import "maps"
23-
2422
// MetricsRecorder records on metrics derived from metric registry.
2523
type MetricsRecorder interface {
2624
// RecordInt64Count records the measurement alongside labels on the int
@@ -39,76 +37,3 @@ type MetricsRecorder interface {
3937
// gauge associated with the provided handle.
4038
RecordInt64Gauge(handle *Int64GaugeHandle, incr int64, labels ...string)
4139
}
42-
43-
// Metric is an identifier for a metric.
44-
type Metric string
45-
46-
// Metrics is a set of metrics to record. Once created, Metrics is immutable,
47-
// however Add and Remove can make copies with specific metrics added or
48-
// removed, respectively.
49-
//
50-
// Do not construct directly; use NewMetrics instead.
51-
type Metrics struct {
52-
// metrics are the set of metrics to initialize.
53-
metrics map[Metric]bool
54-
}
55-
56-
// NewMetrics returns a Metrics containing Metrics.
57-
func NewMetrics(metrics ...Metric) *Metrics {
58-
newMetrics := make(map[Metric]bool)
59-
for _, metric := range metrics {
60-
newMetrics[metric] = true
61-
}
62-
return &Metrics{
63-
metrics: newMetrics,
64-
}
65-
}
66-
67-
// Metrics returns the metrics set. The returned map is read-only and must not
68-
// be modified.
69-
func (m *Metrics) Metrics() map[Metric]bool {
70-
return m.metrics
71-
}
72-
73-
// Add adds the metrics to the metrics set and returns a new copy with the
74-
// additional metrics.
75-
func (m *Metrics) Add(metrics ...Metric) *Metrics {
76-
newMetrics := make(map[Metric]bool)
77-
for metric := range m.metrics {
78-
newMetrics[metric] = true
79-
}
80-
81-
for _, metric := range metrics {
82-
newMetrics[metric] = true
83-
}
84-
return &Metrics{
85-
metrics: newMetrics,
86-
}
87-
}
88-
89-
// Join joins the metrics passed in with the metrics set, and returns a new copy
90-
// with the merged metrics.
91-
func (m *Metrics) Join(metrics *Metrics) *Metrics {
92-
newMetrics := make(map[Metric]bool)
93-
maps.Copy(newMetrics, m.metrics)
94-
maps.Copy(newMetrics, metrics.metrics)
95-
return &Metrics{
96-
metrics: newMetrics,
97-
}
98-
}
99-
100-
// Remove removes the metrics from the metrics set and returns a new copy with
101-
// the metrics removed.
102-
func (m *Metrics) Remove(metrics ...Metric) *Metrics {
103-
newMetrics := make(map[Metric]bool)
104-
for metric := range m.metrics {
105-
newMetrics[metric] = true
106-
}
107-
108-
for _, metric := range metrics {
109-
delete(newMetrics, metric)
110-
}
111-
return &Metrics{
112-
metrics: newMetrics,
113-
}
114-
}

internal/testutils/stats/test_metrics_recorder.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type TestMetricsRecorder struct {
4444
// mu protects data.
4545
mu sync.Mutex
4646
// data is the most recent update for each metric name.
47-
data map[estats.Metric]float64
47+
data map[string]float64
4848
}
4949

5050
// NewTestMetricsRecorder returns a new TestMetricsRecorder.
@@ -56,7 +56,7 @@ func NewTestMetricsRecorder() *TestMetricsRecorder {
5656
floatHistoCh: testutils.NewChannelWithSize(10),
5757
intGaugeCh: testutils.NewChannelWithSize(10),
5858

59-
data: make(map[estats.Metric]float64),
59+
data: make(map[string]float64),
6060
}
6161
}
6262

@@ -65,15 +65,15 @@ func NewTestMetricsRecorder() *TestMetricsRecorder {
6565
func (r *TestMetricsRecorder) Metric(name string) (float64, bool) {
6666
r.mu.Lock()
6767
defer r.mu.Unlock()
68-
data, ok := r.data[estats.Metric(name)]
68+
data, ok := r.data[name]
6969
return data, ok
7070
}
7171

7272
// ClearMetrics clears the metrics data store of the test metrics recorder.
7373
func (r *TestMetricsRecorder) ClearMetrics() {
7474
r.mu.Lock()
7575
defer r.mu.Unlock()
76-
r.data = make(map[estats.Metric]float64)
76+
r.data = make(map[string]float64)
7777
}
7878

7979
// MetricsData represents data associated with a metric.

stats/metrics.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2024 gRPC authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package stats
18+
19+
import "maps"
20+
21+
// MetricSet is a set of metrics to record. Once created, MetricSet is immutable,
22+
// however Add and Remove can make copies with specific metrics added or
23+
// removed, respectively.
24+
//
25+
// Do not construct directly; use NewMetricSet instead.
26+
type MetricSet struct {
27+
// metrics are the set of metrics to initialize.
28+
metrics map[string]bool
29+
}
30+
31+
// NewMetricSet returns a MetricSet containing metricNames.
32+
func NewMetricSet(metricNames ...string) *MetricSet {
33+
newMetrics := make(map[string]bool)
34+
for _, metric := range metricNames {
35+
newMetrics[metric] = true
36+
}
37+
return &MetricSet{metrics: newMetrics}
38+
}
39+
40+
// Metrics returns the metrics set. The returned map is read-only and must not
41+
// be modified.
42+
func (m *MetricSet) Metrics() map[string]bool {
43+
return m.metrics
44+
}
45+
46+
// Add adds the metricNames to the metrics set and returns a new copy with the
47+
// additional metrics.
48+
func (m *MetricSet) Add(metricNames ...string) *MetricSet {
49+
newMetrics := make(map[string]bool)
50+
for metric := range m.metrics {
51+
newMetrics[metric] = true
52+
}
53+
54+
for _, metric := range metricNames {
55+
newMetrics[metric] = true
56+
}
57+
return &MetricSet{metrics: newMetrics}
58+
}
59+
60+
// Join joins the metrics passed in with the metrics set, and returns a new copy
61+
// with the merged metrics.
62+
func (m *MetricSet) Join(metrics *MetricSet) *MetricSet {
63+
newMetrics := make(map[string]bool)
64+
maps.Copy(newMetrics, m.metrics)
65+
maps.Copy(newMetrics, metrics.metrics)
66+
return &MetricSet{metrics: newMetrics}
67+
}
68+
69+
// Remove removes the metricNames from the metrics set and returns a new copy
70+
// with the metrics removed.
71+
func (m *MetricSet) Remove(metricNames ...string) *MetricSet {
72+
newMetrics := make(map[string]bool)
73+
for metric := range m.metrics {
74+
newMetrics[metric] = true
75+
}
76+
77+
for _, metric := range metricNames {
78+
delete(newMetrics, metric)
79+
}
80+
return &MetricSet{metrics: newMetrics}
81+
}

stats/opentelemetry/client_metrics.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,19 @@ func (h *clientStatsHandler) processRPCEnd(ctx context.Context, ai *attemptInfo,
260260
}
261261

262262
const (
263-
// ClientAttemptStarted is the number of client call attempts started.
264-
ClientAttemptStarted estats.Metric = "grpc.client.attempt.started"
265-
// ClientAttemptDuration is the end-to-end time taken to complete a client
266-
// call attempt.
267-
ClientAttemptDuration estats.Metric = "grpc.client.attempt.duration"
268-
// ClientAttemptSentCompressedTotalMessageSize is the compressed message
269-
// bytes sent per client call attempt.
270-
ClientAttemptSentCompressedTotalMessageSize estats.Metric = "grpc.client.attempt.sent_total_compressed_message_size"
271-
// ClientAttemptRcvdCompressedTotalMessageSize is the compressed message
272-
// bytes received per call attempt.
273-
ClientAttemptRcvdCompressedTotalMessageSize estats.Metric = "grpc.client.attempt.rcvd_total_compressed_message_size"
274-
// ClientCallDuration is the time taken by gRPC to complete an RPC from
275-
// application's perspective.
276-
ClientCallDuration estats.Metric = "grpc.client.call.duration"
263+
// ClientAttemptStartedMetricName is the number of client call attempts
264+
// started.
265+
ClientAttemptStartedMetricName string = "grpc.client.attempt.started"
266+
// ClientAttemptDurationMetricName is the end-to-end time taken to complete
267+
// a client call attempt.
268+
ClientAttemptDurationMetricName string = "grpc.client.attempt.duration"
269+
// ClientAttemptSentCompressedTotalMessageSizeMetricName is the compressed
270+
// message bytes sent per client call attempt.
271+
ClientAttemptSentCompressedTotalMessageSizeMetricName string = "grpc.client.attempt.sent_total_compressed_message_size"
272+
// ClientAttemptRcvdCompressedTotalMessageSizeMetricName is the compressed
273+
// message bytes received per call attempt.
274+
ClientAttemptRcvdCompressedTotalMessageSizeMetricName string = "grpc.client.attempt.rcvd_total_compressed_message_size"
275+
// ClientCallDurationMetricName is the time taken by gRPC to complete an RPC
276+
// from application's perspective.
277+
ClientCallDurationMetricName string = "grpc.client.call.duration"
277278
)

stats/opentelemetry/example_test.go

+4-4
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-
estats "google.golang.org/grpc/experimental/stats"
22+
"google.golang.org/grpc/stats"
2323
"google.golang.org/grpc/stats/opentelemetry"
2424

2525
"go.opentelemetry.io/otel/sdk/metric"
@@ -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.ClientAttemptDurationMetricName, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSizeMetricName),
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: estats.NewMetrics(), // Distinct to nil, which creates default metrics. This empty set creates no metrics.
106+
Metrics: stats.NewMetricSet(), // 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: estats.NewMetrics(opentelemetry.ClientAttemptDuration, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSize), // only create these metrics
121+
Metrics: stats.NewMetricSet(opentelemetry.ClientAttemptDurationMetricName, opentelemetry.ClientAttemptRcvdCompressedTotalMessageSizeMetricName), // only create these metrics
122122
},
123123
}
124124
do := opentelemetry.DialOption(opts)

0 commit comments

Comments
 (0)