Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

## Unreleased
* [Fixed] Fix missing transport attribute when flushing telemetry.
* [Added] Add ability to disable statsd via new `statsd_disable` parameter in `initialize`.

## v0.52.0 / 2025-07-08

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ tags = ["version:1", "application:web"]
api.Event.create(title=title, text=text, tags=tags)
```

In development, you can disable any `statsd` metric collection using `DD_DOGSTATSD_DISABLE=True` (or any not-empty value).
In development, you can disable any `statsd` metric collection by either:
1. Setting environment variable `DD_DOGSTATSD_DISABLE` to `True`, `true`, `yes`, or `1`.
2. Setting the `statsd_disable` option to `True` in the `initialize` function.

## DogStatsD

Expand Down
9 changes: 9 additions & 0 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def initialize(
api_host=None, # type: Optional[str]
statsd_host=None, # type: Optional[str]
statsd_port=None, # type: Optional[int]
statsd_disable=False, # type: bool
statsd_disable_aggregation=True, # type: bool
statsd_disable_buffering=True, # type: bool
statsd_aggregation_flush_interval=0.3, # type: float
Expand Down Expand Up @@ -76,6 +77,9 @@ def initialize(
:param statsd_port: Port of DogStatsd server or statsd daemon
:type statsd_port: port

:param statsd_disable: Disable any statsd metric collection (default False).
:type statsd_disable: bool

:param statsd_disable_buffering: Enable/disable statsd client buffering support
(default: True).
:type statsd_disable_buffering: boolean
Expand Down Expand Up @@ -151,6 +155,11 @@ def initialize(
if statsd_constant_tags:
statsd.constant_tags += statsd_constant_tags

if statsd_disable:
statsd.disable_statsd()
else:
statsd.enable_statsd()

if statsd_disable_aggregation:
statsd.disable_aggregation()
else:
Expand Down
17 changes: 14 additions & 3 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def __init__(
port=DEFAULT_PORT, # type: int
max_buffer_size=None, # type: None
flush_interval=DEFAULT_BUFFERING_FLUSH_INTERVAL, # type: float
disable_statsd=False, # type: bool
disable_aggregation=True, # type: bool
disable_buffering=True, # type: bool
namespace=None, # type: Optional[Text]
Expand Down Expand Up @@ -250,6 +251,10 @@ def __init__(
it overrides the default value.
:type flush_interval: float

:disable_statsd: Disable any statsd metric collection (default False).
Overridden by DD_DOGSTATSD_DISABLE enviroment variable.
:type disable_statsd: bool

:disable_aggregation: If true, metrics (Count, Gauge, Set) are no longer aggregated by the client
:type disable_aggregation: bool

Expand Down Expand Up @@ -396,10 +401,10 @@ def __init__(
telemetry_port = os.environ.get("DD_TELEMETRY_PORT", telemetry_port) or port

# Check enabled
if os.environ.get("DD_DOGSTATSD_DISABLE") not in {"True", "true", "yes", "1"}:
self._enabled = True
else:
if os.environ.get("DD_DOGSTATSD_DISABLE", disable_statsd) in {True, "True", "true", "yes", "1"}:
self._enabled = False
else:
self._enabled = True

# Connection
self._max_buffer_len = max_buffer_len
Expand Down Expand Up @@ -693,6 +698,12 @@ def disable_buffering(self, is_disabled):
self._send = self._send_to_buffer
self._start_flush_thread()

def disable_statsd(self):
self._enabled = False

def enable_statsd(self):
self._enabled = True

def disable_aggregation(self):
with self._config_lock:
# If the toggle didn't change anything, this method is a noop
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,20 @@ def _test_flush_interval(self, socket_kind):
u'page.®views®:1|c\n',
fake_socket.recv(2, no_wait=True)
)

def test_statsd_disabled_dgram(self):
self._test_statsd_disabled(socket.SOCK_DGRAM)

def test_statsd_disabled_stream(self):
self._test_statsd_disabled(socket.SOCK_STREAM)

def _test_statsd_disabled(self, socket_kind):
dogstatsd = DogStatsd(disable_statsd=True, telemetry_min_flush_interval=0)
fake_socket = FakeSocket(socket_kind=socket_kind)
dogstatsd.socket = fake_socket
dogstatsd.increment('_test_statsd_disabled')
dogstatsd.flush()
self.assertIsNone(fake_socket.recv(no_wait=True))

def test_aggregation_buffering_simultaneously_dgram(self):
self._test_aggregation_buffering_simultaneously(socket.SOCK_DGRAM)
Expand Down Expand Up @@ -2209,4 +2223,4 @@ def test_transport_attribute_present_on_connection_error(self):
statsd.gauge('test.metric', 1)

assert statsd.socket is None
assert statsd._transport is not None
assert statsd._transport is not None