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

Commit

Permalink
Closes jaegertracing#338 - Deprecated StatsReporter
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed Feb 23, 2018
1 parent 4009b2d commit 7871952
Show file tree
Hide file tree
Showing 13 changed files with 500 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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;
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<String, AtomicLong> counters = new ConcurrentHashMap<String, AtomicLong>();
private Map<String, AtomicLong> timers = new ConcurrentHashMap<String, AtomicLong>();
private Map<String, AtomicLong> gauges = new ConcurrentHashMap<String, AtomicLong>();

@Override
public Counter createCounter(String name, Map<String, String> tags) {
final AtomicLong value = new AtomicLong(0);
counters.put(Metrics.addTagsToMetricName(name, tags), value);

return new Counter() {
@Override
public void inc(long delta) {
synchronized (value) {
value.addAndGet(delta);
}
}
};
}

@Override
public Timer createTimer(final String name, final Map<String, String> tags) {
final AtomicLong value = new AtomicLong(0);
timers.put(Metrics.addTagsToMetricName(name, tags), value);

return new Timer() {
@Override
public void durationMicros(long time) {
synchronized (value) {
value.addAndGet(time);
}
}
};
}

@Override
public Gauge createGauge(final String name, final Map<String, String> 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
* @return the counter value or -1, if no counter exists for the given metric name
*/
public long getCounter(String name) {
return getValue(counters, name);
}

/**
* 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
* @return the gauge value or -1, if no gauge exists for the given metric name
*/
public long getGauge(String name) {
return getValue(gauges, name);
}

/**
* 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
* @return the timer value or -1, if no timer exists for the given metric name
*/
public long getTimer(String name) {
return getValue(timers, name);
}

private long getValue(Map<String, AtomicLong> collection, String name) {
AtomicLong value = collection.get(name);
if (null == value) {
return -1;
} else {
return value.get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashMap;
import java.util.Map;

@Deprecated
public class InMemoryStatsReporter implements StatsReporter {
public Map<String, Long> counters = new HashMap<String, Long>();
public Map<String, Long> gauges = new HashMap<String, Long>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@

public class Metrics {
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())
Expand Down Expand Up @@ -90,6 +98,7 @@ public static String addTagsToMetricName(String name, Map<String, String> tags)
return sb.toString();
}

@Deprecated
public static Metrics fromStatsReporter(StatsReporter reporter) {
return new Metrics(new StatsFactoryImpl(reporter));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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 timer name
* @param tags the tags to add to the timer
* @return a {@link Gauge} with a metric name following the gauge name and tags
*/
Gauge createGauge(String name, Map<String, String> tags);
}
Original file line number Diff line number Diff line change
@@ -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<String, String> tags) {
return new Counter() {
@Override
public void inc(long delta) {
}
};
}

@Override
public Timer createTimer(final String name, final Map<String, String> tags) {
return new Timer() {
@Override
public void durationMicros(long time) {
}
};
}

@Override
public Gauge createGauge(final String name, final Map<String, String> tags) {
return new Gauge() {
@Override
public void update(long amount) {
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

import java.util.Map;

/**
* A stats reporter that is NOOP.
*
* @see NoopMetricsFactory
*/
@Deprecated
public class NullStatsReporter implements StatsReporter {
@Override
public void incCounter(String name, long delta, Map<String, String> tags) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

import java.util.Map;

public interface StatsFactory {
Counter createCounter(String name, Map<String, String> tags);

Timer createTimer(String name, Map<String, String> tags);

Gauge createGauge(String name, Map<String, String> tags);
@Deprecated
public interface StatsFactory extends MetricsFactory {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

import java.util.Map;

/**
*
*/
@Deprecated
public class StatsFactoryImpl implements StatsFactory {
private final StatsReporter reporter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

import java.util.Map;

/**
*
* @see StatsFactory
*/
@Deprecated
public interface StatsReporter {

void incCounter(String name, long delta, Map<String, String> tags);
Expand Down
39 changes: 17 additions & 22 deletions jaeger-core/src/test/java/com/uber/jaeger/TracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.uber.jaeger.metrics.InMemoryMetricsFactory;
import com.uber.jaeger.metrics.InMemoryStatsReporter;
import com.uber.jaeger.metrics.Metrics;
import com.uber.jaeger.metrics.MetricsFactory;
import com.uber.jaeger.metrics.StatsFactory;
import com.uber.jaeger.metrics.StatsFactoryImpl;
import com.uber.jaeger.propagation.Injector;
import com.uber.jaeger.reporters.InMemoryReporter;
Expand All @@ -41,14 +44,14 @@
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", new InMemoryReporter(), new ConstSampler(true))
.withStatsReporter(metricsReporter)
.withMetrics(new Metrics(metricsFactory))
.build();
}

Expand All @@ -64,12 +67,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
Expand All @@ -79,7 +80,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();
Expand Down Expand Up @@ -118,18 +119,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
Expand Down Expand Up @@ -173,12 +171,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"));
}
}
Loading

0 comments on commit 7871952

Please sign in to comment.