Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Normalise metric names: change from jaeger: prefix to jaeger_tracer_ …
Browse files Browse the repository at this point in the history
…with ability to use custom prefix (#564)

Signed-off-by: Gary Brown <gary@brownuk.com>
  • Loading branch information
objectiser authored and jpkrohling committed Oct 30, 2018
1 parent 10c641f commit af610ad
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
public class Metrics {

public Metrics(MetricsFactory factory) {
createMetrics(factory);
this(factory, "jaeger_tracer_");
}

private void createMetrics(MetricsFactory factory) {
public Metrics(MetricsFactory factory, String metricsPrefix) {
createMetrics(factory, metricsPrefix);
}

private void createMetrics(MetricsFactory factory, String metricsPrefix) {
for (Field field : Metrics.class.getDeclaredFields()) {
if (!Counter.class.isAssignableFrom(field.getType())
&& !Timer.class.isAssignableFrom(field.getType())
Expand All @@ -38,7 +42,7 @@ private void createMetrics(MetricsFactory factory) {
continue;
}

StringBuilder metricBuilder = new StringBuilder("jaeger:");
StringBuilder metricBuilder = new StringBuilder(metricsPrefix);
HashMap<String, String> tags = new HashMap<String, String>();

Annotation[] annotations = field.getAnnotations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public void testTraceId128Bit() {

@Test
public void testSpanMetrics() {
assertEquals(1, metricsFactory.getCounter("jaeger:started_spans", "sampled=y"));
assertEquals(1, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=started"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=y"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=y,state=started"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public void testBuildSpan() {
public void testTracerMetrics() {
String expectedOperation = "fry";
tracer.buildSpan(expectedOperation).start();
assertEquals(1, metricsFactory.getCounter("jaeger:started_spans", "sampled=y"));
assertEquals(0, metricsFactory.getCounter("jaeger:started_spans", "sampled=n"));
assertEquals(1, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=started"));
assertEquals(0, metricsFactory.getCounter("jaeger:traces", "sampled=n,state=started"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=y"));
assertEquals(0, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=n"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=y,state=started"));
assertEquals(0, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=n,state=started"));
}

@Test
Expand Down Expand Up @@ -152,7 +152,7 @@ public void testWithBaggageRestrictionManager() {
final String key = "key";
tracer.setBaggage(span, key, "value");

assertEquals(1, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=ok"));
}

@Test
Expand All @@ -179,9 +179,9 @@ public void testSpanContextNotSampled() {
JaegerSpan first = tracer.buildSpan(expectedOperation).start();
tracer.buildSpan(expectedOperation).asChildOf((first.context()).withFlags((byte) 0)).start();

assertEquals(1, metricsFactory.getCounter("jaeger:started_spans", "sampled=y"));
assertEquals(1, metricsFactory.getCounter("jaeger:started_spans", "sampled=n"));
assertEquals(1, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=started"));
assertEquals(0, metricsFactory.getCounter("jaeger:traces", "sampled=n,state=started"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=y"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=n"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=y,state=started"));
assertEquals(0, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=n,state=started"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testInvalidBaggage() {
assertBaggageLogs(jaegerSpan, KEY, value, false, false, true);
assertNull(ctx.getBaggageItem(KEY));

assertEquals(1, metricsFactory.getCounter("jaeger:baggage_updates", "result=err"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=err"));
}

@Test
Expand All @@ -86,8 +86,8 @@ public void testTruncatedBaggage() {
assertBaggageLogs(jaegerSpan, KEY, expected, true, false, false);
assertEquals(expected, ctx.getBaggageItem(KEY));

assertEquals(1, metricsFactory.getCounter("jaeger:baggage_truncations", ""));
assertEquals(1, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_baggage_truncations", ""));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=ok"));
}

@Test
Expand All @@ -101,7 +101,7 @@ public void testOverrideBaggage() {
assertBaggageLogs(child, KEY, value, false, true, false);
assertEquals(value, ctx.getBaggageItem(KEY));

assertEquals(2, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok"));
assertEquals(2, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=ok"));
}

@Test
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testBaggageNullRemoveValue() {
assertBaggageLogs(child, KEY, null, false, true, false);
assertNull(child.getBaggageItem(KEY));

assertEquals(2, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok"));
assertEquals(2, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=ok"));
}

private void assertBaggageLogs(JaegerSpan jaegerSpan, String key, String value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testUpdateBaggageRestrictions() throws Exception {
assertEquals(Restriction.of(true, MAX_VALUE_LENGTH), undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY));
assertFalse(undertest.getRestriction(SERVICE_NAME, "bad-key").isKeyAllowed());
assertTrue(
metricsFactory.getCounter("jaeger:baggage_restrictions_updates.result=ok",
metricsFactory.getCounter("jaeger_tracer_baggage_restrictions_updates.result=ok",
Collections.emptyMap()) > 0L);
}

Expand All @@ -93,7 +93,7 @@ public void testAllowBaggageOnInitializationFailure() throws Exception {
assertFalse(undertest.isReady());
// If baggage restriction update fails, all baggage should still be allowed.
assertTrue(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed());
assertTrue(metricsFactory.getCounter("jaeger:baggage_restrictions_updates.result=err",
assertTrue(metricsFactory.getCounter("jaeger_tracer_baggage_restrictions_updates.result=err",
Collections.emptyMap()) > 0L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void emptyValueForTag() {
.build();

tracer.buildSpan("theoperation").start();
assertEquals(-1, metricsFactory.getCounter("jaeger:started_spans", "sampled"));
assertEquals(-1, metricsFactory.getCounter("jaeger:started_spans", ""));
assertEquals(-1, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled"));
assertEquals(-1, metricsFactory.getCounter("jaeger_tracer_started_spans", ""));
}

@Test
Expand All @@ -123,9 +123,9 @@ public void canBeUsedWithMetrics() {
.build();

tracer.buildSpan("theoperation").start();
assertEquals(1, metricsFactory.getCounter("jaeger:started_spans", "sampled=y"));
assertEquals(0, metricsFactory.getCounter("jaeger:started_spans", "sampled=n"));
assertEquals(1, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=started"));
assertEquals(0, metricsFactory.getCounter("jaeger:traces", "sampled=n,state=started"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=y"));
assertEquals(0, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=n"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=y,state=started"));
assertEquals(0, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=n,state=started"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,28 @@ public void setUp() {
@Test
public void testCounterWithoutExplicitTags() {
metrics.tracesJoinedSampled.inc(1);
assertEquals(1, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=joined"));
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=y,state=joined"));
}

@Test
public void testGaugeWithoutExplicitTags() {
metrics.reporterQueueLength.update(1);
assertEquals(1, metricsFactory.getGauge("jaeger:reporter_queue_length", ""));
assertEquals(1, metricsFactory.getGauge("jaeger_tracer_reporter_queue_length", ""));
}

@Test
public void testAddTagsToMetricName() {
Map<String, String> tags = new HashMap<>();
tags.put("foo", "bar");
assertEquals("thecounter.foo=bar", Metrics.addTagsToMetricName("thecounter", tags));
assertEquals("jaeger:thecounter.foo=bar", Metrics.addTagsToMetricName("jaeger:thecounter", tags));
assertEquals("jaeger_tracer_thecounter.foo=bar", Metrics.addTagsToMetricName("jaeger_tracer_thecounter", tags));
}

@Test
public void testCounterWithCustomPrefix() {
Metrics customPrefixMetrics = new Metrics(metricsFactory, "custom_");
customPrefixMetrics.tracesJoinedSampled.inc(1);
assertEquals(1, metricsFactory.getCounter("custom_traces", "sampled=y,state=joined"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public void testRemoteReporterFlushesOnClose() {
assertEquals(0, sender.getAppended().size());
assertEquals(numberOfSpans, sender.getFlushed().size());

assertEquals(100, metricsFactory.getCounter("jaeger:started_spans", "sampled=y"));
assertEquals(100, metricsFactory.getCounter("jaeger:reporter_spans", "result=ok"));
assertEquals(100, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=started"));
assertEquals(100, metricsFactory.getCounter("jaeger_tracer_started_spans", "sampled=y"));
assertEquals(100, metricsFactory.getCounter("jaeger_tracer_reporter_spans", "result=ok"));
assertEquals(100, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=y,state=started"));
}

@Test
Expand Down Expand Up @@ -170,7 +170,7 @@ public void testAppendWhenQueueFull() {
reporter.report(newSpan());

// Then: one or both spans should be dropped
long droppedCount = metricsFactory.getCounter("jaeger:reporter_spans", "result=dropped");
long droppedCount = metricsFactory.getCounter("jaeger_tracer_reporter_spans", "result=dropped");
assertThat(droppedCount, anyOf(equalTo(1L), equalTo(2L)));
}

Expand Down Expand Up @@ -240,12 +240,12 @@ public void testFlushUpdatesQueueLength() {
reporter.report(newSpan());
}

assertEquals(0, metricsFactory.getGauge("jaeger:reporter_queue_length", ""));
assertEquals(0, metricsFactory.getGauge("jaeger_tracer_reporter_queue_length", ""));

RemoteReporter remoteReporter = (RemoteReporter) reporter;
remoteReporter.flush();

assertTrue(metricsFactory.getGauge("jaeger:reporter_queue_length", "") > 0);
assertTrue(metricsFactory.getGauge("jaeger_tracer_reporter_queue_length", "") > 0);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public class MicrometerTest {

@BeforeClass
public static void initial() {
expectedMetricCounts.put("jaeger:sampler_updates", 2L);
expectedMetricCounts.put("jaeger:finished_spans", 1L);
expectedMetricCounts.put("jaeger:baggage_restrictions_updates", 2L);
expectedMetricCounts.put("jaeger:started_spans", 2L);
expectedMetricCounts.put("jaeger:baggage_updates", 2L);
expectedMetricCounts.put("jaeger:sampler_queries", 2L);
expectedMetricCounts.put("jaeger:baggage_truncations", 1L);
expectedMetricCounts.put("jaeger:reporter_spans", 3L);
expectedMetricCounts.put("jaeger:traces", 4L);
expectedMetricCounts.put("jaeger:span_context_decoding_errors", 1L);
expectedMetricCounts.put("jaeger:reporter_queue_length", 1L);
expectedMetricCounts.put("jaeger_tracer_sampler_updates", 2L);
expectedMetricCounts.put("jaeger_tracer_finished_spans", 1L);
expectedMetricCounts.put("jaeger_tracer_baggage_restrictions_updates", 2L);
expectedMetricCounts.put("jaeger_tracer_started_spans", 2L);
expectedMetricCounts.put("jaeger_tracer_baggage_updates", 2L);
expectedMetricCounts.put("jaeger_tracer_sampler_queries", 2L);
expectedMetricCounts.put("jaeger_tracer_baggage_truncations", 1L);
expectedMetricCounts.put("jaeger_tracer_reporter_spans", 3L);
expectedMetricCounts.put("jaeger_tracer_traces", 4L);
expectedMetricCounts.put("jaeger_tracer_span_context_decoding_errors", 1L);
expectedMetricCounts.put("jaeger_tracer_reporter_queue_length", 1L);
}


Expand All @@ -84,39 +84,40 @@ public void tearDown() {
@Test
public void testCounterWithoutExplicitTags() {
metrics.decodingErrors.inc(1);
assertThat(registry.get("jaeger:span_context_decoding_errors").counter().count(), IsEqual.equalTo(1d));
assertTrue(prometheusRegistry.scrape().contains("jaeger:span_context_decoding_errors"));
assertThat(registry.get("jaeger_tracer_span_context_decoding_errors").counter().count(), IsEqual.equalTo(1d));
assertTrue(prometheusRegistry.scrape().contains("jaeger_tracer_span_context_decoding_errors"));
}

@Test
public void testCounterWithExplicitTags() {
metrics.tracesJoinedSampled.inc(1);
assertThat(registry.get("jaeger:traces").tags("sampled", "y", "state", "joined").counter().count(),
assertThat(registry.get("jaeger_tracer_traces").tags("sampled", "y", "state", "joined").counter().count(),
IsEqual.equalTo(1d)
);
String output = prometheusRegistry.scrape();
assertTrue(output.contains("jaeger:traces"));
assertTrue(output.contains("jaeger_tracer_traces"));
assertTrue(output.contains("sampled=\"y\""));
assertTrue(output.contains("state=\"joined\""));
}

@Test
public void testGaugeWithoutExplicitTags() {
metrics.reporterQueueLength.update(1);
assertThat(registry.get("jaeger:reporter_queue_length").gauge().value(), IsEqual.equalTo(1d));
assertTrue(prometheusRegistry.scrape().contains("jaeger:reporter_queue_length"));
assertThat(registry.get("jaeger_tracer_reporter_queue_length").gauge().value(), IsEqual.equalTo(1d));
assertTrue(prometheusRegistry.scrape().contains("jaeger_tracer_reporter_queue_length"));
}

@Test
public void testTimer() {
// we have no timers on the Metrics class yet, so, we simulate one
Map<String, String> tags = new HashMap<>(1);
tags.put("akey", "avalue");
Timer timer = new MicrometerMetricsFactory().createTimer("jaeger:timed_operation", tags);
Timer timer = new MicrometerMetricsFactory().createTimer("jaeger_tracer_timed_operation", tags);
timer.durationMicros(100);

assertThat(registry.get("jaeger:timed_operation").timer().totalTime(TimeUnit.MICROSECONDS), IsEqual.equalTo(100d));
assertTrue(prometheusRegistry.scrape().contains("jaeger:timed_operation_seconds"));
assertThat(registry.get("jaeger_tracer_timed_operation").timer().totalTime(TimeUnit.MICROSECONDS),
IsEqual.equalTo(100d));
assertTrue(prometheusRegistry.scrape().contains("jaeger_tracer_timed_operation_seconds"));
}


Expand Down Expand Up @@ -164,16 +165,16 @@ public void validateMetricCounts() throws InterruptedException {
createSomeSpans(tracer);
tracer.close();

double finishedSpans = registry.get("jaeger:finished_spans")
double finishedSpans = registry.get("jaeger_tracer_finished_spans")
.counter()
.count();

double startedSpans = registry.get("jaeger:started_spans")
double startedSpans = registry.get("jaeger_tracer_started_spans")
.tag("sampled", "y")
.counter()
.count();

double traces = registry.get("jaeger:traces")
double traces = registry.get("jaeger_tracer_traces")
.tag("sampled", "y")
.tag("state", "started")
.counter()
Expand Down Expand Up @@ -204,15 +205,15 @@ public void testServiceLoader() {
.next();

configuration.getTracer().buildSpan("theoperation").start().finish(100);
assertEquals(1, registry.find("jaeger:started_spans").counter().count(), 0);
assertEquals(1, registry.find("jaeger_tracer_started_spans").counter().count(), 0);
}

private void createSomeSpans(JaegerTracer tracer) {
for (int i = 0; i < 10; i++) {
JaegerSpan span = tracer.buildSpan("metricstest")
.withTag("foo", "bar" + i)
.start();
// Only finish every 3rd span so jaeger:started_spans and finished_spans counts are different
// Only finish every 3rd span so jaeger_tracer_started_spans and finished_spans counts are different
if (i % 3 == 0) {
span.finish();
}
Expand Down

0 comments on commit af610ad

Please sign in to comment.