1
1
package com .amazon .sampleapp ;
2
2
3
+ import io .opentelemetry .api .common .AttributeKey ;
4
+ import io .opentelemetry .api .common .Attributes ;
5
+ import io .opentelemetry .api .metrics .DoubleHistogram ;
3
6
import io .opentelemetry .api .metrics .GlobalMeterProvider ;
4
7
import io .opentelemetry .api .metrics .LongCounter ;
5
- import io .opentelemetry .api .metrics .LongSumObserver ;
6
8
import 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 ;
10
9
import io .opentelemetry .api .metrics .Meter ;
11
- import io .opentelemetry .api .metrics .common .Labels ;
12
10
13
11
public class MetricEmitter {
14
12
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" ) ;
17
15
18
16
static String API_COUNTER_METRIC = "apiBytesSent" ;
19
17
static String API_LATENCY_METRIC = "latency" ;
@@ -23,11 +21,9 @@ public class MetricEmitter {
23
21
static String API_UP_DOWN_SUM_METRIC = "actualQueueSize" ;
24
22
25
23
LongCounter apiBytesSentCounter ;
26
- LongValueRecorder apiLatencyRecorder ;
27
- LongSumObserver totalBytesSentObserver ;
28
- LongValueObserver apiLastLatencyObserver ;
24
+ DoubleHistogram apiLatencyRecorder ;
25
+ LongCounter totalBytesSentObserver ;
29
26
LongUpDownCounter queueSizeCounter ;
30
- LongUpDownSumObserver actualQueueSizeObserver ;
31
27
32
28
long totalBytesSent ;
33
29
long apiLastLatency ;
@@ -39,7 +35,8 @@ public class MetricEmitter {
39
35
String statusCodeValue = "" ;
40
36
41
37
public MetricEmitter () {
42
- Meter meter = GlobalMeterProvider .getMeter ("aws-otel" , "1.0" );
38
+ Meter meter =
39
+ GlobalMeterProvider .get ().meterBuilder ("aws-otel" ).setInstrumentationVersion ("1.0" ).build ();
43
40
44
41
// give a instanceId appending to the metricname so that we can check the metric for each round
45
42
// of integ-test
@@ -65,95 +62,83 @@ public MetricEmitter() {
65
62
66
63
apiBytesSentCounter =
67
64
meter
68
- .longCounterBuilder (apiBytesSentMetricName )
65
+ .counterBuilder (apiBytesSentMetricName )
69
66
.setDescription ("API request load sent in bytes" )
70
67
.setUnit ("one" )
71
68
.build ();
72
69
73
70
apiLatencyRecorder =
74
71
meter
75
- .longValueRecorderBuilder (latencyMetricName )
72
+ .histogramBuilder (latencyMetricName )
76
73
.setDescription ("API latency time" )
77
74
.setUnit ("ms" )
78
75
.build ();
79
76
80
77
queueSizeCounter =
81
78
meter
82
- .longUpDownCounterBuilder (queueSizeChangeMetricName )
79
+ .upDownCounterBuilder (queueSizeChangeMetricName )
83
80
.setDescription ("Queue Size change" )
84
81
.setUnit ("one" )
85
82
.build ();
86
83
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
+ });
157
142
}
158
143
159
144
/**
@@ -167,7 +152,7 @@ public void emitReturnTimeMetric(Long returnTime, String apiName, String statusC
167
152
System .out .println (
168
153
"emit metric with return time " + returnTime + "," + apiName + "," + statusCode );
169
154
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 ));
171
156
}
172
157
173
158
/**
@@ -180,7 +165,7 @@ public void emitReturnTimeMetric(Long returnTime, String apiName, String statusC
180
165
public void emitBytesSentMetric (int bytes , String apiName , String statusCode ) {
181
166
System .out .println ("emit metric with http request size " + bytes + " byte, " + apiName );
182
167
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 ));
184
169
}
185
170
186
171
/**
@@ -194,7 +179,8 @@ public void emitQueueSizeChangeMetric(int queueSizeChange, String apiName, Strin
194
179
System .out .println (
195
180
"emit metric with queue size change " + queueSizeChange + "," + apiName + "," + statusCode );
196
181
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 ));
198
184
}
199
185
200
186
/**
0 commit comments