Skip to content

Commit 9a170dd

Browse files
fix(writer): logging request timing (bp #2064) (#2069)
* fix(writer): logging request timing (#2064) The logging was moved around in a previous patch. Also add a regression test for the logging. (cherry picked from commit 47c4da2) * fix merge conflict * fix test The test assumed that an empty payload could be sent with the agent however the patch for this is not backported Co-authored-by: Kyle Verhoog <[email protected]> Co-authored-by: Kyle Verhoog <[email protected]>
1 parent 12ef190 commit 9a170dd

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

ddtrace/internal/writer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ def _put(self, data, headers):
194194
try:
195195
conn.request("PUT", self._endpoint, data, headers)
196196
resp = compat.get_connection_response(conn)
197+
t = sw.elapsed()
198+
if t >= self.interval:
199+
log_level = logging.WARNING
200+
else:
201+
log_level = logging.DEBUG
202+
log.log(log_level, "sent %s in %.5fs", _human_size(len(data)), t)
197203
return Response.from_http_response(resp)
198204
finally:
199205
conn.close()
200-
t = sw.elapsed()
201-
if t >= self.interval:
202-
log_level = logging.WARNING
203-
else:
204-
log_level = logging.DEBUG
205-
log.log(log_level, "sent %s in %.5fs", _human_size(len(data)), t)
206206

207207
@property
208208
def agent_url(self):

tests/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,3 +911,8 @@ def __eq__(self, other):
911911
class AnyInt(object):
912912
def __eq__(self, other):
913913
return isinstance(other, int)
914+
915+
916+
class AnyFloat(object):
917+
def __eq__(self, other):
918+
return isinstance(other, float)

tests/integration/test_integration.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import subprocess
34
import sys
@@ -16,7 +17,9 @@
1617
from ddtrace.sampler import RateSampler
1718
from ddtrace.sampler import SamplingRule
1819
from ddtrace.vendor import six
20+
from tests import AnyFloat
1921
from tests import AnyInt
22+
from tests import AnyStr
2023
from tests import TracerTestCase
2124
from tests import override_global_config
2225
from tests import snapshot
@@ -504,3 +507,16 @@ def test_sampling(self, tracer):
504507
pass
505508

506509
tracer.shutdown()
510+
511+
512+
@pytest.mark.skipif(AGENT_VERSION == "testagent", reason="Test agent doesn't support empty trace payloads.")
513+
def test_flush_log(caplog):
514+
caplog.set_level(logging.INFO)
515+
tracer = Tracer()
516+
with tracer.trace("test"):
517+
pass
518+
519+
with mock.patch("ddtrace.internal.writer.log") as log:
520+
tracer.writer.flush_queue()
521+
calls = [mock.call(logging.DEBUG, "sent %s in %.5fs", AnyStr(), AnyFloat())]
522+
log.log.assert_has_calls(calls)

0 commit comments

Comments
 (0)