Add explicit names to metrics/trace SPI factories (#1513)
* Add explicit names to metrics/trace SPI factories Signed-off-by: Pavol Loffay <p.loffay@gmail.com> * Fix tests Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
This commit is contained in:
parent
f3559de4a3
commit
c931b1b85a
|
@ -9,7 +9,9 @@ import com.google.auto.service.AutoService;
|
|||
import io.opentelemetry.exporters.jaeger.JaegerGrpcSpanExporter;
|
||||
import io.opentelemetry.javaagent.spi.exporter.SpanExporterFactory;
|
||||
import io.opentelemetry.sdk.trace.export.SpanExporter;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
@AutoService(SpanExporterFactory.class)
|
||||
public class JaegerExporterFactory implements SpanExporterFactory {
|
||||
|
@ -18,4 +20,9 @@ public class JaegerExporterFactory implements SpanExporterFactory {
|
|||
public SpanExporter fromConfig(Properties config) {
|
||||
return JaegerGrpcSpanExporter.builder().readProperties(config).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNames() {
|
||||
return Collections.singleton("jaeger");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,9 @@ package io.opentelemetry.javaagent.exporters.logging;
|
|||
import com.google.auto.service.AutoService;
|
||||
import io.opentelemetry.javaagent.spi.exporter.SpanExporterFactory;
|
||||
import io.opentelemetry.sdk.trace.export.SpanExporter;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
@AutoService(SpanExporterFactory.class)
|
||||
public class LoggingExporterFactory implements SpanExporterFactory {
|
||||
|
@ -16,4 +18,9 @@ public class LoggingExporterFactory implements SpanExporterFactory {
|
|||
public SpanExporter fromConfig(Properties config) {
|
||||
return new LoggingExporter(config.getProperty("otel.logging.prefix", "Logging Exporter:"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNames() {
|
||||
return Collections.singleton("logging");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,10 @@ import com.google.auto.service.AutoService;
|
|||
import io.opentelemetry.exporters.otlp.OtlpGrpcMetricExporter;
|
||||
import io.opentelemetry.javaagent.spi.exporter.MetricExporterFactory;
|
||||
import io.opentelemetry.sdk.metrics.export.MetricExporter;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
@AutoService(MetricExporterFactory.class)
|
||||
public class OtlpMetricExporterFactory implements MetricExporterFactory {
|
||||
|
@ -18,4 +21,9 @@ public class OtlpMetricExporterFactory implements MetricExporterFactory {
|
|||
public MetricExporter fromConfig(Properties config) {
|
||||
return OtlpGrpcMetricExporter.builder().readProperties(config).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNames() {
|
||||
return new HashSet<>(Arrays.asList("otlp", "otlp_metric"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,10 @@ import com.google.auto.service.AutoService;
|
|||
import io.opentelemetry.exporters.otlp.OtlpGrpcSpanExporter;
|
||||
import io.opentelemetry.javaagent.spi.exporter.SpanExporterFactory;
|
||||
import io.opentelemetry.sdk.trace.export.SpanExporter;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
@AutoService(SpanExporterFactory.class)
|
||||
public class OtlpSpanExporterFactory implements SpanExporterFactory {
|
||||
|
@ -18,4 +21,9 @@ public class OtlpSpanExporterFactory implements SpanExporterFactory {
|
|||
public SpanExporter fromConfig(Properties config) {
|
||||
return OtlpGrpcSpanExporter.builder().readProperties(config).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNames() {
|
||||
return new HashSet<>(Arrays.asList("otlp", "otlp_span"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,9 @@ import io.opentelemetry.javaagent.spi.exporter.MetricServer;
|
|||
import io.opentelemetry.sdk.metrics.export.MetricProducer;
|
||||
import io.prometheus.client.exporter.HTTPServer;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -44,4 +46,9 @@ public class PrometheusMetricServer implements MetricServer {
|
|||
log.error("Failed to create Prometheus server", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNames() {
|
||||
return Collections.singleton("prometheus");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,9 @@ import com.google.auto.service.AutoService;
|
|||
import io.opentelemetry.exporters.zipkin.ZipkinSpanExporter;
|
||||
import io.opentelemetry.javaagent.spi.exporter.SpanExporterFactory;
|
||||
import io.opentelemetry.sdk.trace.export.SpanExporter;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
@AutoService(SpanExporterFactory.class)
|
||||
public class ZipkinExporterFactory implements SpanExporterFactory {
|
||||
|
@ -18,4 +20,9 @@ public class ZipkinExporterFactory implements SpanExporterFactory {
|
|||
public SpanExporter fromConfig(Properties config) {
|
||||
return ZipkinSpanExporter.builder().readProperties(config).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNames() {
|
||||
return Collections.singleton("zipkin");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ package io.opentelemetry.javaagent.spi.exporter;
|
|||
|
||||
import io.opentelemetry.sdk.metrics.export.MetricExporter;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A {@link MetricExporterFactory} acts as the bootstrap for a {@link MetricExporter}
|
||||
|
@ -21,4 +22,14 @@ public interface MetricExporterFactory {
|
|||
* @return An implementation of a {@link MetricExporter}
|
||||
*/
|
||||
MetricExporter fromConfig(Properties config);
|
||||
|
||||
/**
|
||||
* Returns names of metric exporters supported by this factory.
|
||||
*
|
||||
* <p>Multiple names are useful for enabling a pair of span and metric exporters using the same
|
||||
* name, while still having separate names for enabling them individually.
|
||||
*
|
||||
* @return The exporter names supported by this factory
|
||||
*/
|
||||
Set<String> getNames();
|
||||
}
|
||||
|
|
|
@ -7,11 +7,15 @@ package io.opentelemetry.javaagent.spi.exporter;
|
|||
|
||||
import io.opentelemetry.sdk.metrics.export.MetricProducer;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A {@link MetricServer} acts as the bootstrap for metric exporters that use {@link MetricProducer}
|
||||
* to consume the metrics.
|
||||
*
|
||||
* <p>Multiple names are useful for enabling a pair of span and metric exporters using the same
|
||||
* name, while still having separate names for enabling them individually.
|
||||
*
|
||||
* <p>Implementation of {@link MetricServer} must be registered through the Java SPI framework.
|
||||
*/
|
||||
public interface MetricServer {
|
||||
|
@ -23,4 +27,11 @@ public interface MetricServer {
|
|||
* @param config The configuration
|
||||
*/
|
||||
void start(MetricProducer producer, Properties config);
|
||||
|
||||
/**
|
||||
* Returns names of metric servers supported by this factory.
|
||||
*
|
||||
* @return The metric server names supported by this factory
|
||||
*/
|
||||
Set<String> getNames();
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ package io.opentelemetry.javaagent.spi.exporter;
|
|||
|
||||
import io.opentelemetry.sdk.trace.export.SpanExporter;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A {@link SpanExporterFactory} acts as the bootstrap for a {@link SpanExporter} implementation. An
|
||||
|
@ -21,4 +22,14 @@ public interface SpanExporterFactory {
|
|||
* @return An implementation of a {@link SpanExporter}
|
||||
*/
|
||||
SpanExporter fromConfig(Properties config);
|
||||
|
||||
/**
|
||||
* Returns names of span exporters supported by this factory.
|
||||
*
|
||||
* <p>Multiple names are useful for enabling a pair of span and metric exporters using the same
|
||||
* name, while still having separate names for enabling them individually.
|
||||
*
|
||||
* @return The exporter names supported by this factory
|
||||
*/
|
||||
Set<String> getNames();
|
||||
}
|
||||
|
|
|
@ -90,11 +90,7 @@ public class TracerInstaller {
|
|||
ServiceLoader.load(MetricExporterFactory.class, TracerInstaller.class.getClassLoader());
|
||||
|
||||
for (MetricExporterFactory metricExporterFactory : serviceLoader) {
|
||||
if (metricExporterFactory
|
||||
.getClass()
|
||||
.getSimpleName()
|
||||
.toLowerCase()
|
||||
.startsWith(exporterName(exporterName).toLowerCase())) {
|
||||
if (metricExporterFactory.getNames().contains(exporterName)) {
|
||||
return metricExporterFactory;
|
||||
}
|
||||
}
|
||||
|
@ -106,11 +102,7 @@ public class TracerInstaller {
|
|||
ServiceLoader.load(MetricServer.class, TracerInstaller.class.getClassLoader());
|
||||
|
||||
for (MetricServer metricServer : serviceLoader) {
|
||||
if (metricServer
|
||||
.getClass()
|
||||
.getSimpleName()
|
||||
.toLowerCase()
|
||||
.startsWith(exporterName(exporterName).toLowerCase())) {
|
||||
if (metricServer.getNames().contains(exporterName)) {
|
||||
return metricServer;
|
||||
}
|
||||
}
|
||||
|
@ -122,21 +114,13 @@ public class TracerInstaller {
|
|||
ServiceLoader.load(SpanExporterFactory.class, TracerInstaller.class.getClassLoader());
|
||||
|
||||
for (SpanExporterFactory spanExporterFactory : serviceLoader) {
|
||||
if (spanExporterFactory
|
||||
.getClass()
|
||||
.getSimpleName()
|
||||
.toLowerCase()
|
||||
.startsWith(exporterName(exporterName).toLowerCase())) {
|
||||
if (spanExporterFactory.getNames().contains(exporterName)) {
|
||||
return spanExporterFactory;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String exporterName(String exporterName) {
|
||||
return exporterName.replace("otlp_span", "otlpspan").replace("otlp_metric", "otlpmetric");
|
||||
}
|
||||
|
||||
private static synchronized void installExportersFromJar(String exporterJar, Properties config) {
|
||||
URL url;
|
||||
try {
|
||||
|
|
|
@ -47,6 +47,11 @@ class ExporterClassLoaderTest extends Specification {
|
|||
MetricExporter fromConfig(Properties config) {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
Set<String> getNames() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
static class MetricExporterFactoryChild implements MetricExporterFactory {
|
||||
|
@ -55,6 +60,11 @@ class ExporterClassLoaderTest extends Specification {
|
|||
MetricExporter fromConfig(Properties config) {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
Set<String> getNames() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
static class SpanExporterFactoryParent implements SpanExporterFactory {
|
||||
|
@ -63,6 +73,11 @@ class ExporterClassLoaderTest extends Specification {
|
|||
SpanExporter fromConfig(Properties config) {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
Set<String> getNames() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
static class SpanExporterFactoryChild implements SpanExporterFactory {
|
||||
|
@ -71,6 +86,11 @@ class ExporterClassLoaderTest extends Specification {
|
|||
SpanExporter fromConfig(Properties config) {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
Set<String> getNames() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
static URL createJarWithClasses(final Class<?>... classes)
|
||||
|
|
Loading…
Reference in New Issue