1616
1717package com .google .cloud .datastore ;
1818
19+ import com .google .api .core .BetaApi ;
1920import io .opentelemetry .api .OpenTelemetry ;
2021import javax .annotation .Nonnull ;
2122import javax .annotation .Nullable ;
2223
24+ /**
25+ * Represents the options that are used to configure the use of OpenTelemetry for telemetry
26+ * collection in the Datastore SDK.
27+ */
2328public class DatastoreOpenTelemetryOptions {
2429 private final boolean tracingEnabled ;
2530 private final boolean metricsEnabled ;
31+ private final boolean exportBuiltinMetricsToGoogleCloudMonitoring ;
2632 private final @ Nullable OpenTelemetry openTelemetry ;
2733
2834 DatastoreOpenTelemetryOptions (Builder builder ) {
2935 this .tracingEnabled = builder .tracingEnabled ;
3036 this .metricsEnabled = builder .metricsEnabled ;
37+ this .exportBuiltinMetricsToGoogleCloudMonitoring =
38+ builder .exportBuiltinMetricsToGoogleCloudMonitoring ;
3139 this .openTelemetry = builder .openTelemetry ;
3240 }
3341
3442 /**
35- * Returns whether either tracing or metrics are enabled. Telemetry is disabled by default.
43+ * Returns whether either tracing or custom metrics (via a user-provided {@link OpenTelemetry}
44+ * instance) are enabled.
45+ *
46+ * <p><b>Note:</b> This method does <em>not</em> reflect the state of built-in metrics export to
47+ * Google Cloud Monitoring, which is controlled separately by {@link
48+ * #isExportBuiltinMetricsToGoogleCloudMonitoring()} and is {@code false} by default. To check
49+ * whether any telemetry is active, also consult that flag.
3650 *
37- * @return {@code true} if either tracing or metrics are enabled, {@code false} otherwise.
51+ * @return {@code true} if tracing or custom OTel metrics are enabled, {@code false} otherwise.
3852 */
3953 public boolean isEnabled () {
4054 return tracingEnabled || metricsEnabled ;
@@ -50,24 +64,54 @@ public boolean isTracingEnabled() {
5064 }
5165
5266 /**
53- * Returns whether metrics are enabled.
67+ * Returns whether metrics are enabled for the custom (user-provided) OpenTelemetry backend .
5468 *
5569 * @return {@code true} if metrics are enabled, {@code false} otherwise.
5670 */
5771 public boolean isMetricsEnabled () {
5872 return metricsEnabled ;
5973 }
6074
75+ /**
76+ * Returns whether built-in metrics should be exported to Google Cloud Monitoring.
77+ *
78+ * <p>When enabled, client-side metrics are automatically exported to Google Cloud Monitoring
79+ * using the Cloud Monitoring API. This is independent of the custom OpenTelemetry backend
80+ * configured via {@link #getOpenTelemetry()}.
81+ *
82+ * @return {@code true} if built-in metrics export to Cloud Monitoring is enabled, {@code false}
83+ * otherwise.
84+ */
85+ @ BetaApi
86+ public boolean isExportBuiltinMetricsToGoogleCloudMonitoring () {
87+ return exportBuiltinMetricsToGoogleCloudMonitoring ;
88+ }
89+
90+ /**
91+ * Returns the custom {@link OpenTelemetry} instance, if one was provided.
92+ *
93+ * @return the custom {@link OpenTelemetry} instance, or {@code null} if none was provided.
94+ */
6195 @ Nullable
6296 public OpenTelemetry getOpenTelemetry () {
6397 return openTelemetry ;
6498 }
6599
100+ /**
101+ * Returns a new {@link Builder} initialized with the values from this options instance.
102+ *
103+ * @return a new {@link Builder}.
104+ */
66105 @ Nonnull
67106 public DatastoreOpenTelemetryOptions .Builder toBuilder () {
68107 return new DatastoreOpenTelemetryOptions .Builder (this );
69108 }
70109
110+ /**
111+ * Returns a new default {@link Builder}.
112+ *
113+ * @return a new {@link Builder}.
114+ */
71115 @ Nonnull
72116 public static DatastoreOpenTelemetryOptions .Builder newBuilder () {
73117 return new DatastoreOpenTelemetryOptions .Builder ();
@@ -77,25 +121,31 @@ public static class Builder {
77121
78122 private boolean tracingEnabled ;
79123 private boolean metricsEnabled ;
124+ private boolean exportBuiltinMetricsToGoogleCloudMonitoring ;
80125
81126 @ Nullable private OpenTelemetry openTelemetry ;
82127
83128 private Builder () {
84129 tracingEnabled = false ;
85130 metricsEnabled = false ;
131+ // TODO(b/405457573): This is disabled by default until the Firestore namespace is deployed
132+ exportBuiltinMetricsToGoogleCloudMonitoring = false ;
86133 openTelemetry = null ;
87134 }
88135
89136 private Builder (DatastoreOpenTelemetryOptions options ) {
90137 this .tracingEnabled = options .tracingEnabled ;
91138 this .metricsEnabled = options .metricsEnabled ;
139+ this .exportBuiltinMetricsToGoogleCloudMonitoring =
140+ options .exportBuiltinMetricsToGoogleCloudMonitoring ;
92141 this .openTelemetry = options .openTelemetry ;
93142 }
94143
95144 /**
96145 * Sets whether tracing should be enabled.
97146 *
98147 * @param enabled Whether tracing should be enabled.
148+ * @return this builder instance.
99149 */
100150 @ Nonnull
101151 public DatastoreOpenTelemetryOptions .Builder setTracingEnabled (boolean enabled ) {
@@ -104,23 +154,41 @@ public DatastoreOpenTelemetryOptions.Builder setTracingEnabled(boolean enabled)
104154 }
105155
106156 /**
107- * Sets whether metrics should be enabled.
157+ * Sets whether metrics should be enabled for the custom (user-provided) OpenTelemetry backend .
108158 *
109159 * @param enabled Whether metrics should be enabled.
110- * @return the builder instance.
160+ * @return this builder instance.
111161 */
112162 @ Nonnull
113163 DatastoreOpenTelemetryOptions .Builder setMetricsEnabled (boolean enabled ) {
114164 this .metricsEnabled = enabled ;
115165 return this ;
116166 }
117167
168+ /**
169+ * Sets whether built-in metrics should be exported to Google Cloud Monitoring.
170+ *
171+ * <p>When enabled, client-side metrics are automatically exported to Google Cloud Monitoring
172+ * using the Cloud Monitoring API. This can be disabled to prevent metrics from being sent to
173+ * Cloud Monitoring while still allowing metrics to flow to a custom OpenTelemetry backend.
174+ *
175+ * @param exportBuiltinMetrics Whether built-in metrics should be exported to Cloud Monitoring.
176+ * @return this builder instance.
177+ */
178+ @ BetaApi
179+ public DatastoreOpenTelemetryOptions .Builder setExportBuiltinMetricsToGoogleCloudMonitoring (
180+ boolean exportBuiltinMetrics ) {
181+ this .exportBuiltinMetricsToGoogleCloudMonitoring = exportBuiltinMetrics ;
182+ return this ;
183+ }
184+
118185 /**
119186 * Sets the {@link OpenTelemetry} to use with this Datastore instance. If telemetry collection
120- * is enabled, but an ` OpenTelemetry` is not provided, the Datastore SDK will attempt to use the
121- * ` GlobalOpenTelemetry` .
187+ * is enabled, but an {@code OpenTelemetry} is not provided, the Datastore SDK will attempt to
188+ * use the {@code GlobalOpenTelemetry} .
122189 *
123190 * @param openTelemetry The OpenTelemetry that should be used by this Datastore instance.
191+ * @return this builder instance.
124192 */
125193 @ Nonnull
126194 public DatastoreOpenTelemetryOptions .Builder setOpenTelemetry (
@@ -129,6 +197,11 @@ public DatastoreOpenTelemetryOptions.Builder setOpenTelemetry(
129197 return this ;
130198 }
131199
200+ /**
201+ * Builds a new {@link DatastoreOpenTelemetryOptions} instance from this builder.
202+ *
203+ * @return a new {@link DatastoreOpenTelemetryOptions}.
204+ */
132205 @ Nonnull
133206 public DatastoreOpenTelemetryOptions build () {
134207 return new DatastoreOpenTelemetryOptions (this );
0 commit comments