Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.mweirauch.micrometer.jvm.extras.ProcessThreadMetrics;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.ImmutableTag;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -339,6 +340,40 @@ public static <T extends Number> T registerGauge(String name, Map<String, String
return number;
}

/**
* Registers a DistributionSummary (with predefined percentiles computed locally) for the given
* name with the service's metric registry and reports it periodically to the configured
* reporters. Apart from the provided tags, the reporting service's default tags also will be
* reported with the metrics.
* <p>
* See https://micrometer.io/docs/concepts#_distribution_summaries for more details.
*/
public static DistributionSummary registerDistributionSummary(String name,
Map<String, String> tags) {
return registerDistributionSummary(name, tags, false);
}

/**
* Registers a DistributionSummary for the given name with the service's metric registry and
* reports it periodically to the configured reporters Apart from the provided tags, the reporting
* service's default tags also will be reported with the metrics.
* <p>
* Param histogram – Determines whether percentile histograms should be published.
* <p>
* For more details - https://micrometer.io/docs/concepts#_distribution_summaries,
* https://micrometer.io/docs/concepts#_histograms_and_percentiles
*/
public static DistributionSummary registerDistributionSummary(String name,
Map<String, String> tags, boolean histogram) {
DistributionSummary.Builder builder = DistributionSummary.builder(name)
.publishPercentiles(0.5, 0.95, 0.99)
.tags(addDefaultTags(tags));
if (histogram) {
builder = builder.publishPercentileHistogram();
}
return builder.register(METER_REGISTRY);
}

/**
* Registers metrics for the given executor service with the service's metric registry and
* reports them periodically to the configured reporters. Apart from the given tags, the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.hypertrace.core.serviceframework.metrics;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -117,6 +122,40 @@ public void testGauge() {
assertEquals(11, gauge.get());
}

@Test
public void testDistributionSummary() {
initializeCustomRegistry(List.of("testing"));

DistributionSummary distribution = PlatformMetricsRegistry
.registerDistributionSummary("my.distribution", Map.of("foo", "bar"));
distribution.record(100);
assertEquals(1, distribution.count());
assertEquals(100, distribution.totalAmount());

// Try to register the same summary again and we should get the same instance.
distribution = PlatformMetricsRegistry
.registerDistributionSummary("my.distribution", Map.of("foo", "bar"));
distribution.record(50);
assertEquals(2, distribution.count());
assertEquals(150, distribution.totalAmount());
assertEquals(75, distribution.mean());
assertTrue(
Arrays.stream(distribution.takeSnapshot().percentileValues()).map(m -> m.percentile())
.collect(
Collectors.toList()).containsAll(List.of(0.5, 0.95, 0.99)));

// Create a new distribution with histogram enabled
distribution = PlatformMetricsRegistry
.registerDistributionSummary("my.distribution", new HashMap<>(), true);
distribution.record(100);
assertEquals(1, distribution.count());
assertEquals(100, distribution.totalAmount());
assertTrue(
Arrays.stream(distribution.takeSnapshot().percentileValues()).map(m -> m.percentile())
.collect(
Collectors.toList()).containsAll(List.of(0.5, 0.95, 0.99)));
}

@Test
public void test_initializePrometheusPushGateway_withNullUrlAddress_throwsException() {
Assertions.assertThrows(IllegalArgumentException.class,
Expand Down