Skip to content

Commit 6ef024a

Browse files
Add env variables to configure Writer behavior (#2581)
* Add env variables to configure Writer behavior DD_TRACE_WRITER_BUFFER_SIZE_BYTES - How many bytes of traces to buffer between flushes to the agent DD_TRACE_WRITER_MAX_PAYLOAD_SIZE_BYTES - Max size of any payload to write to the trace agent DD_TRACE_WRITER_INTERVAL_SECONDS - How many seconds between flushes to the agent * move around and update * go back to DD_TRACE_WRITER_* * fix flake8 * Add release note * Update releasenotes/notes/add-writer-env-config-7c0916dff4f7f80c.yaml Co-authored-by: Kyle Verhoog <[email protected]> * fix tests Co-authored-by: Kyle Verhoog <[email protected]>
1 parent 73401ab commit 6ef024a

File tree

4 files changed

+110
-3
lines changed

4 files changed

+110
-3
lines changed

ddtrace/internal/writer.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ..constants import KEEP_SPANS_RATE_KEY
2222
from ..sampler import BasePrioritySampler
2323
from ..sampler import BaseSampler
24+
from ..utils.formats import get_env
2425
from ..utils.time import StopWatch
2526
from .agent import get_connection
2627
from .buffer import BufferFull
@@ -47,6 +48,29 @@
4748
# to 10 buckets of 1s duration.
4849
DEFAULT_SMA_WINDOW = 10
4950

51+
DEFAULT_BUFFER_SIZE = 8 * 1000000 # 8mb
52+
DEFAULT_MAX_PAYLOAD_SIZE = 8 * 1000000 # 8mb
53+
DEFAULT_PROCESSING_INTERVAL = 1.0
54+
55+
56+
def get_writer_buffer_size():
57+
# type: () -> int
58+
return int(get_env("trace", "writer_buffer_size_bytes", default=DEFAULT_BUFFER_SIZE)) # type: ignore[arg-type]
59+
60+
61+
def get_writer_max_payload_size():
62+
# type: () -> int
63+
return int(
64+
get_env("trace", "writer_max_payload_size_bytes", default=DEFAULT_MAX_PAYLOAD_SIZE) # type: ignore[arg-type]
65+
)
66+
67+
68+
def get_writer_interval_seconds():
69+
# type: () -> float
70+
return float(
71+
get_env("trace", "writer_interval_seconds", default=DEFAULT_PROCESSING_INTERVAL) # type: ignore[arg-type]
72+
)
73+
5074

5175
def _human_size(nbytes):
5276
"""Return a human-readable size."""
@@ -200,11 +224,11 @@ def __init__(
200224
agent_url, # type: str
201225
sampler=None, # type: Optional[BaseSampler]
202226
priority_sampler=None, # type: Optional[BasePrioritySampler]
203-
processing_interval=1.0, # type: float
227+
processing_interval=get_writer_interval_seconds(), # type: float
204228
# Match the payload size since there is no functionality
205229
# to flush dynamically.
206-
buffer_size=8 * 1000000, # type: int
207-
max_payload_size=8 * 1000000, # type: int
230+
buffer_size=get_writer_buffer_size(), # type: int
231+
max_payload_size=get_writer_max_payload_size(), # type: int
208232
timeout=agent.get_trace_agent_timeout(), # type: float
209233
dogstatsd=None, # type: Optional[DogStatsd]
210234
report_metrics=False, # type: bool

docs/configuration.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ below:
8383
- Float
8484
- 2.0
8585
- The timeout in float to use to connect to the Datadog agent.
86+
* - ``DD_TRACE_WRITER_BUFFER_SIZE_BYTES``
87+
- Int
88+
- 8000000
89+
- The max size in bytes of traces to buffer between flushes to the agent.
90+
* - ``DD_TRACE_WRITER_MAX_PAYLOAD_SIZE_BYTES``
91+
- Int
92+
- 8000000
93+
- The max size in bytes of each payload sent to the trace agent. If max payload size is less than buffer size, multiple payloads will be sent to the trace agent.
94+
* - ``DD_TRACE_WRITER_INTERVAL_SECONDS``
95+
- Float
96+
- 1.0
97+
- The time between each flush of traces to the trace agent.
8698
* - ``DD_TRACE_STARTUP_LOGS``
8799
- Boolean
88100
- False
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
features:
3+
- |
4+
Add new environment variables to configure the internal trace writer.
5+
6+
``DD_TRACE_WRITER_MAX_BUFFER_SIZE``, ``DD_TRACE_WRITER_INTERVAL_SECONDS``,
7+
``DD_TRACE_WRITER_MAX_PAYLOAD_SIZE_BYTES``

tests/integration/test_integration.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,67 @@ def test_call_basic_config(ddtrace_run_python_code_in_subprocess, call_basic_con
497497
assert out == six.b("1\n")
498498
else:
499499
assert out == six.b("0\n")
500+
501+
502+
def test_writer_env_configuration(run_python_code_in_subprocess):
503+
env = os.environ.copy()
504+
env["DD_TRACE_WRITER_BUFFER_SIZE_BYTES"] = "1000"
505+
env["DD_TRACE_WRITER_MAX_PAYLOAD_SIZE_BYTES"] = "5000"
506+
env["DD_TRACE_WRITER_INTERVAL_SECONDS"] = "5.0"
507+
508+
out, err, status, pid = run_python_code_in_subprocess(
509+
"""
510+
import ddtrace
511+
512+
assert ddtrace.tracer.writer._buffer.max_size == 1000
513+
assert ddtrace.tracer.writer._buffer.max_item_size == 5000
514+
assert ddtrace.tracer.writer._interval == 5.0
515+
""",
516+
env=env,
517+
)
518+
assert status == 0, (out, err)
519+
520+
521+
def test_writer_env_configuration_defaults(run_python_code_in_subprocess):
522+
out, err, status, pid = run_python_code_in_subprocess(
523+
"""
524+
import ddtrace
525+
526+
assert ddtrace.tracer.writer._buffer.max_size == 8000000
527+
assert ddtrace.tracer.writer._buffer.max_item_size == 8000000
528+
assert ddtrace.tracer.writer._interval == 1.0
529+
""",
530+
)
531+
assert status == 0, (out, err)
532+
533+
534+
def test_writer_env_configuration_ddtrace_run(ddtrace_run_python_code_in_subprocess):
535+
env = os.environ.copy()
536+
env["DD_TRACE_WRITER_BUFFER_SIZE_BYTES"] = "1000"
537+
env["DD_TRACE_WRITER_MAX_PAYLOAD_SIZE_BYTES"] = "5000"
538+
env["DD_TRACE_WRITER_INTERVAL_SECONDS"] = "5.0"
539+
540+
out, err, status, pid = ddtrace_run_python_code_in_subprocess(
541+
"""
542+
import ddtrace
543+
544+
assert ddtrace.tracer.writer._buffer.max_size == 1000
545+
assert ddtrace.tracer.writer._buffer.max_item_size == 5000
546+
assert ddtrace.tracer.writer._interval == 5.0
547+
""",
548+
env=env,
549+
)
550+
assert status == 0, (out, err)
551+
552+
553+
def test_writer_env_configuration_ddtrace_run_defaults(ddtrace_run_python_code_in_subprocess):
554+
out, err, status, pid = ddtrace_run_python_code_in_subprocess(
555+
"""
556+
import ddtrace
557+
558+
assert ddtrace.tracer.writer._buffer.max_size == 8000000
559+
assert ddtrace.tracer.writer._buffer.max_item_size == 8000000
560+
assert ddtrace.tracer.writer._interval == 1.0
561+
""",
562+
)
563+
assert status == 0, (out, err)

0 commit comments

Comments
 (0)