diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index a87b729400581..7079c2945a3b6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -7,6 +7,7 @@ ### Breaking Changes ### Bugs Fixed +- Fix a bug where post requests to Live Metrics returned http 400 due to malformed json. ### Other Changes diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/quickpulse/QuickPulseDataFetcher.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/quickpulse/QuickPulseDataFetcher.java index bbd50a601fc89..9022e1faf2f44 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/quickpulse/QuickPulseDataFetcher.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/quickpulse/QuickPulseDataFetcher.java @@ -6,6 +6,7 @@ import com.azure.core.http.HttpRequest; import com.azure.core.util.logging.ClientLogger; import com.azure.monitor.opentelemetry.exporter.implementation.quickpulse.model.QuickPulseEnvelope; +import com.azure.monitor.opentelemetry.exporter.implementation.quickpulse.model.QuickPulseMonitoringDataPoints; import com.azure.monitor.opentelemetry.exporter.implementation.quickpulse.model.QuickPulseMetrics; import com.azure.monitor.opentelemetry.exporter.implementation.utils.Strings; import org.slf4j.MDC; @@ -123,9 +124,9 @@ private String buildPostEntity(QuickPulseDataCollector.FinalCounters counters) t postEnvelope.setTimeStamp("/Date(" + System.currentTimeMillis() + ")/"); postEnvelope.setMetrics(addMetricsToQuickPulseEnvelope(counters)); envelopes.add(postEnvelope); - + QuickPulseMonitoringDataPoints points = new QuickPulseMonitoringDataPoints(envelopes); // By default '/' is not escaped in JSON, so we need to escape it manually as the backend requires it. - return postEnvelope.toJsonString().replace("/", "\\/"); + return points.toJsonString().replace("/", "\\/"); } private static List diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/quickpulse/model/QuickPulseMonitoringDataPoints.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/quickpulse/model/QuickPulseMonitoringDataPoints.java new file mode 100644 index 0000000000000..f22590bb5c9e5 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/quickpulse/model/QuickPulseMonitoringDataPoints.java @@ -0,0 +1,20 @@ +package com.azure.monitor.opentelemetry.exporter.implementation.quickpulse.model; + +import com.azure.json.JsonSerializable; +import com.azure.json.JsonWriter; + +import java.io.IOException; +import java.util.List; + +public class QuickPulseMonitoringDataPoints implements JsonSerializable { + private List monitoringDataPoints; + + public QuickPulseMonitoringDataPoints(List monitoringDataPoints) { + this.monitoringDataPoints = monitoringDataPoints; + } + + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + return jsonWriter.writeArray(monitoringDataPoints, JsonWriter::writeJson, false); + } +}