Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Otel metrics support #39032

Merged
merged 1 commit into from
Jul 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.exporter.otlp.internal.OtlpMetricExporterProvider;
import io.opentelemetry.exporter.otlp.internal.OtlpSpanExporterProvider;
import io.opentelemetry.instrumentation.annotations.AddingSpanAttributes;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurablePropagatorProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider;
import io.quarkus.agroal.spi.JdbcDataSourceBuildItem;
Expand Down Expand Up @@ -105,6 +107,7 @@ AdditionalBeanBuildItem ensureProducerIsRetained() {
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.ResourceCustomizer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.SamplerCustomizer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.TracerProviderCustomizer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.MetricProviderCustomizer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.TextMapPropagatorCustomizers.class)
.build();
}
Expand Down Expand Up @@ -144,11 +147,12 @@ void handleServices(OTelBuildConfig config,
BuildProducer<RemovedResourceBuildItem> removedResources,
BuildProducer<RuntimeReinitializedClassBuildItem> runtimeReinitialized) throws IOException {

List<String> spanExporterProviders = ServiceUtil.classNamesNamedIn(
final List<String> spanExporterProviders = ServiceUtil.classNamesNamedIn(
Thread.currentThread().getContextClassLoader(),
SPI_ROOT + ConfigurableSpanExporterProvider.class.getName())
.stream()
.filter(p -> !OtlpSpanExporterProvider.class.getName().equals(p)).collect(toList()); // filter out OtlpSpanExporterProvider since it depends on OkHttp
.filter(p -> !OtlpSpanExporterProvider.class.getName().equals(p))
.collect(toList()); // filter out OtlpSpanExporterProvider since it depends on OkHttp
if (!spanExporterProviders.isEmpty()) {
services.produce(
new ServiceProviderBuildItem(ConfigurableSpanExporterProvider.class.getName(), spanExporterProviders));
Expand All @@ -160,8 +164,26 @@ void handleServices(OTelBuildConfig config,
Set.of("META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")));
}

final List<String> metricExporterProviders = ServiceUtil.classNamesNamedIn(
Thread.currentThread().getContextClassLoader(),
SPI_ROOT + ConfigurableMetricExporterProvider.class.getName())
.stream()
.filter(p -> !OtlpMetricExporterProvider.class.getName().equals(p))
.collect(toList()); // filter out OtlpMetricExporterProvider since it depends on OkHttp
if (!metricExporterProviders.isEmpty()) {
services.produce(
new ServiceProviderBuildItem(ConfigurableMetricExporterProvider.class.getName(), metricExporterProviders));
}
if (config.metrics().exporter().stream().noneMatch(ExporterType.Constants.OTLP_VALUE::equals)) {
removedResources.produce(new RemovedResourceBuildItem(
ArtifactKey.fromString("io.opentelemetry:opentelemetry-exporter-otlp"),
Set.of("META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider")));
}

runtimeReinitialized.produce(
new RuntimeReinitializedClassBuildItem("io.opentelemetry.sdk.autoconfigure.TracerProviderConfiguration"));
runtimeReinitialized.produce(
new RuntimeReinitializedClassBuildItem("io.opentelemetry.sdk.autoconfigure.MeterProviderConfiguration"));

services.produce(ServiceProviderBuildItem.allProvidersFromClassPath(
ConfigurableSamplerProvider.class.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@
import org.jboss.jandex.ParameterizedType;
import org.jboss.jandex.Type;

import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.quarkus.arc.deployment.BeanDiscoveryFinishedBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.annotations.*;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.config.build.exporter.OtlpExporterBuildConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.exporter.OtlpExporterRuntimeConfig;
import io.quarkus.opentelemetry.runtime.exporter.otlp.LateBoundBatchSpanProcessor;
import io.quarkus.opentelemetry.runtime.exporter.otlp.OTelExporterRecorder;
import io.quarkus.opentelemetry.runtime.exporter.otlp.tracing.LateBoundBatchSpanProcessor;
import io.quarkus.tls.TlsConfigurationRegistry;
import io.quarkus.tls.TlsRegistryBuildItem;
import io.quarkus.vertx.core.deployment.CoreVertxBuildItem;

@BuildSteps(onlyIf = OtlpExporterProcessor.OtlpExporterEnabled.class)
@BuildSteps
public class OtlpExporterProcessor {

static class OtlpExporterEnabled implements BooleanSupplier {
private static final DotName METRIC_EXPORTER = DotName.createSimple(MetricExporter.class.getName());

static class OtlpTracingExporterEnabled implements BooleanSupplier {
OtlpExporterBuildConfig exportBuildConfig;
OTelBuildConfig otelBuildConfig;

Expand All @@ -43,8 +47,20 @@ public boolean getAsBoolean() {
}
}

static class OtlpMetricsExporterEnabled implements BooleanSupplier {
OtlpExporterBuildConfig exportBuildConfig;
OTelBuildConfig otelBuildConfig;

public boolean getAsBoolean() {
return otelBuildConfig.enabled() &&
otelBuildConfig.metrics().enabled().orElse(Boolean.TRUE) &&
otelBuildConfig.metrics().exporter().contains(CDI_VALUE) &&
exportBuildConfig.enabled();
}
}

@SuppressWarnings("deprecation")
@BuildStep
@BuildStep(onlyIf = OtlpExporterProcessor.OtlpTracingExporterEnabled.class)
@Record(ExecutionTime.RUNTIME_INIT)
@Consume(TlsRegistryBuildItem.class)
void createBatchSpanProcessor(OTelExporterRecorder recorder,
Expand All @@ -70,4 +86,41 @@ void createBatchSpanProcessor(OTelExporterRecorder recorder,
vertxBuildItem.getVertx()))
.done());
}

@BuildStep(onlyIf = OtlpMetricsExporterEnabled.class)
@Record(ExecutionTime.RUNTIME_INIT)
@Consume(TlsRegistryBuildItem.class)
void createMetricsExporterProcessor(
BeanDiscoveryFinishedBuildItem beanDiscovery,
OTelExporterRecorder recorder,
List<ExternalOtelExporterBuildItem> externalOtelExporterBuildItem,
OTelRuntimeConfig otelRuntimeConfig,
OtlpExporterRuntimeConfig exporterRuntimeConfig,
CoreVertxBuildItem vertxBuildItem,
BuildProducer<SyntheticBeanBuildItem> syntheticBeanBuildItemBuildProducer) {

if (!externalOtelExporterBuildItem.isEmpty()) {
// if there is an external exporter, we don't want to create the default one.
// External exporter also use synthetic beans. However, synthetic beans don't show in the BeanDiscoveryFinishedBuildItem
return;
}

if (!beanDiscovery.beanStream().withBeanType(METRIC_EXPORTER).isEmpty()) {
// if there is a MetricExporter bean impl around, we don't want to create the default one
return;
}

syntheticBeanBuildItemBuildProducer.produce(SyntheticBeanBuildItem
.configure(MetricExporter.class)
.types(MetricExporter.class)
.setRuntimeInit()
.scope(Singleton.class)
.unremovable()
.addInjectionPoint(ParameterizedType.create(DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(MetricExporter.class.getName())) }, null))
.addInjectionPoint(ClassType.create(DotName.createSimple(TlsConfigurationRegistry.class)))
.createWith(recorder.createMetricExporter(otelRuntimeConfig, exporterRuntimeConfig,
vertxBuildItem.getVertx()))
.done());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package io.quarkus.opentelemetry.deployment.metric;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Function;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;

import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.metrics.cdi.MetricsProducer;

@BuildSteps(onlyIf = MetricProcessor.MetricEnabled.class)
public class MetricProcessor {
private static final DotName METRIC_EXPORTER = DotName.createSimple(MetricExporter.class.getName());
private static final DotName METRIC_READER = DotName.createSimple(MetricReader.class.getName());
private static final DotName METRIC_PROCESSOR = DotName.createSimple(MetricProcessor.class.getName());

@BuildStep
UnremovableBeanBuildItem ensureProducersAreRetained(
CombinedIndexBuildItem indexBuildItem,
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {

additionalBeans.produce(AdditionalBeanBuildItem.builder()
.setUnremovable()
.addBeanClass(MetricsProducer.class)
.build());

IndexView index = indexBuildItem.getIndex();

// Find all known SpanExporters and SpanProcessors
Collection<String> knownClasses = new HashSet<>();
knownClasses.add(METRIC_EXPORTER.toString());
index.getAllKnownImplementors(METRIC_EXPORTER)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));

knownClasses.add(METRIC_READER.toString());
index.getAllKnownImplementors(METRIC_READER)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));

knownClasses.add(METRIC_PROCESSOR.toString());
index.getAllKnownImplementors(METRIC_PROCESSOR)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));

Set<String> retainProducers = new HashSet<>();

for (AnnotationInstance annotation : index.getAnnotations(DotNames.PRODUCES)) {
AnnotationTarget target = annotation.target();
switch (target.kind()) {
case METHOD:
MethodInfo method = target.asMethod();
String returnType = method.returnType().name().toString();
if (knownClasses.contains(returnType)) {
retainProducers.add(method.declaringClass().name().toString());
}
break;
case FIELD:
FieldInfo field = target.asField();
String fieldType = field.type().name().toString();
if (knownClasses.contains(fieldType)) {
retainProducers.add(field.declaringClass().name().toString());
}
break;
default:
break;
}
}

return new UnremovableBeanBuildItem(new UnremovableBeanBuildItem.BeanClassNamesExclusion(retainProducers));
}

public static class MetricEnabled implements BooleanSupplier {
OTelBuildConfig otelBuildConfig;

public boolean getAsBoolean() {
return otelBuildConfig.metrics().enabled()
.map(new Function<Boolean, Boolean>() {
@Override
public Boolean apply(Boolean enabled) {
return otelBuildConfig.enabled() && enabled;
}
})
.orElseGet(() -> otelBuildConfig.enabled());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public class TracerEnabled implements BooleanSupplier {
OTelBuildConfig otelConfig;

public boolean getAsBoolean() {
return otelConfig.traces().enabled().map(new Function<Boolean, Boolean>() {
@Override
public Boolean apply(Boolean tracerEnabled) {
return otelConfig.enabled() && tracerEnabled;
}
})
return otelConfig.traces().enabled()
.map(new Function<Boolean, Boolean>() {
@Override
public Boolean apply(Boolean tracerEnabled) {
return otelConfig.enabled() && tracerEnabled;
}
})
.orElseGet(() -> otelConfig.enabled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.quarkus.opentelemetry.deployment.common.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider;
import io.quarkus.opentelemetry.deployment.common.TracerRouter;
import io.quarkus.opentelemetry.deployment.traces.TracerRouterUT;
import io.quarkus.test.ContinuousTestingTestUtils;
import io.quarkus.test.ContinuousTestingTestUtils.TestStatus;
import io.quarkus.test.QuarkusDevModeTest;
Expand All @@ -23,7 +24,8 @@ public class OpenTelemetryContinuousTestingTest {
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")
.add(new StringAsset(ContinuousTestingTestUtils.appProperties(
"quarkus.otel.traces.exporter=test-span-exporter")),
"quarkus.otel.traces.exporter=test-span-exporter",
"quarkus.otel.metrics.exporter=none")),
"application.properties"))
.setTestArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(TracerRouterUT.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ public class OpenTelemetryDestroyerTest {
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")
.add(new StringAsset(
"quarkus.otel.traces.exporter=test-span-exporter\n" +
"quarkus.otel.experimental.shutdown-wait-time=PT60S\n"),
"""
quarkus.otel.traces.exporter=test-span-exporter
quarkus.otel.metrics.exporter=none
quarkus.otel.experimental.shutdown-wait-time=PT60S
"""),
"application.properties"));

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class OpenTelemetryDevModeTest {
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")
.add(new StringAsset(ContinuousTestingTestUtils.appProperties(
"quarkus.otel.traces.exporter=test-span-exporter")), "application.properties"));
"quarkus.otel.traces.exporter=test-span-exporter",
"quarkus.otel.metrics.exporter=none")), "application.properties"));

@Test
void testDevMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ public class OpenTelemetryDevServicesDatasourcesTest {
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")
.add(new StringAsset(
"quarkus.datasource.db-kind=h2\n" +
"quarkus.datasource.jdbc.telemetry=true\n" +
"quarkus.otel.traces.exporter=test-span-exporter\n" +
"quarkus.otel.bsp.export.timeout=1s\n" +
"quarkus.otel.bsp.schedule.delay=50\n"),
"""
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.telemetry=true
quarkus.otel.traces.exporter=test-span-exporter
quarkus.otel.metrics.exporter=none
quarkus.otel.bsp.export.timeout=1s
quarkus.otel.bsp.schedule.delay=50
"""),
"application.properties"));

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.OpenTelemetry;
import io.quarkus.opentelemetry.runtime.exporter.otlp.LateBoundBatchSpanProcessor;
import io.quarkus.opentelemetry.runtime.exporter.otlp.tracing.LateBoundBatchSpanProcessor;
import io.quarkus.test.QuarkusUnitTest;

@Disabled("Not implemented")
Expand Down
Loading