-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from LikeTheSalad/okhttp-autoinstrumentation
Okhttp autoinstrumentation stabilization
- Loading branch information
Showing
15 changed files
with
242 additions
and
53 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
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
10 changes: 9 additions & 1 deletion
10
auto-instrumentation/okhttp/okhttp-3.0/library/build.gradle.kts
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
1 change: 1 addition & 0 deletions
1
auto-instrumentation/okhttp/okhttp-3.0/library/consumer-rules.pro
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 @@ | ||
-keep class io.opentelemetry.exporter.internal.InstrumentationUtil { *; } |
40 changes: 40 additions & 0 deletions
40
...in/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/internal/CachedSupplier.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,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.library.okhttp.v3_0.internal; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public class CachedSupplier<T> implements Supplier<T> { | ||
private Supplier<T> supplier; | ||
private T instance; | ||
private final Object lock = new Object(); | ||
|
||
public static <T> CachedSupplier<T> create(Supplier<T> instance) { | ||
return new CachedSupplier<>(instance); | ||
} | ||
|
||
private CachedSupplier(Supplier<T> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
synchronized (lock) { | ||
if (instance == null) { | ||
instance = supplier.get(); | ||
if (instance == null) { | ||
throw new NullPointerException("Supplier provided null."); | ||
} | ||
supplier = null; | ||
} | ||
return instance; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...n/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/internal/LazyInterceptor.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,29 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.library.okhttp.v3_0.internal; | ||
|
||
import java.io.IOException; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public class LazyInterceptor<T extends Interceptor> implements Interceptor { | ||
private final CachedSupplier<T> interceptorSupplier; | ||
|
||
public LazyInterceptor(CachedSupplier<T> interceptorSupplier) { | ||
this.interceptorSupplier = interceptorSupplier; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Response intercept(@NotNull Chain chain) throws IOException { | ||
return interceptorSupplier.get().intercept(chain); | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
...ava/io/opentelemetry/instrumentation/library/okhttp/v3_0/internal/CachedSupplierTest.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,47 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.library.okhttp.v3_0.internal; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.function.Supplier; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CachedSupplierTest { | ||
|
||
@Test | ||
void provideAndCacheSuppliersResult() { | ||
Supplier<String> original = | ||
spy( | ||
new Supplier<String>() { | ||
@Override | ||
public String get() { | ||
return "Hello World"; | ||
} | ||
}); | ||
CachedSupplier<String> cached = CachedSupplier.create(original); | ||
|
||
for (int i = 0; i < 3; i++) { | ||
Assertions.assertThat(cached.get()).isEqualTo("Hello World"); | ||
} | ||
verify(original).get(); | ||
} | ||
|
||
@Test | ||
void validateSupplierDoesNotReturnNull() { | ||
Supplier<String> original = () -> null; | ||
CachedSupplier<String> cached = CachedSupplier.create(original); | ||
|
||
try { | ||
cached.get(); | ||
fail(); | ||
} catch (NullPointerException ignored) { | ||
} | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
auto-instrumentation/okhttp/okhttp-3.0/testing/proguard-rules.pro
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,7 @@ | ||
-keep class kotlin.** { *; } | ||
-keep class okhttp3.** { *; } | ||
-keep class io.opentelemetry.api.** { *; } | ||
-keep class io.opentelemetry.sdk.** { *; } | ||
-keep class io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter { *; } | ||
-keep class io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder { *; } | ||
-dontwarn * |
1 change: 1 addition & 0 deletions
1
auto-instrumentation/okhttp/okhttp-3.0/testing/proguard-test-rules.pro
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 @@ | ||
-include proguard-rules.pro |
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
Oops, something went wrong.