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 11a0895
Show file tree
Hide file tree
Showing 16 changed files with 529 additions and 80 deletions.
32 changes: 23 additions & 9 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 @@ -168,7 +168,7 @@ public enum Propagation {
/**
* A interface that wraps an underlying metrics generator in order to report Jaeger's metrics.
*/
private StatsFactory statsFactory;
private MetricsFactory metricsFactory;

/**
* lazy singleton Tracer initialized in getTracer() method.
Expand Down Expand Up @@ -212,7 +212,7 @@ private Configuration(
}
this.codecConfig = codecConfig;

statsFactory = new StatsFactoryImpl(new NullStatsReporter());
metricsFactory = new NoopMetricsFactory();
}

public static Configuration fromEnv() {
Expand All @@ -224,7 +224,7 @@ public static Configuration fromEnv() {
}

public Tracer.Builder getTracerBuilder() {
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 All @@ -251,16 +251,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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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) {
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
* @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,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
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 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<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.
*
* @deprecated Use {@link NoopMetricsFactory} instead
*/
@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 @@ -14,12 +14,9 @@

package com.uber.jaeger.metrics;

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 Use {@link MetricsFactory} instead
*/
@Deprecated
public interface StatsFactory extends MetricsFactory {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

import java.util.Map;

/**
* @deprecated Use {@link MetricsFactory} instead
*/
@Deprecated
public interface StatsReporter {

void incCounter(String name, long delta, Map<String, String> tags);
Expand Down
Loading

0 comments on commit 11a0895

Please sign in to comment.