Skip to content

Commit

Permalink
chore: improve metrics setup for easier discovery (#2446)
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf authored Dec 6, 2024
1 parent caa622e commit 9a6602c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
/**
* A util class to register built-in metrics on a custom OpenTelemetry instance. This is for
* advanced usage, and is only necessary when wanting to write built-in metrics to cloud monitoring
* and custom sinks. Please refer to {@link CustomOpenTelemetryMetricsProvider} for example usage.
* and custom sinks.
*
* @deprecated Use methods in {@link CustomOpenTelemetryMetricsProvider} instead.
*/
@Deprecated
public class BuiltinMetricsView {

private BuiltinMetricsView() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package com.google.cloud.bigtable.data.v2.stub.metrics;

import com.google.auth.Credentials;
import com.google.common.base.MoreObjects;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import java.io.IOException;

/**
* Set a custom OpenTelemetry instance.
Expand All @@ -26,8 +29,8 @@
* <pre>{@code
* SdkMeterProviderBuilder sdkMeterProvider = SdkMeterProvider.builder();
*
* // register Builtin metrics on your meter provider with default credentials
* BuiltinMetricsView.registerBuiltinMetrics(sdkMeterProvider);
* // Set up SdkMeterProvider for client side metrics
* CustomOpenTelemetryMetricsProvider.setupSdkMeterProvider(sdkMeterProvider);
*
* // register other metrics reader and views
* sdkMeterProvider.registerMetricReader(..);
Expand Down Expand Up @@ -63,6 +66,32 @@ public OpenTelemetry getOpenTelemetry() {
return otel;
}

/**
* Convenient method to set up SdkMeterProviderBuilder with the default credential and endpoint.
*/
public static void setupSdkMeterProvider(SdkMeterProviderBuilder builder) throws IOException {
setupSdkMeterProvider(builder, null, null);
}

/** Convenient method to set up SdkMeterProviderBuilder with a custom credential. */
public static void setupSdkMeterProvider(SdkMeterProviderBuilder builder, Credentials credentials)
throws IOException {
setupSdkMeterProvider(builder, credentials, null);
}

/** Convenient method to set up SdkMeterProviderBuilder with a custom endpoint. */
public static void setupSdkMeterProvider(SdkMeterProviderBuilder builder, String endpoint)
throws IOException {
setupSdkMeterProvider(builder, null, endpoint);
}

/** Convenient method to set up SdkMeterProviderBuilder with a custom credentials and endpoint. */
public static void setupSdkMeterProvider(
SdkMeterProviderBuilder builder, Credentials credentials, String endpoint)
throws IOException {
BuiltinMetricsView.registerBuiltinMetrics(credentials, builder, endpoint);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("openTelemetry", otel).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsView;
import com.google.cloud.bigtable.data.v2.stub.metrics.CustomOpenTelemetryMetricsProvider;
import com.google.cloud.bigtable.test_helpers.env.CloudEnv;
import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator;
Expand Down Expand Up @@ -135,7 +134,7 @@ public void setup() throws IOException {

SdkMeterProviderBuilder meterProvider =
SdkMeterProvider.builder().registerMetricReader(metricReader);
BuiltinMetricsView.registerBuiltinMetrics(testEnvRule.env().getProjectId(), meterProvider);
CustomOpenTelemetryMetricsProvider.setupSdkMeterProvider(meterProvider);
OpenTelemetry openTelemetry =
OpenTelemetrySdk.builder().setMeterProvider(meterProvider.build()).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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.metrics.BuiltinMetricsConstants;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsView;
import com.google.cloud.bigtable.data.v2.stub.metrics.CustomOpenTelemetryMetricsProvider;
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void setup() throws IOException {

SdkMeterProviderBuilder meterProvider =
SdkMeterProvider.builder().registerMetricReader(metricReader);
BuiltinMetricsView.registerBuiltinMetrics(testEnvRule.env().getProjectId(), meterProvider);
CustomOpenTelemetryMetricsProvider.setupSdkMeterProvider(meterProvider);
OpenTelemetry openTelemetry =
OpenTelemetrySdk.builder().setMeterProvider(meterProvider.build()).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsView;
import com.google.cloud.bigtable.data.v2.stub.metrics.CustomOpenTelemetryMetricsProvider;
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
Expand Down Expand Up @@ -71,7 +70,7 @@ public void setup() throws IOException {

SdkMeterProviderBuilder meterProvider =
SdkMeterProvider.builder().registerMetricReader(metricReader);
BuiltinMetricsView.registerBuiltinMetrics(testEnvRule.env().getProjectId(), meterProvider);
CustomOpenTelemetryMetricsProvider.setupSdkMeterProvider(meterProvider);
OpenTelemetry openTelemetry =
OpenTelemetrySdk.builder().setMeterProvider(meterProvider.build()).build();

Expand Down

0 comments on commit 9a6602c

Please sign in to comment.