Skip to content

Sdk builders extended with addProcessorFirst methods #7243

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
merged 6 commits into from
Apr 18, 2025
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
3 changes: 3 additions & 0 deletions docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Comparing source compatibility of opentelemetry-sdk-logs-1.50.0-SNAPSHOT.jar aga
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String getEventName()
+++ NEW ANNOTATION: javax.annotation.Nullable
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder addLogRecordProcessorFirst(io.opentelemetry.sdk.logs.LogRecordProcessor)
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of opentelemetry-sdk-trace-1.50.0-SNAPSHOT.jar against opentelemetry-sdk-trace-1.49.0.jar
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.trace.SdkTracerProviderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.SdkTracerProviderBuilder addSpanProcessorFirst(io.opentelemetry.sdk.trace.SpanProcessor)
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ public SdkLoggerProviderBuilder addLogRecordProcessor(LogRecordProcessor process
return this;
}

/**
* Add a log processor as first. {@link LogRecordProcessor#onEmit(Context, ReadWriteLogRecord)}
* will be called each time a log is emitted by {@link Logger} instances obtained from the {@link
* SdkLoggerProvider}.
*
* <p>Compared to {@link SdkLoggerProviderBuilder#addLogRecordProcessor(LogRecordProcessor)}, this
* method adds the processor to the beginning of the processor pipeline. So the {@code processor}
* given will be executed before than the other processors already added.
*
* @param processor the log processor
* @return this
*/
public SdkLoggerProviderBuilder addLogRecordProcessorFirst(LogRecordProcessor processor) {
Copy link
Member

Choose a reason for hiding this comment

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

This name follows the naming convention of Deque#addFirst, albeit "LogRecordProcessor" inserted between "add" and "First".

While it initially seems like a verbose name, I think its probably the best we can do to balance consistency with the existing "addLogRecordProcessor" and the precedent in java with Deque#addFirst.

requireNonNull(processor, "processor");
logRecordProcessors.add(0, processor);
return this;
}

/**
* Assign a {@link Clock}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ void builder_clockProvided() {

@Test
void builder_multipleProcessors() {
LogRecordProcessor firstProcessor = mock(LogRecordProcessor.class);
assertThat(
SdkLoggerProvider.builder()
.addLogRecordProcessor(logRecordProcessor)
.addLogRecordProcessor(logRecordProcessor)
.addLogRecordProcessorFirst(firstProcessor)
.build())
.extracting("sharedState", as(InstanceOfAssertFactories.type(LoggerSharedState.class)))
.extracting(LoggerSharedState::getLogRecordProcessor)
Expand All @@ -138,7 +140,9 @@ void builder_multipleProcessors() {
.extracting(
"logRecordProcessors",
as(InstanceOfAssertFactories.list(LogRecordProcessor.class)))
.hasSize(2);
.hasSize(3)
.first()
.isEqualTo(firstProcessor);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,27 @@ public SdkTracerProviderBuilder setSampler(Sampler sampler) {
* @return this
*/
public SdkTracerProviderBuilder addSpanProcessor(SpanProcessor spanProcessor) {
requireNonNull(spanProcessor, "spanProcessor");
spanProcessors.add(spanProcessor);
return this;
}

/**
* Add a SpanProcessor to the beginning of the span pipeline that will be built. {@link
* SpanProcessor} will be called each time a {@link Span} is started or ended.
*
* <p>The {@code spanProcessor} must be thread-safe and return immediately (no remote calls, as
* contention free as possible).
*
* @param spanProcessor the processor to be added to the beginning of the span pipeline.
* @return this
*/
public SdkTracerProviderBuilder addSpanProcessorFirst(SpanProcessor spanProcessor) {
requireNonNull(spanProcessor, "spanProcessor");
spanProcessors.add(0, spanProcessor);
return this;
}

/**
* Set the tracer configurator, which computes {@link TracerConfig} for each {@link
* InstrumentationScopeInfo}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package io.opentelemetry.sdk.trace;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.resources.Resource;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

public class SdkTracerProviderBuilderTest {
Expand All @@ -28,4 +30,23 @@ void addResource() {
.extracting("sharedState")
.hasFieldOrPropertyWithValue("resource", Resource.getDefault().merge(customResource));
}

@Test
void addSpanProcessorFirst() {
SpanProcessor firstProcessor = mock(SpanProcessor.class);
SpanProcessor anotherProcessor = mock(SpanProcessor.class);

SdkTracerProvider sdkTracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(anotherProcessor)
.addSpanProcessorFirst(firstProcessor)
.build();

assertThat(sdkTracerProvider)
.extracting("sharedState")
.extracting("activeSpanProcessor")
.extracting("spanProcessorsAll", InstanceOfAssertFactories.list(SpanProcessor.class))
.hasSize(2)
.containsExactly(firstProcessor, anotherProcessor);
}
}
Loading