Skip to content

feat(flask): store request headers #1735

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 12 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ This PR adds support for [`<integration>`](<!--link to relevant integration docs
- [ ] 500-level responses are tagged as errors
- [ ] Async (if applicable)
- [ ] Span parenting behaves as expected.
- [ ] Request headers specified in the config are stored
3 changes: 3 additions & 0 deletions ddtrace/contrib/flask/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from ...internal.logger import get_logger
from ...propagation.http import HTTPPropagator
from ...utils.deprecation import deprecated
from ddtrace.http import store_request_headers
from ddtrace import config

import flask.templating
from flask import g, request, signals
Expand Down Expand Up @@ -155,6 +157,7 @@ def _finish_span(self, span, exception=None):
endpoint = ''
url = ''
if request:
store_request_headers(request.headers, span, config.flask)
method = request.method
endpoint = request.endpoint or code
url = request.base_url or ''
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/feature-flask-4bdb2b839fada0fb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
Store request headers in Flask integration.
15 changes: 15 additions & 0 deletions tests/contrib/flask/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ddtrace.contrib.flask import TraceMiddleware
from ddtrace.constants import SAMPLING_PRIORITY_KEY
from ddtrace.ext import http, errors
from ddtrace import config

from tests.opentracer.utils import init_tracer
from .web import create_app
Expand Down Expand Up @@ -385,3 +386,17 @@ def test_success_200_ot(self):
assert dd_span.error == 0
assert_span_http_status_code(dd_span, 200)
assert dd_span.meta.get(http.METHOD) == 'GET'

def test_http_request_header_tracing(self):
config.flask.http.trace_headers(['Host', 'my-header'])
self.app.get('/', headers={
'my-header': 'my_value',
})
traces = self.tracer.writer.pop_traces()

assert len(traces) == 1
assert len(traces[0]) == 1
span = traces[0][0]

assert span.get_tag('http.request.headers.my-header') == 'my_value'
assert span.get_tag('http.request.headers.host') == 'localhost'