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

com.newrelic.telemetry.micrometer.transform.DistributionSummaryTransformer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 New Relic Corporation. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.newrelic.telemetry.micrometer.transform;

import static java.util.stream.Collectors.toList;

import com.newrelic.telemetry.Attributes;
import com.newrelic.telemetry.metrics.Gauge;
import com.newrelic.telemetry.metrics.Metric;
import com.newrelic.telemetry.metrics.Summary;
import com.newrelic.telemetry.micrometer.util.TimeTracker;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

public class DistributionSummaryTransformer {

  private final AttributesMaker attributesMaker;
  private final TimeTracker timeTracker;

  public DistributionSummaryTransformer(TimeTracker timeTracker, AttributesMaker attributesMaker) {
    this.attributesMaker = attributesMaker;
    this.timeTracker = timeTracker;
  }

  public Collection transform(DistributionSummary distributionSummary) {
    long now = timeTracker.getCurrentTime();

    HistogramSnapshot snapshot = distributionSummary.takeSnapshot();
    Stream summaryMetrics = buildSummaryMetric(distributionSummary, now, snapshot);
    Stream percentileMetrics = buildPercentiles(distributionSummary, now, snapshot);

    return Stream.concat(summaryMetrics, percentileMetrics).collect(toList());
  }

  private Stream buildSummaryMetric(
      DistributionSummary distributionSummary, long now, HistogramSnapshot snapshot) {
    Attributes attributes =
        attributesMaker.make(distributionSummary.getId(), "distributionSummary");
    String name = distributionSummary.getId().getName();
    return Stream.of(
        new Summary(
            name + ".summary",
            (int) snapshot.count(),
            snapshot.total(),
            Double.NaN,
            snapshot.max(),
            timeTracker.getPreviousTime(),
            now,
            attributes));
  }

  // todo: is this redundant with the gauges being produced by the HistogramGaugeCustomizer? If so,
  // we should remove these.
  private Stream buildPercentiles(
      DistributionSummary distributionSummary, long now, HistogramSnapshot snapshot) {
    return Arrays.stream(snapshot.percentileValues())
        .map(
            valueAtPercentile ->
                new Gauge(
                    distributionSummary.getId().getName() + ".percentiles",
                    valueAtPercentile.value(),
                    now,
                    new Attributes().put("percentile", valueAtPercentile.percentile() * 100d)));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy