<s> import re <EOL> from json import dumps <EOL> from metrology . exceptions import ReporterException <EOL> from metrology . instruments import * <EOL> from metrology . reporter . base import Reporter <EOL> from metrology . utils import now <EOL> class LibratoReporter ( Reporter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , email , token , ** options ) : <EOL> self . email = email <EOL> self . token = token <EOL> try : <EOL> import requests <EOL> except : <EOL> raise ReporterException ( "<STR_LIT>" ) <EOL> self . filters = options . get ( '<STR_LIT>' ) <EOL> self . excludes = options . get ( '<STR_LIT>' ) <EOL> self . source = options . get ( '<STR_LIT:source>' ) <EOL> self . prefix = options . get ( '<STR_LIT>' ) <EOL> super ( LibratoReporter , self ) . __init__ ( ** options ) <EOL> def list_metrics ( self ) : <EOL> for name , metric in self . registry : <EOL> if isinstance ( metric , Meter ) : <EOL> yield self . prepare_metric ( name , '<STR_LIT>' , metric , [ <EOL> '<STR_LIT:count>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> if isinstance ( metric , Gauge ) : <EOL> yield self . prepare_metric ( name , '<STR_LIT>' , metric , [ <EOL> '<STR_LIT:value>' <EOL> ] ) <EOL> if isinstance ( metric , UtilizationTimer ) : <EOL> yield self . prepare_metric ( name , '<STR_LIT>' , metric , [ <EOL> '<STR_LIT:count>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] , [ <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> if isinstance ( metric , Timer ) : <EOL> yield self . prepare_metric ( name , '<STR_LIT>' , metric , [ <EOL> '<STR_LIT:count>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] , [ <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> if isinstance ( metric , Counter ) : <EOL> yield self . prepare_metric ( name , '<STR_LIT>' , metric , [ <EOL> '<STR_LIT:count>' <EOL> ] ) <EOL> if isinstance ( metric , Histogram ) : <EOL> yield self . prepare_metric ( name , '<STR_LIT>' , metric , [ <EOL> '<STR_LIT:count>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] , [ <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> def write ( self ) : <EOL> import requests <EOL> metrics = { <EOL> "<STR_LIT>" : [ data for metric in self . list_metrics ( ) for type , data in metric if type == "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ data for metric in self . list_metrics ( ) for type , data in metric if type == "<STR_LIT>" ] <EOL> } <EOL> requests . post ( "<STR_LIT>" , <EOL> data = dumps ( metrics ) , <EOL> auth = ( self . email , self . token ) , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } ) <EOL> def prepare_metric ( self , name , type , metric , keys , snapshot_keys = [ ] ) : <EOL> base_name = re . sub ( r"<STR_LIT>" , "<STR_LIT:_>" , name ) <EOL> if self . prefix : <EOL> base_name = "<STR_LIT>" . format ( self . prefix , base_name ) <EOL> time = now ( ) <EOL> type = "<STR_LIT>" if type != "<STR_LIT>" else "<STR_LIT>" <EOL> if self . filters : <EOL> keys = filter ( lambda key : key in self . filters , keys ) <EOL> snapshot_keys = filter ( lambda key : key in self . filters , snapshot_keys ) <EOL> if self . excludes : <EOL> keys = filter ( lambda key : key not in self . excludes , keys ) <EOL> snapshot_keys = filter ( lambda key : key in self . excludes , snapshot_keys ) <EOL> for name in keys : <EOL> value = getattr ( metric , name ) <EOL> yield type , { <EOL> "<STR_LIT:name>" : "<STR_LIT>" . format ( base_name , name ) , <EOL> "<STR_LIT:source>" : self . source , <EOL> "<STR_LIT:time>" : time , <EOL> "<STR_LIT:value>" : value <EOL> } <EOL> if hasattr ( metric , '<STR_LIT>' ) : <EOL> snapshot = metric . snapshot <EOL> for name in snapshot_keys : <EOL> value = getattr ( snapshot , name ) <EOL> yield type , { <EOL> "<STR_LIT:name>" : "<STR_LIT>" . format ( base_name , name ) , <EOL> "<STR_LIT:source>" : self . source , <EOL> "<STR_LIT:time>" : time , <EOL> "<STR_LIT:value>" : value <EOL> } </s>
