Skip to content

Commit 01aa484

Browse files
author
Anuraag Agrawal
authored
Workaround SDK synchronous counters not reported correctly. (#124)
1 parent e7dc6a7 commit 01aa484

File tree

3 files changed

+75
-60
lines changed

3 files changed

+75
-60
lines changed

sample-apps/spark-awssdkv1/src/main/java/com/amazon/sampleapp/MetricEmitter.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.opentelemetry.api.metrics.DoubleHistogram;
66
import io.opentelemetry.api.metrics.GlobalMeterProvider;
77
import io.opentelemetry.api.metrics.LongCounter;
8-
import io.opentelemetry.api.metrics.LongUpDownCounter;
98
import io.opentelemetry.api.metrics.Meter;
109

1110
public class MetricEmitter {
@@ -20,10 +19,11 @@ public class MetricEmitter {
2019
static String API_UP_DOWN_COUNTER_METRIC = "queueSizeChange";
2120
static String API_UP_DOWN_SUM_METRIC = "actualQueueSize";
2221

23-
LongCounter apiBytesSentCounter;
2422
DoubleHistogram apiLatencyRecorder;
2523
LongCounter totalBytesSentObserver;
26-
LongUpDownCounter queueSizeCounter;
24+
25+
long apiBytesSent;
26+
long queueSizeChange;
2727

2828
long totalBytesSent;
2929
long apiLastLatency;
@@ -58,12 +58,16 @@ public MetricEmitter() {
5858
actualQueueSizeMetricName = API_UP_DOWN_SUM_METRIC + "_" + instanceId;
5959
}
6060

61-
apiBytesSentCounter =
62-
meter
63-
.counterBuilder(apiBytesSentMetricName)
64-
.setDescription("API request load sent in bytes")
65-
.setUnit("one")
66-
.build();
61+
meter
62+
.counterBuilder(apiBytesSentMetricName)
63+
.setDescription("API request load sent in bytes")
64+
.setUnit("one")
65+
.buildWithCallback(
66+
measurement ->
67+
measurement.observe(
68+
apiBytesSent,
69+
Attributes.of(
70+
DIMENSION_API_NAME, apiNameValue, DIMENSION_STATUS_CODE, statusCodeValue)));
6771

6872
apiLatencyRecorder =
6973
meter
@@ -72,12 +76,16 @@ public MetricEmitter() {
7276
.setUnit("ms")
7377
.build();
7478

75-
queueSizeCounter =
76-
meter
77-
.upDownCounterBuilder(queueSizeChangeMetricName)
78-
.setDescription("Queue Size change")
79-
.setUnit("one")
80-
.build();
79+
meter
80+
.upDownCounterBuilder(queueSizeChangeMetricName)
81+
.setDescription("Queue Size change")
82+
.setUnit("one")
83+
.buildWithCallback(
84+
measurement ->
85+
measurement.observe(
86+
queueSizeChange,
87+
Attributes.of(
88+
DIMENSION_API_NAME, apiNameValue, DIMENSION_STATUS_CODE, statusCodeValue)));
8189

8290
meter
8391
.gaugeBuilder(totalApiBytesSentMetricName)
@@ -138,8 +146,7 @@ public void emitReturnTimeMetric(Long returnTime, String apiName, String statusC
138146
* @param statusCode
139147
*/
140148
public void emitBytesSentMetric(int bytes, String apiName, String statusCode) {
141-
apiBytesSentCounter.add(
142-
bytes, Attributes.of(DIMENSION_API_NAME, apiName, DIMENSION_STATUS_CODE, statusCode));
149+
apiBytesSent += bytes;
143150
}
144151

145152
/**
@@ -150,9 +157,7 @@ public void emitBytesSentMetric(int bytes, String apiName, String statusCode) {
150157
* @param statusCode
151158
*/
152159
public void emitQueueSizeChangeMetric(int queueSizeChange, String apiName, String statusCode) {
153-
queueSizeCounter.add(
154-
queueSizeChange,
155-
Attributes.of(DIMENSION_API_NAME, apiName, DIMENSION_STATUS_CODE, statusCode));
160+
queueSizeChange += queueSizeChange;
156161
}
157162

158163
/**

sample-apps/spark/src/main/java/com/amazon/sampleapp/MetricEmitter.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.opentelemetry.api.metrics.DoubleHistogram;
66
import io.opentelemetry.api.metrics.GlobalMeterProvider;
77
import io.opentelemetry.api.metrics.LongCounter;
8-
import io.opentelemetry.api.metrics.LongUpDownCounter;
98
import io.opentelemetry.api.metrics.Meter;
109

1110
public class MetricEmitter {
@@ -20,10 +19,11 @@ public class MetricEmitter {
2019
static String API_UP_DOWN_COUNTER_METRIC = "queueSizeChange";
2120
static String API_UP_DOWN_SUM_METRIC = "actualQueueSize";
2221

23-
LongCounter apiBytesSentCounter;
2422
DoubleHistogram apiLatencyRecorder;
2523
LongCounter totalBytesSentObserver;
26-
LongUpDownCounter queueSizeCounter;
24+
25+
long apiBytesSent;
26+
long queueSizeChange;
2727

2828
long totalBytesSent;
2929
long apiLastLatency;
@@ -58,12 +58,16 @@ public MetricEmitter() {
5858
actualQueueSizeMetricName = API_UP_DOWN_SUM_METRIC + "_" + instanceId;
5959
}
6060

61-
apiBytesSentCounter =
62-
meter
63-
.counterBuilder(apiBytesSentMetricName)
64-
.setDescription("API request load sent in bytes")
65-
.setUnit("one")
66-
.build();
61+
meter
62+
.counterBuilder(apiBytesSentMetricName)
63+
.setDescription("API request load sent in bytes")
64+
.setUnit("one")
65+
.buildWithCallback(
66+
measurement ->
67+
measurement.observe(
68+
apiBytesSent,
69+
Attributes.of(
70+
DIMENSION_API_NAME, apiNameValue, DIMENSION_STATUS_CODE, statusCodeValue)));
6771

6872
apiLatencyRecorder =
6973
meter
@@ -72,12 +76,16 @@ public MetricEmitter() {
7276
.setUnit("ms")
7377
.build();
7478

75-
queueSizeCounter =
76-
meter
77-
.upDownCounterBuilder(queueSizeChangeMetricName)
78-
.setDescription("Queue Size change")
79-
.setUnit("one")
80-
.build();
79+
meter
80+
.upDownCounterBuilder(queueSizeChangeMetricName)
81+
.setDescription("Queue Size change")
82+
.setUnit("one")
83+
.buildWithCallback(
84+
measurement ->
85+
measurement.observe(
86+
queueSizeChange,
87+
Attributes.of(
88+
DIMENSION_API_NAME, apiNameValue, DIMENSION_STATUS_CODE, statusCodeValue)));
8189

8290
meter
8391
.gaugeBuilder(totalApiBytesSentMetricName)
@@ -138,8 +146,7 @@ public void emitReturnTimeMetric(Long returnTime, String apiName, String statusC
138146
* @param statusCode
139147
*/
140148
public void emitBytesSentMetric(int bytes, String apiName, String statusCode) {
141-
apiBytesSentCounter.add(
142-
bytes, Attributes.of(DIMENSION_API_NAME, apiName, DIMENSION_STATUS_CODE, statusCode));
149+
apiBytesSent += bytes;
143150
}
144151

145152
/**
@@ -150,9 +157,7 @@ public void emitBytesSentMetric(int bytes, String apiName, String statusCode) {
150157
* @param statusCode
151158
*/
152159
public void emitQueueSizeChangeMetric(int queueSizeChange, String apiName, String statusCode) {
153-
queueSizeCounter.add(
154-
queueSizeChange,
155-
Attributes.of(DIMENSION_API_NAME, apiName, DIMENSION_STATUS_CODE, statusCode));
160+
queueSizeChange += queueSizeChange;
156161
}
157162

158163
/**

sample-apps/springboot/src/main/java/com/amazon/sampleapp/MetricEmitter.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.opentelemetry.api.metrics.DoubleHistogram;
66
import io.opentelemetry.api.metrics.GlobalMeterProvider;
77
import io.opentelemetry.api.metrics.LongCounter;
8-
import io.opentelemetry.api.metrics.LongUpDownCounter;
98
import io.opentelemetry.api.metrics.Meter;
109

1110
public class MetricEmitter {
@@ -20,10 +19,11 @@ public class MetricEmitter {
2019
static String API_UP_DOWN_COUNTER_METRIC = "queueSizeChange";
2120
static String API_UP_DOWN_SUM_METRIC = "actualQueueSize";
2221

23-
LongCounter apiBytesSentCounter;
2422
DoubleHistogram apiLatencyRecorder;
2523
LongCounter totalBytesSentObserver;
26-
LongUpDownCounter queueSizeCounter;
24+
25+
long apiBytesSent;
26+
long queueSizeChange;
2727

2828
long totalBytesSent;
2929
long apiLastLatency;
@@ -58,12 +58,16 @@ public MetricEmitter() {
5858
actualQueueSizeMetricName = API_UP_DOWN_SUM_METRIC + "_" + instanceId;
5959
}
6060

61-
apiBytesSentCounter =
62-
meter
63-
.counterBuilder(apiBytesSentMetricName)
64-
.setDescription("API request load sent in bytes")
65-
.setUnit("one")
66-
.build();
61+
meter
62+
.counterBuilder(apiBytesSentMetricName)
63+
.setDescription("API request load sent in bytes")
64+
.setUnit("one")
65+
.buildWithCallback(
66+
measurement ->
67+
measurement.observe(
68+
apiBytesSent,
69+
Attributes.of(
70+
DIMENSION_API_NAME, apiNameValue, DIMENSION_STATUS_CODE, statusCodeValue)));
6771

6872
apiLatencyRecorder =
6973
meter
@@ -72,12 +76,16 @@ public MetricEmitter() {
7276
.setUnit("ms")
7377
.build();
7478

75-
queueSizeCounter =
76-
meter
77-
.upDownCounterBuilder(queueSizeChangeMetricName)
78-
.setDescription("Queue Size change")
79-
.setUnit("one")
80-
.build();
79+
meter
80+
.upDownCounterBuilder(queueSizeChangeMetricName)
81+
.setDescription("Queue Size change")
82+
.setUnit("one")
83+
.buildWithCallback(
84+
measurement ->
85+
measurement.observe(
86+
queueSizeChange,
87+
Attributes.of(
88+
DIMENSION_API_NAME, apiNameValue, DIMENSION_STATUS_CODE, statusCodeValue)));
8189

8290
meter
8391
.gaugeBuilder(totalApiBytesSentMetricName)
@@ -138,8 +146,7 @@ public void emitReturnTimeMetric(Long returnTime, String apiName, String statusC
138146
* @param statusCode
139147
*/
140148
public void emitBytesSentMetric(int bytes, String apiName, String statusCode) {
141-
apiBytesSentCounter.add(
142-
bytes, Attributes.of(DIMENSION_API_NAME, apiName, DIMENSION_STATUS_CODE, statusCode));
149+
apiBytesSent += bytes;
143150
}
144151

145152
/**
@@ -150,9 +157,7 @@ public void emitBytesSentMetric(int bytes, String apiName, String statusCode) {
150157
* @param statusCode
151158
*/
152159
public void emitQueueSizeChangeMetric(int queueSizeChange, String apiName, String statusCode) {
153-
queueSizeCounter.add(
154-
queueSizeChange,
155-
Attributes.of(DIMENSION_API_NAME, apiName, DIMENSION_STATUS_CODE, statusCode));
160+
queueSizeChange += queueSizeChange;
156161
}
157162

158163
/**

0 commit comments

Comments
 (0)