diff --git a/jaeger-core/README.md b/jaeger-core/README.md index dcc266fda..1f0eff8bb 100644 --- a/jaeger-core/README.md +++ b/jaeger-core/README.md @@ -91,15 +91,15 @@ More information about using the `TracerResolver` can be found [here](../jaeger- The Jaeger Java Client collects internal metrics and is able to report them via [Micrometer](http://micrometer.io). To accomplish that, include the artifact `com.uber.jaeger:jaeger-micrometer` as a dependency to your project and use -`MicrometerStatsFactory` like this: +`MicrometerMetricsFactory` like this: ```java -MicrometerStatsFactory statsFactory = new MicrometerStatsFactory(); +MicrometerMetricsFactory metricsReporter = new MicrometerMetricsFactory(); Configuration configuration = Configuration.fromEnv(); Tracer tracer = configuration - .getTracerBuilder() - .withMetrics(new Metrics(statsFactory)) - .build(); + .getTracerBuilder() + .withMetrics(new com.uber.jaeger.metrics.Metrics(metricsReporter)) + .build(); ``` ### Development diff --git a/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java b/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java index d8de1399f..d047cccd3 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java @@ -15,9 +15,9 @@ package com.uber.jaeger; import com.uber.jaeger.metrics.Metrics; -import com.uber.jaeger.metrics.NullStatsReporter; +import com.uber.jaeger.metrics.MetricsFactory; +import com.uber.jaeger.metrics.NoopMetricsFactory; import com.uber.jaeger.metrics.StatsFactory; -import com.uber.jaeger.metrics.StatsFactoryImpl; import com.uber.jaeger.propagation.B3TextMapCodec; import com.uber.jaeger.propagation.Codec; import com.uber.jaeger.propagation.CompositeCodec; @@ -165,7 +165,7 @@ public enum Propagation { private SamplerConfiguration samplerConfig; private ReporterConfiguration reporterConfig; private CodecConfiguration codecConfig; - private StatsFactory statsFactory; + private MetricsFactory metricsFactory; private Map tracerTags; /** @@ -201,6 +201,7 @@ private Configuration( this.samplerConfig = samplerConfig; this.reporterConfig = reporterConfig; this.codecConfig = codecConfig; + this.metricsFactory = new NoopMetricsFactory(); } /** @@ -224,10 +225,10 @@ public Tracer.Builder getTracerBuilder() { if (codecConfig == null) { codecConfig = new CodecConfiguration(Collections., List>>emptyMap()); } - if (statsFactory == null) { - statsFactory = new StatsFactoryImpl(new NullStatsReporter()); + if (metricsFactory == null) { + metricsFactory = new NoopMetricsFactory(); } - Metrics metrics = new Metrics(statsFactory); + Metrics metrics = new Metrics(metricsFactory); Reporter reporter = reporterConfig.getReporter(metrics); Sampler sampler = samplerConfig.createSampler(serviceName, metrics); Tracer.Builder builder = new Tracer.Builder(serviceName) @@ -257,16 +258,30 @@ public synchronized void closeTracer() { } /** - * @param statsFactory the factory - * @deprecated Use {@link #setStatsFactory(StatsFactory)} instead + * @see #withMetricsFactory(MetricsFactory) + * @param statsFactory the StatsFactory to use on the Tracer to be built + * @deprecated Use {@link #withMetricsFactory(MetricsFactory)} instead */ @Deprecated public void setStatsFactor(StatsFactory statsFactory) { - this.statsFactory = statsFactory; + this.metricsFactory = statsFactory; } + /** + * @see #withMetricsFactory(MetricsFactory) + * @param statsFactory the StatsFactory to use on the Tracer to be built + * @deprecated Use {@link #withMetricsFactory(MetricsFactory)} instead + */ + @Deprecated public void setStatsFactory(StatsFactory statsFactory) { - this.statsFactory = statsFactory; + this.metricsFactory = statsFactory; + } + + /** + * @param metricsFactory the MetricsFactory to use on the Tracer to be built + */ + public void withMetricsFactory(MetricsFactory metricsFactory) { + this.metricsFactory = metricsFactory; } public Configuration withReporter(ReporterConfiguration reporterConfig) { diff --git a/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java b/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java index 25ac73523..28b532f82 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java @@ -19,7 +19,8 @@ import com.uber.jaeger.baggage.DefaultBaggageRestrictionManager; import com.uber.jaeger.exceptions.UnsupportedFormatException; import com.uber.jaeger.metrics.Metrics; -import com.uber.jaeger.metrics.NullStatsReporter; +import com.uber.jaeger.metrics.MetricsFactory; +import com.uber.jaeger.metrics.NoopMetricsFactory; import com.uber.jaeger.metrics.StatsFactoryImpl; import com.uber.jaeger.metrics.StatsReporter; import com.uber.jaeger.propagation.Extractor; @@ -33,14 +34,12 @@ import com.uber.jaeger.utils.Clock; import com.uber.jaeger.utils.SystemClock; import com.uber.jaeger.utils.Utils; - import io.opentracing.References; import io.opentracing.Scope; import io.opentracing.ScopeManager; import io.opentracing.propagation.Format; import io.opentracing.tag.Tags; import io.opentracing.util.ThreadLocalScopeManager; - import java.io.Closeable; import java.io.InputStream; import java.net.Inet4Address; @@ -445,7 +444,7 @@ public static final class Builder { private Sampler sampler; private Reporter reporter; private final PropagationRegistry registry = new PropagationRegistry(); - private Metrics metrics = new Metrics(new StatsFactoryImpl(new NullStatsReporter())); + private Metrics metrics = new Metrics(new NoopMetricsFactory()); private final String serviceName; private Clock clock = new SystemClock(); private Map tags = new HashMap(); @@ -501,11 +500,25 @@ public Builder registerExtractor(Format format, Extractor extractor) { return this; } + /** + * @deprecated Use {@link #withMetricsFactory(MetricsFactory)} instead + */ + @Deprecated public Builder withStatsReporter(StatsReporter statsReporter) { this.metrics = new Metrics(new StatsFactoryImpl(statsReporter)); return this; } + /** + * Creates a new {@link Metrics} to be used with the tracer, backed by the given {@link MetricsFactory} + * @param metricsFactory the metrics factory to use + * @return this instance of the builder + */ + public Builder withMetricsFactory(MetricsFactory metricsFactory) { + this.metrics = new Metrics(metricsFactory); + return this; + } + public Builder withScopeManager(ScopeManager scopeManager) { this.scopeManager = scopeManager; return this; diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/InMemoryMetricsFactory.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/InMemoryMetricsFactory.java new file mode 100644 index 000000000..04d217bcf --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/InMemoryMetricsFactory.java @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2017, The Jaeger Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.uber.jaeger.metrics; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +/** + * An ephemeral metrics factory, storing data in memory. This metrics factory is not meant to be used for production + * purposes. + */ +public class InMemoryMetricsFactory implements MetricsFactory { + private Map counters = new ConcurrentHashMap(); + private Map timers = new ConcurrentHashMap(); + private Map gauges = new ConcurrentHashMap(); + + @Override + public Counter createCounter(String name, Map tags) { + final AtomicLong value = new AtomicLong(0); + counters.put(Metrics.addTagsToMetricName(name, tags), value); + + return new Counter() { + @Override + public void inc(long delta) { + value.addAndGet(delta); + } + }; + } + + @Override + public Timer createTimer(final String name, final Map tags) { + final AtomicLong value = new AtomicLong(0); + timers.put(Metrics.addTagsToMetricName(name, tags), value); + + return new Timer() { + @Override + public void durationMicros(long time) { + value.addAndGet(time); + } + }; + } + + @Override + public Gauge createGauge(final String name, final Map tags) { + final AtomicLong value = new AtomicLong(0); + gauges.put(Metrics.addTagsToMetricName(name, tags), value); + + return new Gauge() { + @Override + public void update(long amount) { + value.addAndGet(amount); + } + }; + } + + /** + * Returns the counter value information for the counter with the given metric name. + * Note that the metric name is not the counter name, as a metric name usually includes the tags. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the metric name, which includes the tags + * @param tags the metric tags as comma separated list of entries, like "foo=bar,baz=qux" + * @return the counter value or -1, if no counter exists for the given metric name + */ + public long getCounter(String name, String tags) { + return getValue(counters, name, tags); + } + + /** + * Returns the counter value information for the counter with the given metric name. + * Note that the metric name is not the counter name, as a metric name usually includes the tags. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the metric name, which includes the tags + * @param tags the metric tags + * @return the counter value or -1, if no counter exists for the given metric name + */ + public long getCounter(String name, Map tags) { + return getValue(counters, name, tags); + } + + /** + * Returns the current value for the gauge with the given metric name. Note that the metric name is not the gauge + * name, as a metric name usually includes the tags. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the metric name, which includes the tags + * @param tags the metric tags as comma separated list of entries, like "foo=bar,baz=qux" + * @return the gauge value or -1, if no gauge exists for the given metric name + */ + public long getGauge(String name, String tags) { + return getValue(gauges, name, tags); + } + + /** + * Returns the current value for the gauge with the given metric name. Note that the metric name is not the gauge + * name, as a metric name usually includes the tags. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the metric name, which includes the tags + * @param tags the metric tags + * @return the gauge value or -1, if no gauge exists for the given metric name + */ + public long getGauge(String name, Map tags) { + return getValue(gauges, name, tags); + } + + /** + * Returns the current accumulated timing information for the timer with the given metric name. + * Note that the metric name is not the timer name, as a metric name usually includes the tags. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the metric name, which includes the tags + * @param tags the metric tags as comma separated list of entries, like "foo=bar,baz=qux" + * @return the timer value or -1, if no timer exists for the given metric name + */ + public long getTimer(String name, String tags) { + return getValue(timers, name, tags); + } + + /** + * Returns the current accumulated timing information for the timer with the given metric name. + * Note that the metric name is not the timer name, as a metric name usually includes the tags. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the metric name, which includes the tags + * @param tags the metric tags + * @return the timer value or -1, if no timer exists for the given metric name + */ + public long getTimer(String name, Map tags) { + return getValue(timers, name, tags); + } + + private long getValue(Map collection, String name, String tags) { + if (null == tags || tags.isEmpty()) { + return getValue(collection, name); + } + + String[] entries = tags.split(","); + Map tagsAsMap = new HashMap(entries.length); + for (String entry : entries) { + String[] keyValue = entry.split("="); + tagsAsMap.put(keyValue[0], keyValue[1]); + } + + return getValue(collection, Metrics.addTagsToMetricName(name, tagsAsMap)); + } + + private long getValue(Map collection, String name, Map tags) { + return getValue(collection, Metrics.addTagsToMetricName(name, tags)); + } + + private long getValue(Map collection, String name) { + AtomicLong value = collection.get(name); + if (null == value) { + return -1; + } else { + return value.get(); + } + } +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/InMemoryStatsReporter.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/InMemoryStatsReporter.java index aab1ef60f..8f4fe57da 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/metrics/InMemoryStatsReporter.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/InMemoryStatsReporter.java @@ -17,6 +17,10 @@ import java.util.HashMap; import java.util.Map; +/** + * @deprecated Use {@link InMemoryMetricsFactory} instead + */ +@Deprecated public class InMemoryStatsReporter implements StatsReporter { public Map counters = new HashMap(); public Map gauges = new HashMap(); diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java index f35061beb..250b3d999 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java @@ -19,12 +19,22 @@ import java.util.HashMap; import java.util.Map; import java.util.SortedMap; -import java.util.SortedSet; import java.util.TreeMap; -import java.util.TreeSet; public class Metrics { + /** + * @deprecated Use {@link #Metrics(MetricsFactory)} instead + */ + @Deprecated public Metrics(StatsFactory factory) { + createMetrics(factory); + } + + public Metrics(MetricsFactory factory) { + createMetrics(factory); + } + + private void createMetrics(MetricsFactory factory) { for (Field field : Metrics.class.getDeclaredFields()) { if (!Counter.class.isAssignableFrom(field.getType()) && !Timer.class.isAssignableFrom(field.getType()) @@ -90,6 +100,10 @@ public static String addTagsToMetricName(String name, Map tags) return sb.toString(); } + /** + * @deprecated Use {@link MetricsFactory} and {@link Metrics#Metrics(MetricsFactory)} instead + */ + @Deprecated public static Metrics fromStatsReporter(StatsReporter reporter) { return new Metrics(new StatsFactoryImpl(reporter)); } diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/MetricsFactory.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/MetricsFactory.java new file mode 100644 index 000000000..56d702109 --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/MetricsFactory.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2017, The Jaeger Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.uber.jaeger.metrics; + +import java.util.Map; + +/** + * Provides a standardized way to create metrics-related objects, like {@link Counter}, {@link Timer} and {@link Gauge}. + * + */ +public interface MetricsFactory { + /** + * Creates a counter with the given gauge name and set of tags. The actual metric name is a combination of those two + * values. The counter starts at 0. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the counter name + * @param tags the tags to add to the counter + * @return a {@link Counter} with a metric name following the counter name and tags + */ + Counter createCounter(String name, Map tags); + + /** + * Creates a timer with the given timer name and set of tags. The actual metric name is a combination of those two + * values. The timer starts at 0. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the timer name + * @param tags the tags to add to the timer + * @return a {@link Timer} with a metric name following the counter name and tags + */ + Timer createTimer(String name, Map tags); + + /** + * Creates a gauge with the given gauge name and set of tags. The actual metric name is a combination of those two + * values. The timer starts at 0. + * + * @see Metrics#addTagsToMetricName(String, Map) + * @param name the gauge name + * @param tags the tags to add to the gauge + * @return a {@link Gauge} with a metric name following the gauge name and tags + */ + Gauge createGauge(String name, Map tags); +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/NoopMetricsFactory.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/NoopMetricsFactory.java new file mode 100644 index 000000000..6bffda9fa --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/NoopMetricsFactory.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017, The Jaeger Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.uber.jaeger.metrics; + +import java.util.Map; + +/** + * A metrics factory that implements NOOP counters, timers and gauges. + */ +public class NoopMetricsFactory implements MetricsFactory { + @Override + public Counter createCounter(String name, Map tags) { + return new Counter() { + @Override + public void inc(long delta) { + } + }; + } + + @Override + public Timer createTimer(final String name, final Map tags) { + return new Timer() { + @Override + public void durationMicros(long time) { + } + }; + } + + @Override + public Gauge createGauge(final String name, final Map tags) { + return new Gauge() { + @Override + public void update(long amount) { + } + }; + } +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/NullStatsReporter.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/NullStatsReporter.java index c277895da..e26119a12 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/metrics/NullStatsReporter.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/NullStatsReporter.java @@ -16,6 +16,12 @@ import java.util.Map; +/** + * A stats reporter that is NOOP. + * + * @deprecated Use {@link NoopMetricsFactory} instead + */ +@Deprecated public class NullStatsReporter implements StatsReporter { @Override public void incCounter(String name, long delta, Map tags) {} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactory.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactory.java index 12c2b59ac..988c4d167 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactory.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactory.java @@ -14,12 +14,11 @@ package com.uber.jaeger.metrics; -import java.util.Map; - -public interface StatsFactory { - Counter createCounter(String name, Map tags); - - Timer createTimer(String name, Map tags); - - Gauge createGauge(String name, Map tags); +/** + * @deprecated Use {@link MetricsFactory} instead. At the moment of the deprecation, all methods from this interface + * were moved to the new interface and this interface was made to extend the new one. This allows current + * implementations of this interface to seamlessly move to the new interface. + */ +@Deprecated +public interface StatsFactory extends MetricsFactory { } diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactoryImpl.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactoryImpl.java index e9ce603ce..9b9bd487a 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactoryImpl.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsFactoryImpl.java @@ -16,6 +16,12 @@ import java.util.Map; +/** + * A {@link StatsFactory} backed by a {@link StatsReporter}. + * + * @deprecated Use {@link MetricsFactory} instead + */ +@Deprecated public class StatsFactoryImpl implements StatsFactory { private final StatsReporter reporter; diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsReporter.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsReporter.java index fb11857ba..c7294690a 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsReporter.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/StatsReporter.java @@ -16,6 +16,10 @@ import java.util.Map; +/** + * @deprecated Use {@link MetricsFactory} instead + */ +@Deprecated public interface StatsReporter { void incCounter(String name, long delta, Map tags); diff --git a/jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java b/jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java index 8ee54cc63..57d8f296a 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java @@ -24,9 +24,8 @@ import com.uber.jaeger.baggage.BaggageRestrictionManager; import com.uber.jaeger.baggage.DefaultBaggageRestrictionManager; import com.uber.jaeger.baggage.Restriction; -import com.uber.jaeger.metrics.InMemoryStatsReporter; +import com.uber.jaeger.metrics.InMemoryMetricsFactory; import com.uber.jaeger.metrics.Metrics; -import com.uber.jaeger.metrics.StatsFactoryImpl; import com.uber.jaeger.reporters.InMemoryReporter; import com.uber.jaeger.samplers.ConstSampler; import com.uber.jaeger.utils.Clock; @@ -51,18 +50,18 @@ public class SpanTest { private InMemoryReporter reporter; private Tracer tracer; private Span span; - private InMemoryStatsReporter metricsReporter; + private InMemoryMetricsFactory metricsFactory; private Metrics metrics; @Before - public void setUp() throws Exception { - metricsReporter = new InMemoryStatsReporter(); + public void setUp() { + metricsFactory = new InMemoryMetricsFactory(); reporter = new InMemoryReporter(); clock = mock(Clock.class); - metrics = new Metrics(new StatsFactoryImpl(metricsReporter)); + metrics = new Metrics(metricsFactory); tracer = new Tracer.Builder("SamplerTest", reporter, new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(metrics) .withClock(clock) .withBaggageRestrictionManager(new DefaultBaggageRestrictionManager()) .withExpandExceptionLogs() @@ -72,10 +71,8 @@ public void setUp() throws Exception { @Test public void testSpanMetrics() { - assertEquals( - 1L, metricsReporter.counters.get("jaeger:started_spans.sampled=y").longValue()); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:traces.sampled=y.state=started").longValue()); + assertEquals(1, metricsFactory.getCounter("jaeger:started_spans", "sampled=y")); + assertEquals(1, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=started")); } @Test diff --git a/jaeger-core/src/test/java/com/uber/jaeger/TracerTest.java b/jaeger-core/src/test/java/com/uber/jaeger/TracerTest.java index 9090f2e4c..cd102c2b1 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/TracerTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/TracerTest.java @@ -16,16 +16,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import com.uber.jaeger.Tracer.Builder; -import com.uber.jaeger.metrics.InMemoryStatsReporter; +import com.uber.jaeger.metrics.InMemoryMetricsFactory; import com.uber.jaeger.metrics.Metrics; -import com.uber.jaeger.metrics.StatsFactoryImpl; import com.uber.jaeger.propagation.Injector; import com.uber.jaeger.reporters.InMemoryReporter; import com.uber.jaeger.reporters.RemoteReporter; @@ -43,16 +41,16 @@ public class TracerTest { Tracer tracer; - InMemoryStatsReporter metricsReporter; + InMemoryMetricsFactory metricsFactory; @Before public void setUp() throws Exception { - metricsReporter = new InMemoryStatsReporter(); + metricsFactory = new InMemoryMetricsFactory(); tracer = new Tracer.Builder("TracerTestService") .withReporter(new InMemoryReporter()) .withSampler(new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(new Metrics(metricsFactory)) .build(); } @@ -76,12 +74,10 @@ public void testBuildSpan() { public void testTracerMetrics() { String expectedOperation = "fry"; tracer.buildSpan(expectedOperation).start(); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:started_spans.sampled=y").longValue()); - assertNull(metricsReporter.counters.get("jaeger:started_spans.sampled=n")); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:traces.sampled=y.state=started").longValue()); - assertNull(metricsReporter.counters.get("jaeger:traces.sampled=n.state=started")); + 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")); } @Test @@ -91,7 +87,7 @@ public void testRegisterInjector() { Tracer tracer = new Tracer.Builder("TracerTestService", new InMemoryReporter(), new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(new Metrics(new InMemoryMetricsFactory())) .registerInjector(Format.Builtin.TEXT_MAP, injector) .build(); Span span = (Span) tracer.buildSpan("leela").start(); @@ -130,18 +126,15 @@ public void testBuilderIsNotServerRpc() { @Test public void testWithBaggageRestrictionManager() { - metricsReporter = new InMemoryStatsReporter(); - Metrics metrics = new Metrics(new StatsFactoryImpl(metricsReporter)); tracer = new Tracer.Builder("TracerTestService", new InMemoryReporter(), new ConstSampler(true)) - .withMetrics(metrics) + .withMetrics(new Metrics(metricsFactory)) .build(); Span span = (Span) tracer.buildSpan("some-operation").start(); final String key = "key"; tracer.setBaggage(span, key, "value"); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:baggage_updates.result=ok").longValue()); + assertEquals(1, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok")); } @Test @@ -185,12 +178,9 @@ public void testSpanContextNotSampled() { Span first = (Span) tracer.buildSpan(expectedOperation).start(); tracer.buildSpan(expectedOperation).asChildOf(first.context().withFlags((byte) 0)).start(); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:started_spans.sampled=y").longValue()); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:started_spans.sampled=n").longValue()); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:traces.sampled=y.state=started").longValue()); - assertNull(metricsReporter.counters.get("jaeger:traces.sampled=n.state=started")); + 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")); } } diff --git a/jaeger-core/src/test/java/com/uber/jaeger/baggage/BaggageSetterTest.java b/jaeger-core/src/test/java/com/uber/jaeger/baggage/BaggageSetterTest.java index 9dd6d7e59..d6e07b7c9 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/baggage/BaggageSetterTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/baggage/BaggageSetterTest.java @@ -23,15 +23,12 @@ import com.uber.jaeger.Span; import com.uber.jaeger.SpanContext; import com.uber.jaeger.Tracer; -import com.uber.jaeger.metrics.InMemoryStatsReporter; +import com.uber.jaeger.metrics.InMemoryMetricsFactory; import com.uber.jaeger.metrics.Metrics; -import com.uber.jaeger.metrics.StatsFactoryImpl; import com.uber.jaeger.reporters.InMemoryReporter; import com.uber.jaeger.samplers.ConstSampler; - import java.util.List; import java.util.Map; - import org.junit.Before; import org.junit.Test; @@ -40,7 +37,7 @@ public class BaggageSetterTest { private InMemoryReporter reporter; private Tracer tracer; private Span span; - private InMemoryStatsReporter metricsReporter; + private InMemoryMetricsFactory metricsFactory; private Metrics metrics; private BaggageRestrictionManager mgr; private BaggageSetter setter; @@ -50,14 +47,14 @@ public class BaggageSetterTest { @Before public void setUp() throws Exception { - metricsReporter = new InMemoryStatsReporter(); + metricsFactory = new InMemoryMetricsFactory(); reporter = new InMemoryReporter(); - metrics = new Metrics(new StatsFactoryImpl(metricsReporter)); + metrics = new Metrics(metricsFactory); mgr = mock(DefaultBaggageRestrictionManager.class); setter = new BaggageSetter(mgr, metrics); tracer = new Tracer.Builder(SERVICE, reporter, new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(metrics) .build(); span = (Span) tracer.buildSpan("some-operation").startManual(); } @@ -72,8 +69,7 @@ public void testInvalidBaggage() { assertBaggageLogs(span, KEY, value, false, false, true); assertNull(ctx.getBaggageItem(KEY)); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:baggage_updates.result=err").longValue()); + assertEquals(1, metricsFactory.getCounter("jaeger:baggage_updates", "result=err")); } @Test @@ -86,10 +82,8 @@ public void testTruncatedBaggage() { assertBaggageLogs(span, KEY, expected, true, false, false); assertEquals(expected, ctx.getBaggageItem(KEY)); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:baggage_truncations").longValue()); - assertEquals( - 1L, metricsReporter.counters.get("jaeger:baggage_updates.result=ok").longValue()); + assertEquals(1, metricsFactory.getCounter("jaeger:baggage_truncations","")); + assertEquals(1, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok")); } @Test @@ -103,15 +97,14 @@ public void testOverrideBaggage() { assertBaggageLogs(child, KEY, value, false, true, false); assertEquals(value, ctx.getBaggageItem(KEY)); - assertEquals( - 2L, metricsReporter.counters.get("jaeger:baggage_updates.result=ok").longValue()); + assertEquals(2, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok")); } @Test public void testUnsampledSpan() { tracer = new Tracer.Builder("SamplerTest", reporter, new ConstSampler(false)) - .withStatsReporter(metricsReporter) + .withMetrics(metrics) .build(); span = (Span) tracer.buildSpan("some-operation").startManual(); @@ -146,8 +139,7 @@ public void testBaggageNullRemoveValue() { assertBaggageLogs(child, KEY, null, false, true, false); assertNull(child.getBaggageItem(KEY)); - assertEquals( - 2L, metricsReporter.counters.get("jaeger:baggage_updates.result=ok").longValue()); + assertEquals(2, metricsFactory.getCounter("jaeger:baggage_updates", "result=ok")); } private void assertBaggageLogs( diff --git a/jaeger-core/src/test/java/com/uber/jaeger/metrics/InMemoryMetricsFactoryTest.java b/jaeger-core/src/test/java/com/uber/jaeger/metrics/InMemoryMetricsFactoryTest.java new file mode 100644 index 000000000..d2526a9ec --- /dev/null +++ b/jaeger-core/src/test/java/com/uber/jaeger/metrics/InMemoryMetricsFactoryTest.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2017, The Jaeger Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.uber.jaeger.metrics; + +import static org.junit.Assert.assertEquals; + +import com.uber.jaeger.Tracer; +import com.uber.jaeger.reporters.InMemoryReporter; +import com.uber.jaeger.samplers.ConstSampler; +import java.util.Collections; +import java.util.Map; +import org.junit.Test; + +public class InMemoryMetricsFactoryTest { + + @Test + public void metricNameIsUsedForCounter() { + Map tags = Collections.singletonMap("foo", "bar"); + + InMemoryMetricsFactory inMemoryMetricsFactory = new InMemoryMetricsFactory(); + inMemoryMetricsFactory.createCounter("thecounter", tags); + + assertEquals(-1, inMemoryMetricsFactory.getCounter("thecounter", Collections.emptyMap())); + assertEquals(0, inMemoryMetricsFactory.getCounter("thecounter", tags)); + } + + @Test + public void counterValueIsIncreased() { + Map tags = Collections.singletonMap("foo", "bar"); + + InMemoryMetricsFactory inMemoryMetricsFactory = new InMemoryMetricsFactory(); + Counter counter = inMemoryMetricsFactory.createCounter("thecounter", tags); + assertEquals(0, inMemoryMetricsFactory.getCounter("thecounter", tags)); + + counter.inc(1); + + assertEquals(1, inMemoryMetricsFactory.getCounter("thecounter", tags)); + } + + @Test + public void metricNameIsUsedForTimer() { + Map tags = Collections.singletonMap("foo", "bar"); + + InMemoryMetricsFactory inMemoryMetricsFactory = new InMemoryMetricsFactory(); + inMemoryMetricsFactory.createTimer("thetimer", tags); + + assertEquals(-1, inMemoryMetricsFactory.getTimer("thetimer", Collections.emptyMap())); + assertEquals(0, inMemoryMetricsFactory.getTimer("thetimer", tags)); + } + + @Test + public void timerValueIsIncreased() { + Map tags = Collections.singletonMap("foo", "bar"); + + InMemoryMetricsFactory inMemoryMetricsFactory = new InMemoryMetricsFactory(); + Timer timer = inMemoryMetricsFactory.createTimer("thetimer", tags); + assertEquals(0, inMemoryMetricsFactory.getTimer("thetimer", tags)); + + timer.durationMicros(1); + + assertEquals(1, inMemoryMetricsFactory.getTimer("thetimer", tags)); + } + + @Test + public void metricNameIsUsedForGauge() { + Map tags = Collections.singletonMap("foo", "bar"); + + InMemoryMetricsFactory inMemoryMetricsFactory = new InMemoryMetricsFactory(); + inMemoryMetricsFactory.createGauge("thegauge", tags); + + assertEquals(-1, inMemoryMetricsFactory.getGauge("thegauge", Collections.emptyMap())); + assertEquals(0, inMemoryMetricsFactory.getGauge("thegauge", tags)); + } + + @Test + public void gaugeValueIsIncreased() { + Map tags = Collections.singletonMap("foo", "bar"); + + InMemoryMetricsFactory inMemoryMetricsFactory = new InMemoryMetricsFactory(); + Gauge gauge = inMemoryMetricsFactory.createGauge("thegauge", tags); + assertEquals(0, inMemoryMetricsFactory.getGauge("thegauge", tags)); + + gauge.update(1); + + assertEquals(1, inMemoryMetricsFactory.getGauge("thegauge", tags)); + } + + @Test + public void canBeUsedWithMetrics() { + InMemoryMetricsFactory metricsFactory = new InMemoryMetricsFactory(); + Tracer tracer = + new Tracer.Builder("metricsFactoryTest", new InMemoryReporter(), new ConstSampler(true)) + .withMetrics(new Metrics(metricsFactory)) + .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")); + } +} diff --git a/jaeger-core/src/test/java/com/uber/jaeger/metrics/MetricsTest.java b/jaeger-core/src/test/java/com/uber/jaeger/metrics/MetricsTest.java index f4e42ae8d..d6847366c 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/metrics/MetricsTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/metrics/MetricsTest.java @@ -16,61 +16,38 @@ import static org.junit.Assert.assertEquals; -import org.junit.After; +import java.util.HashMap; +import java.util.Map; import org.junit.Before; import org.junit.Test; public class MetricsTest { - InMemoryStatsReporter metricsReporter; + InMemoryMetricsFactory metricsFactory; Metrics metrics; @Before - public void setUp() throws Exception { - metricsReporter = new InMemoryStatsReporter(); - metrics = new Metrics(new StatsFactoryImpl(metricsReporter)); - } - - @After - public void tearDown() { - metricsReporter.reset(); + public void setUp() { + metricsFactory = new InMemoryMetricsFactory(); + metrics = new Metrics(metricsFactory); } @Test - public void testCounterWithoutExplicitTags() throws Exception { + public void testCounterWithoutExplicitTags() { metrics.tracesJoinedSampled.inc(1); - - Object[] metricNames = metricsReporter.counters.keySet().toArray(); - String metricName = (String) metricNames[0]; - long expectedAmount = metricsReporter.counters.get(metricName); - - assertEquals(metricNames.length, 1); - assertEquals(expectedAmount, 1); - assertEquals("jaeger:traces.sampled=y.state=joined", metricName); - } - - @Test - public void testCounterWithExplicitTags() throws Exception { - metrics.tracesJoinedSampled.inc(1); - - Object[] metricNames = metricsReporter.counters.keySet().toArray(); - String metricName = (String) metricNames[0]; - long expectedAmount = metricsReporter.counters.get(metricName); - - assertEquals(metricNames.length, 1); - assertEquals(expectedAmount, 1); - assertEquals("jaeger:traces.sampled=y.state=joined", metricName); + assertEquals(1, metricsFactory.getCounter("jaeger:traces", "sampled=y,state=joined")); } @Test public void testGaugeWithoutExplicitTags() { metrics.reporterQueueLength.update(1); + assertEquals(1, metricsFactory.getGauge("jaeger:reporter_queue_length", "")); + } - Object[] metricNames = metricsReporter.gauges.keySet().toArray(); - String metricName = (String) metricNames[0]; - long expectedAmount = metricsReporter.gauges.get(metricName); - - assertEquals(metricNames.length, 1); - assertEquals(1L, expectedAmount, 0.00001); - assertEquals("jaeger:reporter_queue_length", metricName); + @Test + public void testAddTagsToMetricName() { + Map 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)); } } diff --git a/jaeger-core/src/test/java/com/uber/jaeger/metrics/NoopMetricsFactoryTest.java b/jaeger-core/src/test/java/com/uber/jaeger/metrics/NoopMetricsFactoryTest.java new file mode 100644 index 000000000..828b5bb5a --- /dev/null +++ b/jaeger-core/src/test/java/com/uber/jaeger/metrics/NoopMetricsFactoryTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2017, The Jaeger Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.uber.jaeger.metrics; + +import com.uber.jaeger.Tracer; +import com.uber.jaeger.reporters.InMemoryReporter; +import com.uber.jaeger.samplers.ConstSampler; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; + +/** + * This test is to ensure we have a NOOP implementation that won't crash when used for real. + * + * @see InMemoryMetricsFactoryTest + */ +public class NoopMetricsFactoryTest { + + @Test + public void metricNameIsUsedForCounter() { + Map tags = new HashMap<>(1); + tags.put("foo", "bar"); + + NoopMetricsFactory metricsFactory = new NoopMetricsFactory(); + metricsFactory.createCounter("thecounter", tags); + } + + @Test + public void counterValueIsIncreased() { + Map tags = new HashMap<>(1); + tags.put("foo", "bar"); + + NoopMetricsFactory metricsFactory = new NoopMetricsFactory(); + Counter counter = metricsFactory.createCounter("thecounter", tags); + counter.inc(1); + } + + @Test + public void metricNameIsUsedForTimer() { + Map tags = new HashMap<>(1); + tags.put("foo", "bar"); + + NoopMetricsFactory metricsFactory = new NoopMetricsFactory(); + metricsFactory.createTimer("thetimer", tags); + } + + @Test + public void timerValueIsIncreased() { + Map tags = new HashMap<>(1); + tags.put("foo", "bar"); + + NoopMetricsFactory metricsFactory = new NoopMetricsFactory(); + Timer timer = metricsFactory.createTimer("thetimer", tags); + timer.durationMicros(1); + } + + @Test + public void metricNameIsUsedForGauge() { + Map tags = new HashMap<>(1); + tags.put("foo", "bar"); + + NoopMetricsFactory metricsFactory = new NoopMetricsFactory(); + metricsFactory.createGauge("thegauge", tags); + } + + @Test + public void gaugeValueIsIncreased() { + Map tags = new HashMap<>(1); + tags.put("foo", "bar"); + + NoopMetricsFactory metricsFactory = new NoopMetricsFactory(); + Gauge gauge = metricsFactory.createGauge("thegauge", tags); + gauge.update(1); + } + + @Test + public void canBeUsedWithMetrics() { + NoopMetricsFactory metricsFactory = new NoopMetricsFactory(); + Tracer tracer = + new Tracer.Builder("metricsFactoryTest", new InMemoryReporter(), new ConstSampler(true)) + .withMetrics(new Metrics(metricsFactory)) + .build(); + + tracer.buildSpan("theoperation").start(); + } +} diff --git a/jaeger-core/src/test/java/com/uber/jaeger/reporters/RemoteReporterTest.java b/jaeger-core/src/test/java/com/uber/jaeger/reporters/RemoteReporterTest.java index 7be3d0218..64fca9af7 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/reporters/RemoteReporterTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/reporters/RemoteReporterTest.java @@ -19,26 +19,22 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import com.uber.jaeger.Span; import com.uber.jaeger.Tracer; import com.uber.jaeger.exceptions.SenderException; -import com.uber.jaeger.metrics.InMemoryStatsReporter; +import com.uber.jaeger.metrics.InMemoryMetricsFactory; import com.uber.jaeger.metrics.Metrics; -import com.uber.jaeger.metrics.StatsFactoryImpl; import com.uber.jaeger.samplers.ConstSampler; import com.uber.jaeger.senders.Sender; - import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; - import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -50,12 +46,12 @@ public class RemoteReporterTest { private final int flushInterval = 1000; // in milliseconds private final int maxQueueSize = 500; private Metrics metrics; - InMemoryStatsReporter metricsReporter; + private InMemoryMetricsFactory metricsFactory; @Before public void setUp() throws Exception { - metricsReporter = new InMemoryStatsReporter(); - metrics = new Metrics(new StatsFactoryImpl(metricsReporter)); + metricsFactory = new InMemoryMetricsFactory(); + metrics = new Metrics(metricsFactory); sender = new InMemorySender(); reporter = new RemoteReporter.Builder() @@ -66,7 +62,7 @@ public void setUp() throws Exception { .build(); tracer = new Tracer.Builder("test-remote-reporter", reporter, new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(metrics) .build(); } @@ -98,12 +94,9 @@ public void testRemoteReporterFlushesOnClose() throws Exception { assertEquals(0, sender.getAppended().size()); assertEquals(numberOfSpans, sender.getFlushed().size()); - assertEquals( - 100L, metricsReporter.counters.get("jaeger:started_spans.sampled=y").longValue()); - assertEquals( - 100L, metricsReporter.counters.get("jaeger:reporter_spans.result=ok").longValue()); - assertEquals( - 100L, metricsReporter.counters.get("jaeger:traces.sampled=y.state=started").longValue()); + 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")); } @Test @@ -174,7 +167,7 @@ public void testAppendWhenQueueFull() { reporter.report(newSpan()); // Then: one or both spans should be dropped - Long droppedCount = metricsReporter.counters.get("jaeger:reporter_spans.result=dropped"); + long droppedCount = metricsFactory.getCounter("jaeger:reporter_spans", "result=dropped"); assertThat(droppedCount, anyOf(equalTo(1L), equalTo(2L))); } @@ -184,7 +177,7 @@ public void testCloseWhenQueueFull() { int closeTimeoutMillis = 5; reporter = new RemoteReporter(sender, Integer.MAX_VALUE, maxQueueSize, closeTimeoutMillis, metrics); tracer = new Tracer.Builder("test-remote-reporter", reporter, new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(metrics) .build(); // change sender to blocking mode sender.permitAppend(0); @@ -219,7 +212,7 @@ public void testFlushUpdatesQueueLength() throws Exception { int neverFlushInterval = Integer.MAX_VALUE; reporter = new RemoteReporter(sender, neverFlushInterval, maxQueueSize, metrics); tracer = new Tracer.Builder("test-remote-reporter", reporter, new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(metrics) .build(); // change sender to blocking mode @@ -229,12 +222,12 @@ public void testFlushUpdatesQueueLength() throws Exception { reporter.report(newSpan()); } - assertNull(metricsReporter.gauges.get("jaeger:reporter_queue_length")); + assertEquals(0, metricsFactory.getGauge("jaeger:reporter_queue_length", "")); RemoteReporter remoteReporter = (RemoteReporter) reporter; remoteReporter.flush(); - assertTrue(metricsReporter.gauges.get("jaeger:reporter_queue_length") > 0); + assertTrue(metricsFactory.getGauge("jaeger:reporter_queue_length", "") > 0); } @Test @@ -252,7 +245,7 @@ public int flush() throws SenderException { reporter = new RemoteReporter(sender, flushInterval, maxQueueSize, metrics); tracer = new Tracer.Builder("test-remote-reporter", reporter, new ConstSampler(true)) - .withStatsReporter(metricsReporter) + .withMetrics(metrics) .build(); tracer.buildSpan("mySpan").start().finish(); diff --git a/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/EndToEndBehavior.java b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/EndToEndBehavior.java index 40e2e39ab..e44789edc 100644 --- a/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/EndToEndBehavior.java +++ b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/EndToEndBehavior.java @@ -16,9 +16,7 @@ import com.uber.jaeger.crossdock.api.CreateTracesRequest; import com.uber.jaeger.metrics.Metrics; -import com.uber.jaeger.metrics.NullStatsReporter; -import com.uber.jaeger.metrics.StatsFactory; -import com.uber.jaeger.metrics.StatsFactoryImpl; +import com.uber.jaeger.metrics.NoopMetricsFactory; import com.uber.jaeger.reporters.RemoteReporter; import com.uber.jaeger.reporters.Reporter; import com.uber.jaeger.samplers.ConstSampler; @@ -41,8 +39,7 @@ public EndToEndBehavior(Map tracers) { } public EndToEndBehavior(String samplingHostPort, String serviceName, Sender sender) { - StatsFactory statsFactory = new StatsFactoryImpl(new NullStatsReporter()); - Metrics metrics = new Metrics(statsFactory); + Metrics metrics = new Metrics(new NoopMetricsFactory()); Reporter reporter = new RemoteReporter(sender, 1000, 100, metrics); ConstSampler constSampler = new ConstSampler(true); diff --git a/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/Configuration.java b/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/Configuration.java index f5f9189f2..331c2a0be 100644 --- a/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/Configuration.java +++ b/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/Configuration.java @@ -47,6 +47,6 @@ public synchronized Tracer getTracer() { } public void setMetricRegistry(MetricRegistry metricRegistry) { - setStatsFactory(new StatsFactory(metricRegistry)); + withMetricsFactory(new StatsFactory(metricRegistry)); } } diff --git a/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/StatsFactory.java b/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/StatsFactory.java index 10d6c9bca..894f14a6e 100644 --- a/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/StatsFactory.java +++ b/jaeger-dropwizard/src/main/java/com/uber/jaeger/dropwizard/StatsFactory.java @@ -17,10 +17,11 @@ import com.codahale.metrics.MetricRegistry; import com.uber.jaeger.metrics.Counter; import com.uber.jaeger.metrics.Gauge; +import com.uber.jaeger.metrics.MetricsFactory; import com.uber.jaeger.metrics.Timer; import java.util.Map; -public class StatsFactory implements com.uber.jaeger.metrics.StatsFactory { +public class StatsFactory implements MetricsFactory { private final MetricRegistry registry; public StatsFactory(MetricRegistry registry) { diff --git a/jaeger-micrometer/src/main/java/com/uber/jaeger/micrometer/MicrometerStatsFactory.java b/jaeger-micrometer/src/main/java/com/uber/jaeger/micrometer/MicrometerMetricsFactory.java similarity index 95% rename from jaeger-micrometer/src/main/java/com/uber/jaeger/micrometer/MicrometerStatsFactory.java rename to jaeger-micrometer/src/main/java/com/uber/jaeger/micrometer/MicrometerMetricsFactory.java index 93350434a..b00e0371d 100644 --- a/jaeger-micrometer/src/main/java/com/uber/jaeger/micrometer/MicrometerStatsFactory.java +++ b/jaeger-micrometer/src/main/java/com/uber/jaeger/micrometer/MicrometerMetricsFactory.java @@ -20,7 +20,7 @@ import com.uber.jaeger.metrics.Counter; import com.uber.jaeger.metrics.Gauge; -import com.uber.jaeger.metrics.StatsFactory; +import com.uber.jaeger.metrics.MetricsFactory; import com.uber.jaeger.metrics.Timer; import io.micrometer.core.instrument.ImmutableTag; import io.micrometer.core.instrument.Tag; @@ -29,7 +29,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -public class MicrometerStatsFactory implements StatsFactory { +public class MicrometerMetricsFactory implements MetricsFactory { @Override public Counter createCounter(final String name, final Map tags) { return new Counter() { diff --git a/jaeger-micrometer/src/test/java/com/uber/jaeger/micrometer/MicrometerTest.java b/jaeger-micrometer/src/test/java/com/uber/jaeger/micrometer/MicrometerTest.java index 70e4ae295..a101b378d 100644 --- a/jaeger-micrometer/src/test/java/com/uber/jaeger/micrometer/MicrometerTest.java +++ b/jaeger-micrometer/src/test/java/com/uber/jaeger/micrometer/MicrometerTest.java @@ -40,7 +40,7 @@ public void setUp() { registry = io.micrometer.core.instrument.Metrics.globalRegistry; prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); io.micrometer.core.instrument.Metrics.addRegistry(prometheusRegistry); - metrics = new Metrics(new MicrometerStatsFactory()); + metrics = new Metrics(new MicrometerMetricsFactory()); } @After @@ -79,7 +79,7 @@ public void testTimer() { // we have no timers on the Metrics class yet, so, we simulate one Map tags = new HashMap<>(1); tags.put("akey", "avalue"); - Timer timer = new MicrometerStatsFactory().createTimer("jaeger:timed_operation", tags); + Timer timer = new MicrometerMetricsFactory().createTimer("jaeger:timed_operation", tags); timer.durationMicros(100); assertThat(registry.get("jaeger:timed_operation").timer().totalTime(TimeUnit.MICROSECONDS), IsEqual.equalTo(100d));