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

Load metrics factory service from classpath if available #433

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions jaeger-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ More information about using the `TracerResolver` can be found [here](../jaeger-
#### Reporting internal metrics via Micrometer

The Jaeger Java Client collects internal metrics and is able to report them via [Micrometer](http://micrometer.io).
To accomplish that, include the artifact `io.jaegertracing:jaeger-micrometer` as a dependency to your project and use
`MicrometerMetricsFactory` like this:
To accomplish that, include the artifact `io.jaegertracing:jaeger-micrometer` as a dependency to your project. The
integration should be done automatically via the service loader.

If you prefer a manual configuration, it can be done with a code similar to this:

```java
MicrometerMetricsFactory metricsReporter = new MicrometerMetricsFactory();
Expand Down
18 changes: 17 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 @@ -170,6 +172,7 @@ public Configuration(String serviceName) {
this.serviceName = Tracer.Builder.checkValidServiceName(serviceName);
}


/**
* @return Configuration object from environmental variables
*/
Expand All @@ -192,7 +195,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 +226,19 @@ public synchronized void closeTracer() {
}
}

private MetricsFactory loadMetricsFactory() {
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
10 changes: 10 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 @@ -432,6 +433,15 @@ 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());
}

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