summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/telegraf/protocol.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/pybind/mgr/telegraf/protocol.py
parentInitial commit. (diff)
downloadceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz
ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/pybind/mgr/telegraf/protocol.py')
-rw-r--r--src/pybind/mgr/telegraf/protocol.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/pybind/mgr/telegraf/protocol.py b/src/pybind/mgr/telegraf/protocol.py
new file mode 100644
index 00000000..d243e0c1
--- /dev/null
+++ b/src/pybind/mgr/telegraf/protocol.py
@@ -0,0 +1,44 @@
+from telegraf.utils import format_string, format_value
+
+
+class Line(object):
+ def __init__(self, measurement, values, tags=None, timestamp=None):
+ self.measurement = measurement
+ self.values = values
+ self.tags = tags
+ self.timestamp = timestamp
+
+ def get_output_measurement(self):
+ return format_string(self.measurement)
+
+ def get_output_values(self):
+ if not isinstance(self.values, dict):
+ metric_values = {'value': self.values}
+ else:
+ metric_values = self.values
+
+ sorted_values = sorted(metric_values.items())
+ sorted_values = [(k, v) for k, v in sorted_values if v is not None]
+
+ return u','.join(u'{0}={1}'.format(format_string(k), format_value(v)) for k, v in sorted_values)
+
+ def get_output_tags(self):
+ if not self.tags:
+ self.tags = dict()
+
+ sorted_tags = sorted(self.tags.items())
+
+ return u','.join(u'{0}={1}'.format(format_string(k), format_string(v)) for k, v in sorted_tags)
+
+ def get_output_timestamp(self):
+ return ' {0}'.format(self.timestamp) if self.timestamp else ''
+
+ def to_line_protocol(self):
+ tags = self.get_output_tags()
+
+ return u'{0}{1} {2}{3}'.format(
+ self.get_output_measurement(),
+ "," + tags if tags else '',
+ self.get_output_values(),
+ self.get_output_timestamp()
+ )