Skip to content

Commit b38dfee

Browse files
authored
fix empty histograms from micrometer being sent to the server (#3304)
* fix empty histograms from micrometer being sent to the server * update changelog
1 parent e0df4be commit b38dfee

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
3535
===== Bug fixes
3636
* Prevent bad serialization in edge cases for span compression - {pull}3293[#3293]
3737
* Allow overriding of transaction type for Servlet-API transactions - {pull}3226[#3226]
38-
* Fix micrometer histogram serialization - {pull}3290[#3290]
38+
* Fix micrometer histogram serialization - {pull}3290[#3290], {pull}3304[#3304]
3939
* Fix transactions not being correctly handled in certain edge cases - {pull}3294[#3294]
4040
4141
[float]

apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/MicrometerMeterRegistrySerializer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private static boolean serializeTimer(JsonWriter jw, HistogramSnapshot histogram
201201
serializeValue(id, ".count", count, jw, replaceBuilder, dedotMetricName);
202202
jw.writeByte(JsonWriter.COMMA);
203203
serializeValue(id, ".sum.us", totalTime, jw, replaceBuilder, dedotMetricName);
204-
if (histogramSnapshot != null) {
204+
if (histogramSnapshot != null && histogramSnapshot.histogramCounts().length > 0) {
205205
jw.writeByte(JsonWriter.COMMA);
206206
serializeHistogram(id, histogramSnapshot, jw, replaceBuilder, dedotMetricName);
207207
}
@@ -229,8 +229,10 @@ private static boolean serializeDistributionSummary(JsonWriter jw, HistogramSnap
229229
serializeValue(id, ".count", count, jw, replaceBuilder, dedotMetricName);
230230
jw.writeByte(JsonWriter.COMMA);
231231
serializeValue(id, ".sum", totalAmount, jw, replaceBuilder, dedotMetricName);
232-
jw.writeByte(JsonWriter.COMMA);
233-
serializeHistogram(id, histogramSnapshot, jw, replaceBuilder, dedotMetricName);
232+
if (histogramSnapshot != null && histogramSnapshot.histogramCounts().length > 0) {
233+
jw.writeByte(JsonWriter.COMMA);
234+
serializeHistogram(id, histogramSnapshot, jw, replaceBuilder, dedotMetricName);
235+
}
234236
return true;
235237
}
236238
return hasValue;

apm-agent-plugins/apm-micrometer-plugin/src/test/java/co/elastic/apm/agent/micrometer/MicrometerMeterRegistrySerializerTest.java

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ void serializeDistributionSummary() {
8080
serializeOneMeter(new TestSummary());
8181
}
8282

83+
@Test
84+
void serializeDistributionSummaryWithNoValues() {
85+
serializeOneMeter(new TestSummary(false));
86+
}
87+
8388
@Test
8489
void serializeGauge() {
8590
serializeOneMeter(new TestGauge());
@@ -201,9 +206,18 @@ protected static void checkPathHasValue(JsonNode jsonNode, String[] path, int va
201206
}
202207

203208
protected static JsonNode getPathNode(JsonNode jsonNode, String[] path) {
209+
return getPathNode(jsonNode, path, false);
210+
}
211+
protected static JsonNode getPathNode(JsonNode jsonNode, String[] path, boolean isNull) {
204212
assertThat(jsonNode).isNotNull();
205-
for (String element: path) {
206-
jsonNode = jsonNode.get(element);
213+
for (int i = 0; i < path.length-1; i++) {
214+
jsonNode = jsonNode.get(path[i]);
215+
assertThat(jsonNode).isNotNull();
216+
}
217+
jsonNode = jsonNode.get(path[path.length-1]);
218+
if (isNull) {
219+
assertThat(jsonNode).isNull();
220+
} else {
207221
assertThat(jsonNode).isNotNull();
208222
}
209223
return jsonNode;
@@ -261,26 +275,42 @@ public void checkSerialization(JsonNode jsonNode) {
261275
}
262276

263277
static class TestSummary extends TestMeter {
278+
private final boolean setSLOs;
264279
int sum = 0;
265280
int count = 0;
266281
int under5Count = 0;
267282
int from5To50Count = 0;
268283
int from50to95Count = 0;
269284
int[] values = new int[]{22, 55, 66, 98};
270285

286+
public TestSummary () {
287+
this(true);
288+
}
289+
public TestSummary (boolean setSLOs) {
290+
super();
291+
this.setSLOs = setSLOs;
292+
}
271293
@Override
272294
String meternameExtension() {
273295
return ".count";
274296
}
275297

276298
@Override
277299
public void addToMeterRegistry(MeterRegistry registry) {
278-
meter = DistributionSummary
279-
.builder(metername())
280-
.distributionStatisticBufferLength(20)
281-
.serviceLevelObjectives(5, 50, 95)
282-
.publishPercentileHistogram()
283-
.register(registry);
300+
if (setSLOs) {
301+
meter = DistributionSummary
302+
.builder(metername())
303+
.distributionStatisticBufferLength(20)
304+
.serviceLevelObjectives(5, 50, 95)
305+
.publishPercentileHistogram()
306+
.register(registry);
307+
} else {
308+
meter = DistributionSummary
309+
.builder(metername())
310+
.distributionStatisticBufferLength(20)
311+
.publishPercentileHistogram()
312+
.register(registry);
313+
}
284314
}
285315

286316
@Override
@@ -303,9 +333,20 @@ public void populateValues() {
303333
public void checkSerialization(JsonNode jsonNode) {
304334
checkPathHasValue(jsonNode, path(), count);
305335
checkPathHasValue(jsonNode, path(".sum"), sum);
306-
String[] path = path(".histogram");
307-
path[3] = "values";
308-
JsonNode histoNode1 = getPathNode(jsonNode, path);
336+
String[] temppath = path(".histogram");
337+
String[] path;
338+
if (!setSLOs) {
339+
path = new String[temppath.length-1];
340+
System.arraycopy(temppath, 0, path, 0, path.length);
341+
} else {
342+
path = temppath;
343+
path[3] = "values";
344+
}
345+
JsonNode histoNode1 = getPathNode(jsonNode, path, !setSLOs);
346+
if(!setSLOs) {
347+
assertThat(histoNode1).isNull();
348+
return;
349+
}
309350
assertThat(histoNode1.isArray()).isTrue();
310351
assertThat(histoNode1.size()).isEqualTo(3); //the 3 bucket boundaries of the SLOs 5,50,95
311352
assertThat(histoNode1.get(0).asDouble()).isEqualTo(5.0);

0 commit comments

Comments
 (0)