Skip to content

Disable logger exporter metrics if metrics disabled #47600

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,5 +1,6 @@
package io.quarkus.opentelemetry.deployment;

import static io.quarkus.bootstrap.classloading.QuarkusClassLoader.isClassPresentAtRuntime;
import static io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem.SPI_ROOT;
import static io.quarkus.opentelemetry.runtime.OpenTelemetryRecorder.OPEN_TELEMETRY_DRIVER;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -58,13 +59,11 @@
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RemovedResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.builditem.nativeimage.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use star imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will fix

import io.quarkus.deployment.util.ServiceUtil;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.opentelemetry.OpenTelemetryDestroyer;
import io.quarkus.opentelemetry.deployment.metric.MetricProcessor;
import io.quarkus.opentelemetry.runtime.AutoConfiguredOpenTelemetrySdkBuilderCustomizer;
import io.quarkus.opentelemetry.runtime.DelayedAttributes;
import io.quarkus.opentelemetry.runtime.OpenTelemetryRecorder;
Expand Down Expand Up @@ -94,6 +93,18 @@ public boolean test(AnnotationInstance annotationInstance) {
private static final DotName ADD_SPAN_ATTRIBUTES_INTERCEPTOR = DotName
.createSimple(AddingSpanAttributesInterceptor.class.getName());

@BuildStep(onlyIfNot = MetricProcessor.MetricEnabled.class)
void registerForReflection(BuildProducer<ReflectiveMethodBuildItem> reflectiveItem) {
if (isClassPresentAtRuntime(
"io.opentelemetry.exporter.logging.LoggingMetricExporter")) {
reflectiveItem.produce(new ReflectiveMethodBuildItem(
"Used by OpenTelemetry Export Logging",
false,
"io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil",
"addMeterConfiguratorCondition"));
}
}

@BuildStep
AdditionalBeanBuildItem ensureProducerIsRetained() {
return AdditionalBeanBuildItem.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.opentelemetry.runtime;

import static io.opentelemetry.sdk.internal.ScopeConfiguratorBuilder.nameEquals;
import static java.lang.Boolean.TRUE;
import static java.util.Collections.emptyList;

Expand All @@ -25,6 +26,8 @@
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.internal.MeterConfig;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.IdGenerator;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
Expand Down Expand Up @@ -258,20 +261,34 @@ public MetricProviderCustomizer(OTelBuildConfig oTelBuildConfig,

@Override
public void customize(AutoConfiguredOpenTelemetrySdkBuilder builder) {
if (oTelBuildConfig.metrics().enabled().orElse(TRUE)) {
builder.addMeterProviderCustomizer(
new BiFunction<SdkMeterProviderBuilder, ConfigProperties, SdkMeterProviderBuilder>() {
@Override
public SdkMeterProviderBuilder apply(SdkMeterProviderBuilder metricProvider,
ConfigProperties configProperties) {
builder.addMeterProviderCustomizer(
new BiFunction<SdkMeterProviderBuilder, ConfigProperties, SdkMeterProviderBuilder>() {
@Override
public SdkMeterProviderBuilder apply(SdkMeterProviderBuilder meterProviderBuilder,
ConfigProperties configProperties) {

if (oTelBuildConfig.metrics().enabled().orElse(TRUE)) {

if (clock.isUnsatisfied()) {
throw new IllegalStateException("No Clock bean found");
}
metricProvider.setClock(clock.get());
return metricProvider;
meterProviderBuilder.setClock(clock.get());
} else {
// disable batch exporter metrics if metrics disabled
// In the future we can inject scopes here.
Set<String> dropInstrumentationScopes = Set.of(
"io.opentelemetry.sdk.trace",
"io.opentelemetry.sdk.logs");
for (String target : dropInstrumentationScopes) {
SdkMeterProviderUtil.addMeterConfiguratorCondition(
meterProviderBuilder,
nameEquals(target),
MeterConfig.disabled());
}
}
});
}
return meterProviderBuilder;
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public interface LogsBuildConfig {
* <p>
* This property is not available in the Open Telemetry SDK. It's Quarkus specific.
* <p>
* Support for logs will be enabled if OpenTelemetry support is enabled
* and either this value is true, or this value is unset.
* Support for OpenTelemetry Logs will be enabled if this value is true.
* The OpenTelemetry SDK ( {@link io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig#enabled()} )
* is enabled by default and if disabled, OpenTelemetry Logs will also be disabled.
*/
@WithDefault("false")
Optional<Boolean> enabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public interface MetricsBuildConfig {
* <p>
* This property is not available in the Open Telemetry SDK. It's Quarkus specific.
* <p>
* Support for metrics will be enabled if OpenTelemetry support is enabled
* and either this value is true, or this value is unset.
* Support for OpenTelemetry Metrics will be enabled if this value is true.
* The OpenTelemetry SDK ( {@link io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig#enabled()} )
* is enabled by default and if disabled, OpenTelemetry Metrics will also be disabled.
*/
@WithDefault("false")
Optional<Boolean> enabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public interface TracesBuildConfig {
* <p>
* This property is not available in the Open Telemetry SDK. It's Quarkus specific.
* <p>
* Support for tracing will be enabled if OpenTelemetry support is enabled
* and either this value is true, or this value is unset.
* Support for OpenTelemetry Tracing will be enabled if this value is true.
* The OpenTelemetry SDK ( {@link io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig#enabled()} )
* is enabled by default and if disabled, OpenTelemetry Tracing will also be disabled.
*/
@Deprecated
@WithDefault("true")
Expand Down
170 changes: 170 additions & 0 deletions integration-tests/opentelemetry-exporter-logging/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-integration-tests-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>

<artifactId>quarkus-integration-test-opentelemetry-exporter-logging</artifactId>
<name>Quarkus - Integration Tests - OpenTelemetry exporter logging</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-logging</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<vertx.disableWebsockets>false</vertx.disableWebsockets>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>

<properties>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
<systemPropertyVariables>
<vertx.disableWebsockets>false</vertx.disableWebsockets>
</systemPropertyVariables>
</configuration>
</plugin>

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<vertx.disableWebsockets>false</vertx.disableWebsockets>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.acme;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.quarkus.logging.Log;

@Path("/hello")
public class GreetingResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
Log.info("request received");
return "Hello from Quarkus REST";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
quarkus.application.name=myservice
quarkus.otel.metrics.exporter=logging
# Force quick output of metrics to increase test reliability.
quarkus.otel.metric.export.interval=100ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.acme;

import org.junit.jupiter.api.Disabled;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@Disabled("Manual only")
@QuarkusIntegrationTest
class LoggingMetricsNotPresentIT extends LoggingMetricsNotPresentTest {
// Execute the same tests but in packaged mode.
}
Loading
Loading