def send_to_telegraf(self): url = urlparse(self.config['address']) sock = BaseSocket(url) self.log.debug('Sending data to Telegraf at %s', sock.address) now = self.now() with sock as s: try: for measurement in self.gather_measurements(): self.log.debug(measurement) line = Line(measurement['measurement'], measurement['value'], measurement['tags'], now) self.log.debug(line.to_line_protocol()) s.send(line.to_line_protocol()) except (socket.error, RuntimeError, IOError, OSError): self.log.exception('Failed to send statistics to Telegraf:')
def metric(self, measurement_name, values, tags=None, timestamp=None): """ Append global tags configured for the client to the tags given then converts the data into InfluxDB Line protocol and sends to to socket """ if not measurement_name or not values: # Don't try to send empty data return tags = tags or {} # Do a shallow merge of the metric tags and global tags all_tags = dict(self.tags, **tags) # Create a metric line from the input and then send it to socket line = Line(measurement_name, values, all_tags, timestamp) self.send(line.to_line_protocol())
def test_values_ordered_properly(self): self.assertEquals( Line('some_series', { 'a': 1, 'baa': 1, 'AAA': 1, 'aaa': 1 }).to_line_protocol(), 'some_series AAA=1i,a=1i,aaa=1i,baa=1i')
def test_tags_and_measurement_with_whitespace_and_comma(self): self.assertEquals( Line('white space', { 'value, comma': "foo" }, { 'tag with, comma': 'hello, world' }).to_line_protocol(), """white\ space,tag\ with\,\ comma=hello\,\ world value\,\ comma="foo\"""" )
def test_multiple_values_and_tags(self): self.assertEquals( Line('some_series', { 'value': 232.123, 'value2': 123 }, { 'foo': 'bar', 'foobar': 'baz' }).to_line_protocol(), 'some_series,foo=bar,foobar=baz value=232.123,value2=123i')
def test_with_timestamp(self): self.assertEquals( Line('some_series', 1000, timestamp=1234134).to_line_protocol(), 'some_series value=1000i 1234134')
def test_value_escaped_and_quoted(self): self.assertEquals( Line('some_series', 'foo "bar"').to_line_protocol(), 'some_series value="foo \"bar\""')
def test_boolean_value(self): self.assertEquals( Line('some_series', True).to_line_protocol(), 'some_series value=True')
def test_multiple_values(self): self.assertEquals( Line('some_series', { 'value': 232.123, 'value2': 123 }).to_line_protocol(), 'some_series value=232.123,value2=123i')
def test_single_value_and_tags(self): self.assertEquals( Line('some_series', 1, { 'foo': 'bar', 'foobar': 'baz' }).to_line_protocol(), 'some_series,foo=bar,foobar=baz value=1i')
def test_single_value(self): self.assertEquals( Line('some_series', 1).to_line_protocol(), 'some_series value=1i')