use of com.palantir.tritium.metrics.registry.MetricName in project metric-schema by palantir.
the class UtilityGenerator method generateSimpleMetricFactory.
private static List<MethodSpec> generateSimpleMetricFactory(String namespace, String metricName, Optional<String> libraryName, MetricDefinition definition, ImplementationVisibility visibility) {
boolean isGauge = MetricType.GAUGE.equals(definition.getType());
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Custodian.sanitizeName(metricName)).addModifiers(visibility.apply()).returns(MetricTypes.type(definition.getType())).addParameters(definition.getTagDefinitions().stream().filter(tag -> tag.getValues().size() != 1).map(tag -> ParameterSpec.builder(getTagClassName(metricName, tag), Custodian.sanitizeName(tag.getName())).addAnnotation(Safe.class).build()).collect(ImmutableList.toImmutableList())).addJavadoc(Javadoc.render(definition.getDocs()));
CodeBlock metricNameBlock = metricName(namespace, metricName, libraryName, definition);
MethodSpec metricNameMethod = MethodSpec.methodBuilder(Custodian.sanitizeName(metricName + "MetricName")).addModifiers(visibility.apply()).addModifiers(Modifier.STATIC).returns(MetricName.class).addCode("return $L;", metricNameBlock).build();
String metricRegistryMethod = MetricTypes.registryAccessor(definition.getType());
if (isGauge) {
methodBuilder.addParameter(ParameterizedTypeName.get(ClassName.get(Gauge.class), WildcardTypeName.subtypeOf(Object.class)), ReservedNames.GAUGE_NAME);
// TODO(ckozak): Update to use a method which can log a warning and replace existing gauges.
// See MetricRegistries.registerWithReplacement.
methodBuilder.addStatement("$L.$L($L(), $L)", ReservedNames.REGISTRY_NAME, metricRegistryMethod, metricNameMethod.name, ReservedNames.GAUGE_NAME);
} else {
methodBuilder.addAnnotation(CheckReturnValue.class);
methodBuilder.addStatement("return $L.$L($L)", ReservedNames.REGISTRY_NAME, metricRegistryMethod, metricNameBlock);
}
MethodSpec method = methodBuilder.build();
return isGauge ? ImmutableList.of(method, metricNameMethod) : ImmutableList.of(method);
}
use of com.palantir.tritium.metrics.registry.MetricName in project dialogue by palantir.
the class SimulationMetricsReporter method addSeries.
private void addSeries(Pattern metricNameRegex, Supplier<XYChart> chartSupplier) {
Map<MetricName, List<Double>> map = measurements.asMap();
List<MetricName> columns = map.keySet().stream().filter(metric -> !metric.equals(X_AXIS)).filter(metric -> metricNameRegex.asPredicate().test(asString(metric))).sorted(Comparator.comparing(SimulationMetricsReporter::asString)).collect(Collectors.toList());
for (MetricName column : columns) {
XYChart chart = chartSupplier.get();
// if we render too many samples, it just ends up looking like a wall of colour
int granularity = chart.getWidth() / 3;
double[] xAxis = reduceGranularity(granularity, map.get(X_AXIS).stream().mapToDouble(d -> d).toArray());
String[] nullToolTips = Collections.nCopies(xAxis.length, null).toArray(new String[] {});
double[] series = reduceGranularity(granularity, map.get(column).stream().mapToDouble(d -> d).toArray());
Preconditions.checkState(series.length == xAxis.length, "Series must all be same length", SafeArg.of("column", column), SafeArg.of("xaxis", xAxis.length), SafeArg.of("length", series.length));
chart.addSeries(asString(column) + ".count", xAxis, series).setToolTips(nullToolTips);
}
}
use of com.palantir.tritium.metrics.registry.MetricName in project dialogue by palantir.
the class BalancedScoreTracker method registerGauges.
private static void registerGauges(TaggedMetricRegistry taggedMetrics, String channelName, ImmutableList<ChannelScoreInfo> channels) {
if (channels.size() > 10) {
log.info("Not registering gauges as there are too many nodes {}", SafeArg.of("count", channels.size()));
return;
}
for (int hostIndex = 0; hostIndex < channels.size(); hostIndex++) {
MetricName metricName = DialogueBalancedMetrics.of(taggedMetrics).score().channelName(channelName).hostIndex(Integer.toString(hostIndex)).buildMetricName();
// Weak gauge ensures this object can be GCd. Itherwise the tagged metric registry could hold the last ref!
// Defensive averaging for the possibility that people create multiple channels with the same channelName.
DialogueInternalWeakReducingGauge.getOrCreate(taggedMetrics, metricName, c -> c.computeScoreSnapshot().getScore(), longStream -> {
long[] longs = longStream.toArray();
if (log.isInfoEnabled() && longs.length > 1 && LongStream.of(longs).distinct().count() > 1) {
log.info("Multiple ({}) objects contribute to the same gauge, taking the average " + "(beware this may be misleading) {} {}", SafeArg.of("count", longs.length), SafeArg.of("metricName", metricName), SafeArg.of("values", Arrays.toString(longs)));
}
return Arrays.stream(longs).average().orElse(0);
}, channels.get(hostIndex));
}
}
use of com.palantir.tritium.metrics.registry.MetricName in project atlasdb by palantir.
the class MetricsManager method registerMetric.
/**
* Add a new gauge metric of the given name.
*
* If the metric already exists, this will REPLACE it with a new metric.
* Consider using {@link MetricsManager#registerOrAddToMetric} instead.
*/
public void registerMetric(Class clazz, String metricName, Gauge gauge, Map<String, String> tag) {
MetricName metricToAdd = MetricName.builder().safeName(MetricRegistry.name(clazz, metricName)).safeTags(tag).build();
if (taggedMetricRegistry.getMetrics().containsKey(metricToAdd)) {
log.warn("Replacing the metric [ {} ]. This will happen if you are trying to re-register metrics " + "or have two tagged metrics with the same name across the application.", SafeArg.of("metricName", metricName));
taggedMetricRegistry.remove(metricToAdd);
}
taggedMetricRegistry.gauge(metricToAdd, gauge);
}
use of com.palantir.tritium.metrics.registry.MetricName in project atlasdb by palantir.
the class AtlasDbMetricsTest method instrumentTaggedAsyncFunction.
@Test
public void instrumentTaggedAsyncFunction() {
AsyncTestService asyncTestService = AtlasDbMetrics.instrumentWithTaggedMetrics(taggedMetrics, AsyncTestService.class, this.asyncTestService);
List<ListenableFuture<String>> futures = fireOffTenAsyncPings(asyncTestService);
Instant now = Instant.now();
MetricName metricName = MetricName.builder().safeName(ASYNC_PING_METRIC_NAME).build();
awaitMetricsToBeReported(futures, metricName);
assertThat(Instant.now()).as("in the event of scheduling issues, and despite having 10 concurrent futures, we complete " + "everything with 2*ttl").isBefore(now.plus(ASYNC_DURATION_TTL.plus(ASYNC_DURATION_TTL)));
assertAllAsyncPingMetricsAreAccuratelyRecorded(futures, metricName);
}
Aggregations