11package com .amazon .sampleapp ;
22
3+ import io .opentelemetry .api .common .AttributeKey ;
4+ import io .opentelemetry .api .common .Attributes ;
5+ import io .opentelemetry .api .metrics .DoubleHistogram ;
36import io .opentelemetry .api .metrics .GlobalMeterProvider ;
47import io .opentelemetry .api .metrics .LongCounter ;
5- import io .opentelemetry .api .metrics .LongSumObserver ;
68import io .opentelemetry .api .metrics .LongUpDownCounter ;
7- import io .opentelemetry .api .metrics .LongUpDownSumObserver ;
8- import io .opentelemetry .api .metrics .LongValueObserver ;
9- import io .opentelemetry .api .metrics .LongValueRecorder ;
109import io .opentelemetry .api .metrics .Meter ;
11- import io .opentelemetry .api .metrics .common .Labels ;
1210
1311public class MetricEmitter {
1412
15- static final String DIMENSION_API_NAME = "apiName" ;
16- static final String DIMENSION_STATUS_CODE = "statusCode" ;
13+ static final AttributeKey < String > DIMENSION_API_NAME = AttributeKey . stringKey ( "apiName" ) ;
14+ static final AttributeKey < String > DIMENSION_STATUS_CODE = AttributeKey . stringKey ( "statusCode" ) ;
1715
1816 static String API_COUNTER_METRIC = "apiBytesSent" ;
1917 static String API_LATENCY_METRIC = "latency" ;
@@ -23,11 +21,9 @@ public class MetricEmitter {
2321 static String API_UP_DOWN_SUM_METRIC = "actualQueueSize" ;
2422
2523 LongCounter apiBytesSentCounter ;
26- LongValueRecorder apiLatencyRecorder ;
27- LongSumObserver totalBytesSentObserver ;
28- LongValueObserver apiLastLatencyObserver ;
24+ DoubleHistogram apiLatencyRecorder ;
25+ LongCounter totalBytesSentObserver ;
2926 LongUpDownCounter queueSizeCounter ;
30- LongUpDownSumObserver actualQueueSizeObserver ;
3127
3228 long totalBytesSent ;
3329 long apiLastLatency ;
@@ -39,7 +35,8 @@ public class MetricEmitter {
3935 String statusCodeValue = "" ;
4036
4137 public MetricEmitter () {
42- Meter meter = GlobalMeterProvider .getMeter ("aws-otel" , "1.0" );
38+ Meter meter =
39+ GlobalMeterProvider .get ().meterBuilder ("aws-otel" ).setInstrumentationVersion ("1.0" ).build ();
4340
4441 // give a instanceId appending to the metricname so that we can check the metric for each round
4542 // of integ-test
@@ -65,95 +62,83 @@ public MetricEmitter() {
6562
6663 apiBytesSentCounter =
6764 meter
68- .longCounterBuilder (apiBytesSentMetricName )
65+ .counterBuilder (apiBytesSentMetricName )
6966 .setDescription ("API request load sent in bytes" )
7067 .setUnit ("one" )
7168 .build ();
7269
7370 apiLatencyRecorder =
7471 meter
75- .longValueRecorderBuilder (latencyMetricName )
72+ .histogramBuilder (latencyMetricName )
7673 .setDescription ("API latency time" )
7774 .setUnit ("ms" )
7875 .build ();
7976
8077 queueSizeCounter =
8178 meter
82- .longUpDownCounterBuilder (queueSizeChangeMetricName )
79+ .upDownCounterBuilder (queueSizeChangeMetricName )
8380 .setDescription ("Queue Size change" )
8481 .setUnit ("one" )
8582 .build ();
8683
87- totalBytesSentObserver =
88- meter
89- .longSumObserverBuilder (totalApiBytesSentMetricName )
90- .setDescription ("Total API request load sent in bytes" )
91- .setUnit ("one" )
92- .setUpdater (
93- longResult -> {
94- System .out .println (
95- "emit total http request size "
96- + totalBytesSent
97- + " byte, "
98- + apiNameValue
99- + ","
100- + statusCodeValue );
101- longResult .observe (
102- totalBytesSent ,
103- Labels .of (
104- DIMENSION_API_NAME ,
105- apiNameValue ,
106- DIMENSION_STATUS_CODE ,
107- statusCodeValue ));
108- })
109- .build ();
110-
111- apiLastLatencyObserver =
112- meter
113- .longValueObserverBuilder (lastLatencyMetricName )
114- .setDescription ("The last API latency observed at collection interval" )
115- .setUnit ("ms" )
116- .setUpdater (
117- longResult -> {
118- System .out .println (
119- "emit last api latency "
120- + apiLastLatency
121- + ","
122- + apiNameValue
123- + ","
124- + statusCodeValue );
125- longResult .observe (
126- apiLastLatency ,
127- Labels .of (
128- DIMENSION_API_NAME ,
129- apiNameValue ,
130- DIMENSION_STATUS_CODE ,
131- statusCodeValue ));
132- })
133- .build ();
134- actualQueueSizeObserver =
135- meter
136- .longUpDownSumObserverBuilder (actualQueueSizeMetricName )
137- .setDescription ("The actual queue size observed at collection interval" )
138- .setUnit ("one" )
139- .setUpdater (
140- longResult -> {
141- System .out .println (
142- "emit actual queue size "
143- + actualQueueSize
144- + ","
145- + apiNameValue
146- + ","
147- + statusCodeValue );
148- longResult .observe (
149- actualQueueSize ,
150- Labels .of (
151- DIMENSION_API_NAME ,
152- apiNameValue ,
153- DIMENSION_STATUS_CODE ,
154- statusCodeValue ));
155- })
156- .build ();
84+ meter
85+ .gaugeBuilder (totalApiBytesSentMetricName )
86+ .setDescription ("Total API request load sent in bytes" )
87+ .setUnit ("one" )
88+ .ofLongs ()
89+ .buildWithCallback (
90+ measurement -> {
91+ System .out .println (
92+ "emit total http request size "
93+ + totalBytesSent
94+ + " byte, "
95+ + apiNameValue
96+ + ","
97+ + statusCodeValue );
98+ measurement .observe (
99+ totalBytesSent ,
100+ Attributes .of (
101+ DIMENSION_API_NAME , apiNameValue , DIMENSION_STATUS_CODE , statusCodeValue ));
102+ });
103+
104+ meter
105+ .gaugeBuilder (lastLatencyMetricName )
106+ .setDescription ("The last API latency observed at collection interval" )
107+ .setUnit ("ms" )
108+ .ofLongs ()
109+ .buildWithCallback (
110+ measurement -> {
111+ System .out .println (
112+ "emit last api latency "
113+ + apiLastLatency
114+ + ","
115+ + apiNameValue
116+ + ","
117+ + statusCodeValue );
118+ measurement .observe (
119+ apiLastLatency ,
120+ Attributes .of (
121+ DIMENSION_API_NAME , apiNameValue , DIMENSION_STATUS_CODE , statusCodeValue ));
122+ });
123+ meter
124+ .gaugeBuilder (actualQueueSizeMetricName )
125+ .setDescription ("The actual queue size observed at collection interval" )
126+ .setUnit ("one" )
127+ .ofLongs ()
128+ .buildWithCallback (
129+ measurement -> {
130+ System .out .println (
131+ "emit actual queue size "
132+ + actualQueueSize
133+ + ","
134+ + apiNameValue
135+ + ","
136+ + statusCodeValue );
137+ measurement .observe (
138+ actualQueueSize ,
139+ Attributes .of (
140+ DIMENSION_API_NAME , apiNameValue , DIMENSION_STATUS_CODE , statusCodeValue ));
141+ });
157142 }
158143
159144 /**
@@ -167,7 +152,7 @@ public void emitReturnTimeMetric(Long returnTime, String apiName, String statusC
167152 System .out .println (
168153 "emit metric with return time " + returnTime + "," + apiName + "," + statusCode );
169154 apiLatencyRecorder .record (
170- returnTime , Labels .of (DIMENSION_API_NAME , apiName , DIMENSION_STATUS_CODE , statusCode ));
155+ returnTime , Attributes .of (DIMENSION_API_NAME , apiName , DIMENSION_STATUS_CODE , statusCode ));
171156 }
172157
173158 /**
@@ -180,7 +165,7 @@ public void emitReturnTimeMetric(Long returnTime, String apiName, String statusC
180165 public void emitBytesSentMetric (int bytes , String apiName , String statusCode ) {
181166 System .out .println ("emit metric with http request size " + bytes + " byte, " + apiName );
182167 apiBytesSentCounter .add (
183- bytes , Labels .of (DIMENSION_API_NAME , apiName , DIMENSION_STATUS_CODE , statusCode ));
168+ bytes , Attributes .of (DIMENSION_API_NAME , apiName , DIMENSION_STATUS_CODE , statusCode ));
184169 }
185170
186171 /**
@@ -194,7 +179,8 @@ public void emitQueueSizeChangeMetric(int queueSizeChange, String apiName, Strin
194179 System .out .println (
195180 "emit metric with queue size change " + queueSizeChange + "," + apiName + "," + statusCode );
196181 queueSizeCounter .add (
197- queueSizeChange , Labels .of (DIMENSION_API_NAME , apiName , DIMENSION_STATUS_CODE , statusCode ));
182+ queueSizeChange ,
183+ Attributes .of (DIMENSION_API_NAME , apiName , DIMENSION_STATUS_CODE , statusCode ));
198184 }
199185
200186 /**
0 commit comments