|
| 1 | +# OpenTelemetry v2 integration for the Temporal Java SDK |
| 2 | + |
| 3 | +Module `io.temporal:temporal-opentelemetry-v2` provides replay-safe |
| 4 | +OpenTelemetry tracing, metrics, and logs for Temporal. |
| 5 | + |
| 6 | +## Setup |
| 7 | + |
| 8 | +Use the same version as the rest of your Temporal Java SDK dependencies: |
| 9 | + |
| 10 | +```groovy |
| 11 | +implementation 'io.temporal:temporal-opentelemetry-v2:<temporal-java-sdk-version>' |
| 12 | +// Add the exporters you use, for example: |
| 13 | +implementation 'io.opentelemetry:opentelemetry-exporter-otlp' |
| 14 | +``` |
| 15 | + |
| 16 | +Create a replay-safe OpenTelemetry instance, register it as the global, and |
| 17 | +attach the plugin to your service stubs: |
| 18 | + |
| 19 | +```java |
| 20 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 21 | +import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter; |
| 22 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 23 | +import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; |
| 24 | +import io.temporal.client.WorkflowClient; |
| 25 | +import io.temporal.opentelemetry.v2.OpenTelemetryPlugin; |
| 26 | +import io.temporal.opentelemetry.v2.ReplaySafeOpenTelemetry; |
| 27 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 28 | +import io.temporal.serviceclient.WorkflowServiceStubsOptions; |
| 29 | +import io.temporal.worker.WorkerFactory; |
| 30 | + |
| 31 | +ReplaySafeOpenTelemetry openTelemetry = |
| 32 | + ReplaySafeOpenTelemetry.newBuilder() |
| 33 | + .setTracerProviderBuilder( |
| 34 | + SdkTracerProvider.builder() |
| 35 | + .addSpanProcessor( |
| 36 | + BatchSpanProcessor.builder(OtlpGrpcSpanExporter.builder().build()).build())) |
| 37 | + .build(); |
| 38 | +GlobalOpenTelemetry.set(openTelemetry); |
| 39 | + |
| 40 | +WorkflowServiceStubs service = |
| 41 | + WorkflowServiceStubs.newServiceStubs( |
| 42 | + WorkflowServiceStubsOptions.newBuilder() |
| 43 | + .setPlugins(OpenTelemetryPlugin.newBuilder().build()) |
| 44 | + .build()); |
| 45 | + |
| 46 | +WorkflowClient client = WorkflowClient.newInstance(service); |
| 47 | +WorkerFactory factory = WorkerFactory.newInstance(client); |
| 48 | +``` |
| 49 | + |
| 50 | +Plugins configured on `WorkflowServiceStubsOptions` propagate to clients and |
| 51 | +workers created from those stubs. It can also be configured directly on |
| 52 | +`WorkflowClientOptions` or `WorkerFactoryOptions`. |
| 53 | + |
| 54 | +## Tracing |
| 55 | + |
| 56 | +The plugin propagates application trace context through Temporal headers. |
| 57 | +Application spans remain connected across clients, workflows, activities, and |
| 58 | +Nexus operations. |
| 59 | + |
| 60 | +Set `OpenTelemetryPlugin.Builder.setAddTemporalSpans(true)` to emit spans for |
| 61 | +operations such as `StartWorkflow`, `RunWorkflow`, `RunActivity`, and |
| 62 | +`ContinueAsNew`. |
| 63 | + |
| 64 | +Create spans in workflow, client, and activity code with the standard OpenTelemetry API: |
| 65 | + |
| 66 | +```java |
| 67 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 68 | +import io.opentelemetry.api.trace.Span; |
| 69 | +import io.opentelemetry.context.Scope; |
| 70 | + |
| 71 | +Span span = |
| 72 | + GlobalOpenTelemetry.getTracer("my-workflows") |
| 73 | + .spanBuilder("my-span") |
| 74 | + .startSpan(); |
| 75 | +try (Scope ignored = span.makeCurrent()) { |
| 76 | + activity.doWork(); |
| 77 | +} finally { |
| 78 | + span.end(); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Metrics |
| 83 | + |
| 84 | +Create synchronous metrics in workflow, client, and activity code with the standard OpenTelemetry API: |
| 85 | + |
| 86 | +```java |
| 87 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 88 | +import io.opentelemetry.api.metrics.LongCounter; |
| 89 | + |
| 90 | +LongCounter counter = |
| 91 | + GlobalOpenTelemetry.getMeter("my-workflows") |
| 92 | + .counterBuilder("workflow.items.processed") |
| 93 | + .build(); |
| 94 | +counter.add(1); |
| 95 | +``` |
| 96 | + |
| 97 | +## Logs |
| 98 | + |
| 99 | +Create log records in workflow, client, and activity code with the standard OpenTelemetry API: |
| 100 | + |
| 101 | +```java |
| 102 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 103 | + |
| 104 | +GlobalOpenTelemetry.get() |
| 105 | + .getLogsBridge() |
| 106 | + .get("my-workflows") |
| 107 | + .logRecordBuilder() |
| 108 | + .setBody("workflow step completed") |
| 109 | + .emit(); |
| 110 | +``` |
0 commit comments