Search in sources :

Example 26 with MetricName

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

the class MetricsManager method registerOrGetTaggedCounter.

public Counter registerOrGetTaggedCounter(Class<?> clazz, String metricName, Map<String, String> tags) {
    MetricName name = getTaggedMetricName(clazz, metricName, tags);
    Counter counter = taggedMetricRegistry.counter(name);
    registerTaggedMetricName(name);
    return counter;
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName) Counter(com.codahale.metrics.Counter)

Example 27 with MetricName

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

the class DisjointUnionTaggedMetricSetTest method forEachWorksOnMetricsFromBothRegistries.

@Test
public void forEachWorksOnMetricsFromBothRegistries() {
    Timer timer = registry1.timer(METRIC_NAME_1);
    Histogram histogram = registry2.histogram(METRIC_NAME_2);
    Meter meter = registry1.meter(METRIC_NAME_3);
    Map<MetricName, Metric> stateOfTheWorld = new HashMap<>();
    disjointUnionTaggedMetricSet.forEachMetric(stateOfTheWorld::put);
    assertThat(stateOfTheWorld).containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(METRIC_NAME_1, timer, METRIC_NAME_2, histogram, METRIC_NAME_3, meter));
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName) Histogram(com.codahale.metrics.Histogram) Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) HashMap(java.util.HashMap) Metric(com.codahale.metrics.Metric) Test(org.junit.Test)

Example 28 with MetricName

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

the class UtilityGenerator method generateMetricFactoryBuilder.

/**
 * Produce a private staged builder, which implements public interfaces.
 */
private static void generateMetricFactoryBuilder(String namespaceName, String metricName, Optional<String> libraryName, MetricDefinition definition, TypeSpec.Builder outerBuilder, ImplementationVisibility visibility) {
    boolean isGauge = MetricType.GAUGE.equals(definition.getType());
    MethodSpec.Builder abstractBuildMethodBuilder = MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(MetricTypes.type(definition.getType()));
    if (isGauge) {
        abstractBuildMethodBuilder.addParameter(ParameterizedTypeName.get(ClassName.get(Gauge.class), WildcardTypeName.subtypeOf(Object.class)), ReservedNames.GAUGE_NAME);
    } else {
        abstractBuildMethodBuilder.addAnnotation(CheckReturnValue.class);
    }
    MethodSpec abstractBuildMethod = abstractBuildMethodBuilder.build();
    MethodSpec abstractBuildMetricName = MethodSpec.methodBuilder("buildMetricName").addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(MetricName.class).build();
    List<MethodSpec> abstractBuildMethods = isGauge ? ImmutableList.of(abstractBuildMethod, abstractBuildMetricName) : ImmutableList.of(abstractBuildMethod);
    outerBuilder.addType(TypeSpec.interfaceBuilder(buildStage(metricName)).addModifiers(visibility.apply()).addMethods(abstractBuildMethods).build());
    ImmutableList<TagDefinition> tagList = definition.getTagDefinitions().stream().filter(tagDefinition -> tagDefinition.getValues().size() != 1).collect(ImmutableList.toImmutableList());
    for (int i = 0; i < tagList.size(); i++) {
        boolean lastTag = i == tagList.size() - 1;
        TagDefinition tag = tagList.get(i);
        String tagName = tag.getName();
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Custodian.sanitizeName(tagName));
        tag.getDocs().ifPresent(docs -> methodBuilder.addJavadoc(Javadoc.render(docs)));
        outerBuilder.addType(TypeSpec.interfaceBuilder(stageName(metricName, tagName)).addModifiers(visibility.apply()).addMethod(methodBuilder.addParameter(ParameterSpec.builder(getTagClassName(metricName, tag), Custodian.sanitizeName(tagName)).addAnnotation(Safe.class).build()).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).addAnnotation(CheckReturnValue.class).returns(ClassName.bestGuess(lastTag ? buildStage(metricName) : stageName(metricName, tagList.get(i + 1).getName()))).build()).build());
    }
    CodeBlock metricNameBlock = metricName(namespaceName, metricName, libraryName, definition);
    String metricRegistryMethod = MetricTypes.registryAccessor(definition.getType());
    MethodSpec buildMetricName = MethodSpec.methodBuilder("buildMetricName").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(MetricName.class).addStatement("return $L", metricNameBlock).build();
    MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(MetricTypes.type(definition.getType()));
    if (isGauge) {
        buildMethodBuilder.addParameter(ParameterizedTypeName.get(ClassName.get(Gauge.class), WildcardTypeName.subtypeOf(Object.class)), ReservedNames.GAUGE_NAME).addStatement("$L.$L($L(), $L)", ReservedNames.REGISTRY_NAME, metricRegistryMethod, buildMetricName.name, ReservedNames.GAUGE_NAME);
    } else {
        buildMethodBuilder.addStatement("return $L.$L($L)", ReservedNames.REGISTRY_NAME, metricRegistryMethod, metricNameBlock);
    }
    MethodSpec buildMethod = buildMethodBuilder.build();
    List<MethodSpec> buildMethods = isGauge ? ImmutableList.of(buildMethod, buildMetricName) : ImmutableList.of(buildMethod);
    outerBuilder.addType(TypeSpec.classBuilder(Custodian.anyToUpperCamel(metricName) + "Builder").addModifiers(Modifier.PRIVATE, Modifier.FINAL).addSuperinterfaces(tagList.stream().map(tag -> ClassName.bestGuess(stageName(metricName, tag.getName()))).collect(ImmutableList.toImmutableList())).addSuperinterface(ClassName.bestGuess(buildStage(metricName))).addFields(tagList.stream().map(tag -> FieldSpec.builder(getTagClassName(metricName, tag), Custodian.sanitizeName(tag.getName()), Modifier.PRIVATE).build()).collect(ImmutableList.toImmutableList())).addMethods(buildMethods).addMethods(tagList.stream().map(tag -> MethodSpec.methodBuilder(Custodian.sanitizeName(tag.getName())).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(ClassName.bestGuess(Custodian.anyToUpperCamel(metricName) + "Builder")).addParameter(ParameterSpec.builder(getTagClassName(metricName, tag), Custodian.sanitizeName(tag.getName())).addAnnotation(Safe.class).build()).addStatement("$1T.checkState(this.$2L == null, $3S)", Preconditions.class, Custodian.sanitizeName(tag.getName()), tag.getName() + " is already set").addStatement("this.$1L = $2T.checkNotNull($1L, $3S)", Custodian.sanitizeName(tag.getName()), Preconditions.class, tag.getName() + " is required").addStatement("return this").build()).collect(ImmutableList.toImmutableList())).build());
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Custodian.sanitizeName(metricName)).addModifiers(visibility.apply()).returns(ClassName.bestGuess(stageName(metricName, tagList.get(0).getName()))).addAnnotation(CheckReturnValue.class).addStatement("return new $T()", ClassName.bestGuess(Custodian.anyToUpperCamel(metricName) + "Builder")).addJavadoc(Javadoc.render(definition.getDocs()));
    outerBuilder.addMethod(methodBuilder.build());
}
Also used : Iterables(com.google.common.collect.Iterables) ParameterSpec(com.squareup.javapoet.ParameterSpec) MetricName(com.palantir.tritium.metrics.registry.MetricName) Modifier(javax.lang.model.element.Modifier) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue) MethodSpec(com.squareup.javapoet.MethodSpec) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) TaggedMetricRegistry(com.palantir.tritium.metrics.registry.TaggedMetricRegistry) JavaFile(com.squareup.javapoet.JavaFile) Objects(java.util.Objects) List(java.util.List) Safe(com.palantir.logsafe.Safe) ImmutableList(com.google.common.collect.ImmutableList) Optional(java.util.Optional) Gauge(com.codahale.metrics.Gauge) Preconditions(com.palantir.logsafe.Preconditions) CodeBlock(com.squareup.javapoet.CodeBlock) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) Safe(com.palantir.logsafe.Safe) Gauge(com.codahale.metrics.Gauge) MetricName(com.palantir.tritium.metrics.registry.MetricName) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue) Preconditions(com.palantir.logsafe.Preconditions)

Example 29 with MetricName

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

the class TaggedMetricsServiceInvocationEventHandlerTest method testTaggedServiceMetricsCapturedAsErrors.

@ParameterizedTest
@MethodSource(TestTaggedMetricRegistries.REGISTRIES)
void testTaggedServiceMetricsCapturedAsErrors(TaggedMetricRegistry registry) throws Exception {
    TestImplementation testInterface = new TestImplementation();
    TaggedMetricsServiceInvocationEventHandler handler = new TaggedMetricsServiceInvocationEventHandler(registry, "quux");
    invokeMethod(handler, testInterface, "doFoo", "bar", /* success= */
    false);
    Map<MetricName, Metric> metrics = registry.getMetrics();
    MetricName expectedMetricName = MetricName.builder().safeName("quux-failures").putSafeTags("service-name", "TestImplementation").putSafeTags("endpoint", "doFoo").putSafeTags("cause", SafeRuntimeException.class.getName()).build();
    assertThat(metrics).containsKey(expectedMetricName);
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName) Metric(com.codahale.metrics.Metric) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 30 with MetricName

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

the class TaggedMetricsServiceInvocationEventHandlerTest method testTaggedServiceMetricsCaptured.

@ParameterizedTest
@MethodSource(TestTaggedMetricRegistries.REGISTRIES)
void testTaggedServiceMetricsCaptured(TaggedMetricRegistry registry) throws Exception {
    TestImplementation testInterface = new TestImplementation();
    TaggedMetricsServiceInvocationEventHandler handler = new TaggedMetricsServiceInvocationEventHandler(registry, "quux");
    invokeMethod(handler, testInterface, "doFoo", "bar", /* success= */
    true);
    Map<MetricName, Metric> metrics = registry.getMetrics();
    MetricName expectedMetricName = MetricName.builder().safeName("quux").putSafeTags("service-name", "TestImplementation").putSafeTags("endpoint", "doFoo").build();
    assertThat(metrics).containsKey(expectedMetricName);
}
Also used : MetricName(com.palantir.tritium.metrics.registry.MetricName) Metric(com.codahale.metrics.Metric) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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