Skip to content

Commit

Permalink
feat(tracing): support http server error status env
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Sep 19, 2024
1 parent b07f73a commit afae1c3
Show file tree
Hide file tree
Showing 4 changed files with 32 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
9 changes: 9 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,15 @@ The following environment variables for the tracer are supported:
type: Boolean
default: True
description: Send query strings in http.url tag in http server integrations.


DD_TRACE_HTTP_CLIENT_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 client request.
The status codes are used to set the ``error`` field on the span.


DD_TRACE_SPAN_AGGREGATOR_RLOCK:
type: Boolean
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.
18 changes: 18 additions & 0 deletions tests/tracer/test_trace_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,24 @@ 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_CLIENT_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.http_server.error_statuses = "404-412"

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


@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 afae1c3

Please sign in to comment.