Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
08cb1b8
feat: migrate exporter to OTEL
mutianf May 26, 2023
26a648a
address comments
mutianf Jun 16, 2023
91fcd80
filter out only bigtable metrics
mutianf Jun 28, 2023
32ac2be
fix test
mutianf Jun 28, 2023
af6cbb2
use the bom
mutianf Jun 28, 2023
e87cf91
update
mutianf Jun 30, 2023
e2b7e58
update
mutianf Jul 5, 2023
6d127e3
feat: migrate builtin metrics to OTEl
mutianf Jun 15, 2023
e8be9c2
update completeResultCode
mutianf Jul 12, 2023
cd04252
add a comment
mutianf Jul 12, 2023
31b6d5c
Merge branch 'main' into migrate12
mutianf Aug 23, 2023
3d9b30d
udpate
mutianf Aug 23, 2023
9fcd0f3
fix tests
mutianf Aug 24, 2023
0c80a4c
remove unrelated changes
mutianf Aug 25, 2023
4b66a0e
fix tests
mutianf Aug 25, 2023
9414ece
add documentation
mutianf Sep 1, 2023
a07f817
fix test
mutianf Sep 1, 2023
2c268d2
Merge branch 'migrate1' into migrate12
mutianf Sep 1, 2023
ef4f2be
merge exporter changes
mutianf Sep 1, 2023
896cf4b
Merge branch 'main' into migrate12
mutianf Jan 18, 2024
e9c535f
address comments
mutianf Jan 19, 2024
6f01ea4
Merge branch 'otel' into migrate12
mutianf Jan 23, 2024
28ca070
rebase on otel
mutianf Jan 24, 2024
9a435f7
revert changes in stats
mutianf Jan 24, 2024
a797c96
fix import
mutianf Jan 24, 2024
d534f7f
update
mutianf Jan 24, 2024
2dd6586
merge back the endpoint change
mutianf Jan 25, 2024
54a8d25
refactor constants and settings
mutianf Jan 30, 2024
96099b8
refactor and fix tests
mutianf Jan 30, 2024
803f5b5
remove unused dependency
mutianf Jan 31, 2024
66ab16a
add some javadoc
mutianf Jan 31, 2024
c5fd5ca
Merge branch 'otel' into migrate12
mutianf Feb 5, 2024
4f577fa
address part of the comments
mutianf Feb 6, 2024
0f21a31
update test
mutianf Feb 6, 2024
56f6b71
test with nano
mutianf Feb 7, 2024
14f22a8
measure everything in nanos and publish with double histogram
mutianf Feb 8, 2024
5bfa166
Merge branch 'otel' into migrate12
mutianf Feb 16, 2024
523724b
address comments
mutianf Feb 20, 2024
db6c496
fix test
mutianf Feb 20, 2024
8f511cf
add toString
mutianf Feb 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions google-cloud-bigtable-deps-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>1.27.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
6 changes: 6 additions & 0 deletions google-cloud-bigtable-stats/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
<className>com/google/cloud/bigtable/stats/StatsRecorderWrapper</className>
<method>void putBatchRequestThrottled(long)</method>
</difference>
<!-- Internal API is updated -->
<difference>
<differenceType>7002</differenceType>
<className>com/google/cloud/bigtable/stats/StatsWrapper</className>
<method>*</method>
</difference>
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.opencensus.stats.Stats;
import io.opencensus.stats.View;
import io.opencensus.tags.TagKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -40,18 +39,6 @@ public static StatsRecorderWrapper createRecorder(
operationType, spanName, statsAttributes, Stats.getStatsRecorder());
}

// This is used in integration tests to get the tag value strings from view manager because Stats
// is relocated to com.google.bigtable.veneer.repackaged.io.opencensus.
@InternalApi("Visible for testing")
public static List<String> getOperationLatencyViewTagValueStrings() {
return Stats.getViewManager().getView(BuiltinViewConstants.OPERATION_LATENCIES_VIEW.getName())
.getAggregationMap().entrySet().stream()
.map(Map.Entry::getKey)
.flatMap(x -> x.stream())
.map(x -> x.asString())
.collect(Collectors.toCollection(ArrayList::new));
}

// A workaround to run ITBuiltinViewConstantsTest as integration test. Integration test runs after
// the packaging step. Opencensus classes will be relocated when they are packaged but the
// integration test files will not be. So the integration tests can't reference any transitive
Expand Down
6 changes: 6 additions & 0 deletions google-cloud-bigtable/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,10 @@
<differenceType>8001</differenceType>
<className>com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerBatchedUnaryCallable</className>
</difference>
<!-- InternalApi constructor was updated -->
<difference>
<differenceType>7004</differenceType>
<className>com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory</className>
<method>*</method>
</difference>
</differences>
30 changes: 30 additions & 0 deletions google-cloud-bigtable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigtable-stats</artifactId>
<scope>test</scope>
<!-- Exclude all dependencies that have been shaded. This is to workaround maven's immutable project structure:
after shading, the maven-shade-plugin tries to remove shaded dependencies, but it can't since the project
structure is immutable. So we have to manually exclude the shaded transitive dependencies manually. -->
Expand Down Expand Up @@ -332,6 +333,35 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-common</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.stub.BigtableBatchingCallSettings;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
import com.google.cloud.bigtable.stats.BigtableStackdriverStatsExporter;
import com.google.cloud.bigtable.stats.BuiltinViews;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import io.grpc.ManagedChannelBuilder;
Expand Down Expand Up @@ -197,23 +194,26 @@ public static void enableGfeOpenCensusStats() {
com.google.cloud.bigtable.data.v2.stub.metrics.RpcViews.registerBigtableClientGfeViews();
}

/** Register built in metrics. */
/**
* Register built in metrics.
*
* @deprecated Please use {@link BigtableDataSettings.Builder#setBuiltinMetricsEnabled(boolean)}
* to enable or disable built-in metrics.
*/
@Deprecated
public static void enableBuiltinMetrics() throws IOException {
if (BUILTIN_METRICS_REGISTERED.compareAndSet(false, true)) {
BuiltinViews.registerBigtableBuiltinViews();
BigtableStackdriverStatsExporter.register(GoogleCredentials.getApplicationDefault());
}
BUILTIN_METRICS_REGISTERED.compareAndSet(false, true);
}

/**
* Register built in metrics with credentials. The credentials need to have metric write access
* for all the projects you're publishing to.
*
* @deprecated Please use {@link BigtableDataSettings.Builder#setBuiltinMetricsEnabled(boolean)}
* to enable or disable built-in metrics.
*/
public static void enableBuiltinMetrics(Credentials credentials) throws IOException {
if (BUILTIN_METRICS_REGISTERED.compareAndSet(false, true)) {
BuiltinViews.registerBigtableBuiltinViews();
BigtableStackdriverStatsExporter.register(credentials);
}
BUILTIN_METRICS_REGISTERED.compareAndSet(false, true);
}

/** Returns the target project id. */
Expand Down Expand Up @@ -278,6 +278,11 @@ public boolean isBulkMutationFlowControlEnabled() {
return stubSettings.bulkMutateRowsSettings().isServerInitiatedFlowControlEnabled();
}

/** Gets if built-in metrics is enabled. */
public boolean isBuiltinMetricsEnabled() {
return stubSettings.isBuiltinMetricsEnabled();
}

/** Returns the underlying RPC settings. */
public EnhancedBigtableStubSettings getStubSettings() {
return stubSettings;
Expand Down Expand Up @@ -527,6 +532,17 @@ public boolean isBulkMutationFlowControlEnabled() {
return stubSettings.bulkMutateRowsSettings().isServerInitiatedFlowControlEnabled();
}

/** Set enable to true to enable builtin metrics. */
public Builder setBuiltinMetricsEnabled(boolean enable) {
stubSettings.setBuiltinMetricsEnabled(enable);
return this;
}

/** Gets if built-in metrics is enabled. */
public boolean isBuiltinMetricsEnabled() {
return stubSettings.isBuiltinMetricsEnabled();
}

/**
* Returns the underlying settings for making RPC calls. The settings should be changed with
* care.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@
*/
package com.google.cloud.bigtable.data.v2.stub;

import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.APPLICATION_BLOCKING_LATENCIES_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.APPLICATION_BLOCKING_LATENCIES_VIEW;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.APP_PROFILE;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.ATTEMPT_LATENCIES_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.ATTEMPT_LATENCIES_VIEW;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.CLIENT_BLOCKING_LATENCIES_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.CLIENT_BLOCKING_LATENCIES_VIEW;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.CONNECTIVITY_ERROR_COUNT_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.CONNECTIVITY_ERROR_COUNT_VIEW;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.FIRST_RESPONSE_LATENCIES_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.FIRST_RESPONSE_LATENCIES_VIEW;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.INSTANCE_ID;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.OPERATION_LATENCIES_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.OPERATION_LATENCIES_VIEW;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.PROJECT_ID;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.RETRY_COUNT_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.RETRY_COUNT_VIEW;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.SERVER_LATENCIES_SELECTOR;
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.SERVER_LATENCIES_VIEW;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.gax.batching.Batcher;
Expand All @@ -38,6 +58,7 @@
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.gax.tracing.ApiTracerFactory;
import com.google.api.gax.tracing.OpencensusTracerFactory;
import com.google.api.gax.tracing.SpanName;
import com.google.api.gax.tracing.TracedServerStreamingCallable;
Expand Down Expand Up @@ -87,6 +108,7 @@
import com.google.cloud.bigtable.data.v2.stub.changestream.GenerateInitialChangeStreamPartitionsUserCallable;
import com.google.cloud.bigtable.data.v2.stub.changestream.ReadChangeStreamResumptionStrategy;
import com.google.cloud.bigtable.data.v2.stub.changestream.ReadChangeStreamUserCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableCloudMonitoringExporter;
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracerStreamingCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracerUnaryCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsTracerFactory;
Expand Down Expand Up @@ -118,6 +140,13 @@
import io.opencensus.tags.TagValue;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.Tags;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -219,42 +248,77 @@ public static EnhancedBigtableStubSettings finalizeSettings(
RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID,
TagValue.create(settings.getAppProfileId()))
.build();
ImmutableMap<String, String> builtinAttributes =
ImmutableMap.<String, String>builder()
.put("project_id", settings.getProjectId())
.put("instance", settings.getInstanceId())
.put("app_profile", settings.getAppProfileId())
.build();

ImmutableList.Builder<ApiTracerFactory> tracerFactories = ImmutableList.builder();
tracerFactories
.add(
// Add OpenCensus Tracing
new OpencensusTracerFactory(
ImmutableMap.<String, String>builder()
// Annotate traces with the same tags as metrics
.put(RpcMeasureConstants.BIGTABLE_PROJECT_ID.getName(), settings.getProjectId())
.put(
RpcMeasureConstants.BIGTABLE_INSTANCE_ID.getName(),
settings.getInstanceId())
.put(
RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID.getName(),
settings.getAppProfileId())
// Also annotate traces with library versions
.put("gax", GaxGrpcProperties.getGaxGrpcVersion())
.put("grpc", GaxGrpcProperties.getGrpcVersion())
.put("gapic", Version.VERSION)
.build()))
// Add OpenCensus Metrics
.add(MetricsTracerFactory.create(tagger, stats, attributes))
// Add user configured tracer
.add(settings.getTracerFactory());
Attributes otelAttributes =
Attributes.of(
PROJECT_ID,
settings.getProjectId(),
INSTANCE_ID,
settings.getInstanceId(),
APP_PROFILE,
settings.getAppProfileId());
setupBuiltinMetricsTracerFactory(builder, tracerFactories, otelAttributes);
// Inject Opencensus instrumentation
builder.setTracerFactory(
new CompositeTracerFactory(
ImmutableList.of(
// Add OpenCensus Tracing
new OpencensusTracerFactory(
ImmutableMap.<String, String>builder()
// Annotate traces with the same tags as metrics
.put(
RpcMeasureConstants.BIGTABLE_PROJECT_ID.getName(),
settings.getProjectId())
.put(
RpcMeasureConstants.BIGTABLE_INSTANCE_ID.getName(),
settings.getInstanceId())
.put(
RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID.getName(),
settings.getAppProfileId())
// Also annotate traces with library versions
.put("gax", GaxGrpcProperties.getGaxGrpcVersion())
.put("grpc", GaxGrpcProperties.getGrpcVersion())
.put("gapic", Version.VERSION)
.build()),
// Add OpenCensus Metrics
MetricsTracerFactory.create(tagger, stats, attributes),
BuiltinMetricsTracerFactory.create(builtinAttributes),
// Add user configured tracer
settings.getTracerFactory())));
builder.setTracerFactory(new CompositeTracerFactory(tracerFactories.build()));
return builder.build();
}

private static void setupBuiltinMetricsTracerFactory(
EnhancedBigtableStubSettings.Builder settings,
ImmutableList.Builder<ApiTracerFactory> tracerFactories,
Attributes attributes)
throws IOException {
if (settings.getOpenTelemetry() != null) {
tracerFactories.add(
BuiltinMetricsTracerFactory.create(settings.getOpenTelemetry(), attributes));
} else if (settings.isBuiltinMetricsEnabled()) {
MetricExporter metricExporter =
BigtableCloudMonitoringExporter.create(
settings.getProjectId(), settings.getCredentialsProvider().getCredentials());
Resource resource = Resource.create(attributes);
SdkMeterProvider meterProvider =
SdkMeterProvider.builder()
.setResource(resource)
.registerMetricReader(PeriodicMetricReader.create(metricExporter))
.registerView(OPERATION_LATENCIES_SELECTOR, OPERATION_LATENCIES_VIEW)
.registerView(ATTEMPT_LATENCIES_SELECTOR, ATTEMPT_LATENCIES_VIEW)
.registerView(SERVER_LATENCIES_SELECTOR, SERVER_LATENCIES_VIEW)
.registerView(FIRST_RESPONSE_LATENCIES_SELECTOR, FIRST_RESPONSE_LATENCIES_VIEW)
.registerView(
APPLICATION_BLOCKING_LATENCIES_SELECTOR, APPLICATION_BLOCKING_LATENCIES_VIEW)
.registerView(CLIENT_BLOCKING_LATENCIES_SELECTOR, CLIENT_BLOCKING_LATENCIES_VIEW)
.registerView(RETRY_COUNT_SELECTOR, RETRY_COUNT_VIEW)
.registerView(CONNECTIVITY_ERROR_COUNT_SELECTOR, CONNECTIVITY_ERROR_COUNT_VIEW)
.build();
OpenTelemetry openTelemetry =
OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build();
tracerFactories.add(BuiltinMetricsTracerFactory.create(openTelemetry, attributes));
}
}

private static void patchCredentials(EnhancedBigtableStubSettings.Builder settings)
throws IOException {
int i = settings.getEndpoint().lastIndexOf(":");
Expand Down
Loading