-
Notifications
You must be signed in to change notification settings - Fork 552
Basic OTel support #1772
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
Merged
Merged
Basic OTel support #1772
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d3164cf
OTel support (added SpanProcessor and Propagator)
antonpirker 98d351f
Added tests for NoOpSpan
antonpirker 8580692
Some polishing
antonpirker 2fe376e
Fixed imports
antonpirker 0ba528b
Cleanup
antonpirker 197dc96
Added opentelemetry to the setup.py extras_require
antonpirker 61601b5
Added proper types to NoOpSpan
antonpirker 9e3d9d6
Refactored infinite loop breaking code
antonpirker 5f2b105
Made for loop simpler
antonpirker c3c3f46
Fixed tests
antonpirker f93886b
Added otel span kind
antonpirker fb1afe3
Simplified code.
antonpirker 3fe5ec8
Added checks for valid context
antonpirker dbb22e4
Fixed circular imports
antonpirker b09d094
Checking if context is valid.
antonpirker 031e79a
Made code easier to follow
antonpirker e129dbb
Merge branch 'master' into antonpirker/1687-basic-otel
antonpirker 301a5c4
Better readability
antonpirker 69dc721
Updated ci config
antonpirker 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,73 @@ | ||
name: Test opentelemetry | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- release/** | ||
|
||
pull_request: | ||
|
||
# Cancel in progress workflows on pull_requests. | ||
# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
permissions: | ||
contents: read | ||
|
||
env: | ||
BUILD_CACHE_KEY: ${{ github.sha }} | ||
CACHED_BUILD_PATHS: | | ||
${{ github.workspace }}/dist-serverless | ||
|
||
jobs: | ||
test: | ||
name: opentelemetry, python ${{ matrix.python-version }}, ${{ matrix.os }} | ||
runs-on: ${{ matrix.os }} | ||
timeout-minutes: 45 | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: ["3.7","3.8","3.9","3.10"] | ||
# python3.6 reached EOL and is no longer being supported on | ||
# new versions of hosted runners on Github Actions | ||
# ubuntu-20.04 is the last version that supported python3.6 | ||
# see https://github.com/actions/setup-python/issues/544#issuecomment-1332535877 | ||
os: [ubuntu-20.04] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Setup Test Env | ||
run: | | ||
pip install codecov "tox>=3,<4" | ||
|
||
- name: Test opentelemetry | ||
timeout-minutes: 45 | ||
shell: bash | ||
run: | | ||
set -x # print commands that are executed | ||
coverage erase | ||
|
||
./scripts/runtox.sh "${{ matrix.python-version }}-opentelemetry" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch | ||
coverage combine .coverage* | ||
coverage xml -i | ||
codecov --file coverage.xml | ||
|
||
check_required_tests: | ||
name: All opentelemetry tests passed or skipped | ||
needs: test | ||
# Always run this, even if a dependent job failed | ||
if: always() | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Check for failures | ||
if: contains(needs.test.result, 'failure') | ||
run: | | ||
echo "One of the dependent jobs have failed. You may need to re-run it." && exit 1 |
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,7 @@ | ||
from sentry_sdk.integrations.opentelemetry.span_processor import ( # noqa: F401 | ||
SentrySpanProcessor, | ||
) | ||
|
||
from sentry_sdk.integrations.opentelemetry.propagator import ( # noqa: F401 | ||
SentryPropagator, | ||
) |
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,6 @@ | ||
from opentelemetry.context import ( # type: ignore | ||
create_key, | ||
) | ||
|
||
SENTRY_TRACE_KEY = create_key("sentry-trace") | ||
SENTRY_BAGGAGE_KEY = create_key("sentry-baggage") |
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,113 @@ | ||
from opentelemetry import trace # type: ignore | ||
from opentelemetry.context import ( # type: ignore | ||
Context, | ||
get_current, | ||
set_value, | ||
) | ||
from opentelemetry.propagators.textmap import ( # type: ignore | ||
CarrierT, | ||
Getter, | ||
Setter, | ||
TextMapPropagator, | ||
default_getter, | ||
default_setter, | ||
) | ||
from opentelemetry.trace import ( # type: ignore | ||
TraceFlags, | ||
NonRecordingSpan, | ||
SpanContext, | ||
) | ||
from sentry_sdk.integrations.opentelemetry.consts import ( | ||
SENTRY_BAGGAGE_KEY, | ||
SENTRY_TRACE_KEY, | ||
) | ||
from sentry_sdk.integrations.opentelemetry.span_processor import ( | ||
SentrySpanProcessor, | ||
) | ||
|
||
from sentry_sdk.tracing import ( | ||
BAGGAGE_HEADER_NAME, | ||
SENTRY_TRACE_HEADER_NAME, | ||
) | ||
from sentry_sdk.tracing_utils import Baggage, extract_sentrytrace_data | ||
from sentry_sdk._types import MYPY | ||
|
||
if MYPY: | ||
from typing import Optional | ||
from typing import Set | ||
|
||
|
||
class SentryPropagator(TextMapPropagator): # type: ignore | ||
""" | ||
Propagates tracing headers for Sentry's tracing system in a way OTel understands. | ||
""" | ||
|
||
def extract(self, carrier, context=None, getter=default_getter): | ||
# type: (CarrierT, Optional[Context], Getter) -> Context | ||
if context is None: | ||
context = get_current() | ||
|
||
sentry_trace = getter.get(carrier, SENTRY_TRACE_HEADER_NAME) | ||
if not sentry_trace: | ||
return context | ||
|
||
sentrytrace = extract_sentrytrace_data(sentry_trace[0]) | ||
if not sentrytrace: | ||
return context | ||
|
||
context = set_value(SENTRY_TRACE_KEY, sentrytrace, context) | ||
|
||
trace_id, span_id = sentrytrace["trace_id"], sentrytrace["parent_span_id"] | ||
|
||
span_context = SpanContext( | ||
trace_id=int(trace_id, 16), # type: ignore | ||
span_id=int(span_id, 16), # type: ignore | ||
# we simulate a sampled trace on the otel side and leave the sampling to sentry | ||
trace_flags=TraceFlags(TraceFlags.SAMPLED), | ||
is_remote=True, | ||
) | ||
|
||
baggage_header = getter.get(carrier, BAGGAGE_HEADER_NAME) | ||
|
||
if baggage_header: | ||
baggage = Baggage.from_incoming_header(baggage_header[0]) | ||
else: | ||
# If there's an incoming sentry-trace but no incoming baggage header, | ||
# for instance in traces coming from older SDKs, | ||
# baggage will be empty and frozen and won't be populated as head SDK. | ||
baggage = Baggage(sentry_items={}) | ||
|
||
baggage.freeze() | ||
context = set_value(SENTRY_BAGGAGE_KEY, baggage, context) | ||
|
||
span = NonRecordingSpan(span_context) | ||
modified_context = trace.set_span_in_context(span, context) | ||
return modified_context | ||
|
||
def inject(self, carrier, context=None, setter=default_setter): | ||
# type: (CarrierT, Optional[Context], Setter) -> None | ||
if context is None: | ||
context = get_current() | ||
|
||
current_span = trace.get_current_span(context) | ||
antonpirker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if not current_span.context.is_valid: | ||
return | ||
|
||
span_id = trace.format_span_id(current_span.context.span_id) | ||
|
||
span_map = SentrySpanProcessor().otel_span_map | ||
sentry_span = span_map.get(span_id, None) | ||
if not sentry_span: | ||
return | ||
|
||
setter.set(carrier, SENTRY_TRACE_HEADER_NAME, sentry_span.to_traceparent()) | ||
|
||
baggage = sentry_span.containing_transaction.get_baggage() | ||
if baggage: | ||
setter.set(carrier, BAGGAGE_HEADER_NAME, baggage.serialize()) | ||
|
||
@property | ||
def fields(self): | ||
# type: () -> Set[str] | ||
return {SENTRY_TRACE_HEADER_NAME, BAGGAGE_HEADER_NAME} |
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.