Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / lib / graphite.py
1 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Interface for sending data to Graphite."""
6
7 from __future__ import print_function
8
9 import socket
10
11 from chromite.lib import cros_build_lib
12
13 CARBON_SERVER = 'chromeos-stats.corp.google.com'
14 CARBON_PORT = 2003
15
16 def SendToCarbon(lines, dryrun=False, process_queue=20):
17   """Send data to the statsd/graphite server.
18
19   Example of a line "autotest.scheduler.running_agents_5m 300"
20   5m is the frequency we are sampling (It is not required but it adds clarity
21   to the metric).
22
23   Args:
24     lines: A list of lines of the format "category value"
25     dryrun: Print out what you would send but do not send anything.
26       [default: False]
27     process_queue: How many lines to send to the statsd server at a
28       time. [defualt: 20]
29   """
30   sock = socket.socket()
31
32   try:
33     sock.connect((CARBON_SERVER, CARBON_PORT))
34   except Exception:
35     cros_build_lib.Error('Failed to connect to Carbon.')
36     raise
37
38   slices = [lines[i:i+process_queue]
39             for i in range(0, len(lines), process_queue)]
40   for lines in slices:
41     data = '\n'.join(lines) + '\n'
42     if dryrun:
43       cros_build_lib.Info('Not sending to Graphite via Carbon:\n%s', data)
44     else:
45       cros_build_lib.Debug('Sending to Graphite via Carbon:\n%s', data)
46       sock.sendall(data)