1717package com .google .cloud .bigtable .examples .proxy .metrics ;
1818
1919import com .google .auth .Credentials ;
20+ import com .google .auto .value .AutoValue ;
2021import com .google .cloud .bigtable .examples .proxy .core .CallLabels ;
2122import com .google .cloud .opentelemetry .metric .GoogleCloudMetricExporter ;
2223import com .google .cloud .opentelemetry .metric .MetricConfiguration ;
2829import io .opentelemetry .api .metrics .LongHistogram ;
2930import io .opentelemetry .api .metrics .LongUpDownCounter ;
3031import io .opentelemetry .api .metrics .Meter ;
32+ import io .opentelemetry .api .metrics .MeterProvider ;
3133import io .opentelemetry .contrib .gcp .resource .GCPResourceProvider ;
3234import io .opentelemetry .sdk .common .InstrumentationScopeInfo ;
3335import io .opentelemetry .sdk .metrics .SdkMeterProvider ;
@@ -56,7 +58,7 @@ public class MetricsImpl implements Closeable, Metrics {
5658 public static final String METRIC_PRESENCE_DESC = "Number of proxy processes" ;
5759 public static final String METRIC_PRESENCE_UNIT = "{process}" ;
5860
59- private final SdkMeterProvider meterProvider ;
61+ private final MeterProvider meterProvider ;
6062
6163 private final DoubleHistogram gfeLatency ;
6264 private final LongCounter gfeResponseHeadersMissing ;
@@ -75,7 +77,11 @@ public class MetricsImpl implements Closeable, Metrics {
7577 () -> Resource .create (new GCPResourceProvider ().getAttributes ());
7678
7779 public MetricsImpl (Credentials credentials , String projectId ) throws IOException {
78- meterProvider = createMeterProvider (credentials , projectId );
80+ this (createMeterProvider (credentials , projectId ));
81+ }
82+
83+ MetricsImpl (MeterProvider meterProvider ) {
84+ this .meterProvider = meterProvider ;
7985 Meter meter =
8086 meterProvider
8187 .meterBuilder (INSTRUMENTATION_SCOPE_INFO .getName ())
@@ -172,8 +178,21 @@ public MetricsImpl(Credentials credentials, String projectId) throws IOException
172178 }
173179
174180 @ Override
175- public void close () {
176- meterProvider .close ();
181+ public void close () throws IOException {
182+ if (meterProvider instanceof Closeable ) {
183+ ((Closeable ) meterProvider ).close ();
184+ }
185+ }
186+
187+ @ Override
188+ public MetricsAttributesImpl createAttributes (CallLabels callLabels ) {
189+ return new AutoValue_MetricsImpl_MetricsAttributesImpl (
190+ Attributes .builder ()
191+ .put (MetricsImpl .API_CLIENT_KEY , callLabels .getApiClient ().orElse ("<missing>" ))
192+ .put (MetricsImpl .RESOURCE_KEY , callLabels .getResourceName ().orElse ("<missing>" ))
193+ .put (MetricsImpl .APP_PROFILE_KEY , callLabels .getAppProfileId ().orElse ("<missing>" ))
194+ .put (MetricsImpl .METHOD_KEY , callLabels .getMethodName ())
195+ .build ());
177196 }
178197
179198 private static SdkMeterProvider createMeterProvider (Credentials credentials , String projectId ) {
@@ -194,49 +213,49 @@ private static SdkMeterProvider createMeterProvider(Credentials credentials, Str
194213 }
195214
196215 @ Override
197- public void recordCallStarted (CallLabels labels ) {
198- serverCallsStarted .add (1 , labels . getOtelAttributes ( ));
216+ public void recordCallStarted (MetricsAttributes attrs ) {
217+ serverCallsStarted .add (1 , unwrap ( attrs ));
199218
200219 int outstanding = numOutstandingRpcs .incrementAndGet ();
201220 maxSeen .updateAndGet (n -> Math .max (outstanding , n ));
202221 }
203222
204223 @ Override
205- public void recordCredLatency (CallLabels labels , Status status , Duration duration ) {
224+ public void recordCredLatency (MetricsAttributes attrs , Status status , Duration duration ) {
206225 Attributes attributes =
207- labels . getOtelAttributes ( ).toBuilder ().put (STATUS_KEY , status .getCode ().name ()).build ();
226+ unwrap ( attrs ).toBuilder ().put (STATUS_KEY , status .getCode ().name ()).build ();
208227 clientCredLatencies .record (duration .toMillis (), attributes );
209228 }
210229
211230 @ Override
212- public void recordQueueLatency (CallLabels labels , Duration duration ) {
213- clientQueueLatencies .record (duration .toMillis (), labels . getOtelAttributes ( ));
231+ public void recordQueueLatency (MetricsAttributes attrs , Duration duration ) {
232+ clientQueueLatencies .record (duration .toMillis (), unwrap ( attrs ));
214233 }
215234
216235 @ Override
217- public void recordRequestSize (CallLabels labels , long size ) {
218- requestSizes .record (size , labels . getOtelAttributes ( ));
236+ public void recordRequestSize (MetricsAttributes attrs , long size ) {
237+ requestSizes .record (size , unwrap ( attrs ));
219238 }
220239
221240 @ Override
222- public void recordResponseSize (CallLabels labels , long size ) {
223- responseSizes .record (size , labels . getOtelAttributes ( ));
241+ public void recordResponseSize (MetricsAttributes attrs , long size ) {
242+ responseSizes .record (size , unwrap ( attrs ));
224243 }
225244
226245 @ Override
227- public void recordGfeLatency (CallLabels labels , Duration duration ) {
228- gfeLatency .record (duration .toMillis (), labels . getOtelAttributes ( ));
246+ public void recordGfeLatency (MetricsAttributes attrs , Duration duration ) {
247+ gfeLatency .record (duration .toMillis (), unwrap ( attrs ));
229248 }
230249
231250 @ Override
232- public void recordGfeHeaderMissing (CallLabels labels ) {
233- gfeResponseHeadersMissing .add (1 , labels . getOtelAttributes ( ));
251+ public void recordGfeHeaderMissing (MetricsAttributes attrs ) {
252+ gfeResponseHeadersMissing .add (1 , unwrap ( attrs ));
234253 }
235254
236255 @ Override
237- public void recordCallLatency (CallLabels labels , Status status , Duration duration ) {
256+ public void recordCallLatency (MetricsAttributes attrs , Status status , Duration duration ) {
238257 Attributes attributes =
239- labels . getOtelAttributes ( ).toBuilder ().put (STATUS_KEY , status .getCode ().name ()).build ();
258+ unwrap ( attrs ).toBuilder ().put (STATUS_KEY , status .getCode ().name ()).build ();
240259
241260 clientCallLatencies .record (duration .toMillis (), attributes );
242261 numOutstandingRpcs .decrementAndGet ();
@@ -246,4 +265,13 @@ public void recordCallLatency(CallLabels labels, Status status, Duration duratio
246265 public void updateChannelCount (int delta ) {
247266 channelCounter .add (delta );
248267 }
268+
269+ static Attributes unwrap (MetricsAttributes wrapped ) {
270+ return ((MetricsAttributesImpl ) wrapped ).getAttributes ();
271+ }
272+
273+ @ AutoValue
274+ abstract static class MetricsAttributesImpl implements MetricsAttributes {
275+ abstract Attributes getAttributes ();
276+ }
249277}
0 commit comments