Skip to content

Commit 28d52aa

Browse files
committed
Second pass
1 parent 99d30c5 commit 28d52aa

84 files changed

Lines changed: 543 additions & 32 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/src/main/java/io/opentelemetry/common/ApiUsageLogger.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.common;
77

8-
import javax.annotation.Nullable;
98
import java.util.logging.Level;
109
import java.util.logging.Logger;
1110

context/src/main/java/io/opentelemetry/context/Context.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ default Scope makeCurrent() {
245245
* and then invokes the input {@link Runnable}.
246246
*/
247247
default Runnable wrap(Runnable runnable) {
248+
if (runnable == null) {
249+
ApiUsageLogger.logNullParam(Context.class, "wrap", "runnable");
250+
return () -> {};
251+
}
248252
return () -> {
249253
try (Scope ignored = makeCurrent()) {
250254
runnable.run();
@@ -257,6 +261,10 @@ default Runnable wrap(Runnable runnable) {
257261
* and then invokes the input {@link Runnable}.
258262
*/
259263
default <T> Callable<T> wrap(Callable<T> callable) {
264+
if (callable == null) {
265+
ApiUsageLogger.logNullParam(Context.class, "wrap", "callable");
266+
return () -> null;
267+
}
260268
return () -> {
261269
try (Scope ignored = makeCurrent()) {
262270
return callable.call();
@@ -269,6 +277,10 @@ default <T> Callable<T> wrap(Callable<T> callable) {
269277
* this the {@linkplain Context#current() current context} before each execution.
270278
*/
271279
default Executor wrap(Executor executor) {
280+
if (executor == null) {
281+
ApiUsageLogger.logNullParam(Context.class, "wrap", "executor");
282+
return command -> {};
283+
}
272284
return command -> executor.execute(wrap(command));
273285
}
274286

@@ -300,6 +312,10 @@ default ScheduledExecutorService wrap(ScheduledExecutorService executor) {
300312
* and then invokes the input {@link Function}.
301313
*/
302314
default <T, U> Function<T, U> wrapFunction(Function<T, U> function) {
315+
if (function == null) {
316+
ApiUsageLogger.logNullParam(Context.class, "wrapFunction", "function");
317+
return t -> null;
318+
}
303319
return t -> {
304320
try (Scope ignored = makeCurrent()) {
305321
return function.apply(t);
@@ -312,6 +328,10 @@ default <T, U> Function<T, U> wrapFunction(Function<T, U> function) {
312328
* and then invokes the input {@link BiFunction}.
313329
*/
314330
default <T, U, V> BiFunction<T, U, V> wrapFunction(BiFunction<T, U, V> function) {
331+
if (function == null) {
332+
ApiUsageLogger.logNullParam(Context.class, "wrapFunction", "function");
333+
return (t, u) -> null;
334+
}
315335
return (t, u) -> {
316336
try (Scope ignored = makeCurrent()) {
317337
return function.apply(t, u);
@@ -324,6 +344,10 @@ default <T, U, V> BiFunction<T, U, V> wrapFunction(BiFunction<T, U, V> function)
324344
* and then invokes the input {@link Consumer}.
325345
*/
326346
default <T> Consumer<T> wrapConsumer(Consumer<T> consumer) {
347+
if (consumer == null) {
348+
ApiUsageLogger.logNullParam(Context.class, "wrapConsumer", "consumer");
349+
return t -> {};
350+
}
327351
return t -> {
328352
try (Scope ignored = makeCurrent()) {
329353
consumer.accept(t);
@@ -336,6 +360,10 @@ default <T> Consumer<T> wrapConsumer(Consumer<T> consumer) {
336360
* and then invokes the input {@link BiConsumer}.
337361
*/
338362
default <T, U> BiConsumer<T, U> wrapConsumer(BiConsumer<T, U> consumer) {
363+
if (consumer == null) {
364+
ApiUsageLogger.logNullParam(Context.class, "wrapConsumer", "consumer");
365+
return (t, u) -> {};
366+
}
339367
return (t, u) -> {
340368
try (Scope ignored = makeCurrent()) {
341369
consumer.accept(t, u);
@@ -348,6 +376,10 @@ default <T, U> BiConsumer<T, U> wrapConsumer(BiConsumer<T, U> consumer) {
348376
* and then invokes the input {@link Supplier}.
349377
*/
350378
default <T> Supplier<T> wrapSupplier(Supplier<T> supplier) {
379+
if (supplier == null) {
380+
ApiUsageLogger.logNullParam(Context.class, "wrapSupplier", "supplier");
381+
return () -> null;
382+
}
351383
return () -> {
352384
try (Scope ignored = makeCurrent()) {
353385
return supplier.get();

context/src/main/java/io/opentelemetry/context/ContextExecutorService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Collection;
99
import java.util.List;
10+
import java.util.Objects;
1011
import java.util.concurrent.Callable;
1112
import java.util.concurrent.ExecutionException;
1213
import java.util.concurrent.ExecutorService;
@@ -29,46 +30,54 @@ final Context context() {
2930

3031
@Override
3132
public <T> Future<T> submit(Callable<T> task) {
33+
Objects.requireNonNull(task, "task");
3234
return delegate().submit(context.wrap(task));
3335
}
3436

3537
@Override
3638
public <T> Future<T> submit(Runnable task, T result) {
39+
Objects.requireNonNull(task, "task");
3740
return delegate().submit(context.wrap(task), result);
3841
}
3942

4043
@Override
4144
public Future<?> submit(Runnable task) {
45+
Objects.requireNonNull(task, "task");
4246
return delegate().submit(context.wrap(task));
4347
}
4448

4549
@Override
4650
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
4751
throws InterruptedException {
52+
Objects.requireNonNull(tasks, "tasks");
4853
return delegate().invokeAll(wrap(context, tasks));
4954
}
5055

5156
@Override
5257
public <T> List<Future<T>> invokeAll(
5358
Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
5459
throws InterruptedException {
60+
Objects.requireNonNull(tasks, "tasks");
5561
return delegate().invokeAll(wrap(context, tasks), timeout, unit);
5662
}
5763

5864
@Override
5965
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
6066
throws InterruptedException, ExecutionException {
67+
Objects.requireNonNull(tasks, "tasks");
6168
return delegate().invokeAny(wrap(context, tasks));
6269
}
6370

6471
@Override
6572
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
6673
throws InterruptedException, ExecutionException, TimeoutException {
74+
Objects.requireNonNull(tasks, "tasks");
6775
return delegate().invokeAny(wrap(context, tasks), timeout, unit);
6876
}
6977

7078
@Override
7179
public void execute(Runnable command) {
80+
Objects.requireNonNull(command, "command");
7281
delegate().execute(context.wrap(command));
7382
}
7483
}

context/src/main/java/io/opentelemetry/context/ContextKey.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.context;
77

8+
import java.util.Objects;
9+
810
/**
911
* Key for indexing values of type {@link T} stored in a {@link Context}. {@link ContextKey} are
1012
* compared by reference, so it is expected that only one {@link ContextKey} is created for a
@@ -39,6 +41,7 @@ public interface ContextKey<T> {
3941
* keys.
4042
*/
4143
static <T> ContextKey<T> named(String name) {
44+
Objects.requireNonNull(name, "name");
4245
return new DefaultContextKey<>(name);
4346
}
4447
}

context/src/main/java/io/opentelemetry/context/ContextScheduledExecutorService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.context;
77

8+
import java.util.Objects;
89
import java.util.concurrent.Callable;
910
import java.util.concurrent.ScheduledExecutorService;
1011
import java.util.concurrent.ScheduledFuture;
@@ -24,23 +25,27 @@ ScheduledExecutorService delegate() {
2425

2526
@Override
2627
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
28+
Objects.requireNonNull(command, "command");
2729
return delegate().schedule(context().wrap(command), delay, unit);
2830
}
2931

3032
@Override
3133
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
34+
Objects.requireNonNull(callable, "callable");
3235
return delegate().schedule(context().wrap(callable), delay, unit);
3336
}
3437

3538
@Override
3639
public ScheduledFuture<?> scheduleAtFixedRate(
3740
Runnable command, long initialDelay, long period, TimeUnit unit) {
41+
Objects.requireNonNull(command, "command");
3842
return delegate().scheduleAtFixedRate(context().wrap(command), initialDelay, period, unit);
3943
}
4044

4145
@Override
4246
public ScheduledFuture<?> scheduleWithFixedDelay(
4347
Runnable command, long initialDelay, long delay, TimeUnit unit) {
48+
Objects.requireNonNull(command, "command");
4449
return delegate().scheduleWithFixedDelay(context().wrap(command), initialDelay, delay, unit);
4550
}
4651
}

context/src/main/java/io/opentelemetry/context/CurrentContextExecutorService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Collection;
99
import java.util.List;
10+
import java.util.Objects;
1011
import java.util.concurrent.Callable;
1112
import java.util.concurrent.ExecutionException;
1213
import java.util.concurrent.ExecutorService;
@@ -22,46 +23,54 @@ class CurrentContextExecutorService extends ForwardingExecutorService {
2223

2324
@Override
2425
public <T> Future<T> submit(Callable<T> task) {
26+
Objects.requireNonNull(task, "task");
2527
return delegate().submit(Context.current().wrap(task));
2628
}
2729

2830
@Override
2931
public <T> Future<T> submit(Runnable task, T result) {
32+
Objects.requireNonNull(task, "task");
3033
return delegate().submit(Context.current().wrap(task), result);
3134
}
3235

3336
@Override
3437
public Future<?> submit(Runnable task) {
38+
Objects.requireNonNull(task, "task");
3539
return delegate().submit(Context.current().wrap(task));
3640
}
3741

3842
@Override
3943
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
4044
throws InterruptedException {
45+
Objects.requireNonNull(tasks, "tasks");
4146
return delegate().invokeAll(wrap(Context.current(), tasks));
4247
}
4348

4449
@Override
4550
public <T> List<Future<T>> invokeAll(
4651
Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
4752
throws InterruptedException {
53+
Objects.requireNonNull(tasks, "tasks");
4854
return delegate().invokeAll(wrap(Context.current(), tasks), timeout, unit);
4955
}
5056

5157
@Override
5258
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
5359
throws InterruptedException, ExecutionException {
60+
Objects.requireNonNull(tasks, "tasks");
5461
return delegate().invokeAny(wrap(Context.current(), tasks));
5562
}
5663

5764
@Override
5865
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
5966
throws InterruptedException, ExecutionException, TimeoutException {
67+
Objects.requireNonNull(tasks, "tasks");
6068
return delegate().invokeAny(wrap(Context.current(), tasks), timeout, unit);
6169
}
6270

6371
@Override
6472
public void execute(Runnable command) {
73+
Objects.requireNonNull(command, "command");
6574
delegate().execute(Context.current().wrap(command));
6675
}
6776
}

context/src/main/java/io/opentelemetry/context/CurrentContextScheduledExecutorService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.context;
77

8+
import java.util.Objects;
89
import java.util.concurrent.Callable;
910
import java.util.concurrent.ScheduledExecutorService;
1011
import java.util.concurrent.ScheduledFuture;
@@ -22,23 +23,27 @@ final class CurrentContextScheduledExecutorService extends CurrentContextExecuto
2223

2324
@Override
2425
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
26+
Objects.requireNonNull(command, "command");
2527
return delegate.schedule(Context.current().wrap(command), delay, unit);
2628
}
2729

2830
@Override
2931
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
32+
Objects.requireNonNull(callable, "callable");
3033
return delegate.schedule(Context.current().wrap(callable), delay, unit);
3134
}
3235

3336
@Override
3437
public ScheduledFuture<?> scheduleAtFixedRate(
3538
Runnable command, long initialDelay, long period, TimeUnit unit) {
39+
Objects.requireNonNull(command, "command");
3640
return delegate.scheduleAtFixedRate(command, initialDelay, period, unit);
3741
}
3842

3943
@Override
4044
public ScheduledFuture<?> scheduleWithFixedDelay(
4145
Runnable command, long initialDelay, long delay, TimeUnit unit) {
46+
Objects.requireNonNull(command, "command");
4247
return delegate.scheduleWithFixedDelay(command, initialDelay, delay, unit);
4348
}
4449
}

context/src/main/java/io/opentelemetry/context/StrictContextStorage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.lang.ref.Reference;
2727
import java.util.Arrays;
2828
import java.util.List;
29+
import java.util.Objects;
2930
import java.util.concurrent.ConcurrentHashMap;
3031
import java.util.concurrent.Executor;
3132
import java.util.logging.Level;
@@ -63,6 +64,7 @@ private StrictContextStorage(ContextStorage delegate) {
6364

6465
@Override
6566
public Scope attach(Context context) {
67+
Objects.requireNonNull(context, "context");
6668
Scope scope = delegate.attach(context);
6769

6870
CallerStackTrace caller = new CallerStackTrace(context);

context/src/main/java/io/opentelemetry/context/propagation/MultiTextMapPropagator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.context.propagation;
77

8+
import io.opentelemetry.common.ApiUsageLogger;
89
import io.opentelemetry.context.Context;
910
import java.util.ArrayList;
1011
import java.util.Arrays;
@@ -45,7 +46,12 @@ private static List<String> getAllFields(TextMapPropagator[] textPropagators) {
4546

4647
@Override
4748
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
48-
if (context == null || setter == null) {
49+
if (context == null) {
50+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "inject", "context");
51+
return;
52+
}
53+
if (setter == null) {
54+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "inject", "setter");
4955
return;
5056
}
5157
for (TextMapPropagator textPropagator : textMapPropagators) {
@@ -56,9 +62,11 @@ public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> se
5662
@Override
5763
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
5864
if (context == null) {
65+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "extract", "context");
5966
return Context.root();
6067
}
6168
if (getter == null) {
69+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "extract", "getter");
6270
return context;
6371
}
6472
for (TextMapPropagator textPropagator : textMapPropagators) {

context/src/main/java/io/opentelemetry/context/propagation/NoopTextMapPropagator.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.context.propagation;
77

8+
import io.opentelemetry.common.ApiUsageLogger;
89
import io.opentelemetry.context.Context;
910
import java.util.Collection;
1011
import java.util.Collections;
@@ -23,13 +24,27 @@ public Collection<String> fields() {
2324
}
2425

2526
@Override
26-
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {}
27+
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
28+
if (context == null) {
29+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "inject", "context");
30+
return;
31+
}
32+
if (setter == null) {
33+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "inject", "setter");
34+
return;
35+
}
36+
}
2737

2838
@Override
2939
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
3040
if (context == null) {
41+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "extract", "context");
3142
return Context.root();
3243
}
44+
if (getter == null) {
45+
ApiUsageLogger.logNullParam(TextMapPropagator.class, "extract", "getter");
46+
return context;
47+
}
3348
return context;
3449
}
3550

0 commit comments

Comments
 (0)