-
Notifications
You must be signed in to change notification settings - Fork 1k
add declarative config span logger #14591
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
Merged
laurit
merged 8 commits into
open-telemetry:main
from
zeitlinger:declarative-config-span-logging
Oct 8, 2025
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8fe901e
add declarative config span logger
zeitlinger 8ec7f04
fix test
zeitlinger c62dc31
pr review
zeitlinger acd05e3
fix native image
zeitlinger 82b2e91
fix native image
zeitlinger a573393
Revert "fix native image"
zeitlinger 010ec07
Revert "fix native image"
zeitlinger c7418d6
pr review
zeitlinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...oling/src/main/java/io/opentelemetry/javaagent/tooling/SpanLoggingCustomizerProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizer; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizerProvider; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.ConsoleExporterModel; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SimpleSpanProcessorModel; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanExporterModel; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanProcessorModel; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TracerProviderModel; | ||
|
||
/** Adds span logging exporter for debug mode */ | ||
@AutoService(DeclarativeConfigurationCustomizerProvider.class) | ||
public class SpanLoggingCustomizerProvider implements DeclarativeConfigurationCustomizerProvider { | ||
|
||
@Override | ||
public void customize(DeclarativeConfigurationCustomizer customizer) { | ||
customizer.addModelCustomizer( | ||
model -> { | ||
maybeEnableLoggingExporter(model); | ||
return model; | ||
}); | ||
} | ||
|
||
private static void maybeEnableLoggingExporter(OpenTelemetryConfigurationModel model) { | ||
// read from system properties as it's an early init property and the config bridge is not | ||
// available here | ||
if (!ConfigPropertiesUtil.getBoolean("otel.javaagent.debug", false)) { | ||
return; | ||
} | ||
// don't install another instance if the user has already explicitly requested it. | ||
if (loggingExporterIsAlreadyConfigured(model)) { | ||
return; | ||
} | ||
TracerProviderModel tracerProvider = model.getTracerProvider(); | ||
if (tracerProvider == null) { | ||
tracerProvider = new TracerProviderModel(); | ||
model.withTracerProvider(tracerProvider); | ||
} | ||
SpanProcessorModel processor = | ||
new SpanProcessorModel() | ||
.withSimple( | ||
new SimpleSpanProcessorModel() | ||
.withExporter(new SpanExporterModel().withConsole(new ConsoleExporterModel()))); | ||
tracerProvider.getProcessors().add(processor); | ||
} | ||
|
||
private static boolean loggingExporterIsAlreadyConfigured(OpenTelemetryConfigurationModel model) { | ||
TracerProviderModel tracerProvider = model.getTracerProvider(); | ||
if (tracerProvider == null) { | ||
return false; | ||
} | ||
for (SpanProcessorModel processor : tracerProvider.getProcessors()) { | ||
SimpleSpanProcessorModel simple = processor.getSimple(); | ||
if (simple == null) { | ||
continue; | ||
} | ||
SpanExporterModel exporter = simple.getExporter(); | ||
if (exporter == null) { | ||
continue; | ||
} | ||
if (exporter.getConsole() != null) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...g/src/test/java/io/opentelemetry/javaagent/tooling/SpanLoggingCustomizerProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel; | ||
import java.io.ByteArrayInputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junitpioneer.jupiter.ClearSystemProperty; | ||
|
||
class SpanLoggingCustomizerProviderTest { | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"true, true, false", | ||
"false, false, false", | ||
", false, false", // empty value means property is not set | ||
"invalid, false, false", | ||
"true, true, true", | ||
}) | ||
@ClearSystemProperty(key = "otel.javaagent.debug") | ||
void addSpanLoggingExporter(String propertyValue, boolean expected, boolean alreadyAdded) { | ||
zeitlinger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (propertyValue != null) { | ||
System.setProperty("otel.javaagent.debug", propertyValue); | ||
} | ||
String yaml = | ||
alreadyAdded | ||
? "file_format: \"1.0-rc.1\"\n" | ||
+ "tracer_provider:\n" | ||
+ " processors:\n" | ||
+ " - simple:\n" | ||
+ " exporter:\n" | ||
+ " console: {}\n" | ||
: "file_format: \"1.0-rc.1\"\n"; | ||
|
||
OpenTelemetryConfigurationModel model = | ||
applyCustomizer( | ||
DeclarativeConfiguration.parse( | ||
new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8))), | ||
new SpanLoggingCustomizerProvider()); | ||
|
||
String console = "ConsoleExporterModel"; | ||
if (expected) { | ||
assertThat(model.toString()).containsOnlyOnce(console); | ||
} else { | ||
assertThat(model.toString()).doesNotContain(console); | ||
} | ||
} | ||
|
||
private static OpenTelemetryConfigurationModel applyCustomizer( | ||
OpenTelemetryConfigurationModel model, SpanLoggingCustomizerProvider provider) { | ||
List<Function<OpenTelemetryConfigurationModel, OpenTelemetryConfigurationModel>> customizers = | ||
new ArrayList<>(); | ||
provider.customize(c -> customizers.add(c)); | ||
for (Function<OpenTelemetryConfigurationModel, OpenTelemetryConfigurationModel> customizer : | ||
customizers) { | ||
model = customizer.apply(model); | ||
} | ||
return model; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.