def setup_stats(app): host = app.config.get('STATSD_HOST') port = app.config.get('STATSD_PORT') name = app.config.get('STATSD_PREFIX', '') connection = statsd.Connection(host=host, port=port, sample_rate=1) client = statsd.Client(name, connection) app.stats = StatsDClient(client)
def get_app(): app = Flask('kardboard') app.config.from_object('kardboard.default_settings') if os.getenv('KARDBOARD_SETTINGS', None): app.config.from_envvar('KARDBOARD_SETTINGS') app.secret_key = app.config['SECRET_KEY'] app.db = MongoEngine(app) app.jinja_env.add_extension('kardboard.util.Markdown2Extension') app.jinja_env.filters['slugify'] = slugify app.jinja_env.filters['timesince'] = timesince app.jinja_env.filters['timeuntil'] = timeuntil app.jinja_env.filters['jsonencode'] = jsonencode app.jinja_env.globals['newrelic_head'] = newrelic_head app.jinja_env.globals['newrelic_foot'] = newrelic_foot if app.config.get('COMPILE_TEMPLATES', False): compiled_templates = os.path.join( os.path.abspath(os.path.dirname(__file__)), 'compiled_templates') compiled_files = path.path(compiled_templates).files() if len(compiled_files) <= 1: app.jinja_env.compile_templates(compiled_templates, zip=None, py_compile=True) app.jinja_env.loader = ModuleLoader(compiled_templates) configure_logging(app) app.wsgi_app = FixGunicorn(app.wsgi_app) statsd_conf = app.config.get('STATSD_CONF', {}) statsd_connection = statsd.Connection( host=statsd_conf.get('host', '127.0.0.1'), port=statsd_conf.get('port', 8125), sample_rate=statsd_conf.get('sample_rate', 1), ) machine_name = socket.getfqdn().split('.')[0] environment_name = app.config.get('ENV_MAPPING', {}).get(machine_name, 'default') prefix_name = '%s.%s.kardboard' % (environment_name, machine_name) app.statsd = statsd.Client(prefix_name, statsd_connection) if SENTRY_SUPPORT and 'SENTRY_DSN' in app.config.keys(): sentry = Sentry(app) sentry return app
def get_client(): try: host = conf.statsd.host port = conf.statsd.get("port", statsd.Connection.default_port) prefix = conf.statsd.prefix statsd.Connection.set_defaults( host=host, port=port, ) except AttributeError as exc: log.info( "Could not find statsd configuration; disabling statsd. " "Error message was: %s", str(exc)) prefix = None statsd.Connection.set_defaults(disabled=True, ) return statsd.Client(prefix)
def get_client(request): settings = request.registry.settings conn = statsd.Connection(host=settings.get('statsd.host'), port=settings.get('statsd.port')) return statsd.Client(__package__, connection=conn)
def test_average_shortcut(self): average = statsd.Client('average').get_average() assert isinstance(average, statsd.Average)
def test_timer_shortcut(self): timer = statsd.Client('timer').get_timer() assert isinstance(timer, statsd.Timer)
def test_raw_shortcut(self): raw = statsd.Client('raw').get_raw() assert isinstance(raw, statsd.Raw)
def test_gauge_shortcut(self): gauge = statsd.Client('gauge').get_gauge() assert isinstance(gauge, statsd.Gauge)
def test_counter_shortcut(self): counter = statsd.Client('counter').get_counter() assert isinstance(counter, statsd.Counter)
import statsd import time if __name__ == '__main__': # Create a new connection for the client connection = statsd.Connection( host='localhost', port=8125, sample_rate=1, ) # Create the client client = statsd.Client("python", connection) # Create counter counter = client.get_counter("counter") # Create gauge gauge = client.get_gauge("gauge") # Create average average = client.get_average("average") while True: # Create a timer timer = client.get_timer("timer") # Will send the elapsed time once all the block has been executed with timer: counter += 1 # Increment by one the counter
# from .celery import app from aliyunsdkcms.request.v20180308 import QueryMetricListRequest from aliyunsdkcore import client from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest from aliyunsdkvpc.request.v20160428 import DescribeBandwidthPackagesRequest, \ DescribeBandwidthPackagePublicIpMonitorDataRequest ########################## statsd 配置 ########################## SERVER = '127.0.0.1' PORT = 8125 PREFIX = 'mwee.network.eye' DELAYTIME = 3 STATSD_CONNECTION = statsd.Connection(host=SERVER, port=PORT) STATSD_CLIENT = statsd.Client("mwee.network.eye", STATSD_CONNECTION) STATSD_GAUGE = STATSD_CLIENT.get_client(class_=statsd.Gauge) ########################## statsd 配置 ########################## class AliyunBase(): key = '' secret = '' regionId = '' def __init__(self, regionId='cn-shanghai'): self.regionId = regionId self.initClient(self.regionId, self.key, self.secret) def initClient(self, regionId, key, secret):
def main(): # Stats statsd_connection = statsd.Connection(host='lg-head', port=8125, sample_rate=1) statsd_client = statsd.Client('usage', statsd_connection) #active_counter = statsd_client.get_client( name = 'active', class_ = statsd.Counter ) #idle_counter = statsd_client.get_client( name = 'idle', class_ = statsd.Counter ) # Init timers state = IDLE_STATE idle_timer = statsd_client.get_client(name='idle', class_=statsd.Timer) idle_timer.start() active_timer = None if len(sys.argv) != 2: print "Usage:", sys.argv[0], "<command>" print "<command> will be called every", tour_check_per, "seconds", print "after", (tour_check_per * tour_wait_for_trigger), "seconds" print "if USB devs are not touched." sys.exit(1) cmd = sys.argv[1] cnt = 0 while True: # update tour wait time from shell.conf try: tour_wait_for_trigger = int(checkConfig('LG_SAVER_WAITS')) except ValueError: print 'WARNING: LG_SAVER_WAITS is not an integer!' if checkConfig('LG_MASTERSLAVE').find('master') == -1: print 'Not master, sleeping...' state = DEAD_STATE cnt = 0 if state == IDLE_STATE: idle_timer.stop() elif state == ACTIVE_STATE: active_timer.stop() state = DEAD_STATE time.sleep(tour_check_per) elif Touched(space_nav, quanta_ts): print "Touched." cnt = 0 if state != ACTIVE_STATE: state = ACTIVE_STATE if idle_timer != None: idle_timer.stop() active_timer = statsd_client.get_client(name='active', class_=statsd.Timer) active_timer.start() else: active_timer.intermediate(subname='total') else: cnt += 1 if cnt >= tour_wait_for_trigger: print cmd os.system(cmd) else: print "Wait...", cnt if state != IDLE_STATE: state = IDLE_STATE if active_timer != None: active_timer.stop() idle_timer = statsd_client.get_client(name='idle', class_=statsd.Timer) idle_timer.start() else: idle_timer.intermediate(subname='total')
def get_raw_client(self, client_name): return statsd.Client(settings.STATSD_BASE_CLIENT_NAME, self.connection).get_client(name=client_name, class_=statsd.Raw)