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
add ExtendedBaggageBuilder #6063
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.extension.incubator.trace; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.api.baggage.BaggageBuilder; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import java.util.Map; | ||
|
||
public class ExtendedBaggageBuilder { | ||
private final BaggageBuilder delegate; | ||
|
||
private ExtendedBaggageBuilder(BaggageBuilder delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public static ExtendedBaggageBuilder create(BaggageBuilder builder) { | ||
return new ExtendedBaggageBuilder(builder); | ||
} | ||
|
||
public static ExtendedBaggageBuilder current() { | ||
return create(Baggage.current().toBuilder()); | ||
} | ||
|
||
public ExtendedBaggageBuilder set(String key, String value) { | ||
delegate.put(key, value); | ||
return this; | ||
} | ||
|
||
public ExtendedBaggageBuilder setAll(Map<String, String> baggage) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this ends up being the only helpful thing we get. I'm comfortable extending |
||
baggage.forEach(delegate::put); | ||
return this; | ||
} | ||
|
||
/** | ||
* Set baggage items inside the given {@link SpanCallable}. | ||
* | ||
* @param spanCallable the {@link SpanCallable} to call | ||
* @param <E> the type of the exception | ||
* @return the result of the {@link SpanCallable} | ||
*/ | ||
public <T, E extends Throwable> T call(SpanCallable<T, E> spanCallable) throws E { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is right. This is a builder, and I think we should stick to builders being comprised of strictly setters and
You gave the feedback:
I think we should extend BaggageBuilder to include a To add, call I also think that we could extend Context with
The user still has to know that baggage goes in context, but I actually think its better the user is forced to understand that up front. So taken together, my recommendations are:
If we add these, I think we can improve the ergonomics without needed to incubate anything at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jack-berg it was good that we split this out - this is more difficult to get right than I thought 😄
Why is that better? In this simple use case, it can be transparent to the user. However, I'm fine with teaching the user - if we can find a way that doesn't hurt autocompletion/explorability. In particular, this is what I guess users will stumble across:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you propose we proceed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add it to the meeting agenda: https://docs.google.com/document/d/1WK9h4p55p8ZjPkxO75-ApI9m0wfea6ENZmMoFRvXSCw/edit#bookmark=id.ptrb4y8kwfc4 |
||
Context context = delegate.build().storeInContext(Context.current()); | ||
try (Scope ignore = context.makeCurrent()) { | ||
return spanCallable.callInSpan(); | ||
} | ||
} | ||
|
||
public <E extends Throwable> void run(SpanRunnable<E> spanRunnable) throws E { | ||
Context context = delegate.build().storeInContext(Context.current()); | ||
try (Scope ignore = context.makeCurrent()) { | ||
spanRunnable.runInSpan(); | ||
Check warning on line 56 in extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/trace/ExtendedBaggageBuilder.java Codecov / codecov/patchextensions/incubator/src/main/java/io/opentelemetry/extension/incubator/trace/ExtendedBaggageBuilder.java#L54-L56
|
||
} | ||
} | ||
Check warning on line 58 in extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/trace/ExtendedBaggageBuilder.java Codecov / codecov/patchextensions/incubator/src/main/java/io/opentelemetry/extension/incubator/trace/ExtendedBaggageBuilder.java#L58
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.extension.incubator.trace; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import java.util.Collections; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ExtendedBaggageBuilderTest { | ||
|
||
@Test | ||
void add() { | ||
String value = | ||
ExtendedBaggageBuilder.current() | ||
.set("key", "value") | ||
.call(() -> Baggage.current().getEntryValue("key")); | ||
|
||
assertThat(value).isEqualTo("value"); | ||
} | ||
|
||
@Test | ||
void addMap() { | ||
String value = | ||
ExtendedBaggageBuilder.current() | ||
.setAll(Collections.singletonMap("key", "value")) | ||
.call(() -> Baggage.current().getEntryValue("key")); | ||
|
||
assertThat(value).isEqualTo("value"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem right. Why have a method that performs the same function with a different name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a different level of abstraction. "put" relates to
Map
- as a user, I don't case if it's stored in a Map.