Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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;
}
Copy link
Member

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?

Copy link
Member Author

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.


public ExtendedBaggageBuilder setAll(Map<String, String> baggage) {
Copy link
Member

Choose a reason for hiding this comment

The 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 BaggageBuilder with a putAll(Map<String, String>) method an skipping incubation.

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 {
Copy link
Member

Choose a reason for hiding this comment

The 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 build method. Another problem with this is that it accepts a SpanCallable, but has nothing to do with spans. Maybe this could be fixed by renaming SpanCallable to something like ThrowingCallable, but let's go back to my original suggestion:

String result = Baggage.fromMap(Map.of("key", "value"))
  .storeInContext(Context.current())
  .wrap(() -> "result")
  .call();

You gave the feedback:

it's not clear if my current baggage items are added or replaced
I have to understand that baggage is stored in Context - which I actually don't have to be aware of
I have to know that wrap comes before call - it's not necessarily long, but harder to discover

I think we should extend BaggageBuilder to include a putAll method (as mentioned above). This would remove the ambiguity about when items are added or replaced.

To add, call Baggage.current().toBuilder().putAll(Map.of("key", "value"))
To replace (i.e. start with a clean slate), call Baggage.empty().toBuilder().putAll(Map.of("key", "value"))

I also think that we could extend Context with wrapAndRun and wrapAndCall methods (in addition to the existing wrap and call methods. This simplifies the call to something like:

String result = Baggage.current().toBuilder().putAll(Map.of("key", "value"))
  .storeInContext(Context.current())
  .wrapAndCall(() -> "result")

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:

  • Add putAll(Map<String, String>) to BaggageBuilder
  • Add wrapAndRun(Runnable), T wrapAndCall(Callable<T>) to Context

If we add these, I think we can improve the ergonomics without needed to incubate anything at all.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 😄

its better the user is forced to understand that up front.

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:

  • what is the "current" baggage vs. "empty"
  • what is a "context"
  • what is the "current" context
  • what is "wrapping"

Copy link
Member

Choose a reason for hiding this comment

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

How do you propose we proceed?

Copy link
Member Author

Choose a reason for hiding this comment

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

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

View check run for this annotation

Codecov / codecov/patch

extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/trace/ExtendedBaggageBuilder.java#L54-L56

Added lines #L54 - L56 were not covered by tests
}
}

Check warning on line 58 in extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/trace/ExtendedBaggageBuilder.java

View check run for this annotation

Codecov / codecov/patch

extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/trace/ExtendedBaggageBuilder.java#L58

Added line #L58 was not covered by tests
}
@@ -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");
}
}