Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit defce0d

Browse files
committed
feat: Add gRPC A66/A94 metrics.
This change adds the `grpc.client.attempt.started` metric in the [A66](https://github.com/grpc/proposal/blob/master/A66-otel-stats.md) proposal and all the subchannel metrics in the [A94](https://github.com/grpc/proposal/blob/master/A94-subchannel-otel-metrics.md) proposal. Metric-specific attributes were also added. The change also updates the metric exporter to use the same timestamp for start and end time for GAUGE type data point. This is required by the StackDriver or otherwise the export will fail.
1 parent 6356ef2 commit defce0d

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInMetricsConstant.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public class BuiltInMetricsConstant {
7070

7171
static final Collection<String> GRPC_METRICS_TO_ENABLE =
7272
ImmutableList.of(
73+
"grpc.client.attempt.started",
74+
"grpc.subchannel.open_connections",
75+
"grpc.subchannel.disconnections",
76+
"grpc.subchannel.connection_attempts_succeeded",
77+
"grpc.subchannel.connection_attempts_failed",
7378
"grpc.lb.rls.default_target_picks",
7479
"grpc.lb.rls.target_picks",
7580
"grpc.xds_client.server_failure",
@@ -100,8 +105,6 @@ public class BuiltInMetricsConstant {
100105
AttributeKey.stringKey("directpath_used");
101106
public static final AttributeKey<String> REQUEST_ID_KEY =
102107
AttributeKey.stringKey(REQUEST_ID_HEADER_NAME);
103-
public static final AttributeKey<String> GRPC_XDS_RESOURCE_TYPE_KEY =
104-
AttributeKey.stringKey("grpc.xds.resource_type");
105108
public static Set<String> ALLOWED_EXEMPLARS_ATTRIBUTES =
106109
new HashSet<>(Arrays.asList(REQUEST_ID_HEADER_NAME));
107110

@@ -124,8 +127,16 @@ public class BuiltInMetricsConstant {
124127
DIRECT_PATH_ENABLED_KEY,
125128
DIRECT_PATH_USED_KEY);
126129

127-
static final Set<String> GRPC_LB_RLS_ATTRIBUTES =
128-
ImmutableSet.of("grpc.lb.rls.data_plane_target", "grpc.lb.pick_result");
130+
static final Set<String> GRPC_ATTRIBUTES =
131+
ImmutableSet.of(
132+
"grpc.disconnect_error",
133+
"grpc.method",
134+
"grpc.target",
135+
"grpc.lb.backend_service",
136+
"grpc.lb.locality",
137+
"grpc.lb.pick_result",
138+
"grpc.lb.rls.data_plane_target",
139+
"grpc.xds.resource_type");
129140

130141
static List<Double> BUCKET_BOUNDARIES =
131142
ImmutableList.of(
@@ -140,7 +151,6 @@ public class BuiltInMetricsConstant {
140151
ImmutableList.of(
141152
"grpc.client.attempt.sent_total_compressed_message_size",
142153
"grpc.client.attempt.rcvd_total_compressed_message_size",
143-
"grpc.client.attempt.started",
144154
"grpc.client.attempt.duration",
145155
"grpc.client.call.duration");
146156

@@ -235,8 +245,7 @@ private static void defineGRPCView(ImmutableMap.Builder<InstrumentSelector, View
235245
BuiltInMetricsConstant.COMMON_ATTRIBUTES.stream()
236246
.map(AttributeKey::getKey)
237247
.collect(Collectors.toSet());
238-
attributesFilter.addAll(BuiltInMetricsConstant.GRPC_LB_RLS_ATTRIBUTES);
239-
attributesFilter.add(BuiltInMetricsConstant.GRPC_XDS_RESOURCE_TYPE_KEY.getKey());
248+
attributesFilter.addAll(BuiltInMetricsConstant.GRPC_ATTRIBUTES);
240249

241250
View view =
242251
View.builder()

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporterUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ private static TimeSeries convertPointToSpannerTimeSeries(
116116
PointData pointData,
117117
MonitoredResource.Builder monitoredResourceBuilder,
118118
String projectId) {
119+
MetricKind metricKind = convertMetricKind(metricData);
119120
TimeSeries.Builder builder =
120121
TimeSeries.newBuilder()
121-
.setMetricKind(convertMetricKind(metricData))
122+
.setMetricKind(metricKind)
122123
.setValueType(convertValueType(metricData.getType()));
123124
Metric.Builder metricBuilder = Metric.newBuilder().setType(metricData.getName());
124125

@@ -143,7 +144,11 @@ private static TimeSeries convertPointToSpannerTimeSeries(
143144

144145
TimeInterval timeInterval =
145146
TimeInterval.newBuilder()
146-
.setStartTime(Timestamps.fromNanos(pointData.getStartEpochNanos()))
147+
.setStartTime(
148+
// For gauge metrics, the start and end time should be the same.
149+
metricKind == MetricKind.GAUGE
150+
? Timestamps.fromNanos(pointData.getEpochNanos())
151+
: Timestamps.fromNanos(pointData.getStartEpochNanos()))
147152
.setEndTime(Timestamps.fromNanos(pointData.getEpochNanos()))
148153
.build();
149154

0 commit comments

Comments
 (0)