From 3d9b30db69ab61c7bb53350e31bc86ab0b5094bd Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Wed, 23 Aug 2023 17:18:04 -0400 Subject: [PATCH] udpate --- .../clirr-ignored-differences.xml | 6 + .../data/v2/stub/EnhancedBigtableStub.java | 122 ++++++++++++++---- .../v2/stub/EnhancedBigtableStubSettings.java | 23 ++++ .../BigtableCloudMonitoringExporter.java | 6 +- .../metrics/BuiltinMetricsAttributes.java | 34 ++--- .../metrics/BuiltinMetricsTracerFactory.java | 71 ++-------- .../v2/stub/metrics/BuiltinMetricsView.java | 66 ++++++++++ .../EnhancedBigtableStubSettingsTest.java | 1 + .../metrics/BuiltinMetricsTracerTest.java | 6 +- 9 files changed, 224 insertions(+), 111 deletions(-) create mode 100644 google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsView.java diff --git a/google-cloud-bigtable/clirr-ignored-differences.xml b/google-cloud-bigtable/clirr-ignored-differences.xml index 4bb4684c38..4f363b94f9 100644 --- a/google-cloud-bigtable/clirr-ignored-differences.xml +++ b/google-cloud-bigtable/clirr-ignored-differences.xml @@ -150,4 +150,10 @@ 8001 com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerBatchedUnaryCallable + + + 7004 + com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory + * + diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java index b367401b17..7c2e64d3c2 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java @@ -15,6 +15,26 @@ */ package com.google.cloud.bigtable.data.v2.stub; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APPLICATION_BLOCKING_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APPLICATION_BLOCKING_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APP_PROFILE; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.ATTEMPT_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.ATTEMPT_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CLIENT_BLOCKING_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CLIENT_BLOCKING_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CONNECTIVITY_ERROR_COUNT_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CONNECTIVITY_ERROR_COUNT_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.FIRST_RESPONSE_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.FIRST_RESPONSE_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.INSTANCE_ID; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.OPERATION_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.OPERATION_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.PROJECT_ID; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.RETRY_COUNT_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.RETRY_COUNT_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.SERVER_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.SERVER_LATENCIES_VIEW; + import com.google.api.core.BetaApi; import com.google.api.core.InternalApi; import com.google.api.gax.batching.Batcher; @@ -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; @@ -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; @@ -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; @@ -219,36 +248,77 @@ public static EnhancedBigtableStubSettings finalizeSettings( RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID, TagValue.create(settings.getAppProfileId())) .build(); + + ImmutableList.Builder tracerFactories = ImmutableList.builder(); + tracerFactories + .add( + // Add OpenCensus Tracing + new OpencensusTracerFactory( + ImmutableMap.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.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(settings), - // Add user configured tracer - settings.getTracerFactory()))); + builder.setTracerFactory(new CompositeTracerFactory(tracerFactories.build())); return builder.build(); } + private static void setupBuiltinMetricsTracerFactory( + EnhancedBigtableStubSettings.Builder settings, + ImmutableList.Builder 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(":"); diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java index 7943748cf4..b24f92c057 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java @@ -52,6 +52,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import io.opentelemetry.api.OpenTelemetry; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -230,6 +231,8 @@ public class EnhancedBigtableStubSettings extends StubSettings getJwtAudienceMapping() { return jwtAudienceMapping; @@ -1051,6 +1073,7 @@ public String toString() { .add("readChangeStreamSettings", readChangeStreamSettings) .add("pingAndWarmSettings", pingAndWarmSettings) .add("isBuiltinMetricsEnabled", isBuiltinMetricsEnabled) + .add("openTelemetry", openTelemetry) .add("parent", super.toString()) .toString(); } diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java index 7e7912ed82..6a84e18c8c 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java @@ -16,6 +16,7 @@ package com.google.cloud.bigtable.data.v2.stub.metrics; import com.google.api.MonitoredResource; +import com.google.api.core.InternalApi; import com.google.api.gax.core.FixedCredentialsProvider; import com.google.auth.Credentials; import com.google.cloud.monitoring.v3.MetricServiceClient; @@ -38,7 +39,8 @@ import org.threeten.bp.Duration; /** Bigtable Cloud Monitoring OpenTelemetry Exporter. */ -final class BigtableCloudMonitoringExporter implements MetricExporter { +@InternalApi +public final class BigtableCloudMonitoringExporter implements MetricExporter { private static final Logger logger = Logger.getLogger(BigtableCloudMonitoringExporter.class.getName()); @@ -51,7 +53,7 @@ final class BigtableCloudMonitoringExporter implements MetricExporter { private static final String RESOURCE_TYPE = "bigtable_client_raw"; - static BigtableCloudMonitoringExporter create(String projectId, Credentials credentials) + public static BigtableCloudMonitoringExporter create(String projectId, Credentials credentials) throws IOException { MetricServiceSettings.Builder settingsBuilder = MetricServiceSettings.newBuilder(); settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)); diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsAttributes.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsAttributes.java index f8a20d961e..f65916e9bb 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsAttributes.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsAttributes.java @@ -15,6 +15,7 @@ */ package com.google.cloud.bigtable.data.v2.stub.metrics; +import com.google.api.core.InternalApi; import com.google.common.collect.ImmutableList; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.sdk.metrics.Aggregation; @@ -22,6 +23,7 @@ import io.opentelemetry.sdk.metrics.InstrumentType; import io.opentelemetry.sdk.metrics.View; +@InternalApi public class BuiltinMetricsAttributes { public static final AttributeKey PROJECT_ID = AttributeKey.stringKey("project_id"); @@ -56,14 +58,14 @@ public class BuiltinMetricsAttributes { static final String SCOPE = "bigtable.googleapis.com"; - static final InstrumentSelector OPERATION_LATENCIES_SELECTOR = + public static final InstrumentSelector OPERATION_LATENCIES_SELECTOR = InstrumentSelector.builder() .setName(OPERATION_LATENCIES_NAME) .setMeterName(SCOPE) .setType(InstrumentType.HISTOGRAM) .setUnit("ms") .build(); - static final InstrumentSelector ATTEMPT_LATENCIES_SELECTOR = + public static final InstrumentSelector ATTEMPT_LATENCIES_SELECTOR = InstrumentSelector.builder() .setName(ATTEMPT_LATENCIES_NAME) .setMeterName(SCOPE) @@ -71,7 +73,7 @@ public class BuiltinMetricsAttributes { .setUnit("ms") .build(); - static final InstrumentSelector RETRY_COUNT_SELECTOR = + public static final InstrumentSelector RETRY_COUNT_SELECTOR = InstrumentSelector.builder() .setName(RETRY_COUNT_NAME) .setMeterName(SCOPE) @@ -79,35 +81,35 @@ public class BuiltinMetricsAttributes { .setUnit("1") .build(); - static final InstrumentSelector SERVER_LATENCIES_SELECTOR = + public static final InstrumentSelector SERVER_LATENCIES_SELECTOR = InstrumentSelector.builder() .setName(SERVER_LATENCIES_NAME) .setMeterName(SCOPE) .setType(InstrumentType.HISTOGRAM) .setUnit("ms") .build(); - static final InstrumentSelector FIRST_RESPONSE_LATENCIES_SELECTOR = + public static final InstrumentSelector FIRST_RESPONSE_LATENCIES_SELECTOR = InstrumentSelector.builder() .setName(FIRST_RESPONSE_LATENCIES_NAME) .setMeterName(SCOPE) .setType(InstrumentType.HISTOGRAM) .setUnit("ms") .build(); - static final InstrumentSelector CLIENT_BLOCKING_LATENCIES_SELECTOR = + public static final InstrumentSelector CLIENT_BLOCKING_LATENCIES_SELECTOR = InstrumentSelector.builder() .setName(CLIENT_BLOCKING_LATENCIES_NAME) .setMeterName(SCOPE) .setType(InstrumentType.HISTOGRAM) .setUnit("ms") .build(); - static final InstrumentSelector APPLICATION_BLOCKING_LATENCIES_SELECTOR = + public static final InstrumentSelector APPLICATION_BLOCKING_LATENCIES_SELECTOR = InstrumentSelector.builder() .setName(APPLICATION_BLOCKING_LATENCIES_NAME) .setMeterName(SCOPE) .setType(InstrumentType.HISTOGRAM) .setUnit("ms") .build(); - static final InstrumentSelector CONNECTIVITY_ERROR_COUNT_SELECTOR = + public static final InstrumentSelector CONNECTIVITY_ERROR_COUNT_SELECTOR = InstrumentSelector.builder() .setName(CONNECTIVITY_ERROR_COUNT_NAME) .setMeterName(SCOPE) @@ -115,42 +117,42 @@ public class BuiltinMetricsAttributes { .setUnit("1") .build(); - static final View OPERATION_LATENCIES_VIEW = + public static final View OPERATION_LATENCIES_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/operation_latencies") .setAggregation(AGGREGATION_WITH_MILLIS_HISTOGRAM) .build(); - static final View ATTEMPT_LATENCIES_VIEW = + public static final View ATTEMPT_LATENCIES_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/attempt_latencies") .setAggregation(AGGREGATION_WITH_MILLIS_HISTOGRAM) .build(); - static final View SERVER_LATENCIES_VIEW = + public static final View SERVER_LATENCIES_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/server_latencies") .setAggregation(AGGREGATION_WITH_MILLIS_HISTOGRAM) .build(); - static final View FIRST_RESPONSE_LATENCIES_VIEW = + public static final View FIRST_RESPONSE_LATENCIES_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/first_response_latencies") .setAggregation(AGGREGATION_WITH_MILLIS_HISTOGRAM) .build(); - static final View APPLICATION_BLOCKING_LATENCIES_VIEW = + public static final View APPLICATION_BLOCKING_LATENCIES_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/application_latencies") .setAggregation(AGGREGATION_WITH_MILLIS_HISTOGRAM) .build(); - static final View CLIENT_BLOCKING_LATENCIES_VIEW = + public static final View CLIENT_BLOCKING_LATENCIES_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/throttling_latencies") .setAggregation(AGGREGATION_WITH_MILLIS_HISTOGRAM) .build(); - static final View RETRY_COUNT_VIEW = + public static final View RETRY_COUNT_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/retry_count") .setAggregation(Aggregation.sum()) .build(); - static final View CONNECTIVITY_ERROR_COUNT_VIEW = + public static final View CONNECTIVITY_ERROR_COUNT_VIEW = View.builder() .setName("bigtable.googleapis.com/internal/client/connectivity_error_count") .setAggregation(Aggregation.sum()) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory.java index aab9433bbd..97bfc88eca 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory.java @@ -15,40 +15,15 @@ */ package com.google.cloud.bigtable.data.v2.stub.metrics; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APPLICATION_BLOCKING_LATENCIES_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APPLICATION_BLOCKING_LATENCIES_VIEW; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APP_PROFILE; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.ATTEMPT_LATENCIES_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.ATTEMPT_LATENCIES_VIEW; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CLIENT_BLOCKING_LATENCIES_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CLIENT_BLOCKING_LATENCIES_VIEW; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CONNECTIVITY_ERROR_COUNT_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CONNECTIVITY_ERROR_COUNT_VIEW; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.FIRST_RESPONSE_LATENCIES_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.FIRST_RESPONSE_LATENCIES_VIEW; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.INSTANCE_ID; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.OPERATION_LATENCIES_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.OPERATION_LATENCIES_VIEW; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.PROJECT_ID; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.RETRY_COUNT_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.RETRY_COUNT_VIEW; import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.SCOPE; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.SERVER_LATENCIES_SELECTOR; -import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.SERVER_LATENCIES_VIEW; import com.google.api.core.InternalApi; import com.google.api.gax.tracing.ApiTracer; import com.google.api.gax.tracing.ApiTracerFactory; import com.google.api.gax.tracing.BaseApiTracerFactory; import com.google.api.gax.tracing.SpanName; -import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings; 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; /** @@ -57,48 +32,18 @@ */ @InternalApi("For internal use only") public class BuiltinMetricsTracerFactory extends BaseApiTracerFactory { - private final Attributes attributes; + private final BigtableMetricsRecorder bigtableMetricsRecorder; + private final Attributes attributes; - public static BuiltinMetricsTracerFactory create(EnhancedBigtableStubSettings settings) - throws IOException { - return new BuiltinMetricsTracerFactory(settings); + public static BuiltinMetricsTracerFactory create( + OpenTelemetry openTelemetry, Attributes attributes) throws IOException { + return new BuiltinMetricsTracerFactory(openTelemetry, attributes); } - BuiltinMetricsTracerFactory(EnhancedBigtableStubSettings settings) throws IOException { - this.attributes = - Attributes.builder() - .put(PROJECT_ID, settings.getProjectId()) - .put(INSTANCE_ID, settings.getInstanceId()) - .put(APP_PROFILE, settings.getAppProfileId()) - .build(); - - if (settings.isBuiltinMetricsEnabled()) { - Resource resource = Resource.create(attributes); - MetricExporter metricExporter = - BigtableCloudMonitoringExporter.create( - settings.getProjectId(), settings.getCredentialsProvider().getCredentials()); - - 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(); - bigtableMetricsRecorder = new BuiltinInMetricsRecorder(openTelemetry.getMeter(SCOPE)); - } else { - bigtableMetricsRecorder = new BigtableMetricsRecorder(); - } + BuiltinMetricsTracerFactory(OpenTelemetry openTelemetry, Attributes attributes) { + this.attributes = attributes; + bigtableMetricsRecorder = new BuiltinInMetricsRecorder(openTelemetry.getMeter(SCOPE)); } @Override diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsView.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsView.java new file mode 100644 index 0000000000..6ff4fc6336 --- /dev/null +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsView.java @@ -0,0 +1,66 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.cloud.bigtable.data.v2.stub.metrics; + +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APPLICATION_BLOCKING_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.APPLICATION_BLOCKING_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.ATTEMPT_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.ATTEMPT_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CLIENT_BLOCKING_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CLIENT_BLOCKING_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CONNECTIVITY_ERROR_COUNT_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.CONNECTIVITY_ERROR_COUNT_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.FIRST_RESPONSE_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.FIRST_RESPONSE_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.OPERATION_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.OPERATION_LATENCIES_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.RETRY_COUNT_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.RETRY_COUNT_VIEW; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.SERVER_LATENCIES_SELECTOR; +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsAttributes.SERVER_LATENCIES_VIEW; + +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; +import io.opentelemetry.sdk.metrics.export.MetricExporter; +import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; +import java.io.IOException; + +/** Register built-in metrics on a custom OpenTelemetry instance. */ +public class BuiltinMetricsView { + + public static void registerBuiltinMetrics(String projectId, SdkMeterProviderBuilder builder) + throws IOException { + BuiltinMetricsView.registerBuiltinMetrics( + projectId, GoogleCredentials.getApplicationDefault(), builder); + } + + public static void registerBuiltinMetrics( + String projectId, Credentials credentials, SdkMeterProviderBuilder builder) + throws IOException { + MetricExporter metricExporter = BigtableCloudMonitoringExporter.create(projectId, credentials); + builder + .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); + } +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java index 262c583d67..e5654361ed 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java @@ -800,6 +800,7 @@ public void isRefreshingChannelFalseValueTest() { "readChangeStreamSettings", "pingAndWarmSettings", "isBuiltinMetricsEnabled", + "openTelemetry", }; @Test diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java index f0d86a7aa8..0a5590fce1 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java @@ -682,18 +682,16 @@ public void testBatchBlockingLatencies() throws InterruptedException { .map(a -> a.get(TABLE_ID)) .collect(Collectors.toList())) .containsExactly(TABLE, TABLE, TABLE); - // TODO: the order of batched tracer unary callable seems to be incorrect, causing the first - // attempt returning global assertThat( attributes.getAllValues().stream() .map(a -> a.get(ZONE_ID)) .collect(Collectors.toList())) - .containsExactly("global", ZONE, ZONE); + .containsExactly(ZONE, ZONE, ZONE); assertThat( attributes.getAllValues().stream() .map(a -> a.get(CLUSTER_ID)) .collect(Collectors.toList())) - .containsExactly("unspecified", CLUSTER, CLUSTER); + .containsExactly(CLUSTER, CLUSTER, CLUSTER); assertThat( attributes.getAllValues().stream() .map(a -> a.get(CLIENT_NAME))