def setUpClass(cls): user = os.environ.get('LIBRATO_USER') token = os.environ.get('LIBRATO_TOKEN') """Initialize the Librato Connection""" assert user and token, "Must set LIBRATO_USER and LIBRATO_TOKEN to run tests" cls.conn = librato.connect(user, token) cls.conn_sanitize = librato.connect(user, token, sanitizer=librato.sanitize_metric_name)
def setup_class(cls): user = os.environ.get('LIBRATO_USER') token = os.environ.get('LIBRATO_TOKEN') """Initialize the Librato Connection""" assert user and token, "Must set LIBRATO_USER and LIBRATO_TOKEN to run tests" cls.conn = librato.connect(user, token) cls.conn_sanitize = librato.connect(user, token, sanitizer=librato.sanitize_metric_name)
def librato_connect(): ''' Connect to the Librato service. See https://metrics.librato.com/ ''' apikey = os.getenv('LIBRATOKEY') libratoapi = librato.connect('*****@*****.**', apikey) return libratoapi
def run(): try: parent_proc = psutil.Process( int(file("{{ lantern_pid }}").read().strip())) java_proc, = parent_proc.get_children() meminfo = java_proc.get_ext_memory_info() process_memory = meminfo.rss - meminfo.shared connections = len(java_proc.get_connections(kind='inet')) io = java_proc.get_io_counters() io_ops = io.read_count + io.write_count io_bytes = io.read_bytes + io.write_bytes except (IOError, ValueError, psutil.NoSuchProcess): logging.warn("Lantern not running; reporting zero stats.") process_memory = connections = io_ops = io_bytes = 0 net = psutil.net_io_counters() conn = librato.connect(LIBRATO_USERNAME, LIBRATO_TOKEN) q = conn.new_queue() for k, v in [('process_memory', process_memory), ('process_connections', connections), ('process_io_ops', io_ops), ('process_io_bytes', io_bytes), ('bytes_sent', net.bytes_sent), ('bytes_received', net.bytes_recv), ('net_errors', net.errin + net.errout), ('net_drops', net.dropin + net.dropout), ('page_outs', get_swapouts())]: q.add(k, v, source=SOURCE) q.submit()
def __init__(self, **kwargs): self.email = kwargs.pop('email', None) self.token = kwargs.pop('token', None) if (self.email or self.token) is None: raise exceptions.MissingRequiredConfig( 'Missing authentication info') self.connection = librato.connect(self.email, self.token)
def __init__(self, config=None): """ Create a new instance of the LibratoHandler class """ # Initialize Handler Handler.__init__(self, config) logging.debug("Initialized Librato handler.") if librato is None: logging.error("Failed to load librato module") return # Initialize Options api = librato.connect(self.config['user'], self.config['apikey']) self.queue = api.new_queue() self.queue_max_size = int(self.config['queue_max_size']) self.queue_max_interval = int(self.config['queue_max_interval']) self.queue_max_timestamp = int(time.time() + self.queue_max_interval) self.current_n_measurements = 0 # If a user leaves off the ending comma, cast to a array for them include_filters = self.config['include_filters'] if isinstance(include_filters, basestring): include_filters = [include_filters] self.include_reg = re.compile(r'(?:%s)' % '|'.join(include_filters))
def __init__(self, mode, source): threading.Thread.__init__(self, name='metrics-worker') self._mode = mode self._source = source self.queue = METRICS_QUEUE self.logger = logging.getLogger('graphlab.metrics') self._tracker = None # librato metrics tracker self._mixpanel = None # Mixpanel metrics tracker buffer_size = 5 offline_buffer_size = 25 self._sys_info_set = False self._usable = False try: if self._mode != 'PROD': self.logger.info("Using MetricMock instead of real metrics, mode is: %s" % self._mode) self._tracker = MetricMock() self._mixpanel = MetricMock() else: self._tracker = librato.connect(CONFIG.librato_user, CONFIG.librato_token) self._mixpanel = mixpanel.Mixpanel(CONFIG.mixpanel_user) except Exception, e: self.logger.warning("Unexpected exception connecting to Metrics service, disabling metrics, exception %s" % e)
def __init__(self, config=None): """ Create a new instance of the LibratoHandler class """ # Initialize Handler Handler.__init__(self, config) logging.debug("Initialized Librato handler.") if librato is None: logging.error("Failed to load librato module") return # Initialize Options self.api = librato.connect(self.config["user"], self.config["apikey"]) self.queue = [] self.queue_max_age = int(self.config["queue_max_age"]) self.queue_max_size = int(self.config["queue_max_size"]) self.queue_max_interval = int(self.config["queue_max_interval"]) self.queue_max_timestamp = int(time.time() + self.queue_max_interval) self.disable_force_flush = bool(self.config["disable_force_flush"]) # If a user leaves off the ending comma, cast to a array for them include_filters = self.config["include_filters"] if isinstance(include_filters, basestring): include_filters = [include_filters] self.include_reg = re.compile(r"(?:%s)" % "|".join(include_filters))
def available_streams(cls, data): """ Return a list with available streams for the class implementing this. Should return a list : [{'value': v1, 'time': t1}, {'value': v2, 'time': t2}] """ # Get CPU measutements (we use it get the ID of the instances) libr = librato.connect(data['credentials']['username'], data['credentials']['token']) metric = libr.get(data['metric'], count=100, resolution=1) instances_list = [i for i in metric.measurements] # Get sources display names sources = libr._mexe('sources')['sources'] # pylint: disable=W0212 names = {} for s in sources: if s['display_name'] is not None: names[s['name']] = s['display_name'] else: names[s['name']] = s['name'] logger.info('Librato names: %s', names) result = [] for id_ in instances_list: name = names.get(id_, id_) result.append({ 'id': id_, 'name': name }) # Doesn't have a way to get better names return result
def __init__(self, config): super(LibratometricsStream, self).__init__(config) # Set Librato object self.libr = librato.connect(config['credentials']['username'], config['credentials']['token']) # Set metric to use self.metric = config['metric'] # Get unit self._value_unit = self.libr.get(self.metric, count=1, resolution=1).attributes.get( 'display_units_short', 'u') self._value_label = self.metric # Setup logging self.logger = logger or logging.getLogger(__name__) handler = logging.handlers.RotatingFileHandler( os.environ['LOG_DIR'] + "/stream_%s.log" % self.name, maxBytes=1024 * 1024, backupCount=4, ) handler.setFormatter( logging.Formatter( '[%(levelname)s/%(processName)s][%(asctime)s] %(name)s %(message)s' )) handler.setLevel(logging.INFO) self.logger.addHandler(handler) self.logger.setLevel(logging.INFO)
def __init__(self, *args, **kwargs): self.source = kwargs.pop('source', None) self.report = librato.connect(*args, **kwargs) self.report.set_timeout( 1 ) # Report quick or skip it, errors will show up as missing reports super(LibratoReport, self).__init__()
def setUp(self): self.conn = librato.connect('user_test', 'key_test') self.sample_payload = { 'title': 'Email Ops', 'type': 'mail', 'settings': {'addresses': '*****@*****.**'} } server.clean()
def setUpClass(cls): """ Auth """ user = os.environ.get('LIBRATO_USER') token = os.environ.get('LIBRATO_TOKEN') assert user and token, "Must set LIBRATO_USER and LIBRATO_TOKEN to run tests" print "%s and %s" % (user, token) """ Ensure user really wants to run these tests """ are_you_sure = os.environ.get('LIBRATO_ALLOW_INTEGRATION_TESTS') assert are_you_sure == 'Y', "INTEGRATION TESTS WILL DELETE METRICS " \ "IN YOUR ACCOUNT!!! " \ "If you are absolutely sure that you want to run tests "\ "against %s, please set LIBRATO_ALLOW_INTEGRATION_TESTS "\ "to 'Y'" % user """Initialize the Librato Connection""" cls.conn = librato.connect(user, token) cls.conn_sanitize = librato.connect( user, token, sanitizer=librato.sanitize_metric_name)
def librato_metric_task(name, num, attributes=None, metric_type="gauge", **kwargs): connection = librato.connect(settings.APP_METRICS_LIBRATO_USER, settings.APP_METRICS_LIBRATO_TOKEN) if metric_type == "counter": metric = LibratoCounter(connection, name, attributes=attributes) else: metric = LibratoGauge(connection, name, attributes=attributes) metric.add(num, source=settings.APP_METRICS_LIBRATO_SOURCE)
def queue(cls, config): """ Cached Librato queue for batch submitting """ if not hasattr(cls, "_queue"): api = librato.connect(config.LIBRATO_USER, config.LIBRATO_TOKEN) cls._queue = api.new_queue(auto_submit_count=config.LIBRATO_QUEUE_LENGTH) return cls._queue
def __init__(self, user, token, queue, upload_size=100, on_error=None): """Create a consumer thread.""" Thread.__init__(self) self.api = librato.connect(user, token) self.daemon = True # set as a daemon so the program can exit self.upload_size = upload_size self.on_error = on_error self.queue = queue self.retries = 3
def __init__(self, units='celsius', username='******', token='mytoken', source='braubuddy'): self._source = source self._api = librato.connect(username, token) super(LibratoAPIOutput, self).__init__(units)
def test_constructor_tags(self): conn = librato.connect('user_test', 'key_test', tags={'sky': 'blue'}) q = conn.new_queue(tags={'sky': 'red', 'coal': 'black'}) tags = q.get_tags() assert len(tags) == 2 assert 'sky' in tags assert tags['sky'] == 'red' assert 'coal' in tags assert tags['coal'] == 'black'
def queue(cls, config): """ Cached Librato queue for batch submitting """ if not hasattr(cls, "_queue"): api = librato.connect(config.LIBRATO_USER, config.LIBRATO_TOKEN) cls._queue = api.new_queue( auto_submit_count=config.LIBRATO_QUEUE_LENGTH) return cls._queue
def librato_connect(): ''' Connect to the Librato service. See https://metrics.librato.com/ ''' apikey = os.getenv('LIBRATOKEY') if apikey == None: sys.stderr.write('Error: LIBRATOKEY environment variable is not set!!') sys.exit(1) libratoapi = librato.connect('*****@*****.**', apikey) return libratoapi
def queue(cls, config): """ Cached Librato queue for batch submitting """ if not hasattr(cls, "_queue"): api = librato.connect(os.environ.get('LIBRATO_USER'), os.environ.get('LIBRATO_TOKEN')) queue_length = int(os.environ.get('LIBRATO_QUEUE_LENGTH')) cls._queue = api.new_queue(auto_submit_count= queue_length) return cls._queue
def connect_to_librato(): import librato try: librato_user = os.environ['LIBRATO_USER'] librato_token = os.environ['LIBRATO_TOKEN'] except KeyError: raise Exception('librato user or token not set in enviroment settings ' + 'LIBRATO_USER and LIBRATO_TOKEN despite set COVERAGE_ENABLED enviroment variable') return librato.connect(librato_user, librato_token)
def _get_queue(self): """Gets a Librato Queue object for bulk submission of metrics. Returns: A Librato Queue object. """ import librato from librato import Queue connection = librato.connect(self._api_user, self._api_token) return Queue(connection)
def __init__(self, name, user=None, token=None, source=None, extract=lambda r, _: r): Action.__init__(self) self.extract = extract self.name = name self.source = source self.librato_api = librato.connect(user, token)
def setUpClass(cls): """ Auth """ user = os.environ.get("LIBRATO_USER") token = os.environ.get("LIBRATO_TOKEN") assert user and token, "Must set LIBRATO_USER and LIBRATO_TOKEN to run tests" print "%s and %s" % (user, token) """ Ensure user really wants to run these tests """ are_you_sure = os.environ.get("LIBRATO_ALLOW_INTEGRATION_TESTS") assert are_you_sure == "Y", ( "INTEGRATION TESTS WILL DELETE METRICS " "IN YOUR ACCOUNT!!! " "If you are absolutely sure that you want to run tests " "against %s, please set LIBRATO_ALLOW_INTEGRATION_TESTS " "to 'Y'" % user ) """Initialize the Librato Connection""" cls.conn = librato.connect(user, token) cls.conn_sanitize = librato.connect(user, token, sanitizer=librato.sanitize_metric_name)
def _get_librato(ret=None): ''' Return a Librato connection object. ''' _options = _get_options(ret) conn = librato.connect(_options.get('email'), _options.get('api_token'), sanitizer=librato.sanitize_metric_name, hostname=_options.get('api_url')) log.info("Connected to librato.") return conn
def send_heartbeat(retries=3): for i in range(retries): try: user = '******' token = '<your_token>' api = librato.connect(user, token) api.submit('runner.{0}.heartbeat'.format(namespace), 1) break except Exception as e: log.exception('Exception sending health check to librato') notify("Exception sending health check to librato\n\nException is: " + traceback.format_exc(), severity="WARNING") time.sleep(1)
class _MetricsWorkerThread(threading.Thread): """Worker Thread for publishing metrics in the background.""" def __init__(self, mode, source): threading.Thread.__init__(self, name='metrics-worker') if CONFIG.version.endswith('.gpu'): self._version = CONFIG.version.split('.gpu')[0] self._isgpu = True else: self._version = CONFIG.version self._isgpu = False self._mode = mode self._source = source try: # product key from .. import product_key self._product_key = product_key.get_product_key() except Exception, e: self._product_key = None self.queue = METRICS_QUEUE root_package_name = __import__(__name__.split('.')[0]).__name__ self.logger = logging.getLogger(root_package_name + '.metrics') self._tracker = None # librato metrics tracker self._mixpanel = None # Mixpanel metrics tracker buffer_size = 5 offline_buffer_size = 25 self._sys_info_set = False self._usable = False try: self._metrics_url = CONFIG.metrics_url self._requests = _requests # support mocking out requests library in unit-tests if self._mode != 'PROD': self.logger.info( "Using MetricMock instead of real metrics, mode is: %s" % self._mode) self._tracker = MetricMock() self._mixpanel = MetricMock() else: self._tracker = librato.connect(CONFIG.librato_user, CONFIG.librato_token) self._mixpanel = mixpanel.Mixpanel(CONFIG.mixpanel_user) except Exception, e: self.logger.warning( "Unexpected exception connecting to Metrics service, disabling metrics, exception %s" % e)
def __init__(self, config=None): """ Create a new instance of the LibratoHandler class """ # Initialize Handler Handler.__init__(self, config) logging.debug("Initialized statsd handler.") # Initialize Options api = librato.connect(self.conf['user'], self.conf['apikey']) self.queue = api.new_queue() self.batch_size = 300 self.current_n_measurements = 0
def _get_librato(ret=None): """ Return a Librato connection object. """ _options = _get_options(ret) conn = librato.connect( _options.get("email"), _options.get("api_token"), sanitizer=librato.sanitize_metric_name, hostname=_options.get("api_url"), ) log.info("Connected to librato.") return conn
def connect(): librato_running = False q = None while librato_running is not True: try: api = librato.connect(librato_user, librato_token) q = api.new_queue() librato_running = True except: print("unable to connect to Librato server") sys.stdout.flush() time.sleep(interval) return q
def send_heartbeat(retries=3): for i in range(retries): try: user = '******' token = '<your_token>' api = librato.connect(user, token) api.submit('runner.{0}.heartbeat'.format(namespace), 1) break except Exception as e: log.exception('Exception sending health check to librato') notify( "Exception sending health check to librato\n\nException is: " + traceback.format_exc(), severity="WARNING") time.sleep(1)
def test_inherited_tags(self): conn = librato.connect('user_test', 'key_test', tags={'sky': 'blue'}) assert conn.get_tags() == {'sky': 'blue'} q = conn.new_queue() q.add_tagged('user_cpu', 10) q.submit() # Measurement must inherit 'sky' tag from connection resp = self.conn.get_tagged('user_cpu', duration=60, tags_search="sky=blue") assert len(resp['series']) == 1 assert resp['series'][0].get('tags', {}) == conn.get_tags() measurements = resp['series'][0]['measurements'] assert len(measurements) == 1 assert measurements[0]['value'] == 10
def __init__(self, librato_user, librato_api_token, pct_threshold=90, debug=False, flush_interval=60000, no_aggregate_counters=False, expire=0, source_prefix='', librato_hostname=LIBRATO_HOSTNAME, prefix=None): self.buf = 8192 self.flush_interval = float(flush_interval / 1000) self.pct_threshold = pct_threshold self.no_aggregate_counters = no_aggregate_counters self.debug = debug self.expire = expire self.hostname = socket.gethostname() parts = librato_hostname.split("://") if len(parts) > 2: raise ValueError("Malformed hostname: {}".format(librato_hostname)) elif len(parts) == 2: protocol, librato_hostname = parts if protocol not in ["http", "https"]: raise ValueError("Unsupported protocol: {}".format(protocol)) else: protocol = "https" self.api = librato.connect(librato_user, librato_api_token, hostname=librato_hostname, protocol=protocol, sanitizer=librato.sanitize_metric_name) self.counters = {} self.timers = {} self.gauges = {} self.aliases = {} self._sock = None self.prefix = prefix if source_prefix: self.source = '{}-{}'.format(source_prefix, self.hostname) else: self.source = self.hostname
def main(): config_file = 'config.json' if len(sys.argv) > 1: config_file = sys.argv[1] with open(config_file, 'r') as f: config = json.load(f) librato_client = None if 'librato' in config: librato_client = librato.connect(config['librato']['user'], config['librato']['token']) carbon_socket = None if 'carbon' in config: carbon_socket = (config['carbon']['server'], config['carbon']['port']) publish_forever(config, librato_client, carbon_socket)
def __init__(self, config=None): """ Create a new instance of the LibratoHandler class """ # Initialize Handler Handler.__init__(self, config) logging.debug("Initialized statsd handler.") # Initialize Options api = librato.connect(self.config['user'], self.config['apikey']) self.queue = api.new_queue() self.queue_max_size = int(self.config.get('queue_max_size',300)) self.queue_max_interval = int(self.config.get('queue_max_interval',60)) self.queue_max_timestamp = int(time.time() + self.queue_max_interval) self.current_n_measurements = 0 self.include_reg = re.compile(r'(?:%s)' % '|'.join( self.config.get('include_filters',['^.*'])))
def __init__(self, config=None): """ Create a new instance of the LibratoHandler class """ # Initialize Handler Handler.__init__(self, config) logging.debug("Initialized statsd handler.") # Initialize Options api = librato.connect(self.config["user"], self.config["apikey"]) self.queue = api.new_queue() self.queue_max_size = int(self.config.get("queue_max_size", 300)) self.queue_max_interval = int(self.config.get("queue_max_interval", 60)) self.queue_max_timestamp = int(time.time() + self.queue_max_interval) self.current_n_measurements = 0 # If a user leaves off the ending comma, cast to a array for them include_filters = self.config.get("include_filters", ["^.*"]) if isinstance(include_filters, basestring): include_filters = [include_filters] self.include_reg = re.compile(r"(?:%s)" % "|".join(include_filters))
def handler(event, context): page = requests.get("http://www.nytimes.com/elections/results/president") tree = html.fromstring(page.content) democrat_electoral = tree.xpath( "//div[@class='eln-group eln-democrat ']/div[@class='eln-count']/text()" )[0] republican_electoral = tree.xpath( "//div[@class='eln-group eln-republican eln-winner']/div[@class='eln-count']/text()" )[0] print(democrat_electoral) print(republican_electoral) democrat_popular = tree.xpath( "//div[@class='eln-group eln-democrat']/div[@class='eln-value']/span[@class='eln-popular-vote-count']/text()" )[0].split(" votes")[0].replace(",", "") republican_popular = tree.xpath( "//div[@class='eln-group eln-republican']/div[@class='eln-value']/span[@class='eln-popular-vote-count']/text()" )[0].split(" votes")[0].replace(",", "") print(democrat_popular) print(republican_popular) # Send em to Librato username = os.environ['LIBRATO_USER'] token = os.environ['LIBRATO_TOKEN'] api = librato.connect(username, token) api.submit("election.electoral_votes.democrats", democrat_electoral, description="Democrats") api.submit("election.electoral_votes.gop", republican_electoral, description="GOP") api.submit("election.popular_vote.democrats", int(democrat_popular), description="Democrats") api.submit("election.popular_vote.gop", int(republican_popular), description="GOP")
def __init__(self, config=None): """ Create a new instance of the LibratoHandler class """ # Initialize Handler Handler.__init__(self, config) logging.debug("Initialized statsd handler.") # Initialize Options api = librato.connect(self.config['user'], self.config['apikey']) self.queue = api.new_queue() self.queue_max_size = int(self.config.get('queue_max_size', 300)) self.queue_max_interval = int(self.config.get('queue_max_interval', 60)) self.queue_max_timestamp = int(time.time() + self.queue_max_interval) self.current_n_measurements = 0 # If a user leaves off the ending comma, cast to a array for them include_filters = self.config.get('include_filters', ['^.*']) if isinstance(include_filters, basestring): include_filters = [include_filters] self.include_reg = re.compile(r'(?:%s)' % '|'.join(include_filters))
def __init__(self, librato_user, librato_api_token, pct_threshold=90, debug=False, flush_interval=60000, no_aggregate_counters=False, expire=0, source_prefix='', librato_hostname=LIBRATO_HOSTNAME, prefix=None): self.buf = 8192 self.flush_interval = float(flush_interval/1000) self.pct_threshold = pct_threshold self.no_aggregate_counters = no_aggregate_counters self.debug = debug self.expire = expire self.hostname = socket.gethostname() parts = librato_hostname.split("://") if len(parts) > 2: raise ValueError("Malformed hostname: {}".format(librato_hostname)) elif len(parts) == 2: protocol, librato_hostname = parts if protocol not in ["http", "https"]: raise ValueError("Unsupported protocol: {}".format(protocol)) else: protocol = "https" self.api = librato.connect(librato_user, librato_api_token, hostname=librato_hostname, protocol=protocol, sanitizer=librato.sanitize_metric_name) self.counters = {} self.timers = {} self.gauges = {} self.aliases = {} self._sock = None self.prefix = prefix if source_prefix: self.source = '{}-{}'.format(source_prefix, self.hostname) else: self.source = self.hostname
def setUp(self): self.conn = librato.connect('user_test', 'key_test') server.clean() self.q = self.conn.new_queue()
def librato_metric_task(name, num, **kwargs): api = librato.connect(settings.APP_METRICS_LIBRATO_USER, settings.APP_METRICS_LIBRATO_TOKEN) source = settings.APP_METRICS_LIBRATO_SOURCE api.submit(name, num, source=source, **kwargs)
def client(config): return librato.connect(config["user"], config["token"])
#!flask/bin/python from flask import Flask from flask import request from flask import abort from flask import jsonify import hashlib import hmac import base64 import librato app = Flask(__name__) user = '******' token = 'TOKEN' secret = 'secret' api = librato.connect(user, token) @app.route('/') def index(): return "Hello, World!" @app.route('/proxy', methods=['POST']) def proxy_temp(): if not request.json or \ not 'plain' in request.json or \ not 'hash' in request.json: abort(400, 'invalid request') plain_temp = str(request.json["plain"]) hash_temp = str(request.json["hash"]) local_hash = hmac.new(secret, plain_temp).hexdigest()
def setup_class(cls): """Initialize the Librato Connection""" assert("LIBRATO_USER" in os.environ and "LIBRATO_TOKEN" in os.environ), \ "Must set LIBRATO_USER and LIBRATO_TOKEN to run tests" cls.conn = librato.connect(os.environ['LIBRATO_USER'], os.environ['LIBRATO_TOKEN'])
def setUp(self): self.conn = librato.connect("user_test", "key_test") server.clean() self.q = self.conn.new_queue()
def setUp(self): self.conn = librato.connect('user_test', 'key_test', tags={'sky': 'blue'}) server.clean()
def setUp(self): self.conn = librato.connect('user_test', 'key_test') server.clean()