Skip to main content

telemetry

Datadog logging handlers for sending logs to Datadog's API.

This module provides handlers that integrate with Python's logging framework to send logs to Datadog, with automatic batching and size management.

Module

Functions

flush_datadog_telemetry

def flush_datadog_telemetry()> None:

Flush the Datadog telemetry buffer.

This should be called to ensure all buffered logs are sent to Datadog.

setup_datadog_telemetry

def setup_datadog_telemetry(    dd_client_token: Optional[str] = None,    dd_site: Optional[str] = None,    service: str = 'pod',    hostname: Optional[str] = None,    tags: Optional[list[str]] = None,)> None:

Setup Datadog telemetry logging if credentials are available.

If credentials are not provided, the telemetry logger will exist but have no handlers, meaning all telemetry logs will be silently dropped.

This function is idempotent - calling it multiple times is safe.

Arguments

  • dd_client_token: The Datadog client token.
  • dd_site: The Datadog site to use (e.g., 'datadoghq.com', 'datadoghq.eu').
  • service: The service to use for the logs.
  • hostname: The hostname to use for the logs. Defaults to system hostname.
  • tags: The tags to use for the logs.

Returns None

shutdown_datadog_telemetry

def shutdown_datadog_telemetry()> None:

Shutdown Datadog telemetry logging and flush any pending logs.

This should be called during application shutdown to ensure all buffered logs are sent to Datadog.

Classes

DatadogLogsHandler

class DatadogLogsHandler(    api_instance: datadog_api_client.v2.api.logs_api.LogsApi,    source: str,    hostname: str,    service: str,    tags: Optional[list[str]] = None,    capacity: int = 1000,):

A MemoryHandler that sends logs to Datadog.

This handler automatically flushes when the buffer approaches a 5MB limit. These numbers are taken from Datadog's Logs API documentation: https://docs.datadoghq.com/api/latest/logs/

We piggyback off Python's MemoryHandler class since we do not want to implement our own buffer management system, including what happens when there are records left in the buffer when the handler is closed.

Initialize the DatadogMemoryHandler.

Arguments

  • api_instance: The Datadog API instance.
  • source: The source of the logs.
  • hostname: The hostname of the logs.
  • service: The service of the logs.
  • tags: The tags of the logs.
  • capacity: The capacity of the buffer, defaulted to 1000 according to Datadog's documentation.

Variables

  • static BUFFER_PERCENTAGE
  • static MAX_BUFFER_SIZE

Methods


emit

def emit(self, record: logging.LogRecord)> None:

Emit a record to the buffer.

Note that we immediately build the HTTPLogItem object and add it to a separate buffer, rather than waiting for the flush operation to do so.

Arguments

  • record: The log record.

flush

def flush(self)> None:

Flush the buffer by sending all records to Datadog in a single request.

Override the parent's flush method to flush records instead of calling emit() per record.

shouldFlush

def shouldFlush(self, record: logging.LogRecord, item_size: int = 0)> bool:

Determine if we should flush the buffer.

Arguments

  • record: The log record.
  • item_size: The size of the item to add to the buffer.

Returns True if we should flush the buffer, False otherwise.