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 27, 2018
1 parent 581e20f commit 74a82e6
Show file tree
Hide file tree
Showing 24 changed files with 655 additions and 155 deletions.
10 changes: 5 additions & 5 deletions jaeger-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 25 additions & 10 deletions jaeger-core/src/main/java/com/uber/jaeger/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,7 +165,7 @@ public enum Propagation {
private SamplerConfiguration samplerConfig;
private ReporterConfiguration reporterConfig;
private CodecConfiguration codecConfig;
private StatsFactory statsFactory;
private MetricsFactory metricsFactory;
private Map<String, String> tracerTags;

/**
Expand Down Expand Up @@ -201,6 +201,7 @@ private Configuration(
this.samplerConfig = samplerConfig;
this.reporterConfig = reporterConfig;
this.codecConfig = codecConfig;
this.metricsFactory = new NoopMetricsFactory();
}

/**
Expand All @@ -224,10 +225,10 @@ public Tracer.Builder getTracerBuilder() {
if (codecConfig == null) {
codecConfig = new CodecConfiguration(Collections.<Format<?>, List<Codec<TextMap>>>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)
Expand Down Expand Up @@ -257,16 +258,30 @@ public synchronized void closeTracer() {
}

/**
* @param statsFactory the factory
* @deprecated Use {@link #setStatsFactory(StatsFactory)} instead
* @see #setMetricsFactory(MetricsFactory)
* @param statsFactory the StatsFactory to use on the Tracer to be built
* @deprecated Use {@link #setMetricsFactory(MetricsFactory)} instead
*/
@Deprecated
public void setStatsFactor(StatsFactory statsFactory) {
this.statsFactory = statsFactory;
this.metricsFactory = statsFactory;
}

/**
* @see #setMetricsFactory(MetricsFactory)
* @param statsFactory the StatsFactory to use on the Tracer to be built
* @deprecated Use {@link #setMetricsFactory(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 setMetricsFactory(MetricsFactory metricsFactory) {
this.metricsFactory = metricsFactory;
}

public Configuration withReporter(ReporterConfiguration reporterConfig) {
Expand Down
21 changes: 17 additions & 4 deletions jaeger-core/src/main/java/com/uber/jaeger/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, Object> tags = new HashMap<String, Object>();
Expand Down Expand Up @@ -501,11 +500,25 @@ public <T> Builder registerExtractor(Format<T> format, Extractor<T> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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) {
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) {
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
* @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<String, String> 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<String, String> 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<String, String> tags) {
return getValue(timers, name, tags);
}

private long getValue(Map<String, AtomicLong> collection, String name, String tags) {
if (null == tags || tags.isEmpty()) {
return getValue(collection, name);
}

String[] entries = tags.split(",");
Map<String, String> tagsAsMap = new HashMap<String, String>(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<String, AtomicLong> collection, String name, Map<String, String> tags) {
return getValue(collection, Metrics.addTagsToMetricName(name, tags));
}

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,10 @@
import java.util.HashMap;
import java.util.Map;

/**
* @deprecated Use {@link InMemoryMetricsFactory} instead
*/
@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
18 changes: 16 additions & 2 deletions jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -90,6 +100,10 @@ public static String addTagsToMetricName(String name, Map<String, String> 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));
}
Expand Down
Loading

0 comments on commit 74a82e6

Please sign in to comment.