From efd9e0069df3861ead39cf8dd7b78e3ab7f07c66 Mon Sep 17 00:00:00 2001 From: "Tahir H. Butt" Date: Fri, 12 Nov 2021 12:06:10 -0500 Subject: [PATCH 1/2] Add agent endpoint to log messages --- ddtrace/internal/writer.py | 10 +++++++--- tests/integration/test_integration.py | 22 ++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/ddtrace/internal/writer.py b/ddtrace/internal/writer.py index a6d1ed6246b..ad1fe77111a 100644 --- a/ddtrace/internal/writer.py +++ b/ddtrace/internal/writer.py @@ -303,6 +303,10 @@ def __init__( ) self._log_error_payloads = asbool(os.environ.get("_DD_TRACE_WRITER_LOG_ERROR_PAYLOADS", False)) + @property + def _agent_endpoint(self): + return "{}/{}".format(self.agent_url, self._endpoint) + def _metrics_dist(self, name, count=1, tags=None): self._metrics[name]["count"] += count if tags: @@ -358,7 +362,7 @@ def _put(self, data, headers): log_level = logging.WARNING else: log_level = logging.DEBUG - log.log(log_level, "sent %s in %.5fs to %s", _human_size(len(data)), t, self.agent_url) + log.log(log_level, "sent %s in %.5fs to %s", _human_size(len(data)), t, self._agent_endpoint) return Response.from_http_response(resp) finally: conn.close() @@ -416,7 +420,7 @@ def _send_payload(self, payload, count): elif response.status >= 400: msg = "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s" log_args = ( - self.agent_url, + self._agent_endpoint, response.status, response.reason, ) @@ -513,7 +517,7 @@ def flush_queue(self, raise_exc=False): if raise_exc: e.reraise() else: - log.error("failed to send traces to Datadog Agent at %s", self.agent_url, exc_info=True) + log.error("failed to send traces to Datadog Agent at %s", self._agent_endpoint, exc_info=True) finally: if self._report_metrics and self.dogstatsd: # Note that we cannot use the batching functionality of dogstatsd because diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py index 895ab77c8e8..609bbb6fed7 100644 --- a/tests/integration/test_integration.py +++ b/tests/integration/test_integration.py @@ -137,7 +137,11 @@ def test_uds_wrong_socket_path(encoding, monkeypatch): t.trace("client.testing").finish() t.shutdown() calls = [ - mock.call("failed to send traces to Datadog Agent at %s", "unix:///tmp/ddagent/nosockethere", exc_info=True) + mock.call( + "failed to send traces to Datadog Agent at %s", + "unix:///tmp/ddagent/nosockethere/{}/traces".format(encoding if encoding else "v0.4"), + exc_info=True, + ) ] log.error.assert_has_calls(calls) @@ -270,7 +274,13 @@ def test_trace_bad_url(encoding, monkeypatch): pass t.shutdown() - calls = [mock.call("failed to send traces to Datadog Agent at %s", "http://bad:1111", exc_info=True)] + calls = [ + mock.call( + "failed to send traces to Datadog Agent at %s", + "http://bad:1111/{}/traces".format(encoding if encoding else "v0.4"), + exc_info=True, + ) + ] log.error.assert_has_calls(calls) @@ -386,7 +396,7 @@ def encode_traces(self, traces): calls = [ mock.call( "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s", - "http://localhost:8126", + "http://localhost:8126/v0.4", 400, "Bad Request", ) @@ -419,7 +429,7 @@ def encode_traces(self, traces): calls = [ mock.call( "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s, payload %s", - "http://localhost:8126", + "http://localhost:8126/v0.4", 400, "Bad Request", "6261645f7061796c6f6164", @@ -463,7 +473,7 @@ def encode_traces(self, traces): calls = [ mock.call( "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s, payload %s", - "http://localhost:8126", + "http://localhost:8126/v0.4", 400, "Bad Request", "bad_payload", @@ -554,7 +564,7 @@ def test_flush_log(caplog, encoding, monkeypatch): "sent %s in %.5fs to %s", AnyStr(), AnyFloat(), - writer.agent_url, + "{}/{}/traces".format(writer.agent_url, encoding if encoding else "v0.5"), ) ] log.log.assert_has_calls(calls) From 0be5970eebb0b42a0f7a435379c114f4b26034c4 Mon Sep 17 00:00:00 2001 From: "Tahir H. Butt" Date: Fri, 12 Nov 2021 13:24:27 -0500 Subject: [PATCH 2/2] more test fixes --- tests/integration/test_encoding.py | 2 +- tests/integration/test_integration.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/integration/test_encoding.py b/tests/integration/test_encoding.py index 9b9b2c897a6..384d66ad225 100644 --- a/tests/integration/test_encoding.py +++ b/tests/integration/test_encoding.py @@ -73,7 +73,7 @@ def _assert_bad_trace_refused_by_agent(self, mock_log): calls = [ mock.call( "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s", - agent.get_trace_url(), + "{}/{}/traces".format(agent.get_trace_url(), "v0.4"), 400, "Bad Request", ) diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py index 609bbb6fed7..073900b812e 100644 --- a/tests/integration/test_integration.py +++ b/tests/integration/test_integration.py @@ -396,7 +396,7 @@ def encode_traces(self, traces): calls = [ mock.call( "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s", - "http://localhost:8126/v0.4", + "http://localhost:8126/v0.4/traces", 400, "Bad Request", ) @@ -429,7 +429,7 @@ def encode_traces(self, traces): calls = [ mock.call( "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s, payload %s", - "http://localhost:8126/v0.4", + "http://localhost:8126/v0.4/traces", 400, "Bad Request", "6261645f7061796c6f6164", @@ -473,7 +473,7 @@ def encode_traces(self, traces): calls = [ mock.call( "failed to send traces to Datadog Agent at %s: HTTP error status %s, reason %s, payload %s", - "http://localhost:8126/v0.4", + "http://localhost:8126/v0.4/traces", 400, "Bad Request", "bad_payload", @@ -558,13 +558,15 @@ def test_flush_log(caplog, encoding, monkeypatch): with mock.patch("ddtrace.internal.writer.log") as log: writer.write([]) writer.flush_queue(raise_exc=True) + # for latest agent, default to v0.3 since no priority sampler is set + expected_encoding = "v0.3" if AGENT_VERSION == "v5" else (encoding or "v0.3") calls = [ mock.call( logging.DEBUG, "sent %s in %.5fs to %s", AnyStr(), AnyFloat(), - "{}/{}/traces".format(writer.agent_url, encoding if encoding else "v0.5"), + "{}/{}/traces".format(writer.agent_url, expected_encoding), ) ] log.log.assert_has_calls(calls)