Skip to content

Commit fafb056

Browse files
committed
feat: migrate builtin metrics to OTEl
1 parent 08cb1b8 commit fafb056

File tree

11 files changed

+770
-181
lines changed

11 files changed

+770
-181
lines changed

google-cloud-bigtable/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@
338338
<artifactId>opentelemetry-api</artifactId>
339339
<version>${opentelemetry.version}</version>
340340
</dependency>
341+
<dependency>
342+
<groupId>io.opentelemetry</groupId>
343+
<artifactId>opentelemetry-sdk</artifactId>
344+
<version>${opentelemetry.version}</version>
345+
</dependency>
346+
<dependency>
347+
<groupId>io.opentelemetry</groupId>
348+
<artifactId>opentelemetry-sdk-testing</artifactId>
349+
<version>${opentelemetry.version}</version>
350+
<scope>test</scope>
351+
</dependency>
341352
<dependency>
342353
<groupId>io.opentelemetry</groupId>
343354
<artifactId>opentelemetry-sdk-metrics</artifactId>

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,6 @@ public static EnhancedBigtableStubSettings finalizeSettings(
220220
RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID,
221221
TagValue.create(settings.getAppProfileId()))
222222
.build();
223-
ImmutableMap<String, String> builtinAttributes =
224-
ImmutableMap.<String, String>builder()
225-
.put("project_id", settings.getProjectId())
226-
.put("instance", settings.getInstanceId())
227-
.put("app_profile", settings.getAppProfileId())
228-
.build();
229223
// Inject Opencensus instrumentation
230224
builder.setTracerFactory(
231225
new CompositeTracerFactory(
@@ -250,7 +244,7 @@ public static EnhancedBigtableStubSettings finalizeSettings(
250244
.build()),
251245
// Add OpenCensus Metrics
252246
MetricsTracerFactory.create(tagger, stats, attributes),
253-
BuiltinMetricsTracerFactory.create(builtinAttributes),
247+
BuiltinMetricsTracerFactory.create(settings),
254248
// Add user configured tracer
255249
settings.getTracerFactory())));
256250
return builder.build();

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ public class EnhancedBigtableStubSettings extends StubSettings<EnhancedBigtableS
228228

229229
private final FeatureFlags featureFlags;
230230

231+
private final boolean isBuiltinMetricsEnabled;
232+
231233
private EnhancedBigtableStubSettings(Builder builder) {
232234
super(builder);
233235

@@ -252,6 +254,7 @@ private EnhancedBigtableStubSettings(Builder builder) {
252254
isRefreshingChannel = builder.isRefreshingChannel;
253255
primedTableIds = builder.primedTableIds;
254256
jwtAudienceMapping = builder.jwtAudienceMapping;
257+
isBuiltinMetricsEnabled = builder.isBuiltinMetricsEnabled;
255258

256259
// Per method settings.
257260
readRowsSettings = builder.readRowsSettings.build();
@@ -313,6 +316,10 @@ public Map<String, String> getJwtAudienceMapping() {
313316
return jwtAudienceMapping;
314317
}
315318

319+
public boolean isBuiltinMetricsEnabled() {
320+
return isBuiltinMetricsEnabled;
321+
}
322+
316323
/** Returns a builder for the default ChannelProvider for this service. */
317324
public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() {
318325
return BigtableStubSettings.defaultGrpcTransportProviderBuilder()
@@ -613,6 +620,8 @@ public static class Builder extends StubSettings.Builder<EnhancedBigtableStubSet
613620

614621
private FeatureFlags.Builder featureFlags;
615622

623+
private boolean isBuiltinMetricsEnabled;
624+
616625
/**
617626
* Initializes a new Builder with sane defaults for all settings.
618627
*
@@ -624,6 +633,7 @@ public static class Builder extends StubSettings.Builder<EnhancedBigtableStubSet
624633
private Builder() {
625634
this.appProfileId = SERVER_DEFAULT_APP_PROFILE_ID;
626635
this.isRefreshingChannel = true;
636+
this.isBuiltinMetricsEnabled = true;
627637
primedTableIds = ImmutableList.of();
628638
jwtAudienceMapping = DEFAULT_JWT_AUDIENCE_MAPPING;
629639
setCredentialsProvider(defaultCredentialsProviderBuilder().build());
@@ -886,6 +896,17 @@ public Builder setJwtAudienceMapping(Map<String, String> jwtAudienceMapping) {
886896
return this;
887897
}
888898

899+
/** Returns true if builtin metrics is enabled. */
900+
public boolean isBuiltinMetricsEnabled() {
901+
return isBuiltinMetricsEnabled;
902+
}
903+
904+
/** Set if builtin metrics will be enabled. */
905+
public Builder setBuiltinMetricsEnabled(boolean enable) {
906+
this.isBuiltinMetricsEnabled = enable;
907+
return this;
908+
}
909+
889910
@InternalApi("Used for internal testing")
890911
public Map<String, String> getJwtAudienceMapping() {
891912
return jwtAudienceMapping;
@@ -1030,6 +1051,7 @@ public String toString() {
10301051
generateInitialChangeStreamPartitionsSettings)
10311052
.add("readChangeStreamSettings", readChangeStreamSettings)
10321053
.add("pingAndWarmSettings", pingAndWarmSettings)
1054+
.add("isBuiltinMetricsEnabled", isBuiltinMetricsEnabled)
10331055
.add("parent", super.toString())
10341056
.toString();
10351057
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
3131
import io.opentelemetry.sdk.metrics.data.MetricData;
3232
import io.opentelemetry.sdk.metrics.export.MetricExporter;
33+
import java.io.IOException;
3334
import java.util.Collection;
3435
import java.util.List;
3536
import java.util.Map;
@@ -48,7 +49,7 @@ final class BigtableCloudMonitoringExporter implements MetricExporter {
4849

4950
private static final String RESOURCE_TYPE = "bigtable_client_raw";
5051

51-
BigtableCloudMonitoringExporter(Credentials credentials) throws Exception {
52+
BigtableCloudMonitoringExporter(Credentials credentials) throws IOException {
5253
MetricServiceSettings.Builder settingsBuilder =
5354
MetricServiceSettings.newBuilder()
5455
.setTransportChannelProvider(InstantiatingGrpcChannelProvider.newBuilder().build());
@@ -99,7 +100,9 @@ public CompletableResultCode export(Collection<MetricData> collection) {
99100
logger.log(
100101
Level.WARNING,
101102
"Exception thrown when exporting TimeSeries for projectName="
102-
+ projectName.getProject(),
103+
+ projectName.getProject()
104+
+ " metric="
105+
+ metricData.getName(),
103106
e);
104107
}
105108
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableExporterUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class BigtableExporterUtils {
6767
private static final Logger logger = Logger.getLogger(BigtableExporterUtils.class.getName());
6868
private static final long NANO_PER_SECOND = (long) 1e9;
6969

70+
private static final String METRIC_PREFIX = "bigtable.googleapis.com/internal/client/";
71+
7072
static String getDefaultTaskValue() {
7173
// Something like '<pid>@<hostname>'
7274
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
@@ -113,7 +115,7 @@ static TimeSeries convertPointToTimeSeries(
113115
.setMetricKind(convertMetricKind(metricData))
114116
.setMetric(
115117
Metric.newBuilder()
116-
.setType(metricData.getName())
118+
.setType(METRIC_PREFIX + metricData.getName())
117119
.putAllLabels(metricLabels.build())
118120
.build())
119121
.setValueType(convertValueType(metricData.getType()));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2023 Google LLC
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+
* https://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+
package com.google.cloud.bigtable.data.v2.stub.metrics;
17+
18+
import io.opentelemetry.api.common.Attributes;
19+
20+
public class BigtableInstruments {
21+
22+
void recordOperationLatencies(long value, Attributes attributes) {}
23+
24+
void recordAttemptLatencies(long value, Attributes attributes) {}
25+
26+
void recordFirstResponseLatencies(long value, Attributes attributes) {}
27+
28+
void recordRetryCount(long value, Attributes attributes) {}
29+
30+
void recordServerLatencies(long value, Attributes attributes) {}
31+
32+
void recordConnectivityErrorCount(long value, Attributes attributes) {}
33+
34+
void recordApplicationBlockingLatencies(long value, Attributes attributes) {}
35+
36+
void recordClientBlockingLatencies(long value, Attributes attributes) {}
37+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2023 Google LLC
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+
* https://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+
package com.google.cloud.bigtable.data.v2.stub.metrics;
17+
18+
import io.opentelemetry.api.common.Attributes;
19+
import io.opentelemetry.api.metrics.LongCounter;
20+
import io.opentelemetry.api.metrics.LongHistogram;
21+
import io.opentelemetry.api.metrics.Meter;
22+
23+
class BuiltinInstruments extends BigtableInstruments {
24+
25+
private static final String MILLISECOND = "ms";
26+
private static final String COUNT = "1";
27+
28+
private final LongHistogram operationLatencies;
29+
private final LongHistogram attemptLatencies;
30+
private final LongHistogram serverLatencies;
31+
private final LongHistogram firstResponseLatencies;
32+
private final LongHistogram clientBlockingLatencies;
33+
private final LongHistogram applicationBlockingLatencies;
34+
private final LongCounter connectivityErrorCount;
35+
private final LongCounter retryCount;
36+
37+
BuiltinInstruments(Meter meter) {
38+
operationLatencies =
39+
meter
40+
.histogramBuilder("operation_latencies")
41+
.ofLongs()
42+
.setDescription(
43+
"Total time until final operation success or failure, including retries and backoff.")
44+
.setUnit(MILLISECOND)
45+
.build();
46+
attemptLatencies =
47+
meter
48+
.histogramBuilder("attempt_latencies")
49+
.ofLongs()
50+
.setDescription("Client observed latency per RPC attempt.")
51+
.setUnit(MILLISECOND)
52+
.build();
53+
serverLatencies =
54+
meter
55+
.histogramBuilder("server_latencies")
56+
.ofLongs()
57+
.setDescription(
58+
"The latency measured from the moment that the RPC entered the Google data center until the RPC was completed.")
59+
.setUnit(MILLISECOND)
60+
.build();
61+
firstResponseLatencies =
62+
meter
63+
.histogramBuilder("first_response_latencies")
64+
.ofLongs()
65+
.setDescription(
66+
"Latency from operation start until the response headers were received. The publishing of the measurement will be delayed until the attempt response has been received.")
67+
.setUnit(MILLISECOND)
68+
.build();
69+
clientBlockingLatencies =
70+
meter
71+
.histogramBuilder("throttling_latencies")
72+
.ofLongs()
73+
.setDescription(
74+
"The artificial latency introduced by the client to limit the number of outstanding requests. The publishing of the measurement will be delayed until the attempt trailers have been received.")
75+
.setUnit(MILLISECOND)
76+
.build();
77+
applicationBlockingLatencies =
78+
meter
79+
.histogramBuilder("application_latencies")
80+
.ofLongs()
81+
.setDescription(
82+
"The latency of the client application consuming available response data.")
83+
.setUnit(MILLISECOND)
84+
.build();
85+
connectivityErrorCount =
86+
meter
87+
.counterBuilder("connectivity_error_count")
88+
.setDescription(
89+
"Number of requests that failed to reach the Google datacenter. (Requests without google response headers")
90+
.setUnit(COUNT)
91+
.build();
92+
retryCount =
93+
meter
94+
.counterBuilder("retry_count")
95+
.setDescription("The number of additional RPCs sent after the initial attempt.")
96+
.setUnit(COUNT)
97+
.build();
98+
}
99+
100+
@Override
101+
void recordOperationLatencies(long value, Attributes attributes) {
102+
operationLatencies.record(value, attributes);
103+
}
104+
105+
@Override
106+
void recordAttemptLatencies(long value, Attributes attributes) {
107+
attemptLatencies.record(value, attributes);
108+
}
109+
110+
@Override
111+
void recordFirstResponseLatencies(long value, Attributes attributes) {
112+
firstResponseLatencies.record(value, attributes);
113+
}
114+
115+
@Override
116+
void recordRetryCount(long value, Attributes attributes) {
117+
retryCount.add(value, attributes);
118+
}
119+
120+
@Override
121+
void recordServerLatencies(long value, Attributes attributes) {
122+
serverLatencies.record(value, attributes);
123+
}
124+
125+
@Override
126+
void recordConnectivityErrorCount(long value, Attributes attributes) {
127+
connectivityErrorCount.add(value, attributes);
128+
}
129+
130+
@Override
131+
void recordApplicationBlockingLatencies(long value, Attributes attributes) {
132+
applicationBlockingLatencies.record(value, attributes);
133+
}
134+
135+
@Override
136+
void recordClientBlockingLatencies(long value, Attributes attributes) {
137+
clientBlockingLatencies.record(value, attributes);
138+
}
139+
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsAttributes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import io.opentelemetry.api.common.AttributeKey;
1919

20-
class BuiltinMetricsAttributes {
20+
public class BuiltinMetricsAttributes {
2121

2222
public static final AttributeKey<String> PROJECT_ID = AttributeKey.stringKey("project_id");
2323
public static final AttributeKey<String> INSTANCE_ID = AttributeKey.stringKey("instance");
@@ -26,7 +26,7 @@ class BuiltinMetricsAttributes {
2626
public static final AttributeKey<String> ZONE_ID = AttributeKey.stringKey("zone");
2727
static final AttributeKey<String> CLIENT_UID = AttributeKey.stringKey("client_uid");
2828

29-
static final AttributeKey<String> APP_PROFILE = AttributeKey.stringKey("app_profile");
29+
public static final AttributeKey<String> APP_PROFILE = AttributeKey.stringKey("app_profile");
3030
static final AttributeKey<Boolean> STREAMING = AttributeKey.booleanKey("streaming");
3131
static final AttributeKey<String> METHOD = AttributeKey.stringKey("method");
3232
static final AttributeKey<String> STATUS = AttributeKey.stringKey("status");

0 commit comments

Comments
 (0)