To export metrics to a StatsD server, use the ToStatsDMetricsExporter class. This requires a target object that implements the StatsDBuffer protocol.
To implement StatsDBuffer, your target class must provide the following methods:
count(name: str, value: Union[float, int])timing(name: str, value: Union[float, int])gauge(name: str, value: Union[float, int])
You can pass an optional prefix string to the exporter to customize the metric name hierarchy.
import statsd
from typing import Union
from aqueduct import Flow, FlowStep
from aqueduct.metrics import ToStatsDMetricsExporter
from aqueduct.metrics.export import StatsDBuffer
# 1. Implement the StatsDBuffer protocol
class AqueductStatsd(StatsDBuffer):
def __init__(self):
self.statsd_client = statsd.StatsClient('localhost', 8125)
def count(self, name: str, value: Union[float, int]):
self.statsd_client.incr(name, value)
def timing(self, name: str, value: Union[float, int]):
self.statsd_client.timing(name, value)
def gauge(self, name: str, value: Union[float, int]):
self.statsd_client.gauge(name, value)
def create_flow() -> Flow:
aqueduct_statsd = AqueductStatsd()
return Flow(
FlowStep(
SumHandler()
),
# 2. Pass the exporter to the Flow
metrics_exporter=ToStatsDMetricsExporter(
target=aqueduct_statsd,
prefix='app_custom_prefix'
),
)