Skip to content

Commit 4ed455a

Browse files
authored
feat: Add Metrics host for built in metrics (#3519)
This PR allows users to set the custom monitoring host for built in metrics.
1 parent 01eafce commit 4ed455a

File tree

6 files changed

+57
-15
lines changed

6 files changed

+57
-15
lines changed

google-cloud-spanner/clirr-ignored-differences.xml

+7
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,13 @@
709709
<method>boolean isEnableBuiltInMetrics()</method>
710710
</difference>
711711

712+
<!-- Added Monitoring host option -->
713+
<difference>
714+
<differenceType>7012</differenceType>
715+
<className>com/google/cloud/spanner/SpannerOptions$SpannerEnvironment</className>
716+
<method>java.lang.String getMonitoringHost()</method>
717+
</difference>
718+
712719
<!-- Added ExcludeTxnFromChangeStreams -->
713720
<difference>
714721
<differenceType>7012</differenceType>

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ final class BuiltInOpenTelemetryMetricsProvider {
5959

6060
private BuiltInOpenTelemetryMetricsProvider() {}
6161

62-
OpenTelemetry getOrCreateOpenTelemetry(String projectId, @Nullable Credentials credentials) {
62+
OpenTelemetry getOrCreateOpenTelemetry(
63+
String projectId, @Nullable Credentials credentials, @Nullable String monitoringHost) {
6364
try {
6465
if (this.openTelemetry == null) {
6566
SdkMeterProviderBuilder sdkMeterProviderBuilder = SdkMeterProvider.builder();
6667
BuiltInOpenTelemetryMetricsView.registerBuiltinMetrics(
67-
SpannerCloudMonitoringExporter.create(projectId, credentials), sdkMeterProviderBuilder);
68+
SpannerCloudMonitoringExporter.create(projectId, credentials, monitoringHost),
69+
sdkMeterProviderBuilder);
6870
SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.build();
6971
this.openTelemetry = OpenTelemetrySdk.builder().setMeterProvider(sdkMeterProvider).build();
7072
Runtime.getRuntime().addShutdownHook(new Thread(sdkMeterProvider::close));

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

+5-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.google.cloud.monitoring.v3.MetricServiceClient;
3030
import com.google.cloud.monitoring.v3.MetricServiceSettings;
3131
import com.google.common.annotations.VisibleForTesting;
32-
import com.google.common.base.MoreObjects;
3332
import com.google.common.collect.Iterables;
3433
import com.google.common.util.concurrent.MoreExecutors;
3534
import com.google.monitoring.v3.CreateTimeSeriesRequest;
@@ -63,13 +62,6 @@ class SpannerCloudMonitoringExporter implements MetricExporter {
6362
private static final Logger logger =
6463
Logger.getLogger(SpannerCloudMonitoringExporter.class.getName());
6564

66-
// This system property can be used to override the monitoring endpoint
67-
// to a different environment. It's meant for internal testing only.
68-
private static final String MONITORING_ENDPOINT =
69-
MoreObjects.firstNonNull(
70-
System.getProperty("spanner.test-monitoring-endpoint"),
71-
MetricServiceSettings.getDefaultEndpoint());
72-
7365
// This the quota limit from Cloud Monitoring. More details in
7466
// https://cloud.google.com/monitoring/quotas#custom_metrics_quotas.
7567
private static final int EXPORT_BATCH_SIZE_LIMIT = 200;
@@ -78,7 +70,8 @@ class SpannerCloudMonitoringExporter implements MetricExporter {
7870
private final MetricServiceClient client;
7971
private final String spannerProjectId;
8072

81-
static SpannerCloudMonitoringExporter create(String projectId, @Nullable Credentials credentials)
73+
static SpannerCloudMonitoringExporter create(
74+
String projectId, @Nullable Credentials credentials, @Nullable String monitoringHost)
8275
throws IOException {
8376
MetricServiceSettings.Builder settingsBuilder = MetricServiceSettings.newBuilder();
8477
CredentialsProvider credentialsProvider;
@@ -88,7 +81,9 @@ static SpannerCloudMonitoringExporter create(String projectId, @Nullable Credent
8881
credentialsProvider = FixedCredentialsProvider.create(credentials);
8982
}
9083
settingsBuilder.setCredentialsProvider(credentialsProvider);
91-
settingsBuilder.setEndpoint(MONITORING_ENDPOINT);
84+
if (monitoringHost != null) {
85+
settingsBuilder.setEndpoint(monitoringHost);
86+
}
9287

9388
org.threeten.bp.Duration timeout = Duration.ofMinutes(1);
9489
// TODO: createServiceTimeSeries needs special handling if the request failed. Leaving

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public class SpannerOptions extends ServiceOptions<Spanner, SpannerOptions> {
164164
private final boolean enableBuiltInMetrics;
165165
private final boolean enableExtendedTracing;
166166
private final boolean enableEndToEndTracing;
167+
private final String monitoringHost;
167168

168169
enum TracingFramework {
169170
OPEN_CENSUS,
@@ -672,6 +673,7 @@ protected SpannerOptions(Builder builder) {
672673
enableExtendedTracing = builder.enableExtendedTracing;
673674
enableBuiltInMetrics = builder.enableBuiltInMetrics;
674675
enableEndToEndTracing = builder.enableEndToEndTracing;
676+
monitoringHost = builder.monitoringHost;
675677
}
676678

677679
/**
@@ -712,6 +714,10 @@ default boolean isEnableBuiltInMetrics() {
712714
default boolean isEnableEndToEndTracing() {
713715
return false;
714716
}
717+
718+
default String getMonitoringHost() {
719+
return null;
720+
}
715721
}
716722

717723
/**
@@ -728,6 +734,7 @@ private static class SpannerEnvironmentImpl implements SpannerEnvironment {
728734
private static final String SPANNER_ENABLE_END_TO_END_TRACING =
729735
"SPANNER_ENABLE_END_TO_END_TRACING";
730736
private static final String SPANNER_DISABLE_BUILTIN_METRICS = "SPANNER_DISABLE_BUILTIN_METRICS";
737+
private static final String SPANNER_MONITORING_HOST = "SPANNER_MONITORING_HOST";
731738

732739
private SpannerEnvironmentImpl() {}
733740

@@ -763,6 +770,11 @@ public boolean isEnableBuiltInMetrics() {
763770
public boolean isEnableEndToEndTracing() {
764771
return Boolean.parseBoolean(System.getenv(SPANNER_ENABLE_END_TO_END_TRACING));
765772
}
773+
774+
@Override
775+
public String getMonitoringHost() {
776+
return System.getenv(SPANNER_MONITORING_HOST);
777+
}
766778
}
767779

768780
/** Builder for {@link SpannerOptions} instances. */
@@ -828,6 +840,7 @@ public static class Builder
828840
private boolean enableExtendedTracing = SpannerOptions.environment.isEnableExtendedTracing();
829841
private boolean enableEndToEndTracing = SpannerOptions.environment.isEnableEndToEndTracing();
830842
private boolean enableBuiltInMetrics = SpannerOptions.environment.isEnableBuiltInMetrics();
843+
private String monitoringHost = SpannerOptions.environment.getMonitoringHost();
831844

832845
private static String createCustomClientLibToken(String token) {
833846
return token + " " + ServiceOptions.getGoogApiClientLibName();
@@ -895,6 +908,7 @@ protected Builder() {
895908
this.enableExtendedTracing = options.enableExtendedTracing;
896909
this.enableBuiltInMetrics = options.enableBuiltInMetrics;
897910
this.enableEndToEndTracing = options.enableEndToEndTracing;
911+
this.monitoringHost = options.monitoringHost;
898912
}
899913

900914
@Override
@@ -1417,6 +1431,12 @@ public Builder setBuiltInMetricsEnabled(boolean enableBuiltInMetrics) {
14171431
return this;
14181432
}
14191433

1434+
/** Sets the monitoring host to be used for Built-in client side metrics */
1435+
public Builder setMonitoringHost(String monitoringHost) {
1436+
this.monitoringHost = monitoringHost;
1437+
return this;
1438+
}
1439+
14201440
/**
14211441
* Sets whether to enable extended OpenTelemetry tracing. Enabling this option will add the
14221442
* following additional attributes to the traces that are generated by the client:
@@ -1727,7 +1747,7 @@ private ApiTracerFactory getDefaultApiTracerFactory() {
17271747
private ApiTracerFactory createMetricsApiTracerFactory() {
17281748
OpenTelemetry openTelemetry =
17291749
this.builtInOpenTelemetryMetricsProvider.getOrCreateOpenTelemetry(
1730-
this.getProjectId(), getCredentials());
1750+
this.getProjectId(), getCredentials(), this.monitoringHost);
17311751

17321752
return openTelemetry != null
17331753
? new MetricsTracerFactory(
@@ -1754,6 +1774,11 @@ public boolean isEnableBuiltInMetrics() {
17541774
return enableBuiltInMetrics;
17551775
}
17561776

1777+
/** Returns the override metrics Host. */
1778+
String getMonitoringHost() {
1779+
return monitoringHost;
1780+
}
1781+
17571782
@BetaApi
17581783
public boolean isUseVirtualThreads() {
17591784
return useVirtualThreads;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerCloudMonitoringExporterTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void testExportingSumDataInBatches() {
337337
@Test
338338
public void getAggregationTemporality() throws IOException {
339339
SpannerCloudMonitoringExporter actualExporter =
340-
SpannerCloudMonitoringExporter.create(projectId, null);
340+
SpannerCloudMonitoringExporter.create(projectId, null, null);
341341
assertThat(actualExporter.getAggregationTemporality(InstrumentType.COUNTER))
342342
.isEqualTo(AggregationTemporality.CUMULATIVE);
343343
}
@@ -348,7 +348,7 @@ public void testSkipExportingDataIfMissingInstanceId() throws IOException {
348348
Attributes.builder().putAll(attributes).remove(INSTANCE_ID_KEY).build();
349349

350350
SpannerCloudMonitoringExporter actualExporter =
351-
SpannerCloudMonitoringExporter.create(projectId, null);
351+
SpannerCloudMonitoringExporter.create(projectId, null, null);
352352
assertThat(actualExporter.getAggregationTemporality(InstrumentType.COUNTER))
353353
.isEqualTo(AggregationTemporality.CUMULATIVE);
354354
ArgumentCaptor<CreateTimeSeriesRequest> argumentCaptor =

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,19 @@ public void testEndToEndTracingEnablement() {
754754
.isEndToEndTracingEnabled());
755755
}
756756

757+
@Test
758+
public void testmonitoringHost() {
759+
String metricsEndpoint = "test-endpoint:443";
760+
assertNull(SpannerOptions.newBuilder().setProjectId("p").build().getMonitoringHost());
761+
assertThat(
762+
SpannerOptions.newBuilder()
763+
.setProjectId("p")
764+
.setMonitoringHost(metricsEndpoint)
765+
.build()
766+
.getMonitoringHost())
767+
.isEqualTo(metricsEndpoint);
768+
}
769+
757770
@Test
758771
public void testSetDirectedReadOptions() {
759772
final DirectedReadOptions directedReadOptions =

0 commit comments

Comments
 (0)