forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfluxdb.py
executable file
·168 lines (148 loc) · 5.85 KB
/
influxdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""InfluxDB client that pushes time series points from JSON."""
import httplib
import json
import sys
import time
import traceback
import urllib
class Point(object):
"""Represents an InfluxDB time series point and handles serialization."""
def __init__(self, measurement, tags, fields, time_stamp):
"""Creates a new Point."""
self.measurement = measurement
self.tags = tags
self.fields = fields
self.time = time_stamp
@staticmethod
def from_dict(dct):
"""Deserialization function to be used by json.load()."""
if any(k not in dct for k in ['measurement', 'tags', 'fields']):
return dct
return Point(
dct['measurement'],
dct['tags'],
dct['fields'],
dct.get('time'),
)
def serialize(self):
"""Encodes the Point in InfluxDB Line Protocol syntax.
For a detailed explanation of Line Protocol's syntax go to:
https://docs.influxdata.com/influxdb/v1.2/write_protocols/line_protocol_tutorial
Here is simple example from that tutorial:
weather,location=us-midwest temperature=82 1465839830100400200
| -------------------- -------------- |
| | | |
| | | |
+-----------+--------+-+---------+-+---------+
|measurement|,tag_set| |field_set| |timestamp|
+-----------+--------+-+---------+-+---------+
"""
if not self.fields:
raise ValueError('Point must have at least one field.\n')
def value_repr(val):
"""Encodes a field value."""
if isinstance(val, bool):
return str(val)
if isinstance(val, basestring):
return '\"%s\"' % val.replace('"', '\"')
return repr(val)
def measure_repr(measurement):
"""Encodes a measurement name."""
return measurement.replace(',', r'\,').replace(' ', r'\ ')
def label_repr(label):
"""Encodes a tag name, tag value, or field name."""
return label.replace(',', r'\,').replace(' ', r'\ ').replace('=', r'\=')
# Tag set must be prefixed with, and joined by a comma.
tags = ''.join([
',%s=%s' % (label_repr(key), label_repr(val))
for key, val in self.tags.iteritems()
])
# Field set is only joined by a comma.
fields = ','.join([
'%s=%s' % (label_repr(key), value_repr(val))
for key, val in self.fields.iteritems()
])
return '%s%s %s%s' % (
measure_repr(self.measurement),
tags,
fields,
' %d' % self.time if self.time else ''
)
class Pusher(object):
"""Client that pushes Point objects to an InfluxDB."""
def __init__(self, host_port, path, user, password):
self.host_port = host_port
self.path = path or ""
self.user = user
self.password = password
@staticmethod
def from_config(config_path):
"""Creates an Pusher for the InfluxDB described by a json config."""
with open(config_path) as config_file:
config = json.load(config_file)
def check_config(field):
if not field in config:
raise ValueError('Pusher config requires field \'%s\'' % field)
check_config('hostport')
check_config('user')
check_config('password')
return Pusher(
config['hostport'],
config.get('path'),
config['user'],
config['password']
)
def push(self, points, database):
"""Pushes time series data points to an InfluxDB in batches."""
params = urllib.urlencode(
{'db': database, 'u': self.user, 'p': self.password, 'precision': 's'}
)
stamp = int(time.time())
for point in points:
if not point.time:
point.time = stamp
while points:
body = '\n'.join(p.serialize() for p in points[:100])
points = points[100:]
for attempt in range(5):
if attempt:
time.sleep(2 ** (attempt - 1))
try:
conn = httplib.HTTPConnection(self.host_port)
conn.request('POST', '%s/write?%s' % (self.path, params), body)
resp = conn.getresponse()
except httplib.HTTPException:
print >>sys.stderr, (
'Exception POSTing influx points to: %s\n%s'
% (self.host_port, traceback.format_exc())
)
continue
if resp.status >= 500:
continue
if resp.status >= 400:
raise Error(
'Error writing InfluxDB points (attempt #%d, status code %d): %s'
% (attempt, resp.status, resp.read())
)
break
else:
raise Error(
'Failed to write InfluxDB points with %d attempts. (status code %d): %s'
% (attempt, resp.status, resp.read())
)
class Error(Exception):
pass