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