diff --git a/ddtrace/settings/config.py b/ddtrace/settings/config.py index a491dff35dd..17904a3323c 100644 --- a/ddtrace/settings/config.py +++ b/ddtrace/settings/config.py @@ -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 diff --git a/docs/configuration.rst b/docs/configuration.rst index cb707117b68..29b78f51f1b 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -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 diff --git a/releasenotes/notes/support_http_client_error_status_env-acdf049c64330a20.yaml b/releasenotes/notes/support_http_client_error_status_env-acdf049c64330a20.yaml new file mode 100644 index 00000000000..a00925d78c3 --- /dev/null +++ b/releasenotes/notes/support_http_client_error_status_env-acdf049c64330a20.yaml @@ -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. diff --git a/tests/tracer/test_trace_utils.py b/tests/tracer/test_trace_utils.py index 57403f796b8..f593d6af8d0 100644 --- a/tests/tracer/test_trace_utils.py +++ b/tests/tracer/test_trace_utils.py @@ -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