Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resettable DistributionSummary and Timer for Dynatrace registry #3093

Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ default boolean enrichWithDynatraceMetadata() {
return getBoolean(this, "enrichWithDynatraceMetadata").orElse(true);
}

/**
* Return whether to fall back to the built-in micrometer instruments for Timer and DistributionSummary.
*
* @return {@code true} if the resetting Dynatrace instruments should be used, and false if the registry should
* fall back to the built-in Micrometer instruments.
* @since 1.9.0
*/
default boolean useDynatraceSummaryInstruments() {
if (apiVersion() == V1) {
return false;
}
return getBoolean(this, "useDynatraceSummaryInstruments").orElse(true);
}

@Override
default Validated<?> validate() {
return checkAll(this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@
package io.micrometer.dynatrace;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.config.MeterFilterReply;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.micrometer.core.instrument.step.StepMeterRegistry;
import io.micrometer.core.instrument.util.NamedThreadFactory;
import io.micrometer.core.ipc.http.HttpSender;
import io.micrometer.core.ipc.http.HttpUrlConnectionSender;
import io.micrometer.core.util.internal.logging.InternalLogger;
import io.micrometer.core.util.internal.logging.InternalLoggerFactory;
import io.micrometer.dynatrace.types.DynatraceDistributionSummary;
import io.micrometer.dynatrace.types.DynatraceTimer;
import io.micrometer.dynatrace.v1.DynatraceExporterV1;
import io.micrometer.dynatrace.v2.DynatraceExporterV2;

import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
Expand All @@ -53,6 +59,8 @@ public class DynatraceMeterRegistry extends StepMeterRegistry {
private static final ThreadFactory DEFAULT_THREAD_FACTORY = new NamedThreadFactory("dynatrace-metrics-publisher");
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DynatraceMeterRegistry.class);

private final DynatraceApiVersion apiVersion;
private final boolean useDynatraceSummaryInstruments;
private final AbstractDynatraceExporter exporter;

@SuppressWarnings("deprecation")
Expand All @@ -63,9 +71,13 @@ public DynatraceMeterRegistry(DynatraceConfig config, Clock clock) {
private DynatraceMeterRegistry(DynatraceConfig config, Clock clock, ThreadFactory threadFactory, HttpSender httpClient) {
super(config, clock);

if (config.apiVersion() == DynatraceApiVersion.V2) {
apiVersion = config.apiVersion();
useDynatraceSummaryInstruments = config.useDynatraceSummaryInstruments();

if (apiVersion == DynatraceApiVersion.V2) {
logger.info("Exporting to Dynatrace metrics API v2");
this.exporter = new DynatraceExporterV2(config, clock, httpClient);
// Not used for Timer and DistributionSummary in V2 anymore, but still used for the other timer types.
registerMinPercentile();
} else {
logger.info("Exporting to Dynatrace metrics API v1");
Expand All @@ -89,6 +101,22 @@ protected TimeUnit getBaseTimeUnit() {
return this.exporter.getBaseTimeUnit();
}

@Override
protected DistributionSummary newDistributionSummary(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, double scale) {
if (apiVersion == DynatraceApiVersion.V2 && useDynatraceSummaryInstruments) {
return new DynatraceDistributionSummary(id, clock, distributionStatisticConfig, scale);
}
return super.newDistributionSummary(id, distributionStatisticConfig, scale);
}

@Override
protected Timer newTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector) {
if (apiVersion == DynatraceApiVersion.V2 && useDynatraceSummaryInstruments) {
return new DynatraceTimer(id, clock, distributionStatisticConfig, pauseDetector, exporter.getBaseTimeUnit());
}
return super.newTimer(id, distributionStatisticConfig, pauseDetector);
}

/**
* As the micrometer summary statistics (DistributionSummary, and a number of timer meter types)
* do not provide the minimum values that are required by Dynatrace to ingest summary metrics,
Expand All @@ -106,16 +134,17 @@ private void registerMinPercentile() {
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
double[] percentiles;

if (config.getPercentiles() == null) {
percentiles = new double[] {0};
double[] configPercentiles = config.getPercentiles();
if (configPercentiles == null) {
percentiles = new double[]{0};
metersWithArtificialZeroPercentile.add(id.getName() + ".percentile");
} else if (!containsZeroPercentile(config)) {
percentiles = new double[config.getPercentiles().length + 1];
System.arraycopy(config.getPercentiles(), 0, percentiles, 0, config.getPercentiles().length);
percentiles[config.getPercentiles().length] = 0; // theoretically this is already zero
percentiles = new double[configPercentiles.length + 1];
System.arraycopy(configPercentiles, 0, percentiles, 0, configPercentiles.length);
percentiles[configPercentiles.length] = 0; // theoretically this is already zero
metersWithArtificialZeroPercentile.add(id.getName() + ".percentile");
} else {
percentiles = config.getPercentiles();
percentiles = configPercentiles;
}

return DistributionStatisticConfig.builder()
Expand All @@ -133,7 +162,7 @@ public MeterFilterReply accept(Meter.Id id) {
}

private boolean containsZeroPercentile(DistributionStatisticConfig config) {
return Arrays.stream(config.getPercentiles()).anyMatch(percentile -> percentile == 0);
return Arrays.stream(Objects.requireNonNull(config.getPercentiles())).anyMatch(percentile -> percentile == 0);
}

private boolean hasArtificialZerothPercentile(Meter.Id id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2022 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.dynatrace.types;

import io.micrometer.core.instrument.AbstractDistributionSummary;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
* Resettable {@link DistributionSummary} implementation for Dynatrace exporters.
*
* @author Georg Pirklbauer
* @since 1.9.0
jonatan-ivanov marked this conversation as resolved.
Show resolved Hide resolved
*/
public final class DynatraceDistributionSummary extends AbstractDistributionSummary implements DynatraceSummarySnapshotSupport {
private final DynatraceSummary summary = new DynatraceSummary();
private static final Logger LOGGER = LoggerFactory.getLogger(DynatraceDistributionSummary.class.getName());

// Configuration that will set the Histogram in AbstractTimer to a NoopHistogram.
private static final DistributionStatisticConfig NOOP_HISTOGRAM_CONFIG =
DistributionStatisticConfig.builder().percentilesHistogram(false).percentiles().build();

public DynatraceDistributionSummary(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig, double scale) {
super(id, clock, NOOP_HISTOGRAM_CONFIG, scale, false);

if (distributionStatisticConfig != DistributionStatisticConfig.NONE) {
LOGGER.warn("Distribution statistic config is currently ignored.");
}
}

@Override
protected void recordNonNegative(double amount) {
summary.recordNonNegative(amount);
}

@Override
public long count() {
return summary.getCount();
}

@Override
public double totalAmount() {
return summary.getTotal();
}

@Override
public double max() {
return summary.getMax();
}

public double min() {
return summary.getMin();
}

@Override
public boolean hasValues() {
return count() > 0;
}

@Override
public DynatraceSummarySnapshot takeSummarySnapshot() {
return new DynatraceSummarySnapshot(min(), max(), totalAmount(), count());
}

@Override
public DynatraceSummarySnapshot takeSummarySnapshot(TimeUnit timeUnit) {
LOGGER.debug("Called takeSummarySnapshot with a TimeUnit on a DistributionSummary. Ignoring TimeUnit.");
return takeSummarySnapshot();
}

@Override
public DynatraceSummarySnapshot takeSummarySnapshotAndReset() {
DynatraceSummarySnapshot snapshot = takeSummarySnapshot();
summary.reset();
return snapshot;
}

@Override
public DynatraceSummarySnapshot takeSummarySnapshotAndReset(TimeUnit unit) {
LOGGER.debug("Called takeSummarySnapshot with a TimeUnit on a DistributionSummary. Ignoring TimeUnit.");
return takeSummarySnapshotAndReset();
}

@Override
public HistogramSnapshot takeSnapshot() {
LOGGER.warn("Called takeSnapshot on a Dynatrace Distribution Summary, no percentiles will be exported.");
DynatraceSummarySnapshot dtSnapshot = takeSummarySnapshot();
return HistogramSnapshot.empty(dtSnapshot.getCount(), dtSnapshot.getTotal(), dtSnapshot.getMax());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2022 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.dynatrace.types;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.DoubleAdder;
import java.util.concurrent.atomic.LongAdder;

/**
* Internal class for resettable summary statistics
*
* @author Georg Pirklbauer
* @since 1.9.0
*/
final class DynatraceSummary {
private final LongAdder count = new LongAdder();
private final DoubleAdder total = new DoubleAdder();
private final AtomicLong min = new AtomicLong(0);
private final AtomicLong max = new AtomicLong(0);

void recordNonNegative(double amount) {
if (amount < 0) {
return;
}

synchronized (this) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking if we can get into trouble because of this or using a ReadWriteLock and non concurrent types would be more performant. Since Atomic* and *Adder classes are lock free, I'm not sure.

long longBits = Double.doubleToLongBits(amount);

max.getAndUpdate(prev -> Math.max(prev, longBits));
// have to check if a value was already recorded before, otherwise min will always stay 0 (because the default is 0).
min.getAndUpdate(prev -> count.longValue() > 0 ? Math.min(prev, longBits) : longBits);

total.add(amount);
count.increment();
}
}


public long getCount() {
return count.longValue();
}

public double getTotal() {
return total.doubleValue();
}

public double getMin() {
return Double.longBitsToDouble(min.longValue());
}

public double getMax() {
return Double.longBitsToDouble(max.longValue());
}

void reset() {
synchronized (this) {
min.set(0);
max.set(0);
total.reset();
count.reset();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.dynatrace.types;

import javax.annotation.concurrent.Immutable;


/**
* Snapshot of a Dynatrace summary object.
*
* @author Georg Pirklbauer
* @since 1.9.0
*/
@Immutable
public final class DynatraceSummarySnapshot {
private final double min;
private final double max;
private final double total;
private final long count;

DynatraceSummarySnapshot(double min, double max, double total, long count) {
this.min = min;
this.max = max;
this.total = total;
this.count = count;
}

public double getMin() {
return min;
}

public double getMax() {
return max;
}

public double getTotal() {
return total;
}

public long getCount() {
return count;
}
}

Loading