-
Notifications
You must be signed in to change notification settings - Fork 153
Add OpenTelemetry v2 integration with enhanced features and comprehensive testing #1314
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
tconley1428
wants to merge
12
commits into
main
Choose a base branch
from
opentelemetryv2-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6214033
Add OpenTelemetry v2 interceptor with enhanced features
tconley1428 b748c4b
Enhance OpenTelemetry v2 integration with comprehensive testing and l…
tconley1428 066438b
Add debugging to comprehensive test
tconley1428 68f17fd
Fix formatting
tconley1428 fa25bbc
Skip test on timeskipping
tconley1428 809b22d
Merge opentelemetry contribs
tconley1428 0518e77
Switch to batch processor
tconley1428 15e77b4
Merge remote-tracking branch 'origin/main' into opentelemetryv2-impro…
tconley1428 a838aeb
Use new workflow random functionality for id generation
tconley1428 0628859
Update to remove global state modification from plugin, and span proc…
tconley1428 94eda7a
Remove inaccurate comment
tconley1428 ae1e7f5
Move otel tests
tconley1428 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,22 @@ | ||
| """OpenTelemetry v2 integration for Temporal SDK. | ||
|
|
||
| This package provides OpenTelemetry tracing integration for Temporal workflows, | ||
| activities, and other operations. It includes automatic span creation and | ||
| propagation for distributed tracing. | ||
| """ | ||
|
|
||
| from temporalio.contrib.opentelemetry._interceptor import ( | ||
| TracingInterceptor, | ||
| TracingWorkflowInboundInterceptor, | ||
| ) | ||
| from temporalio.contrib.opentelemetry._otel_interceptor import OpenTelemetryInterceptor | ||
| from temporalio.contrib.opentelemetry._plugin import OpenTelemetryPlugin | ||
| from temporalio.contrib.opentelemetry._tracer_provider import init_tracer_provider | ||
|
|
||
| __all__ = [ | ||
| "TracingInterceptor", | ||
| "TracingWorkflowInboundInterceptor", | ||
| "OpenTelemetryInterceptor", | ||
| "OpenTelemetryPlugin", | ||
| "init_tracer_provider", | ||
| ] | ||
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,74 @@ | ||
| import random | ||
|
|
||
| from opentelemetry.sdk.trace.id_generator import IdGenerator | ||
| from opentelemetry.trace import ( | ||
| INVALID_SPAN_ID, | ||
| INVALID_TRACE_ID, | ||
| ) | ||
|
|
||
| import temporalio.workflow | ||
|
|
||
|
|
||
| def _get_workflow_random() -> random.Random | None: | ||
| if ( | ||
| temporalio.workflow.in_workflow() | ||
| and not temporalio.workflow.unsafe.is_read_only() | ||
| ): | ||
| # Cache the random on the workflow instance because this IdGenerator is created outside of the workflow | ||
| # but the random should be reseeded for each workflow task | ||
| if ( | ||
| getattr(temporalio.workflow.instance(), "__temporal_otel_id_random", None) | ||
| is None | ||
| ): | ||
| setattr( | ||
| temporalio.workflow.instance(), | ||
| "__temporal_otel_id_random", | ||
| temporalio.workflow.new_random(), | ||
| ) | ||
| return getattr(temporalio.workflow.instance(), "__temporal_otel_id_random") | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| class TemporalIdGenerator(IdGenerator): | ||
| """OpenTelemetry ID generator that uses Temporal's deterministic random generator. | ||
|
|
||
| .. warning:: | ||
| This class is experimental and may change in future versions. | ||
| Use with caution in production environments. | ||
|
|
||
| This generator uses Temporal's workflow-safe random number generator when | ||
| inside a workflow execution, ensuring deterministic span and trace IDs | ||
| across workflow replays. Falls back to standard random generation outside | ||
| of workflows. | ||
| """ | ||
|
|
||
| def __init__(self, id_generator: IdGenerator): | ||
| """Initialize a TemporalIdGenerator.""" | ||
| self._id_generator = id_generator | ||
|
|
||
| def generate_span_id(self) -> int: | ||
| """Generate a span ID using Temporal's deterministic random when in workflow. | ||
|
|
||
| Returns: | ||
| A 64-bit span ID. | ||
| """ | ||
| if workflow_random := _get_workflow_random(): | ||
| span_id = workflow_random.getrandbits(64) | ||
| while span_id == INVALID_SPAN_ID: | ||
| span_id = workflow_random.getrandbits(64) | ||
| return span_id | ||
| return self._id_generator.generate_span_id() | ||
|
|
||
| def generate_trace_id(self) -> int: | ||
| """Generate a trace ID using Temporal's deterministic random when in workflow. | ||
|
|
||
| Returns: | ||
| A 128-bit trace ID. | ||
| """ | ||
| if workflow_random := _get_workflow_random(): | ||
| trace_id = workflow_random.getrandbits(128) | ||
| while trace_id == INVALID_TRACE_ID: | ||
| trace_id = workflow_random.getrandbits(128) | ||
| return trace_id | ||
| return self._id_generator.generate_trace_id() |
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.