Skip to content

Commit

Permalink
feat(tracing): support http server error status configuration (#10723)
Browse files Browse the repository at this point in the history
DD_TRACE_HTTP_SERVER_ERROR_STATUSES can only be enabled via the
programmatic api. This PR adds an environment variable that can be used
to update this configuration.

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [ ] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
mabdinur authored Oct 2, 2024
1 parent 013ae12 commit da7625f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ddtrace/settings/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class Config(object):
"""

class _HTTPServerConfig(object):
_error_statuses = "500-599" # type: str
_error_statuses = os.getenv("DD_TRACE_HTTP_SERVER_ERROR_STATUSES", "500-599") # type: str
_error_ranges = get_error_ranges(_error_statuses) # type: List[Tuple[int, int]]

@property
Expand Down
8 changes: 8 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ The following environment variables for the tracer are supported:
default: True
description: Send query strings in http.url tag in http server integrations.

DD_TRACE_HTTP_SERVER_ERROR_STATUSES:
type: String
default: "500-599"
description: |
Comma-separated list of HTTP status codes that should be considered errors when returned by an HTTP request.
Multiple comma separated error ranges can be set (ex: ``200,400-404,500-599``).
The status codes are used to set the ``error`` field on the span.

DD_TRACE_METHODS:
type: String
default: ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
tracing: Adds ``DD_TRACE_HTTP_CLIENT_ERROR_STATUSES`` environment variable to configure the list of HTTP status codes that should be considered errors when instrumenting HTTP severs.
20 changes: 20 additions & 0 deletions tests/tracer/test_trace_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,26 @@ def test_set_http_meta_custom_errors(mock_log, span, int_config, error_codes, st
mock_log.exception.assert_not_called()


@pytest.mark.subprocess(env={"DD_TRACE_HTTP_SERVER_ERROR_STATUSES": "404-412"})
def test_set_http_meta_custom_errors_via_env():
from ddtrace import config
from ddtrace import tracer
from ddtrace.contrib.trace_utils import set_http_meta

config._add("myint", dict())
with tracer.trace("error") as span1:
set_http_meta(span1, config.myint, status_code=405)
assert span1.error == 1

with tracer.trace("noterror") as span2:
set_http_meta(span2, config.myint, status_code=403)
assert span2.error == 0

with tracer.trace("noterror2") as span3:
set_http_meta(span3, config.myint, status_code=413)
assert span3.error == 0


@mock.patch("ddtrace.contrib.trace_utils._store_headers")
def test_set_http_meta_no_headers(mock_store_headers, span, int_config):
assert int_config.myint.is_header_tracing_configured is False
Expand Down

0 comments on commit da7625f

Please sign in to comment.