-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for simpler creation of start-only and end-only SpanProcessors. (…
- Loading branch information
1 parent
f1deb8e
commit e447e34
Showing
5 changed files
with
173 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
49 changes: 49 additions & 0 deletions
49
...ator/src/main/java/io/opentelemetry/sdk/extension/incubator/trace/OnEndSpanProcessor.java
This file contains 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,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.trace; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
|
||
/** A SpanProcessor implementation that is only capable of processing spans when they end. */ | ||
public final class OnEndSpanProcessor implements SpanProcessor { | ||
private final OnEnd onEnd; | ||
|
||
private OnEndSpanProcessor(OnEnd onEnd) { | ||
this.onEnd = onEnd; | ||
} | ||
|
||
static SpanProcessor create(OnEnd onEnd) { | ||
return new OnEndSpanProcessor(onEnd); | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) { | ||
onEnd.apply(span); | ||
} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onStart(Context parentContext, ReadWriteSpan span) { | ||
// nop | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return false; | ||
} | ||
|
||
@FunctionalInterface | ||
public interface OnEnd { | ||
void apply(ReadableSpan span); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...or/src/main/java/io/opentelemetry/sdk/extension/incubator/trace/OnStartSpanProcessor.java
This file contains 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,50 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.trace; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
|
||
/** A SpanProcessor that only handles onStart(). */ | ||
public final class OnStartSpanProcessor implements SpanProcessor { | ||
|
||
private final OnStart onStart; | ||
|
||
private OnStartSpanProcessor(OnStart onStart) { | ||
this.onStart = onStart; | ||
} | ||
|
||
public static SpanProcessor create(OnStart onStart) { | ||
return new OnStartSpanProcessor(onStart); | ||
} | ||
|
||
@Override | ||
public void onStart(Context parentContext, ReadWriteSpan span) { | ||
onStart.apply(parentContext, span); | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) { | ||
// nop | ||
} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return false; | ||
} | ||
|
||
@FunctionalInterface | ||
public interface OnStart { | ||
void apply(Context context, ReadWriteSpan span); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/test/java/io/opentelemetry/sdk/extension/incubator/trace/OnEndSpanProcessorTest.java
This file contains 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,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.trace; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class OnEndSpanProcessorTest { | ||
|
||
@Test | ||
void endOnly() { | ||
AtomicReference<ReadableSpan> seenSpan = new AtomicReference<>(); | ||
ReadWriteSpan inputSpan = mock(ReadWriteSpan.class); | ||
|
||
SpanProcessor processor = OnEndSpanProcessor.create(seenSpan::set); | ||
|
||
assertThat(processor.isStartRequired()).isFalse(); | ||
assertThat(processor.isEndRequired()).isTrue(); | ||
processor.onEnd(inputSpan); | ||
assertThat(seenSpan.get()).isSameAs(inputSpan); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...rc/test/java/io/opentelemetry/sdk/extension/incubator/trace/OnStartSpanProcessorTest.java
This file contains 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,39 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.trace; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class OnStartSpanProcessorTest { | ||
|
||
@Test | ||
void startOnly() { | ||
AtomicReference<Context> seenContext = new AtomicReference<>(); | ||
AtomicReference<ReadWriteSpan> seenSpan = new AtomicReference<>(); | ||
Context context = mock(Context.class); | ||
ReadWriteSpan inputSpan = mock(ReadWriteSpan.class); | ||
|
||
SpanProcessor processor = | ||
OnStartSpanProcessor.create( | ||
(ctx, span) -> { | ||
seenContext.set(ctx); | ||
seenSpan.set(span); | ||
}); | ||
|
||
assertThat(processor.isStartRequired()).isTrue(); | ||
assertThat(processor.isEndRequired()).isFalse(); | ||
processor.onStart(context, inputSpan); | ||
assertThat(seenContext.get()).isSameAs(context); | ||
assertThat(seenSpan.get()).isSameAs(inputSpan); | ||
} | ||
} |