Search in sources :

Example 11 with MetricName

use of com.palantir.tritium.metrics.registry.MetricName in project tritium by palantir.

the class MetricRegistries method registerAll.

/**
 * Registers a Dropwizard {@link MetricSet} with a Tritium {@link TaggedMetricRegistry}. Semantics match calling
 * {@link MetricRegistry#register(String, Metric)} with a {@link MetricSet}.
 *
 * @param registry Target Tritium registry
 * @param prefix Metric name prefix
 * @param metricSet Set to register with the tagged registry
 */
public static void registerAll(TaggedMetricRegistry registry, @Safe String prefix, MetricSet metricSet) {
    Preconditions.checkNotNull(registry, "TaggedMetricRegistry is required");
    Preconditions.checkNotNull(prefix, "Prefix is required");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(prefix), "Prefix cannot be blank");
    Preconditions.checkNotNull(metricSet, "MetricSet is required");
    metricSet.getMetrics().forEach((name, metric) -> {
        String safeName = MetricRegistry.name(prefix, name);
        MetricName metricName = MetricName.builder().safeName(safeName).build();
        if (metric instanceof Gauge) {
            registry.registerWithReplacement(metricName, (Gauge<?>) metric);
        } else if (metric instanceof Counter) {
            registry.counter(metricName, () -> (Counter) metric);
        } else if (metric instanceof Histogram) {
            registry.histogram(metricName, () -> (Histogram) metric);
        } else if (metric instanceof Meter) {
            registry.meter(metricName, () -> (Meter) metric);
        } else if (metric instanceof Timer) {
            registry.timer(metricName, () -> (Timer) metric);
        } else if (metric instanceof MetricSet) {
            registerAll(registry, safeName, (MetricSet) metric);
        } else {
            throw new SafeIllegalArgumentException("Unknown Metric Type", SafeArg.of("type", metric.getClass()));
        }
    });
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName) Histogram(com.codahale.metrics.Histogram) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) Gauge(com.codahale.metrics.Gauge) MetricSet(com.codahale.metrics.MetricSet)

Example 12 with MetricName

use of com.palantir.tritium.metrics.registry.MetricName in project tritium by palantir.

the class TaggedMetricsServiceInvocationEventHandler method onFailure.

@Override
public final void onFailure(@Nullable InvocationContext context, @Nonnull Throwable cause) {
    globalFailureMeter.mark();
    debugIfNullContext(context);
    if (context != null) {
        MetricName failuresMetricName = MetricName.builder().safeName(serviceName + "-" + FAILURES_METRIC_NAME).putSafeTags("service-name", context.getMethod().getDeclaringClass().getSimpleName()).putSafeTags("endpoint", context.getMethod().getName()).putSafeTags("cause", cause.getClass().getName()).build();
        taggedMetricRegistry.meter(failuresMetricName).mark();
    }
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName)

Example 13 with MetricName

use of com.palantir.tritium.metrics.registry.MetricName in project tritium by palantir.

the class TritiumTest method testInstrumentWithTags.

@Test
public void testInstrumentWithTags() {
    assertThat(delegate.invocationCount()).isZero();
    assertThat(taggedMetricRegistry.getMetrics()).containsOnlyKeys(MetricName.builder().safeName("failures").build());
    taggedInstrumentedService.test();
    assertThat(delegate.invocationCount()).isOne();
    Map<MetricName, Metric> metrics = taggedMetricRegistry.getMetrics();
    assertThat(metrics.keySet()).containsExactly(EXPECTED_TAGGED_METRIC_NAME, MetricName.builder().safeName("failures").build());
    Metric actual = metrics.get(EXPECTED_TAGGED_METRIC_NAME);
    assertThat(actual).isInstanceOf(Timer.class);
    Timer timer = (Timer) actual;
    assertThat(timer.getCount()).isOne();
    taggedInstrumentedService.test();
    assertThat(metrics.get(EXPECTED_TAGGED_METRIC_NAME)).isSameAs(timer);
    assertThat(timer.getCount()).isEqualTo(2);
    assertThat(timer.getSnapshot().getMax()).isGreaterThan(-1);
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName) Timer(com.codahale.metrics.Timer) Metric(com.codahale.metrics.Metric) Test(org.junit.jupiter.api.Test)

Example 14 with MetricName

use of com.palantir.tritium.metrics.registry.MetricName in project tritium by palantir.

the class InstrumentationTest method testTaggedMetrics.

@Test
void testTaggedMetrics() {
    TestImplementation delegate = new TestImplementation();
    TestInterface runnable = Instrumentation.builder(TestInterface.class, delegate).withTaggedMetrics(taggedMetricRegistry, "testPrefix").withMetrics(metrics).build();
    assertThat(delegate.invocationCount()).isZero();
    runnable.test();
    assertThat(delegate.invocationCount()).isOne();
    Map<MetricName, Metric> taggedMetrics = taggedMetricRegistry.getMetrics();
    assertThat(taggedMetrics.keySet()).containsExactly(MetricName.builder().safeName("testPrefix").putSafeTags("service-name", "TestInterface").putSafeTags("endpoint", "test").build(), // The failures metric is created eagerly
    MetricName.builder().safeName("failures").build());
    assertThat(taggedMetrics.values()).first().isInstanceOf(Timer.class).extracting(m -> ((Timer) m).getCount()).isEqualTo(1L);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestImplementation(com.palantir.tritium.test.TestImplementation) LoggerFactory(org.slf4j.LoggerFactory) InstrumentationFilter(com.palantir.tritium.api.event.InstrumentationFilter) TracingInvocationEventHandler(com.palantir.tritium.tracing.TracingInvocationEventHandler) InvocationContext(com.palantir.tritium.event.InvocationContext) InstrumentationProperties(com.palantir.tritium.event.InstrumentationProperties) Tagged(com.palantir.tritium.Tagged) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) Method(java.lang.reflect.Method) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ImmutableSet(com.google.common.collect.ImmutableSet) Metric(com.codahale.metrics.Metric) LoggingInvocationEventHandler(com.palantir.tritium.event.log.LoggingInvocationEventHandler) Set(java.util.Set) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) TestInterface(com.palantir.tritium.test.TestInterface) MetricRegistries(com.palantir.tritium.metrics.MetricRegistries) MetricsInvocationEventHandler(com.palantir.tritium.event.metrics.MetricsInvocationEventHandler) Test(org.junit.jupiter.api.Test) InvocationEventHandler(com.palantir.tritium.event.InvocationEventHandler) Slf4jReporter(com.codahale.metrics.Slf4jReporter) Timer(com.codahale.metrics.Timer) TaggedMetricsServiceInvocationEventHandler(com.palantir.tritium.event.metrics.TaggedMetricsServiceInvocationEventHandler) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) SortedMap(java.util.SortedMap) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Proxy(java.lang.reflect.Proxy) MetricName(com.palantir.tritium.metrics.registry.MetricName) Stopwatch(com.google.common.base.Stopwatch) Mock(org.mockito.Mock) Supplier(java.util.function.Supplier) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Runnables(com.google.common.util.concurrent.Runnables) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) LoggingLevel(com.codahale.metrics.Slf4jReporter.LoggingLevel) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) TaggedMetricRegistry(com.palantir.tritium.metrics.registry.TaggedMetricRegistry) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) InstrumentationFilters(com.palantir.tritium.event.InstrumentationFilters) ConsoleReporter(com.codahale.metrics.ConsoleReporter) LessSpecificReturn(com.palantir.tritium.test.LessSpecificReturn) Collections(java.util.Collections) MetricName(com.palantir.tritium.metrics.registry.MetricName) Timer(com.codahale.metrics.Timer) TestImplementation(com.palantir.tritium.test.TestImplementation) TestInterface(com.palantir.tritium.test.TestInterface) Metric(com.codahale.metrics.Metric) Test(org.junit.jupiter.api.Test)

Example 15 with MetricName

use of com.palantir.tritium.metrics.registry.MetricName in project tritium by palantir.

the class CaffeineCacheStatsTest method after.

@AfterEach
// dumping metrics to standard out
@SuppressWarnings("SystemOut")
void after() {
    System.out.println("Metrics");
    try (ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build()) {
        reporter.report();
    }
    System.out.println("Tagged Metrics");
    Map<MetricName, Metric> metrics = taggedMetricRegistry.getMetrics();
    metrics.forEach((metricName, metric) -> System.out.printf("metric: %s = %s%n", metricName, getValue(metric)));
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName) ConsoleReporter(com.codahale.metrics.ConsoleReporter) Metric(com.codahale.metrics.Metric) AfterEach(org.junit.jupiter.api.AfterEach)

Aggregations

MetricName (com.palantir.tritium.metrics.registry.MetricName)37 TaggedMetricRegistry (com.palantir.tritium.metrics.registry.TaggedMetricRegistry)11 Test (org.junit.jupiter.api.Test)10 Metric (com.codahale.metrics.Metric)9 DefaultTaggedMetricRegistry (com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry)9 Timer (com.codahale.metrics.Timer)7 Counter (com.codahale.metrics.Counter)6 Histogram (com.codahale.metrics.Histogram)6 Meter (com.codahale.metrics.Meter)6 Test (org.junit.Test)6 Gauge (com.codahale.metrics.Gauge)5 Closeable (java.io.Closeable)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ConsoleReporter (com.codahale.metrics.ConsoleReporter)3 MetricSet (com.codahale.metrics.MetricSet)3 OkHttpClient (okhttp3.OkHttpClient)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 ExponentiallyDecayingReservoir (com.codahale.metrics.ExponentiallyDecayingReservoir)2