-
Notifications
You must be signed in to change notification settings - Fork 250
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
+480
−7
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # OpenTelemetry v2 integration for the Temporal Java SDK | ||
|
|
||
| Module `io.temporal:temporal-opentelemetry-v2` provides replay-safe | ||
| OpenTelemetry tracing, metrics, and logs for Temporal. | ||
|
|
||
| ## 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.trace.OtlpGrpcSpanExporter; | ||
| 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())) | ||
| .build(); | ||
| 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` or `WorkerFactoryOptions`. | ||
|
|
||
| ## 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 synchronous metrics in workflow, client, and activity code with the standard OpenTelemetry API: | ||
|
|
||
| ```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 log records in workflow, client, and activity code with the standard OpenTelemetry API: | ||
|
|
||
| ```java | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
|
|
||
| GlobalOpenTelemetry.get() | ||
| .getLogsBridge() | ||
| .get("my-workflows") | ||
| .logRecordBuilder() | ||
| .setBody("workflow step completed") | ||
| .emit(); | ||
| ``` | ||
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
168 changes: 168 additions & 0 deletions
168
...pentelemetry-v2/src/main/java/io/temporal/opentelemetry/v2/internal/ReplaySafeLogger.java
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,168 @@ | ||
| package io.temporal.opentelemetry.v2.internal; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.Value; | ||
| import io.opentelemetry.api.logs.LogRecordBuilder; | ||
| import io.opentelemetry.api.logs.Logger; | ||
| import io.opentelemetry.api.logs.Severity; | ||
| import io.opentelemetry.context.Context; | ||
| import java.time.Instant; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * Wraps a logger so the records it builds are not emitted by replaying workflow code, which would | ||
| * otherwise emit them again on every replay. | ||
| */ | ||
| public final class ReplaySafeLogger implements Logger { | ||
| private final Logger delegate; | ||
|
|
||
| public ReplaySafeLogger(Logger delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder logRecordBuilder() { | ||
| return new ReplaySafeLogRecordBuilder(delegate.logRecordBuilder()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled(Severity severity, Context context) { | ||
| return !OpenTelemetrySuppression.shouldSuppress() && delegate.isEnabled(severity, context); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled(Severity severity) { | ||
| return !OpenTelemetrySuppression.shouldSuppress() && delegate.isEnabled(severity); | ||
| } | ||
|
|
||
| private static final class ReplaySafeLogRecordBuilder implements LogRecordBuilder { | ||
| private final LogRecordBuilder delegate; | ||
|
|
||
| ReplaySafeLogRecordBuilder(LogRecordBuilder delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public void emit() { | ||
| if (OpenTelemetrySuppression.shouldSuppress()) { | ||
| return; | ||
| } | ||
| delegate.emit(); | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setTimestamp(long timestamp, TimeUnit unit) { | ||
| delegate.setTimestamp(timestamp, unit); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setTimestamp(Instant instant) { | ||
| delegate.setTimestamp(instant); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setObservedTimestamp(long timestamp, TimeUnit unit) { | ||
| delegate.setObservedTimestamp(timestamp, unit); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setObservedTimestamp(Instant instant) { | ||
| delegate.setObservedTimestamp(instant); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setContext(Context context) { | ||
| delegate.setContext(context); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setSeverity(Severity severity) { | ||
| delegate.setSeverity(severity); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setSeverityText(String severityText) { | ||
| delegate.setSeverityText(severityText); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setBody(String body) { | ||
| delegate.setBody(body); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setBody(Value<?> body) { | ||
| delegate.setBody(body); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setAllAttributes(Attributes attributes) { | ||
| delegate.setAllAttributes(attributes); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public <T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value) { | ||
| delegate.setAttribute(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setAttribute(String key, String value) { | ||
| delegate.setAttribute(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setAttribute(String key, long value) { | ||
| delegate.setAttribute(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setAttribute(String key, double value) { | ||
| delegate.setAttribute(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setAttribute(String key, boolean value) { | ||
| delegate.setAttribute(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setAttribute(String key, int value) { | ||
| delegate.setAttribute(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setAttribute(String key, Value<?> value) { | ||
| delegate.setAttribute(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setEventName(String eventName) { | ||
| delegate.setEventName(eventName); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public LogRecordBuilder setException(Throwable throwable) { | ||
| delegate.setException(throwable); | ||
| return this; | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Wire metric and log exporters in the setup sample
This sample configures only
setTracerProviderBuilder, andReplaySafeOpenTelemetry.Builderdefaults the meter and logger providers to bareSdkMeterProvider.builder()andSdkLoggerProvider.builder()with no reader or processor. A user who follows this Setup section and then the Metrics and Logs sections verbatim records into providers that export nothing, with no hint why. Please add.setMeterProviderBuilder(SdkMeterProvider.builder().registerMetricReader(PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build()).build()))and.setLoggerProviderBuilder(SdkLoggerProvider.builder().addLogRecordProcessor(BatchLogRecordProcessor.builder(OtlpGrpcLogRecordExporter.builder().build()).build()))to the sample, or one sentence in each section saying an exporter must be configured on the corresponding provider.