Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Python Dict to InfluxDB?

I've been playing around trying to figure out how to write a python dict into InfluxDB with no luck. I've tried using the InfluxDBClient as well as just using a Requests Post via API.

I have the following code:

for server in  data['service_group_stat']['member_stat_list']:
    metrics = {}
    metrics['measurement'] = "LB01"
    metrics['tags'] = {}
    metrics['fields'] = {}
    metrics['tags']['SGNAME'] = name
    metrics['tags']['SRVNAME'] = server['server']
    metrics['fields']['CURCONNS'] = server['cur_conns']
    metrics['fields']['TOTCONNS'] = server['tot_conns']
    metrics['fields']['REQBYTES'] = server['req_bytes']
    metrics['fields']['REQPKTS'] = server['req_pkts']
    metrics['fields']['RESPBYTES'] = server['resp_bytes']
    metrics['fields']['RESPPKTS'] = server['resp_pkts']
    pprint(metrics)

Which will give me the following output:

{'fields': {'CURCONNS': 33,
            'REQBYTES': 3151292236,
            'REQPKTS': 21160834,
            'RESPBYTES': 66671993850,
            'RESPPKTS': 51709706,
            'TOTCONNS': 332177},
 'measurement': 'LB01',
 'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
          'SRVNAME': u'WWW0006'}}
{'fields': {'CURCONNS': 39,
            'REQBYTES': 3387948728,
            'REQPKTS': 23103920,
            'RESPBYTES': 71703285665,
            'RESPPKTS': 55687510,
            'TOTCONNS': 369628},
 'measurement': 'LB01',
 'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
          'SRVNAME': u'WWW0005'}}
{'fields': {'CURCONNS': 16,
            'REQBYTES': 3401134891,
            'REQPKTS': 24014042,
            'RESPBYTES': 70917802336,
            'RESPPKTS': 55029480,
            'TOTCONNS': 342360},
 'measurement': 'LB01',
 'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
          'SRVNAME': u'WWW0004'}}

I've tried just using a requests.post to leverage the InfluxDB API:

url = "http://localhost:8086/write?db=metrics"

r = requests.post(url, metrics)

However I seem to always get a 400 response:

reply: 'HTTP/1.1 400 Bad Request\r\n'

I've also tried using the InfluxDBClient:

from influxdb import InfluxDBClient

# Define InfluxDB Client Information

client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='metrics')

for server in  data['service_group_stat']['member_stat_list']:
    metrics = {}
    metrics['measurement'] = "LB01"
    metrics['tags'] = {}
    metrics['fields'] = {}
    metrics['tags']['SGNAME'] = name
    metrics['tags']['SRVNAME'] = server['server']
    metrics['fields']['CURCONNS'] = server['cur_conns']
    metrics['fields']['TOTCONNS'] = server['tot_conns']
    metrics['fields']['REQBYTES'] = server['req_bytes']
    metrics['fields']['REQPKTS'] = server['req_pkts']
    metrics['fields']['RESPBYTES'] = server['resp_bytes']
    metrics['fields']['RESPPKTS'] = server['resp_pkts']

client.write_points(metrics)

Which ends up giving me the following error:

    client.write_points(metrics)
  File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 456, in write_points
    tags=tags, protocol=protocol)
  File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 506, in _write_points
    protocol=protocol
  File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 292, in write
    data = make_lines(data, precision).encode('utf-8')
  File "/usr/lib/python2.7/site-packages/influxdb/line_protocol.py", line 126, in make_lines
    point.get('measurement', data.get('measurement'))
AttributeError: 'str' object has no attribute 'get'

Any advice on what what I'm doing wrong?

like image 429
ddevalco Avatar asked Jun 15 '26 22:06

ddevalco


1 Answers

You have to pass a list of dicts to InfluxDBClient.write_points() when using the json protocol (which is default).
You can check the source code of the make_lines() method to be convinced that it iterates over points.

Answer : use client.write_points([metrics])

like image 174
Phylante Avatar answered Jun 17 '26 11:06

Phylante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!