Skip to content

Commit

Permalink
Added trace response headers to Falcon
Browse files Browse the repository at this point in the history
  • Loading branch information
owais committed Apr 13, 2021
1 parent 370952f commit f3ff87f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'release/*'
pull_request:
env:
CORE_REPO_SHA: cad261e5dae1fe986c87e6965664b45cc9ab73c3
CORE_REPO_SHA: f6b04c483f6c416e1927f010c07e71a17a5d79d0

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#299](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/299))
- `opentelemetry-instrumenation-django` now supports request and response hooks.
([#407](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/407))
- `opentelemetry-instrumenation-falcon` added trace response headers support to Falcon.
([#432](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/432))

### Removed
- Remove `http.status_text` from span attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def on_get(self, req, resp):
from opentelemetry import context, trace
from opentelemetry.instrumentation.falcon.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.propagators import (
FuncSetter,
get_global_back_propagator,
)
from opentelemetry.instrumentation.utils import (
extract_attributes_from_object,
http_status_to_status_code,
Expand Down Expand Up @@ -148,6 +152,8 @@ def _start_response(status, response_headers, *args, **kwargs):
class _TraceMiddleware:
# pylint:disable=R0201,W0613

back_propagation_setter = FuncSetter(falcon.api.Response.append_header)

def __init__(self, tracer=None, traced_request_attrs=None):
self.tracer = tracer
self._traced_request_attrs = _traced_request_attrs
Expand Down Expand Up @@ -209,3 +215,7 @@ def process_response(
description=reason,
)
)

propagator = get_global_back_propagator()
if propagator:
propagator.inject(resp, setter=self.back_propagation_setter)
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
from falcon import testing

from opentelemetry.instrumentation.falcon import FalconInstrumentor
from opentelemetry.instrumentation.propagators import (
TraceResponsePropagator,
get_global_back_propagator,
set_global_back_propagator,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import StatusCode
from opentelemetry.trace import StatusCode, format_span_id, format_trace_id
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs

from .app import make_app
Expand Down Expand Up @@ -192,6 +197,28 @@ def test_traced_request_attributes(self):
self.assertEqual(span.attributes["query_string"], "q=abc")
self.assertNotIn("not_available_attr", span.attributes)

def test_trace_response(self):
orig = get_global_back_propagator()
set_global_back_propagator(TraceResponsePropagator())

response = self.client().simulate_get(path="/hello?q=abc")
headers = response.headers
span = self.memory_exporter.get_finished_spans()[0]

self.assertIn("traceresponse", headers)
self.assertEqual(
headers["access-control-expose-headers"], "traceresponse",
)
self.assertEqual(
headers["traceresponse"],
"00-{0}-{1}-01".format(
format_trace_id(span.get_span_context().trace_id),
format_span_id(span.get_span_context().span_id),
),
)

set_global_back_propagator(orig)

def test_traced_not_recording(self):
mock_tracer = Mock()
mock_span = Mock()
Expand Down

0 comments on commit f3ff87f

Please sign in to comment.