Skip to content

Commit

Permalink
Use default TraceContext propagator in example (open-telemetry#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t committed Jan 31, 2020
1 parent 27f04b6 commit aecc347
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
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
23 changes: 11 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,17 @@ 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),
)

0 comments on commit aecc347

Please sign in to comment.