|
| 1 | +# Library Instrumentation for RxJava version 1.0 and higher |
| 2 | + |
| 3 | +Provides OpenTelemetry instrumentation utilities for [RxJava](https://github.com/ReactiveX/RxJava/tree/1.x), |
| 4 | +enabling context propagation through RxJava's execution model. |
| 5 | + |
| 6 | +**Note**: This library is primarily designed for use by other OpenTelemetry instrumentation |
| 7 | +libraries and framework integrations, not for direct end-user application usage. |
| 8 | + |
| 9 | +## Quickstart |
| 10 | + |
| 11 | +### Add these dependencies to your project |
| 12 | + |
| 13 | +Replace `OPENTELEMETRY_VERSION` with the [latest release](https://central.sonatype.com/artifact/io.opentelemetry.instrumentation/opentelemetry-rxjava-1.0). |
| 14 | + |
| 15 | +For Maven, add to your `pom.xml` dependencies: |
| 16 | + |
| 17 | +```xml |
| 18 | +<dependencies> |
| 19 | + <dependency> |
| 20 | + <groupId>io.opentelemetry.instrumentation</groupId> |
| 21 | + <artifactId>opentelemetry-rxjava-1.0</artifactId> |
| 22 | + <version>OPENTELEMETRY_VERSION</version> |
| 23 | + </dependency> |
| 24 | +</dependencies> |
| 25 | +``` |
| 26 | + |
| 27 | +For Gradle, add to your dependencies: |
| 28 | + |
| 29 | +```kotlin |
| 30 | +implementation("io.opentelemetry.instrumentation:opentelemetry-rxjava-1.0:OPENTELEMETRY_VERSION") |
| 31 | +``` |
| 32 | + |
| 33 | +### Usage |
| 34 | + |
| 35 | +This library provides the `TracedOnSubscribe` class for instrumenting RxJava Observables with |
| 36 | +OpenTelemetry spans: |
| 37 | + |
| 38 | +```java |
| 39 | +import io.opentelemetry.api.OpenTelemetry; |
| 40 | +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; |
| 41 | +import io.opentelemetry.instrumentation.rxjava.v1_0.TracedOnSubscribe; |
| 42 | +import rx.Observable; |
| 43 | + |
| 44 | +public class RxJavaExample { |
| 45 | + public static void main(String[] args) { |
| 46 | + // Get an OpenTelemetry instance |
| 47 | + OpenTelemetry openTelemetry = ...; |
| 48 | + |
| 49 | + // Create an instrumenter for your specific request type |
| 50 | + Instrumenter<String, Void> instrumenter = Instrumenter.<String, Void>builder( |
| 51 | + openTelemetry, |
| 52 | + "instrumentation-name", |
| 53 | + request -> "operation-name") |
| 54 | + .buildInstrumenter(); |
| 55 | + |
| 56 | + // Create a regular Observable |
| 57 | + Observable<String> originalObservable = Observable.just("Hello", "World"); |
| 58 | + |
| 59 | + // Wrap it with tracing |
| 60 | + Observable<String> tracedObservable = Observable.create( |
| 61 | + new TracedOnSubscribe<>(originalObservable, instrumenter, "request-context")); |
| 62 | + |
| 63 | + // Subscribe to the traced observable |
| 64 | + tracedObservable.subscribe(System.out::println); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
0 commit comments