All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.newrelic.telemetry.micrometer.json.AttributesWriter Maven / Gradle / Ivy

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);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy