1717package com .google .cloud .datastore .telemetry ;
1818
1919import com .google .api .core .InternalApi ;
20- import com .google .api .gax .tracing .OpenTelemetryMetricsRecorder ;
21- import com .google .cloud .TransportOptions ;
22- import com .google .cloud .grpc .GrpcTransportOptions ;
2320import com .google .common .collect .ImmutableSet ;
2421import io .opentelemetry .api .common .AttributeKey ;
2522import java .util .Set ;
3330 */
3431@ InternalApi
3532public class TelemetryConstants {
36- // Built-in metrics constants for Cloud Monitoring export
33+
34+ // The Firestore namespace has not been deployed yet. Must target the custom namespace
35+ // until this is implemented.
3736 public static final String METRIC_PREFIX = "custom.googleapis.com/internal/client" ;
38- public static final String GAX_METER_NAME = OpenTelemetryMetricsRecorder .GAX_METER_NAME ;
3937 public static final String DATASTORE_METER_NAME = "java-datastore" ;
4038
4139 // Monitored resource type for Cloud Monitoring
4240 public static final String DATASTORE_RESOURCE_TYPE = "global" ;
4341
4442 // Resource label keys for the monitored resource
43+ // The Firestore namespace has not been deployed yet. Must target the global
44+ // Monitored Resource until this is implemented.
4545 public static final String RESOURCE_LABEL_PROJECT_ID = "project_id" ;
4646 public static final String RESOURCE_LABEL_DATABASE_ID = "database_id" ;
4747 public static final String RESOURCE_LABEL_LOCATION = "location" ;
4848 public static final Set <String > DATASTORE_RESOURCE_LABELS =
49- ImmutableSet .of (RESOURCE_LABEL_PROJECT_ID );
50-
51- // Resource attribute keys (used on OTel Resource)
52- public static final AttributeKey <String > PROJECT_ID_KEY = AttributeKey .stringKey ("project_id" );
53- public static final AttributeKey <String > DATABASE_ID_KEY = AttributeKey .stringKey ("database_id" );
54- public static final AttributeKey <String > LOCATION_ID_KEY = AttributeKey .stringKey ("location" );
55-
56- // Metric attribute keys (used on metric data points)
57- public static final AttributeKey <String > CLIENT_UID_KEY = AttributeKey .stringKey ("client_uid" );
58- public static final AttributeKey <String > CLIENT_NAME_KEY = AttributeKey .stringKey ("client_name" );
59- public static final AttributeKey <String > METHOD_KEY = AttributeKey .stringKey ("method" );
60- public static final AttributeKey <String > STATUS_KEY = AttributeKey .stringKey ("status" );
61- public static final AttributeKey <String > DATABASE_KEY = AttributeKey .stringKey ("database_id" );
62- public static final AttributeKey <String > LIBRARY_VERSION_KEY =
63- AttributeKey .stringKey ("library_version" );
64- public static final AttributeKey <String > TRANSPORT_KEY = AttributeKey .stringKey ("transport" );
65- public static final AttributeKey <String > SERVICE_KEY = AttributeKey .stringKey ("service" );
66-
67- public static final String SERVICE_VALUE = "datastore.googleapis.com" ;
68-
69- /**
70- * The allowlist of metric attributes that are permitted on every exported data point.
71- *
72- * <p>Cloud Monitoring is strict about label schemas: exporting a label that was not present when
73- * the metric descriptor was first created will cause the entire {@code createTimeSeries} call to
74- * fail.
75- */
76- public static final Set <AttributeKey <?>> COMMON_ATTRIBUTES =
77- ImmutableSet .of (
78- CLIENT_UID_KEY ,
79- CLIENT_NAME_KEY ,
80- METHOD_KEY ,
81- STATUS_KEY ,
82- DATABASE_KEY ,
83- LIBRARY_VERSION_KEY ,
84- TRANSPORT_KEY ,
85- SERVICE_KEY );
86-
87- // Metric names (short names, without prefix)
88- public static final String METRIC_NAME_SHORT_OPERATION_LATENCY = "operation_latency" ;
89- public static final String METRIC_NAME_SHORT_ATTEMPT_LATENCY = "attempt_latency" ;
90- public static final String METRIC_NAME_SHORT_OPERATION_COUNT = "operation_count" ;
91- public static final String METRIC_NAME_SHORT_ATTEMPT_COUNT = "attempt_count" ;
92- public static final String METRIC_NAME_SHORT_TRANSACTION_LATENCY = "transaction_latency" ;
93- public static final String METRIC_NAME_SHORT_TRANSACTION_ATTEMPT_COUNT =
94- "transaction_attempt_count" ;
95-
96- // Metrics collected at the GAX layer vs Datastore SDK layer
97- public static final Set <String > GAX_METRICS =
98- ImmutableSet .of (
99- METRIC_NAME_SHORT_OPERATION_LATENCY ,
100- METRIC_NAME_SHORT_ATTEMPT_LATENCY ,
101- METRIC_NAME_SHORT_OPERATION_COUNT ,
102- METRIC_NAME_SHORT_ATTEMPT_COUNT );
103-
104- public static final Set <String > DATASTORE_METRICS =
10549 ImmutableSet .of (
106- METRIC_NAME_SHORT_TRANSACTION_LATENCY , METRIC_NAME_SHORT_TRANSACTION_ATTEMPT_COUNT );
107-
108- // Environment variable to enable/disable built-in metrics
109- public static final String ENABLE_METRICS_ENV_VAR = "DATASTORE_ENABLE_METRICS" ;
50+ RESOURCE_LABEL_PROJECT_ID , RESOURCE_LABEL_DATABASE_ID , RESOURCE_LABEL_LOCATION );
11051
11152 // Existing attribute key constants (string-based, used by MetricsHelper/TelemetryUtils)
11253 public static final String ATTRIBUTES_KEY_DOCUMENT_COUNT = "doc_count" ;
@@ -130,23 +71,58 @@ public class TelemetryConstants {
13071 /** Attribute key for the Datastore database ID. */
13172 public static final String ATTRIBUTES_KEY_DATABASE_ID = "database_id" ;
13273
133- public static final String ATTRIBUTES_KEY_LIBRARY_VERSION = "library_version" ;
74+ // Resource attribute keys (used on OTel Resource)
75+ public static final AttributeKey <String > PROJECT_ID_KEY = AttributeKey .stringKey ("project_id" );
76+ public static final AttributeKey <String > DATABASE_ID_KEY = AttributeKey .stringKey ("database_id" );
77+ public static final AttributeKey <String > LOCATION_ID_KEY = AttributeKey .stringKey ("location" );
78+
79+ // Metric attribute keys (used on metric data points)
80+ public static final AttributeKey <String > CLIENT_UID_KEY = AttributeKey .stringKey ("client_uid" );
81+ public static final AttributeKey <String > METHOD_KEY = AttributeKey .stringKey ("method" );
82+ public static final AttributeKey <String > STATUS_KEY = AttributeKey .stringKey ("status" );
83+ public static final AttributeKey <String > SERVICE_KEY = AttributeKey .stringKey ("service" );
84+
85+ public static final String SERVICE_VALUE = "datastore.googleapis.com" ;
13486
135- public static final String ATTRIBUTES_KEY_TRANSPORT = "transport" ;
87+ /** String key for the {@code service} metric attribute (value: {@code "service"}). */
88+ public static final String ATTRIBUTES_KEY_SERVICE = SERVICE_KEY .getKey ();
89+
90+ /**
91+ * The allowlist of metric attributes that are permitted on every exported data point.
92+ *
93+ * <p>Cloud Monitoring is strict about label schemas: exporting a label that was not present when
94+ * the metric descriptor was first created will cause the entire {@code createTimeSeries} call to
95+ * fail. Only {@code status}, {@code method}, {@code service}, and {@code client_uid} are
96+ * accepted; all other attributes must be omitted from every {@code record*()} call.
97+ */
98+ public static final Set <AttributeKey <?>> COMMON_ATTRIBUTES =
99+ ImmutableSet .of (CLIENT_UID_KEY , METHOD_KEY , STATUS_KEY , SERVICE_KEY );
136100
137101 /** Metric name for the total latency of a transaction. */
138102 public static final String METRIC_NAME_TRANSACTION_LATENCY =
139- METRIC_PREFIX + "/transaction_latency " ;
103+ METRIC_PREFIX + "/transaction_latencies " ;
140104
141105 /** Metric name for the number of attempts a transaction took. */
142106 public static final String METRIC_NAME_TRANSACTION_ATTEMPT_COUNT =
143107 METRIC_PREFIX + "/transaction_attempt_count" ;
144108
145- /** Metric name for the total latency of an operation (one full RPC call including retries). */
146- public static final String METRIC_NAME_OPERATION_LATENCY = METRIC_PREFIX + "/operation_latency" ;
109+ /**
110+ * Metric name for the total latency of an operation (one full RPC call including retries).
111+ *
112+ * <p>The plural form ({@code operation_latencies}) is intentional: it matches the internal Cloud
113+ * Monitoring metric descriptor name. {@link OpenTelemetryDatastoreMetricsRecorder} overrides the
114+ * inherited GAX method to record to this name rather than the singular GAX default.
115+ */
116+ public static final String METRIC_NAME_OPERATION_LATENCY = METRIC_PREFIX + "/operation_latencies" ;
147117
148- /** Metric name for the latency of a single RPC attempt. */
149- public static final String METRIC_NAME_ATTEMPT_LATENCY = METRIC_PREFIX + "/attempt_latency" ;
118+ /**
119+ * Metric name for the latency of a single RPC attempt.
120+ *
121+ * <p>The plural form ({@code attempt_latencies}) is intentional: it matches the internal Cloud
122+ * Monitoring metric descriptor name. {@link OpenTelemetryDatastoreMetricsRecorder} overrides the
123+ * inherited GAX method to record to this name rather than the singular GAX default.
124+ */
125+ public static final String METRIC_NAME_ATTEMPT_LATENCY = METRIC_PREFIX + "/attempt_latencies" ;
150126
151127 /** Metric name for the count of operations. */
152128 public static final String METRIC_NAME_OPERATION_COUNT = METRIC_PREFIX + "/operation_count" ;
@@ -191,13 +167,5 @@ public String getTransport() {
191167 }
192168 }
193169
194- public static String getTransportName (TransportOptions transportOptions ) {
195- if (transportOptions instanceof GrpcTransportOptions ) {
196- return Transport .GRPC .getTransport ();
197- } else {
198- return Transport .HTTP .getTransport ();
199- }
200- }
201-
202170 private TelemetryConstants () {}
203171}
0 commit comments