Repository navigation
Make the OpenTelemetry v2 logger provider replay safe #3091
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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,164 @@ | ||
| # OpenTelemetry v2 integration for the Temporal Java SDK | ||
|
|
||
| Module `io.temporal:temporal-opentelemetry-v2` provides replay-safe | ||
| OpenTelemetry tracing, metrics, and logs for Temporal. | ||
|
|
||
| > [!WARNING] | ||
| > This module is experimental, and its API may change. | ||
|
|
||
| ## Setup | ||
|
|
||
| Use the same version as the rest of your Temporal Java SDK dependencies: | ||
|
|
||
| ```groovy | ||
| implementation 'io.temporal:temporal-opentelemetry-v2:<temporal-java-sdk-version>' | ||
| // Add the exporters you use, such as this OTLP exporter. | ||
| implementation 'io.opentelemetry:opentelemetry-exporter-otlp' | ||
| ``` | ||
|
|
||
| Create a replay-safe OpenTelemetry instance, register it as the global, and | ||
| attach the plugin to your service stubs: | ||
|
|
||
| ```java | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter; | ||
| import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter; | ||
| import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter; | ||
| import io.opentelemetry.sdk.logs.SdkLoggerProvider; | ||
| import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor; | ||
| import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
| import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.opentelemetry.v2.OpenTelemetryPlugin; | ||
| import io.temporal.opentelemetry.v2.ReplaySafeOpenTelemetry; | ||
| import io.temporal.serviceclient.WorkflowServiceStubs; | ||
| import io.temporal.serviceclient.WorkflowServiceStubsOptions; | ||
| import io.temporal.worker.WorkerFactory; | ||
|
|
||
| ReplaySafeOpenTelemetry openTelemetry = | ||
| ReplaySafeOpenTelemetry.newBuilder() | ||
| .setTracerProviderBuilder( | ||
| SdkTracerProvider.builder() | ||
| .addSpanProcessor( | ||
| BatchSpanProcessor.builder(OtlpGrpcSpanExporter.builder().build()).build())) | ||
| .setMeterProviderBuilder( | ||
| SdkMeterProvider.builder() | ||
| .registerMetricReader( | ||
| PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build()) | ||
| .build())) | ||
| .setLoggerProviderBuilder( | ||
| SdkLoggerProvider.builder() | ||
| .addLogRecordProcessor( | ||
| BatchLogRecordProcessor.builder(OtlpGrpcLogRecordExporter.builder().build()) | ||
| .build())) | ||
| .build(); | ||
|
patbeqo marked this conversation as resolved.
|
||
| GlobalOpenTelemetry.set(openTelemetry); | ||
|
|
||
| WorkflowServiceStubs service = | ||
| WorkflowServiceStubs.newServiceStubs( | ||
| WorkflowServiceStubsOptions.newBuilder() | ||
| .setPlugins(OpenTelemetryPlugin.newBuilder().build()) | ||
| .build()); | ||
|
|
||
| WorkflowClient client = WorkflowClient.newInstance(service); | ||
| WorkerFactory factory = WorkerFactory.newInstance(client); | ||
| ``` | ||
|
|
||
| Plugins configured on `WorkflowServiceStubsOptions` propagate to clients and | ||
| workers created from those stubs. It can also be configured directly on | ||
| `WorkflowClientOptions`. | ||
|
|
||
| Build `ReplaySafeOpenTelemetry` before anything else uses the OpenTelemetry | ||
| `Context`, and register it as the global before building the plugin. Otherwise, | ||
| `OpenTelemetryPlugin.Builder.build()` throws `IllegalStateException`. | ||
|
|
||
| Call `ReplaySafeOpenTelemetry.close()` after your workers and clients have | ||
| stopped to export buffered telemetry and shut down its providers. | ||
|
|
||
| ## Tracing | ||
|
|
||
| The plugin propagates application trace context through Temporal headers. | ||
| Application spans remain connected across clients, workflows, activities, and | ||
| Nexus operations. | ||
|
|
||
| Set `OpenTelemetryPlugin.Builder.setAddTemporalSpans(true)` to emit spans for | ||
| operations such as `StartWorkflow`, `RunWorkflow`, `RunActivity`, and | ||
| `ContinueAsNew`. | ||
|
|
||
| Create spans in workflow, client, and activity code with the standard OpenTelemetry API: | ||
|
|
||
| ```java | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.context.Scope; | ||
|
|
||
| Span span = | ||
| GlobalOpenTelemetry.getTracer("my-workflows") | ||
| .spanBuilder("my-span") | ||
| .startSpan(); | ||
| try (Scope ignored = span.makeCurrent()) { | ||
| activity.doWork(); | ||
| } finally { | ||
| span.end(); | ||
| } | ||
| ``` | ||
|
|
||
| ## Metrics | ||
|
|
||
| Create metrics in workflow, client, and activity code with the standard | ||
| OpenTelemetry API. Workflow code supports only synchronous instruments: | ||
|
|
||
| ```java | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.metrics.LongCounter; | ||
|
|
||
| LongCounter counter = | ||
| GlobalOpenTelemetry.getMeter("my-workflows") | ||
| .counterBuilder("workflow.items.processed") | ||
| .build(); | ||
| counter.add(1); | ||
| ``` | ||
|
|
||
| ## Logs | ||
|
|
||
| Create logs in workflow, client, and activity code with SLF4J, and send them to | ||
| OpenTelemetry by installing a logging appender, such as the | ||
| [OpenTelemetry Logback appender](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/logback/logback-appender-1.0/library), | ||
| with the replay-safe instance. The appender drops records from replaying | ||
| workflow code only when it is installed with `ReplaySafeOpenTelemetry`, not a | ||
| plain `OpenTelemetrySdk`: | ||
|
|
||
| ```java | ||
| import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter; | ||
| import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender; | ||
| import io.opentelemetry.sdk.logs.SdkLoggerProvider; | ||
| import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor; | ||
| import io.temporal.opentelemetry.v2.ReplaySafeOpenTelemetry; | ||
|
|
||
| ReplaySafeOpenTelemetry openTelemetry = | ||
| ReplaySafeOpenTelemetry.newBuilder() | ||
| .setLoggerProviderBuilder( | ||
| SdkLoggerProvider.builder() | ||
| .addLogRecordProcessor( | ||
| BatchLogRecordProcessor.builder(OtlpGrpcLogRecordExporter.builder().build()) | ||
| .build())) | ||
| .build(); | ||
| OpenTelemetryAppender.install(openTelemetry); | ||
| ``` | ||
|
|
||
| Then log with SLF4J: | ||
|
|
||
| ```java | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| Logger logger = LoggerFactory.getLogger(MyWorkflowImpl.class); | ||
| logger.info("workflow step completed"); | ||
| ``` | ||
|
|
||
| Configure the appender as its setup guide describes. Replay is checked on the | ||
| thread that emits the record, so behind an asynchronous appender, such as | ||
| Logback's `AsyncAppender`, log from workflow code with `Workflow.getLogger()`, | ||
| which skips logging during replay. | ||
|
patbeqo marked this conversation as resolved.
|
||
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.