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