4545import com .google .cloud .RetryHelper ;
4646import com .google .cloud .RetryHelper .RetryHelperException ;
4747import com .google .cloud .ServiceOptions ;
48- import com .google .cloud .TransportOptions ;
4948import com .google .cloud .datastore .execution .AggregationQueryExecutor ;
5049import com .google .cloud .datastore .spi .v1 .DatastoreRpc ;
51- import com .google .cloud .datastore .telemetry .MetricsRecorder ;
50+ import com .google .cloud .datastore .telemetry .DatastoreMetricsRecorder ;
5251import com .google .cloud .datastore .telemetry .TelemetryConstants ;
5352import com .google .cloud .datastore .telemetry .TelemetryUtils ;
5453import com .google .cloud .datastore .telemetry .TraceUtil ;
5554import com .google .cloud .datastore .telemetry .TraceUtil .Scope ;
56- import com .google .cloud .http .HttpTransportOptions ;
5755import com .google .common .base .MoreObjects ;
5856import com .google .common .base .Preconditions ;
5957import com .google .common .base .Stopwatch ;
7573import java .util .ArrayList ;
7674import java .util .Arrays ;
7775import java .util .Collections ;
78- import java .util .HashMap ;
7976import java .util .Iterator ;
8077import java .util .LinkedHashMap ;
8178import java .util .LinkedHashSet ;
@@ -101,16 +98,13 @@ final class DatastoreImpl extends BaseService<DatastoreOptions> implements Datas
10198
10299 private final com .google .cloud .datastore .telemetry .TraceUtil otelTraceUtil =
103100 getOptions ().getTraceUtil ();
104- private final MetricsRecorder metricsRecorder = getOptions ().getMetricsRecorder ();
105- private final boolean isHttpTransport ;
106-
101+ private final DatastoreMetricsRecorder metricsRecorder = getOptions ().getMetricsRecorder ();
107102 private final ReadOptionProtoPreparer readOptionProtoPreparer ;
108103 private final AggregationQueryExecutor aggregationQueryExecutor ;
109104
110105 DatastoreImpl (DatastoreOptions options ) {
111106 super (options );
112107 this .datastoreRpc = options .getDatastoreRpcV1 ();
113- this .isHttpTransport = options .getTransportOptions () instanceof HttpTransportOptions ;
114108 retrySettings =
115109 MoreObjects .firstNonNull (options .getRetrySettings (), ServiceOptions .getNoRetrySettings ());
116110
@@ -185,15 +179,15 @@ public boolean isClosed() {
185179 static class ReadWriteTransactionCallable <T > implements Callable <T > {
186180 private final Datastore datastore ;
187181 private final TransactionCallable <T > callable ;
188- private final MetricsRecorder metricsRecorder ;
182+ private final DatastoreMetricsRecorder metricsRecorder ;
189183 private volatile TransactionOptions options ;
190184 private volatile Transaction transaction ;
191185
192186 ReadWriteTransactionCallable (
193187 Datastore datastore ,
194188 TransactionCallable <T > callable ,
195189 TransactionOptions options ,
196- MetricsRecorder metricsRecorder ) {
190+ DatastoreMetricsRecorder metricsRecorder ) {
197191 this .datastore = datastore ;
198192 this .callable = callable ;
199193 this .options = options ;
@@ -227,7 +221,7 @@ public T call() throws DatastoreException {
227221 }
228222 throw DatastoreException .propagateUserException (ex );
229223 } finally {
230- recordAttempt (attemptStatus , datastore . getOptions (). getTransportOptions () );
224+ recordAttempt (attemptStatus );
231225 // If the transaction is active, then commit the rollback. If it was already successfully
232226 // rolled back, the transaction is inactive (prevents rolling back an already rolled back
233227 // transaction).
@@ -245,18 +239,10 @@ public T call() throws DatastoreException {
245239 * Records a single transaction commit attempt with the given status code. This is called once
246240 * per invocation of {@link #call()}, capturing the outcome of each individual commit attempt.
247241 */
248- private void recordAttempt (String status , TransportOptions transportOptions ) {
249- Map <String , String > attributes = new HashMap <>();
250- attributes .put (TelemetryConstants .ATTRIBUTES_KEY_STATUS , status );
251- attributes .put (
252- TelemetryConstants .ATTRIBUTES_KEY_METHOD , TelemetryConstants .METHOD_TRANSACTION_COMMIT );
253- attributes .put (
254- TelemetryConstants .ATTRIBUTES_KEY_PROJECT_ID , datastore .getOptions ().getProjectId ());
255- attributes .put (
256- TelemetryConstants .ATTRIBUTES_KEY_DATABASE_ID , datastore .getOptions ().getDatabaseId ());
257- attributes .put (
258- TelemetryConstants .ATTRIBUTES_KEY_TRANSPORT ,
259- TelemetryConstants .getTransportName (transportOptions ));
242+ private void recordAttempt (String status ) {
243+ Map <String , String > attributes =
244+ TelemetryUtils .buildMetricAttributes (
245+ TelemetryConstants .METHOD_TRANSACTION_COMMIT , status );
260246 metricsRecorder .recordTransactionAttemptCount (1 , attributes );
261247 }
262248 }
@@ -293,15 +279,8 @@ public <T> T runInTransaction(
293279 throw DatastoreException .translateAndThrow (e );
294280 } finally {
295281 long latencyMs = stopwatch .elapsed (TimeUnit .MILLISECONDS );
296- Map <String , String > attributes = new HashMap <>();
297- attributes .put (TelemetryConstants .ATTRIBUTES_KEY_STATUS , status );
298- attributes .put (
299- TelemetryConstants .ATTRIBUTES_KEY_METHOD , TelemetryConstants .METHOD_TRANSACTION_RUN );
300- attributes .put (TelemetryConstants .ATTRIBUTES_KEY_PROJECT_ID , getOptions ().getProjectId ());
301- attributes .put (TelemetryConstants .ATTRIBUTES_KEY_DATABASE_ID , getOptions ().getDatabaseId ());
302- attributes .put (
303- TelemetryConstants .ATTRIBUTES_KEY_TRANSPORT ,
304- TelemetryConstants .getTransportName (getOptions ().getTransportOptions ()));
282+ Map <String , String > attributes =
283+ TelemetryUtils .buildMetricAttributes (TelemetryConstants .METHOD_TRANSACTION_RUN , status );
305284 metricsRecorder .recordTransactionLatency (latencyMs , attributes );
306285 span .end ();
307286 }
@@ -805,15 +784,12 @@ private <T> T runWithObservability(
805784 ResultRetryAlgorithm <?> exceptionHandler ) {
806785 com .google .cloud .datastore .telemetry .TraceUtil .Span span = otelTraceUtil .startSpan (spanName );
807786
808- // Gax already records operation and attempt metrics. Since Datastore HttpJson does not
809- // integrate with Gax, manually instrument these metrics when using HttpJson for parity
810- Stopwatch operationStopwatch = isHttpTransport ? Stopwatch .createStarted () : null ;
787+ Stopwatch operationStopwatch = Stopwatch .createStarted ();
811788 String operationStatus = StatusCode .Code .OK .toString ();
812789
813790 DatastoreOptions options = getOptions ();
814791 Callable <T > attemptCallable =
815- TelemetryUtils .attemptMetricsCallable (
816- callable , metricsRecorder , options , isHttpTransport , methodName );
792+ TelemetryUtils .attemptMetricsCallable (callable , metricsRecorder , methodName );
817793 try (TraceUtil .Scope ignored = span .makeCurrent ()) {
818794 return RetryHelper .runWithRetries (
819795 attemptCallable , retrySettings , exceptionHandler , options .getClock ());
@@ -823,12 +799,7 @@ private <T> T runWithObservability(
823799 throw DatastoreException .translateAndThrow (e );
824800 } finally {
825801 TelemetryUtils .recordOperationMetrics (
826- metricsRecorder ,
827- options ,
828- isHttpTransport ,
829- operationStopwatch ,
830- methodName ,
831- operationStatus );
802+ metricsRecorder , operationStopwatch , methodName , operationStatus );
832803 span .end ();
833804 }
834805 }
0 commit comments