|
39 | 39 | import com.google.common.base.Strings;
|
40 | 40 | import com.google.common.collect.Sets;
|
41 | 41 | import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
|
| 42 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 43 | +import io.opentelemetry.api.OpenTelemetry; |
42 | 44 | import java.io.IOException;
|
43 | 45 | import java.lang.reflect.Constructor;
|
44 | 46 | import java.lang.reflect.InvocationTargetException;
|
|
54 | 56 | import java.util.regex.Matcher;
|
55 | 57 | import java.util.regex.Pattern;
|
56 | 58 | import java.util.stream.Stream;
|
| 59 | +import javax.annotation.Nonnull; |
57 | 60 | import javax.annotation.Nullable;
|
58 | 61 |
|
59 | 62 | /**
|
@@ -169,6 +172,7 @@ public String[] getValidValues() {
|
169 | 172 | static final boolean DEFAULT_RETRY_ABORTS_INTERNALLY = true;
|
170 | 173 | static final boolean DEFAULT_USE_VIRTUAL_THREADS = false;
|
171 | 174 | static final boolean DEFAULT_USE_VIRTUAL_GRPC_TRANSPORT_THREADS = false;
|
| 175 | + static final boolean DEFAULT_ENABLE_OPENTELEMETRY_METRICS = false; |
172 | 176 | private static final String DEFAULT_CREDENTIALS = null;
|
173 | 177 | private static final String DEFAULT_OAUTH_TOKEN = null;
|
174 | 178 | private static final String DEFAULT_MIN_SESSIONS = null;
|
@@ -211,6 +215,9 @@ public String[] getValidValues() {
|
211 | 215 | /** Name of the property to enable/disable virtual threads for gRPC transport. */
|
212 | 216 | public static final String USE_VIRTUAL_GRPC_TRANSPORT_THREADS_PROPERTY_NAME =
|
213 | 217 | "useVirtualGrpcTransportThreads";
|
| 218 | + /** Name of the property to enable/disable OpenTelemtry metrics. */ |
| 219 | + public static final String ENABLE_OPENTELEMETRY_METRICS_PROPERTY_NAME = |
| 220 | + "enableOpenTelemetryMetrics"; |
214 | 221 | /** Name of the 'credentials' connection property. */
|
215 | 222 | public static final String CREDENTIALS_PROPERTY_NAME = "credentials";
|
216 | 223 | /** Name of the 'encodedCredentials' connection property. */
|
@@ -310,6 +317,10 @@ private static String generateGuardedConnectionPropertyError(
|
310 | 317 | "Use a virtual thread instead of a platform thread for the gRPC executor (true/false). "
|
311 | 318 | + "This option only has any effect if the application is running on Java 21 or higher. In all other cases, the option is ignored.",
|
312 | 319 | DEFAULT_USE_VIRTUAL_GRPC_TRANSPORT_THREADS),
|
| 320 | + ConnectionProperty.createBooleanProperty( |
| 321 | + ENABLE_OPENTELEMETRY_METRICS_PROPERTY_NAME, |
| 322 | + "Enable OpenTelemetry metrics in the client library (true/false). ", |
| 323 | + DEFAULT_ENABLE_OPENTELEMETRY_METRICS), |
313 | 324 | ConnectionProperty.createStringProperty(
|
314 | 325 | CREDENTIALS_PROPERTY_NAME,
|
315 | 326 | "The location of the credentials file to use for this connection. If neither this property or encoded credentials are set, the connection will use the default Google Cloud credentials for the runtime environment."),
|
@@ -484,6 +495,7 @@ public static class Builder {
|
484 | 495 | private List<StatementExecutionInterceptor> statementExecutionInterceptors =
|
485 | 496 | Collections.emptyList();
|
486 | 497 | private SpannerOptionsConfigurator configurator;
|
| 498 | + private OpenTelemetry openTelemetry; |
487 | 499 |
|
488 | 500 | private Builder() {}
|
489 | 501 |
|
@@ -633,6 +645,11 @@ Builder setCredentials(Credentials credentials) {
|
633 | 645 | return this;
|
634 | 646 | }
|
635 | 647 |
|
| 648 | + public Builder setOpenTelemetry(OpenTelemetry openTelemetry) { |
| 649 | + this.openTelemetry = openTelemetry; |
| 650 | + return this; |
| 651 | + } |
| 652 | + |
636 | 653 | /** @return the {@link ConnectionOptions} */
|
637 | 654 | public ConnectionOptions build() {
|
638 | 655 | Preconditions.checkState(this.uri != null, "Connection URI is required");
|
@@ -691,6 +708,8 @@ public static Builder newBuilder() {
|
691 | 708 | private final boolean retryAbortsInternally;
|
692 | 709 | private final boolean useVirtualThreads;
|
693 | 710 | private final boolean useVirtualGrpcTransportThreads;
|
| 711 | + private final boolean enableOpenTelemetryMetrics; |
| 712 | + private final OpenTelemetry openTelemetry; |
694 | 713 | private final List<StatementExecutionInterceptor> statementExecutionInterceptors;
|
695 | 714 | private final SpannerOptionsConfigurator configurator;
|
696 | 715 |
|
@@ -792,6 +811,8 @@ private ConnectionOptions(Builder builder) {
|
792 | 811 | this.retryAbortsInternally = parseRetryAbortsInternally(this.uri);
|
793 | 812 | this.useVirtualThreads = parseUseVirtualThreads(this.uri);
|
794 | 813 | this.useVirtualGrpcTransportThreads = parseUseVirtualGrpcTransportThreads(this.uri);
|
| 814 | + this.enableOpenTelemetryMetrics = parseEnableOpenTelemetryMetrics(this.uri); |
| 815 | + this.openTelemetry = builder.openTelemetry; |
795 | 816 | this.statementExecutionInterceptors =
|
796 | 817 | Collections.unmodifiableList(builder.statementExecutionInterceptors);
|
797 | 818 | this.configurator = builder.configurator;
|
@@ -856,6 +877,19 @@ private static Integer parseIntegerProperty(String propertyName, String value) {
|
856 | 877 | return null;
|
857 | 878 | }
|
858 | 879 |
|
| 880 | + /** |
| 881 | + * Returns an instance of OpenTelemetry. If OpenTelemetry object is not set then |
| 882 | + * GlobalOpenTelemetry will be used as fallback. |
| 883 | + */ |
| 884 | + @Nonnull |
| 885 | + OpenTelemetry getOpenTelemetry() { |
| 886 | + if (this.openTelemetry != null) { |
| 887 | + return this.openTelemetry; |
| 888 | + } else { |
| 889 | + return GlobalOpenTelemetry.get(); |
| 890 | + } |
| 891 | + } |
| 892 | + |
859 | 893 | SpannerOptionsConfigurator getConfigurator() {
|
860 | 894 | return configurator;
|
861 | 895 | }
|
@@ -906,6 +940,12 @@ static boolean parseUseVirtualGrpcTransportThreads(String uri) {
|
906 | 940 | return value != null ? Boolean.parseBoolean(value) : DEFAULT_USE_VIRTUAL_GRPC_TRANSPORT_THREADS;
|
907 | 941 | }
|
908 | 942 |
|
| 943 | + @VisibleForTesting |
| 944 | + static boolean parseEnableOpenTelemetryMetrics(String uri) { |
| 945 | + String value = parseUriProperty(uri, ENABLE_OPENTELEMETRY_METRICS_PROPERTY_NAME); |
| 946 | + return value != null ? Boolean.parseBoolean(value) : DEFAULT_ENABLE_OPENTELEMETRY_METRICS; |
| 947 | + } |
| 948 | + |
909 | 949 | @VisibleForTesting
|
910 | 950 | static @Nullable String parseCredentials(String uri) {
|
911 | 951 | String value = parseUriProperty(uri, CREDENTIALS_PROPERTY_NAME);
|
@@ -1336,6 +1376,11 @@ public boolean isUseVirtualGrpcTransportThreads() {
|
1336 | 1376 | return useVirtualGrpcTransportThreads;
|
1337 | 1377 | }
|
1338 | 1378 |
|
| 1379 | + /** Whether connections should enable OpenTelemetry metrics. */ |
| 1380 | + public boolean isEnabledOpenTelemetryMetrics() { |
| 1381 | + return enableOpenTelemetryMetrics; |
| 1382 | + } |
| 1383 | + |
1339 | 1384 | /** Any warnings that were generated while creating the {@link ConnectionOptions} instance. */
|
1340 | 1385 | @Nullable
|
1341 | 1386 | public String getWarnings() {
|
|
0 commit comments