Skip to content
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

Use default TraceContext propagator in example #188

Merged
merged 8 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import requests

import opentelemetry.ext.http_requests
from opentelemetry import propagators, trace
from opentelemetry import trace
from opentelemetry.ext.flask import instrument_app
from opentelemetry.sdk.context.propagation.b3_format import B3Format
from opentelemetry.sdk.trace import TracerSource


Expand All @@ -43,17 +42,14 @@ def configure_opentelemetry(flask_app: flask.Flask):
"""
# Start by configuring all objects required to ensure
# a complete end to end workflow.
# the preferred implementation of these objects must be set,
# The preferred implementation of these objects must be set,
# as the opentelemetry-api defines the interface with a no-op
# implementation.
trace.set_preferred_tracer_source_implementation(lambda _: TracerSource())

# Next, we need to configure how the values that are used by
# traces and metrics are propagated (such as what specific headers
# carry this value).

# TBD: can remove once default TraceContext propagators are installed.
propagators.set_global_httptextformat(B3Format())

# Integrations are the glue that binds the OpenTelemetry API
# and the frameworks and libraries that are used together, automatically
# creating Spans and propagating context as appropriate.
Expand Down
22 changes: 10 additions & 12 deletions examples/opentelemetry-example-app/tests/test_flask_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from werkzeug.wrappers import BaseResponse

import opentelemetry_example_app.flask_example as flask_example
from opentelemetry.sdk import trace
from opentelemetry.sdk.context.propagation import b3_format
from opentelemetry import trace
from opentelemetry.sdk import trace as trace_sdk


class TestFlaskExample(unittest.TestCase):
Expand All @@ -46,7 +46,7 @@ def tearDown(self):
self.send_patcher.stop()

def test_full_path(self):
trace_id = trace.generate_trace_id()
trace_id = trace_sdk.generate_trace_id()
# We need to use the Werkzeug test app because
# The headers are injected at the wsgi layer.
# The flask test app will not include these, and
Expand All @@ -56,18 +56,16 @@ def test_full_path(self):
client.get(
"/",
headers={
"x-b3-traceid": b3_format.format_trace_id(trace_id),
"x-b3-spanid": b3_format.format_span_id(
trace.generate_span_id()
),
"x-b3-sampled": "1",
"traceparent": "00-{:032x}-{:016x}-{:02x}".format(
trace_id,
trace_sdk.generate_span_id(),
trace.TraceOptions.SAMPLED,
)
},
)
# assert the http request header was propagated through.
prepared_request = self.send.call_args[0][1]
headers = prepared_request.headers
for required_header in {"x-b3-traceid", "x-b3-spanid", "x-b3-sampled"}:
self.assertIn(required_header, headers)
self.assertEqual(
headers["x-b3-traceid"], b3_format.format_trace_id(trace_id)
self.assertRegex(
headers["traceparent"], r"00-{:032x}-[0-9a-f]{{16}}-01".format(trace_id)
)