Skip to content

Commit

Permalink
Fix processor ordering (#3795)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Jul 24, 2024
1 parent 19c3706 commit 4139b7b
Showing 1 changed file with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.AutoConfigureListener;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentSelector;
Expand All @@ -75,6 +75,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -236,6 +237,8 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
}
telemetryClient.setQuickPulse(quickPulse);

AtomicBoolean firstLogRecordProcessor = new AtomicBoolean(true);

autoConfiguration
.addPropertiesSupplier(
() -> {
Expand All @@ -258,6 +261,20 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
return props;
})
.addPropertiesCustomizer(new AiConfigCustomizer())
.addLogRecordProcessorCustomizer(
(logRecordProcessor, configProperties) -> {
if (firstLogRecordProcessor.getAndSet(false)) {
// hack to run our log record processors first, before any other log processors
// (in particular before the batch log processor which performs the export)
// see https://github.com/open-telemetry/opentelemetry-java/issues/6599
List<LogRecordProcessor> logRecordProcessors =
getLogRecordProcessors(configuration);
logRecordProcessors.add(logRecordProcessor);
return LogRecordProcessor.composite(
logRecordProcessors.toArray(new LogRecordProcessor[0]));
}
return logRecordProcessor;
})
.addSpanExporterCustomizer(
(spanExporter, configProperties) -> {
if (spanExporter instanceof AzureMonitorSpanExporterProvider.MarkerSpanExporter) {
Expand Down Expand Up @@ -286,9 +303,7 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
.addTracerProviderCustomizer(
(builder, otelConfig) -> configureTracing(builder, configuration))
.addMeterProviderCustomizer(
(builder, otelConfig) -> configureMetrics(builder, configuration))
.addLoggerProviderCustomizer(
(builder, otelConfig) -> configureLogging(builder, configuration));
(builder, otelConfig) -> configureMetrics(builder, configuration));

AiContextCustomizerHolder.setInstance(
new AiContextCustomizer<>(
Expand Down Expand Up @@ -617,28 +632,28 @@ private static List<ProcessorConfig> getSpanProcessorConfigs(Configuration confi
// QuickPulse is injected into the logging pipeline because QuickPulse displays exception
// telemetry and exception telemetry can be reported as either span events or as log records with
// an exception stack traces
private static SdkLoggerProviderBuilder configureLogging(
SdkLoggerProviderBuilder builder, Configuration configuration) {
private static List<LogRecordProcessor> getLogRecordProcessors(Configuration configuration) {
List<LogRecordProcessor> logRecordProcessors = new ArrayList<>();

builder.addLogRecordProcessor(new AzureMonitorLogProcessor());
logRecordProcessors.add(new AzureMonitorLogProcessor());

if (ConfigurationBuilder.inAzureFunctionsWorker(System::getenv)) {
builder.addLogRecordProcessor(new AzureFunctionsLogProcessor());
logRecordProcessors.add(new AzureFunctionsLogProcessor());
}

if (!configuration.preview.inheritedAttributes.isEmpty()) {
builder.addLogRecordProcessor(
logRecordProcessors.add(
new InheritedAttributesLogProcessor(configuration.preview.inheritedAttributes));
}
// adding this even if there are no connectionStringOverrides, in order to support
// "ai.preview.connection_string" being set programmatically on CONSUMER spans
// (or "ai.preview.instrumentation_key" for backwards compatibility)
builder.addLogRecordProcessor(new InheritedConnectionStringLogProcessor());
logRecordProcessors.add(new InheritedConnectionStringLogProcessor());
// adding this even if there are no roleNameOverrides, in order to support
// "ai.preview.service_name" being set programmatically on CONSUMER spans
builder.addLogRecordProcessor(new InheritedRoleNameLogProcessor());
logRecordProcessors.add(new InheritedRoleNameLogProcessor());

return builder;
return logRecordProcessors;
}

private static LogRecordExporter createLogExporter(
Expand Down

0 comments on commit 4139b7b

Please sign in to comment.