com.newrelic.telemetry.micrometer.json.AttributesWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micrometer-registry-new-relic Show documentation
Show all versions of micrometer-registry-new-relic Show documentation
Micrometer registry implementation that sends data to New Relic as dimensional metrics
The newest version!
/*
* ---------------------------------------------------------------------------------------------
* Copyright (c) 2019 New Relic Corporation. All rights reserved.
* Licensed under the Apache 2.0 License. See LICENSE in the project root directory for license information.
* --------------------------------------------------------------------------------------------
*/
package com.newrelic.telemetry.micrometer.json;
import static com.newrelic.telemetry.micrometer.json.JsonUtil.object;
import static com.newrelic.telemetry.micrometer.json.JsonUtil.unknownType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class AttributesWriter {
public static void writeAttributesObject(StringBuilder buff, Map attributes) {
object(buff, () -> writeAttributesInner(buff, attributes), false);
}
public static void writeAttributes(StringBuilder buff, Map attributes) {
if (attributes.isEmpty()) {
return;
}
buff.append(",");
object(buff, "attributes", () -> writeAttributesInner(buff, attributes), false);
}
private static void writeAttributesInner(StringBuilder buff, Map attributes) {
List> entries = new ArrayList<>(attributes.entrySet());
entries
.subList(0, entries.size() - 1)
.forEach(
entry -> {
unknownType(buff, entry.getKey(), entry.getValue());
});
Entry last = entries.get(attributes.size() - 1);
unknownType(buff, last.getKey(), last.getValue(), false);
}
}