io.micrometer.core.instrument.ImmutableTag Java Examples

The following examples show how to use io.micrometer.core.instrument.ImmutableTag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MicrometerMetricsReporterTest.java    From java-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithTagAndBaggageLabels() {
    String metricName = "testWithTagAndBaggageLabels";

    // prepare
    SpanData spanData = defaultMockSpanData();
    MicrometerMetricsReporter reporter = MicrometerMetricsReporter.newMetricsReporter()
            .withName(metricName)
            .withBaggageLabel(BAGGAGE_LABEL_NAME, BAGGAGE_LABEL_VALUE)
            .withTagLabel(TAG_LABEL_NAME, TAG_LABEL_VALUE)
            .withConstLabel("span.kind", Tags.SPAN_KIND_CLIENT)
            .build();

    // test
    reporter.reportSpan(spanData);

    // verify
    List<Tag> tags = defaultTags();
    tags.add(new ImmutableTag(BAGGAGE_LABEL_NAME, BAGGAGE_LABEL_VALUE));
    tags.add(new ImmutableTag(TAG_LABEL_NAME, TAG_LABEL_VALUE));

    assertEquals(100, (long) registry.find(metricName).timer().totalTime(TimeUnit.MILLISECONDS));
    assertEquals(1, Metrics.timer(metricName, tags).count());
}
 
Example #2
Source File: MicrometerMetricsReporterTest.java    From java-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCustomLabel() {
    String metricName = "testWithCustomLabel";

    // prepare
    SpanData spanData = defaultMockSpanData();
    MetricLabel metricLabel = new BaggageMetricLabel(METRIC_LABEL_NAME, METRIC_LABEL_VALUE);
    MicrometerMetricsReporter reporter = MicrometerMetricsReporter.newMetricsReporter()
            .withName(metricName)
            .withCustomLabel(metricLabel)
            .withConstLabel("span.kind", Tags.SPAN_KIND_CLIENT)
            .build();

    // test
    reporter.reportSpan(spanData);

    // verify
    List<Tag> tags = defaultTags();
    tags.add(new ImmutableTag(METRIC_LABEL_NAME, METRIC_LABEL_VALUE));

    assertEquals(100, (long) registry.find(metricName).timer().totalTime(TimeUnit.MILLISECONDS));
    assertEquals(1, Metrics.timer(metricName, tags).count());
}
 
Example #3
Source File: MetricsListener.java    From spring-boot-starter-batch-web with Apache License 2.0 6 votes vote down vote up
@Override
public void afterJob(JobExecution jobExecution) {
	long jobDuration = jobExecution.getEndTime().getTime() - jobExecution.getStartTime().getTime();
	meterRegistry.gauge(METRIC_NAME, Arrays.asList(//
			new ImmutableTag("context", jobExecution.getJobInstance().getJobName()), //
			new ImmutableTag("name", "duration")//
	), jobDuration);
	// What the f*** is that Thread.sleep doing here? ;-)
	// Metrics are written asynchronously to Spring Boot's repository. In our tests we experienced
	// that sometimes batch execution was so fast that this listener couldn't export the metrics
	// because they hadn't been written. It all happened in the same millisecond. So we added
	// a Thread.sleep of 100 milliseconds which gives us enough safety and doesn't hurt anyone.
	try {
		Thread.sleep(100);
	} catch (InterruptedException e) {
		throw new RuntimeException(e);
	}
	// Export Metrics to Console
	Search search = meterRegistry.find(METRIC_NAME);
	LOGGER.info(metricsOutputFormatter.format(search.gauges(), search.timers()));
}
 
Example #4
Source File: MicrometerMetricsReporterTest.java    From java-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testOverriddenTagAndBaggageLabels() {
    String metricName = "testOverriddenTagAndBaggageLabels";

    // prepare
    SpanData spanData = defaultMockSpanData();
    when(spanData.getBaggageItem(BAGGAGE_LABEL_NAME)).thenReturn("NewBaggageValue");
    when(spanData.getTags()).thenReturn(Collections.singletonMap(TAG_LABEL_NAME, "NewTagValue"));

    MicrometerMetricsReporter reporter = MicrometerMetricsReporter.newMetricsReporter()
            .withName(metricName)
            .withBaggageLabel(BAGGAGE_LABEL_NAME, BAGGAGE_LABEL_VALUE)
            .withTagLabel(TAG_LABEL_NAME, TAG_LABEL_VALUE)
            .withConstLabel("span.kind", Tags.SPAN_KIND_CLIENT)
            .build();

    // test
    reporter.reportSpan(spanData);

    // verify
    List<Tag> tags = defaultTags();
    tags.add(new ImmutableTag(BAGGAGE_LABEL_NAME, "NewBaggageValue"));
    tags.add(new ImmutableTag(TAG_LABEL_NAME, "NewTagValue"));

    assertEquals(100, (long) registry.find(metricName).timer().totalTime(TimeUnit.MILLISECONDS));
    assertEquals(1, Metrics.timer(metricName, tags).count());
}
 
Example #5
Source File: MicrometerMetricsReporterTest.java    From java-metrics with Apache License 2.0 5 votes vote down vote up
private List<Tag> defaultTags() {
    List<Tag> tags = new ArrayList<>();
    tags.add(new ImmutableTag("error", "false"));
    tags.add(new ImmutableTag("operation", "testop"));
    tags.add(new ImmutableTag("span.kind", Tags.SPAN_KIND_CLIENT));
    return tags;
}
 
Example #6
Source File: AbstractBatchMetricsAspect.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
protected Object profileMethod(ProceedingJoinPoint pjp) throws Throwable {
	Timer.Sample sample = Timer.start(meterRegistry);
	try {
		return pjp.proceed();
	} finally {
		sample.stop(meterRegistry.timer(MetricsListener.METRIC_NAME, Arrays.asList(//
				new ImmutableTag("context", getStepIdentifier()), //
				new ImmutableTag("method", ClassUtils.getShortName(pjp.getTarget().getClass()) + "."
						+ pjp.getSignature().getName()))));
	}
}
 
Example #7
Source File: MetricsListener.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
	// Calculate step execution time
	// Why is stepExecution.getEndTime().getTime() not available here? (see AbstractStep)
	long stepDuration = System.currentTimeMillis() - stepExecution.getStartTime().getTime();
	meterRegistry.gauge(METRIC_NAME, Arrays.asList(//
			new ImmutableTag("context", getStepExecutionIdentifier(stepExecution)), //
			new ImmutableTag("name", "duration")//
	), stepDuration);
	long itemCount = stepExecution.getWriteCount() + stepExecution.getSkipCount();
	meterRegistry.gauge(METRIC_NAME, Arrays.asList(//
			new ImmutableTag("context", getStepExecutionIdentifier(stepExecution)), //
			new ImmutableTag("name", "item.count")//
	), itemCount);
	// Calculate execution time per item
	long durationPerItem = 0;
	if (itemCount > 0) {
		durationPerItem = stepDuration / itemCount;
	}
	meterRegistry.gauge(METRIC_NAME, Arrays.asList(//
			new ImmutableTag("context", getStepExecutionIdentifier(stepExecution)), //
			new ImmutableTag("name", "item.duration")//
	), durationPerItem);
	// Export metrics from StepExecution to MetricRepositories
	Set<Entry<String, Object>> metrics = stepExecution.getExecutionContext().entrySet();
	for (Entry<String, Object> metric : metrics) {
		if (metric.getValue() instanceof Number) {
			meterRegistry.gauge(METRIC_NAME, Arrays.asList(//
					new ImmutableTag("context", getStepExecutionIdentifier(stepExecution)), //
					new ImmutableTag("name", metric.getKey())//
			), (Number) metric.getValue());
		}
	}
	return null;
}
 
Example #8
Source File: ClassificationMetrics.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
public ClassificationMetrics(ClassificationMetricsConfig classificationMetricsConfig) {
    this(classificationMetricsConfig, Arrays.asList(new ImmutableTag("machinelearning","classification")));
}
 
Example #9
Source File: MultiLabelMetrics.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
public MultiLabelMetrics(MultiLabelMetricsConfig multiLabelMetricsConfig) {
    this(multiLabelMetricsConfig, Arrays.asList(new ImmutableTag("machinelearning","multilabel")));
}
 
Example #10
Source File: RegressionMetrics.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
public RegressionMetrics(RegressionMetricsConfig regressionMetricsConfig) {
    this(regressionMetricsConfig, Arrays.asList(new ImmutableTag("machinelearning","regression")));
}
 
Example #11
Source File: MicrometerMetricsReporter.java    From java-metrics with Apache License 2.0 4 votes vote down vote up
@Override
public void reportSpan(SpanData spanData) {
    boolean skip = Arrays.stream(this.metricLabels).anyMatch(m -> m.value(spanData) == null);
    if (skip) {
        return;
    }

    List<Tag> tags = Arrays.stream(this.metricLabels)
        .map(m -> new ImmutableTag(m.name(), m.value(spanData).toString()))
        .collect(Collectors.toList());

    Timer timer = this.registry.find(this.name).tags(tags).timer();
    if (null != timer) {
        // we have a metric registered already, just record the timing:
        timer.record(spanData.getDuration(), TimeUnit.MICROSECONDS);
        return;
    }

    // would be awesome if we could reuse the builder, but looks like we can't, as we can't override the name
    Timer.Builder builder = Timer.builder(this.name).tags(tags);
    if (publishPercentileHistogram) {
        builder.publishPercentileHistogram();
    }

    if (null != percentiles) {
        builder.publishPercentiles(percentiles);
    }

    if (null != sla) {
        builder.sla(sla);
    }

    if (null != minimumExpectedValue) {
        builder.minimumExpectedValue(minimumExpectedValue);
    }

    if (null != maximumExpectedValue) {
        builder.maximumExpectedValue(maximumExpectedValue);
    }

    builder.register(this.registry).record(spanData.getDuration(), TimeUnit.MICROSECONDS);
}