Skip to content

Disallowing null values in ContextSnapshot #102

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 10 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -162,6 +163,9 @@ static DefaultContextSnapshot captureFromContext(Predicate<Object> keyPredicate,
snapshot = (snapshot != null ? snapshot : new DefaultContextSnapshot(contextRegistry));
((ContextAccessor<Object, ?>) accessor).readValues(context, keyPredicate, snapshot);
}
if (snapshot != null) {
snapshot.values().removeIf(Objects::isNull);
}
return (snapshot != null ? snapshot : emptyContextSnapshot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ void should_propagate_thread_local() {
}
}

@Test
void test_null_value_in_source_context() {
this.registry.registerContextAccessor(new TestContextAccessor());
this.registry.registerThreadLocalAccessor(new ObservationThreadLocalAccessor());

String key = ObservationThreadLocalAccessor.KEY;

String emptyValue = ObservationThreadLocalHolder.getValue();
Map<String, String> sourceContext = Collections.singletonMap(key, emptyValue);

ContextSnapshot snapshot = ContextSnapshot.captureAll(this.registry, sourceContext);

try (Scope scope = snapshot.setThreadLocals()) {
assertThat(ObservationThreadLocalHolder.getValue()).isEqualTo(emptyValue);
}
assertThat(ObservationThreadLocalHolder.getValue()).isEqualTo(emptyValue);
}

@Test
void should_propagate_single_thread_local_value() {
this.registry.registerContextAccessor(new TestContextAccessor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.micrometer.context;

import java.util.Objects;

/**
* Example {@link ThreadLocalAccessor} implementation.
*/
Expand All @@ -34,6 +36,9 @@ public String getValue() {

@Override
public void setValue(String value) {
// ThreadLocalAccessor API is @NonNullApi by default
// so we don't expect null here
Objects.requireNonNull(value);
ObservationThreadLocalHolder.setValue(value);
}

Expand All @@ -42,4 +47,12 @@ public void reset() {
ObservationThreadLocalHolder.reset();
}

@Override
public void restore(String previousValue) {
// ThreadLocalAccessor API is @NonNullApi by default
// so we don't expect null here
Objects.requireNonNull(previousValue);
setValue(previousValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ public class ObservationThreadLocalHolder {

private static final ThreadLocal<String> holder = new ThreadLocal<>();

public static void resetValue() {
holder.remove();
}

public static void setValue(String value) {
holder.set(value);
}

@Nullable
public static String getValue() {
return holder.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.micrometer.context;

import java.util.Objects;

/**
* ThreadLocalAccessor for testing purposes with a given key and {@link ThreadLocal}
* instance.
Expand Down Expand Up @@ -46,6 +48,9 @@ public String getValue() {

@Override
public void setValue(String value) {
// ThreadLocalAccessor API is @NonNullApi by default
// so we don't expect null here
Objects.requireNonNull(value);
this.threadLocal.set(value);
}

Expand Down