本文整理汇总了Python中tempodb.Client.write_bulk方法的典型用法代码示例。如果您正苦于以下问题:Python Client.write_bulk方法的具体用法?Python Client.write_bulk怎么用?Python Client.write_bulk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tempodb.Client
的用法示例。
在下文中一共展示了Client.write_bulk方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Client
# 需要导入模块: from tempodb import Client [as 别名]
# 或者: from tempodb.Client import write_bulk [as 别名]
"""
http://tempo-db.com/api/write-series/#bulk-write-multiple-series
"""
import datetime
from tempodb import Client
client = Client("your-api-key", "your-api-secret")
ts = datetime.datetime.now()
data = [
{"key": "custom-series-key1", "v": 1.11},
{"key": "custom-series-key2", "v": 2.22},
{"key": "custom-series-key3", "v": 3.33},
{"key": "custom-series-key4", "v": 4.44},
]
print client.write_bulk(ts, data)
示例2: StatsdServer
# 需要导入模块: from tempodb import Client [as 别名]
# 或者: from tempodb.Client import write_bulk [as 别名]
class StatsdServer(object):
def __init__(self, conf):
TRUE_VALUES = set(('true', '1', 'yes', 'on', 't', 'y'))
self.logger = logging.getLogger('statsdpy')
self.logger.setLevel(logging.INFO)
self.syslog = SysLogHandler()
self.formatter = logging.Formatter('%(name)s: %(message)s')
self.syslog.setFormatter(self.formatter)
self.logger.addHandler(self.syslog)
self.conf = conf
self.graphite_host = conf.get('graphite_host', '127.0.0.1')
self.graphite_port = int(conf.get('graphite_port', '2003'))
self.listen_addr = conf.get('listen_addr', '127.0.0.1')
self.listen_port = int(conf.get('listen_port', 8125))
self.debug = conf.get('debug', 'yes') in TRUE_VALUES
self.combined_events = conf.get('combined_events', 'no') in TRUE_VALUES
self.flush_interval = int(conf.get('flush_interval', 10))
self.pct_threshold = int(conf.get('percent_threshold', 90))
self.graphite_addr = (self.graphite_host, self.graphite_port)
self.keycheck = re.compile(r'\s+|/|[^a-zA-Z_\-0-9\.]')
self.ratecheck = re.compile('^@([\d\.]+)')
self.counters = {}
self.timers = {}
self.stats_seen = 0
self.tempodb_enabled = conf.get('tempodb_enabled', 'false') in TRUE_VALUES
self.tempodb_key = conf.get('tempodb_key', '')
self.tempodb_secret = conf.get('tempodb_secret', '')
self.tempodb_host = conf.get('tempodb_host', 'localhost')
self.tempodb_port = int(conf.get('tempodb_port', 443))
self.tempodb_secure = conf.get('tempodb_secure', "true") in TRUE_VALUES
self.tempodb_client = TempoDBClient(self.tempodb_key, self.tempodb_secret, \
self.tempodb_host, self.tempodb_port, \
self.tempodb_secure)
def report_stats(self, payload):
"""
Send data
"""
if self.debug:
print "reporting stats -> {\n%s}" % "".join(payload)
self.report_stats_graphite(payload)
if self.tempodb_enabled:
self.report_stats_tempodb(payload)
def report_stats_graphite(self, payload):
"""
Send data to graphite host
:param stats: Data to send to graphite (list of graphite strings)
"""
try:
with eventlet.Timeout(5, True) as timeout:
graphite = socket.socket()
graphite.connect(self.graphite_addr)
graphite.sendall("".join(payload))
graphite.close()
except Exception as err:
self.logger.critical("error connecting to graphite: %s" % err)
if self.debug:
print "error connecting to graphite: %s" % err
def report_stats_tempodb(self, payload):
data = {}
for stat in payload:
components = stat[:-1].split(" ")
if len(components) == 3:
key = components[0]
value = float(components[1])
ts = datetime.datetime.utcfromtimestamp(int(components[2]))
data.setdefault(ts, list()).append({
'key': key,
'v': value,
})
try:
with eventlet.Timeout(5, True):
for key, value in data.items():
self.tempodb_client.write_bulk(key, value)
except Exception as err:
self.logger.critical("error connecting to tempodb: %s" % err)
if self.debug:
print "error connecting to tempodb: %s" % err
def stats_flush(self):
"""
Periodically flush stats to graphite
"""
payload = []
while True:
tstamp = int(time.time())
eventlet.sleep(self.flush_interval)
if self.debug:
print "seen %d stats so far." % self.stats_seen
print "current counters: %s" % self.counters
for item in self.counters:
stats = 'stats.%s %s %s\n' % (item,
#.........这里部分代码省略.........
示例3: Client
# 需要导入模块: from tempodb import Client [as 别名]
# 或者: from tempodb.Client import write_bulk [as 别名]
"""
http://tempo-db.com/api/write-series/#bulk-write-multiple-series
"""
import datetime
from tempodb import Client
client = Client("your-api-key", "your-api-secret")
bulk = {
"t": datetime.datetime.now().isoformat(),
"data": [
{"key": "custom-series-key1", "v": 1.11},
{"key": "custom-series-key2", "v": 2.22},
{"key": "custom-series-key3", "v": 3.33},
{"key": "custom-series-key4", "v": 4.44},
],
}
print client.write_bulk(bulk)
示例4: ClientTest
# 需要导入模块: from tempodb import Client [as 别名]
# 或者: from tempodb.Client import write_bulk [as 别名]
#.........这里部分代码省略.........
data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)]
result = self.client.write_key("key1", data)
self.client.session.post.assert_called_once_with(
'https://example.com/v1/series/key/key1/data/',
auth=('key', 'secret'),
data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""",
headers=self.post_headers
)
self.assertEquals(result, '')
def test_write_key_escape(self):
self.client.session.post.return_value = MockResponse(200, "")
data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)]
result = self.client.write_key("ke:y/1", data)
self.client.session.post.assert_called_once_with(
'https://example.com/v1/series/key/ke%3Ay%2F1/data/',
auth=('key', 'secret'),
data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""",
headers=self.post_headers
)
self.assertEquals(result, '')
def test_increment_id(self):
self.client.session.post.return_value = MockResponse(200, "")
data = [DataPoint(datetime.datetime(2012, 3, 27), 1)]
result = self.client.increment_id("id1", data)
self.client.session.post.assert_called_once_with(
'https://example.com/v1/series/id/id1/increment/',
auth=('key', 'secret'),
data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""",
headers=self.post_headers
)
self.assertEquals(result, '')
def test_increment_key(self):
self.client.session.post.return_value = MockResponse(200, "")
data = [DataPoint(datetime.datetime(2012, 3, 27), 1)]
result = self.client.increment_key("key1", data)
self.client.session.post.assert_called_once_with(
'https://example.com/v1/series/key/key1/increment/',
auth=('key', 'secret'),
data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""",
headers=self.post_headers
)
self.assertEquals(result, '')
def test_increment_key_escape(self):
self.client.session.post.return_value = MockResponse(200, "")
data = [DataPoint(datetime.datetime(2012, 3, 27), 1)]
result = self.client.increment_key("ke:y/1", data)
self.client.session.post.assert_called_once_with(
'https://example.com/v1/series/key/ke%3Ay%2F1/increment/',
auth=('key', 'secret'),
data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""",
headers=self.post_headers
)
self.assertEquals(result, '')
def test_write_bulk(self):
self.client.session.post.return_value = MockResponse(200, "")
data = [
{ 'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4.164 },
{ 'id': '38268c3b231f1266a392931e15e99231', 'v': 73.13 },
{ 'key': 'your-custom-key', 'v': 55.423 },
{ 'key': 'foo', 'v': 324.991 },
]
ts = datetime.datetime(2012, 3, 27)
result = self.client.write_bulk(ts, data)
self.client.session.post.assert_called_once_with(
'https://example.com/v1/data/',
auth=('key', 'secret'),
data="""{"data": %s, "t": "2012-03-27T00:00:00"}""" % simplejson.dumps(data),
headers=self.post_headers
)
self.assertEqual(result, '')
def test_increment_bulk(self):
self.client.session.post.return_value = MockResponse(200, "")
data = [
{ 'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4 },
{ 'id': '38268c3b231f1266a392931e15e99231', 'v': 2 },
{ 'key': 'your-custom-key', 'v': 1 },
{ 'key': 'foo', 'v': 1 },
]
ts = datetime.datetime(2012, 3, 27)
result = self.client.increment_bulk(ts, data)
self.client.session.post.assert_called_once_with(
'https://example.com/v1/increment/',
auth=('key', 'secret'),
data="""{"data": %s, "t": "2012-03-27T00:00:00"}""" % simplejson.dumps(data),
headers=self.post_headers
)
self.assertEqual(result, '')
示例5: Client
# 需要导入模块: from tempodb import Client [as 别名]
# 或者: from tempodb.Client import write_bulk [as 别名]
"""
http://tempo-db.com/api/write-series/#bulk-write-multiple-series
"""
from __future__ import print_function
import datetime
from tempodb import Client
# Modify these with your credentials found at: http://tempo-db.com/manage/
API_KEY = 'your-api-key'
API_SECRET = 'your-api-secret'
SERIES_KEY = 'your-custom-key'
client = Client(API_KEY, API_SECRET)
ts = datetime.datetime.now()
data = [
{ 'key': 'custom-series-key1', 'v': 1.11 },
{ 'key': 'custom-series-key2', 'v': 2.22 },
{ 'key': 'custom-series-key3', 'v': 3.33 },
{ 'key': 'custom-series-key4', 'v': 4.44 },
]
print(client.write_bulk(ts, data))