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

Commit

Permalink
Load metrics factory service from classpath if available
Browse files Browse the repository at this point in the history
Closes #339

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed May 28, 2018
1 parent 3ffaf9d commit 43eef19
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
25 changes: 24 additions & 1 deletion jaeger-core/src/main/java/io/jaegertracing/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -135,6 +137,11 @@ public class Configuration {
*/
public static final String JAEGER_PROPAGATION = JAEGER_PREFIX + "PROPAGATION";

/**
* Whether to skip loading a metrics factory from the classpath. Boolean. Optional.
*/
public static final String JAEGER_SKIP_METRICS_FACTORY_LOADER = JAEGER_PREFIX + "SKIP_METRICS_FACTORY_LOADER";

/**
* The supported trace context propagation formats.
*/
Expand Down Expand Up @@ -170,6 +177,7 @@ public Configuration(String serviceName) {
this.serviceName = Tracer.Builder.checkValidServiceName(serviceName);
}


/**
* @return Configuration object from environmental variables
*/
Expand All @@ -192,7 +200,7 @@ public Tracer.Builder getTracerBuilder() {
codecConfig = new CodecConfiguration();
}
if (metricsFactory == null) {
metricsFactory = new NoopMetricsFactory();
metricsFactory = loadMetricsFactory();
}
Metrics metrics = new Metrics(metricsFactory);
Reporter reporter = reporterConfig.getReporter(metrics);
Expand Down Expand Up @@ -223,6 +231,21 @@ public synchronized void closeTracer() {
}
}

private MetricsFactory loadMetricsFactory() {
if (!getPropertyAsBool(JAEGER_SKIP_METRICS_FACTORY_LOADER)) {
ServiceLoader<MetricsFactory> loader = ServiceLoader.load(MetricsFactory.class);

Iterator<MetricsFactory> iterator = loader.iterator();
if (iterator.hasNext()) {
MetricsFactory metricsFactory = iterator.next();
log.info("Found a Metrics Factory service: {}", metricsFactory.getClass());
return metricsFactory;
}
}

return new NoopMetricsFactory();
}

/**
* @param metricsFactory the MetricsFactory to use on the Tracer to be built
*/
Expand Down
35 changes: 35 additions & 0 deletions jaeger-core/src/test/java/io/jaegertracing/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.jaegertracing.Configuration.SamplerConfiguration;
import io.jaegertracing.metrics.InMemoryMetricsFactory;
import io.jaegertracing.metrics.Metrics;
import io.jaegertracing.metrics.MockMetricsFactory;
import io.jaegertracing.propagation.Codec;
import io.jaegertracing.samplers.ConstSampler;
import io.jaegertracing.samplers.ProbabilisticSampler;
Expand Down Expand Up @@ -73,6 +74,7 @@ public void clearProperties() throws NoSuchFieldException, IllegalAccessExceptio
System.clearProperty(Configuration.JAEGER_USER);
System.clearProperty(Configuration.JAEGER_PASSWORD);
System.clearProperty(Configuration.JAEGER_PROPAGATION);
System.clearProperty(Configuration.JAEGER_SKIP_METRICS_FACTORY_LOADER);

System.clearProperty(TEST_PROPERTY);

Expand Down Expand Up @@ -432,6 +434,39 @@ private <C> void assertInjectExtract(io.opentracing.Tracer tracer, Format<C> for
assertEquals(contextToInject.getSpanId(), extractedContext.getSpanId());
}

@Test
public void testMetricsFactoryFromServiceLoader() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");

int instances = MockMetricsFactory.instances.size();
Configuration.fromEnv().getTracer();
assertEquals(++instances, MockMetricsFactory.instances.size());
}

@Test
public void testSkipMetricsFactoryFromServiceLoader() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");
System.setProperty(Configuration.JAEGER_SKIP_METRICS_FACTORY_LOADER, "true");

int instances = MockMetricsFactory.instances.size();
Configuration.fromEnv().getTracer();
System.clearProperty(Configuration.JAEGER_SKIP_METRICS_FACTORY_LOADER);

assertEquals(instances, MockMetricsFactory.instances.size());
}

@Test
public void testDoNotSkipMetricsFactoryFromServiceLoader() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");
System.setProperty(Configuration.JAEGER_SKIP_METRICS_FACTORY_LOADER, "false");

int instances = MockMetricsFactory.instances.size();
Configuration.fromEnv().getTracer();
System.clearProperty(Configuration.JAEGER_SKIP_METRICS_FACTORY_LOADER);

assertEquals(++instances, MockMetricsFactory.instances.size());
}

static class TestTextMap implements TextMap {

private Map<String,String> values = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2018, 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 io.jaegertracing.metrics;

import java.util.ArrayList;
import java.util.List;

/**
* This metrics factory serves only to get loaded via a service loader, while still keeping compatibility with tests
* that make use of the {@link InMemoryMetricsFactory}
*/
public class MockMetricsFactory extends InMemoryMetricsFactory {
public static final List<MockMetricsFactory> instances = new ArrayList<>();

public MockMetricsFactory() {
instances.add(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.jaegertracing.metrics.MockMetricsFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.jaegertracing.micrometer.MicrometerMetricsFactory
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ public void validateMetricCounts() throws InterruptedException {
assertEquals("Wrong number of traces", 10.0, traces, assertDelta);
}

@Test
public void testServiceLoader() {
System.setProperty(Configuration.JAEGER_SAMPLER_TYPE, ConstSampler.TYPE);
System.setProperty(Configuration.JAEGER_SAMPLER_PARAM, "1");
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");

// the fact that there's a service on the classpath is enough to get it loaded, unless we have an env var
// saying to skip it
final Configuration configuration = Configuration.fromEnv();

System.clearProperty(Configuration.JAEGER_SERVICE_NAME);
System.clearProperty(Configuration.JAEGER_SAMPLER_TYPE);
System.clearProperty(Configuration.JAEGER_SAMPLER_PARAM);

PrometheusMeterRegistry registry = (PrometheusMeterRegistry) io.micrometer.core.instrument.Metrics.globalRegistry
.getRegistries()
.iterator()
.next();

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

private void createSomeSpans(Tracer tracer) {
for (int i = 0; i < 10; i++) {
Span span = tracer.buildSpan("metricstest")
Expand Down

0 comments on commit 43eef19

Please sign in to comment.