2020import javax .annotation .Nonnull ;
2121import javax .annotation .Nullable ;
2222
23+ /**
24+ * Represents the options that are used to configure the use of OpenTelemetry for telemetry
25+ * collection in the Datastore SDK.
26+ */
2327public class DatastoreOpenTelemetryOptions {
2428 private final boolean tracingEnabled ;
2529 private final boolean metricsEnabled ;
30+ private final boolean exportBuiltinMetricsToGoogleCloudMonitoring ;
2631 private final @ Nullable OpenTelemetry openTelemetry ;
2732
2833 DatastoreOpenTelemetryOptions (Builder builder ) {
2934 this .tracingEnabled = builder .tracingEnabled ;
3035 this .metricsEnabled = builder .metricsEnabled ;
36+ this .exportBuiltinMetricsToGoogleCloudMonitoring =
37+ builder .exportBuiltinMetricsToGoogleCloudMonitoring ;
3138 this .openTelemetry = builder .openTelemetry ;
3239 }
3340
@@ -50,24 +57,53 @@ public boolean isTracingEnabled() {
5057 }
5158
5259 /**
53- * Returns whether metrics are enabled.
60+ * Returns whether metrics are enabled for the custom (user-provided) OpenTelemetry backend .
5461 *
5562 * @return {@code true} if metrics are enabled, {@code false} otherwise.
5663 */
5764 public boolean isMetricsEnabled () {
5865 return metricsEnabled ;
5966 }
6067
68+ /**
69+ * Returns whether built-in metrics should be exported to Google Cloud Monitoring.
70+ *
71+ * <p>When enabled, client-side metrics are automatically exported to Google Cloud Monitoring using
72+ * the Cloud Monitoring API. This is independent of the custom OpenTelemetry backend configured via
73+ * {@link #getOpenTelemetry()}.
74+ *
75+ * @return {@code true} if built-in metrics export to Cloud Monitoring is enabled, {@code false}
76+ * otherwise.
77+ */
78+ public boolean isExportBuiltinMetricsToGoogleCloudMonitoring () {
79+ return exportBuiltinMetricsToGoogleCloudMonitoring ;
80+ }
81+
82+ /**
83+ * Returns the custom {@link OpenTelemetry} instance, if one was provided.
84+ *
85+ * @return the custom {@link OpenTelemetry} instance, or {@code null} if none was provided.
86+ */
6187 @ Nullable
6288 public OpenTelemetry getOpenTelemetry () {
6389 return openTelemetry ;
6490 }
6591
92+ /**
93+ * Returns a new {@link Builder} initialized with the values from this options instance.
94+ *
95+ * @return a new {@link Builder}.
96+ */
6697 @ Nonnull
6798 public DatastoreOpenTelemetryOptions .Builder toBuilder () {
6899 return new DatastoreOpenTelemetryOptions .Builder (this );
69100 }
70101
102+ /**
103+ * Returns a new default {@link Builder}.
104+ *
105+ * @return a new {@link Builder}.
106+ */
71107 @ Nonnull
72108 public static DatastoreOpenTelemetryOptions .Builder newBuilder () {
73109 return new DatastoreOpenTelemetryOptions .Builder ();
@@ -77,25 +113,30 @@ public static class Builder {
77113
78114 private boolean tracingEnabled ;
79115 private boolean metricsEnabled ;
116+ private boolean exportBuiltinMetricsToGoogleCloudMonitoring ;
80117
81118 @ Nullable private OpenTelemetry openTelemetry ;
82119
83120 private Builder () {
84121 tracingEnabled = false ;
85122 metricsEnabled = false ;
123+ exportBuiltinMetricsToGoogleCloudMonitoring = true ;
86124 openTelemetry = null ;
87125 }
88126
89127 private Builder (DatastoreOpenTelemetryOptions options ) {
90128 this .tracingEnabled = options .tracingEnabled ;
91129 this .metricsEnabled = options .metricsEnabled ;
130+ this .exportBuiltinMetricsToGoogleCloudMonitoring =
131+ options .exportBuiltinMetricsToGoogleCloudMonitoring ;
92132 this .openTelemetry = options .openTelemetry ;
93133 }
94134
95135 /**
96136 * Sets whether tracing should be enabled.
97137 *
98138 * @param enabled Whether tracing should be enabled.
139+ * @return this builder instance.
99140 */
100141 @ Nonnull
101142 public DatastoreOpenTelemetryOptions .Builder setTracingEnabled (boolean enabled ) {
@@ -104,23 +145,42 @@ public DatastoreOpenTelemetryOptions.Builder setTracingEnabled(boolean enabled)
104145 }
105146
106147 /**
107- * Sets whether metrics should be enabled.
148+ * Sets whether metrics should be enabled for the custom (user-provided) OpenTelemetry backend .
108149 *
109150 * @param enabled Whether metrics should be enabled.
110- * @return the builder instance.
151+ * @return this builder instance.
111152 */
112153 @ Nonnull
113154 DatastoreOpenTelemetryOptions .Builder setMetricsEnabled (boolean enabled ) {
114155 this .metricsEnabled = enabled ;
115156 return this ;
116157 }
117158
159+ /**
160+ * Sets whether built-in metrics should be exported to Google Cloud Monitoring.
161+ *
162+ * <p>When enabled (the default), client-side metrics are automatically exported to Google Cloud
163+ * Monitoring using the Cloud Monitoring API. This can be disabled to prevent metrics from being
164+ * sent to Cloud Monitoring while still allowing metrics to flow to a custom OpenTelemetry
165+ * backend.
166+ *
167+ * @param exportBuiltinMetrics Whether built-in metrics should be exported to Cloud Monitoring.
168+ * @return this builder instance.
169+ */
170+ @ Nonnull
171+ public DatastoreOpenTelemetryOptions .Builder setExportBuiltinMetricsToGoogleCloudMonitoring (
172+ boolean exportBuiltinMetrics ) {
173+ this .exportBuiltinMetricsToGoogleCloudMonitoring = exportBuiltinMetrics ;
174+ return this ;
175+ }
176+
118177 /**
119178 * 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` .
179+ * is enabled, but an {@code OpenTelemetry} is not provided, the Datastore SDK will attempt to
180+ * use the {@code GlobalOpenTelemetry} .
122181 *
123182 * @param openTelemetry The OpenTelemetry that should be used by this Datastore instance.
183+ * @return this builder instance.
124184 */
125185 @ Nonnull
126186 public DatastoreOpenTelemetryOptions .Builder setOpenTelemetry (
@@ -129,6 +189,11 @@ public DatastoreOpenTelemetryOptions.Builder setOpenTelemetry(
129189 return this ;
130190 }
131191
192+ /**
193+ * Builds a new {@link DatastoreOpenTelemetryOptions} instance from this builder.
194+ *
195+ * @return a new {@link DatastoreOpenTelemetryOptions}.
196+ */
132197 @ Nonnull
133198 public DatastoreOpenTelemetryOptions build () {
134199 return new DatastoreOpenTelemetryOptions (this );
0 commit comments