2020-01-06 15:24:52 +01:00
|
|
|
from typing import Optional, Dict
|
|
|
|
|
|
|
|
from platypush.plugins import Plugin, action
|
|
|
|
|
|
|
|
|
|
|
|
class GraphitePlugin(Plugin):
|
|
|
|
"""
|
|
|
|
Plugin for sending data to a Graphite instance.
|
|
|
|
"""
|
|
|
|
|
2020-01-06 19:22:04 +01:00
|
|
|
def __init__(self, host: str = 'localhost', port: int = 2003, timeout: int = 5, **kwargs):
|
2020-01-06 15:24:52 +01:00
|
|
|
"""
|
|
|
|
:param host: Default Graphite host (default: 'localhost').
|
|
|
|
:param port: Default Graphite port (default: 2003).
|
2020-01-06 19:22:04 +01:00
|
|
|
:param timeout: Communication timeout in seconds (default: 5).
|
2020-01-06 15:24:52 +01:00
|
|
|
"""
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2020-01-06 19:22:04 +01:00
|
|
|
self.timeout = timeout
|
2020-01-06 15:24:52 +01:00
|
|
|
|
|
|
|
@action
|
|
|
|
def send(self,
|
2020-01-06 19:22:04 +01:00
|
|
|
metric: str,
|
|
|
|
value,
|
|
|
|
host: Optional[str] = None,
|
|
|
|
port: Optional[int] = None,
|
|
|
|
timeout: Optional[int] = None,
|
|
|
|
tags: Optional[Dict[str, str]] = None,
|
|
|
|
prefix: str = '',
|
|
|
|
protocol: str = 'tcp'):
|
2020-01-06 15:24:52 +01:00
|
|
|
"""
|
|
|
|
Send data to a Graphite instance.
|
|
|
|
|
|
|
|
:param metric: Metric name.
|
|
|
|
:param value: Value to be sent.
|
|
|
|
:param host: Graphite host (default: default configured ``host``).
|
|
|
|
:param port: Graphite port (default: default configured ``port``).
|
|
|
|
:param tags: Map of tags for the metric.
|
|
|
|
:param prefix: Metric prefix name (default: empty string).
|
|
|
|
:param protocol: Communication protocol - possible values: 'tcp', 'udp' (default: 'tcp').
|
|
|
|
"""
|
|
|
|
import graphyte
|
|
|
|
|
|
|
|
host = host or self.host
|
|
|
|
port = port or self.port
|
|
|
|
tags = tags or {}
|
|
|
|
protocol = protocol.lower()
|
|
|
|
|
2020-01-06 19:22:04 +01:00
|
|
|
graphyte.init(host, port=port, prefix=prefix, protocol=protocol, timeout=timeout or self.timeout)
|
2020-01-06 15:24:52 +01:00
|
|
|
graphyte.send(metric, value, tags=tags)
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|