<s> from __future__ import absolute_import <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> from contextlib import contextmanager <EOL> from django . conf import settings <EOL> from random import random <EOL> from time import time <EOL> import logging <EOL> def get_default_backend ( ) : <EOL> from sentry . utils . imports import import_string <EOL> cls = import_string ( settings . SENTRY_METRICS_BACKEND ) <EOL> return cls ( ** settings . SENTRY_METRICS_OPTIONS ) <EOL> backend = get_default_backend ( ) <EOL> def _get_key ( key ) : <EOL> prefix = settings . SENTRY_METRICS_PREFIX <EOL> if prefix : <EOL> return '<STR_LIT>' . format ( prefix , key ) <EOL> return key <EOL> def _should_sample ( ) : <EOL> sample_rate = settings . SENTRY_METRICS_SAMPLE_RATE <EOL> return sample_rate >= <NUM_LIT:1> or random ( ) >= <NUM_LIT:1> - sample_rate <EOL> def _sampled_value ( value ) : <EOL> sample_rate = settings . SENTRY_METRICS_SAMPLE_RATE <EOL> if sample_rate < <NUM_LIT:1> : <EOL> value = int ( value * ( <NUM_LIT:1.0> / sample_rate ) ) <EOL> return value <EOL> def _incr_internal ( key , instance = None , tags = None , amount = <NUM_LIT:1> ) : <EOL> from sentry . app import tsdb <EOL> if _should_sample ( ) : <EOL> amount = _sampled_value ( amount ) <EOL> if instance : <EOL> full_key = '<STR_LIT>' . format ( key , instance ) <EOL> else : <EOL> full_key = key <EOL> try : <EOL> tsdb . incr ( tsdb . models . internal , full_key , count = amount ) <EOL> except Exception : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . exception ( '<STR_LIT>' ) <EOL> def incr ( key , amount = <NUM_LIT:1> , instance = None , tags = None ) : <EOL> sample_rate = settings . SENTRY_METRICS_SAMPLE_RATE <EOL> _incr_internal ( key , instance , tags , amount ) <EOL> try : <EOL> backend . incr ( key , instance , tags , amount , sample_rate ) <EOL> except Exception : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . exception ( '<STR_LIT>' ) <EOL> def timing ( key , value , instance = None , tags = None ) : <EOL> sample_rate = settings . SENTRY_METRICS_SAMPLE_RATE <EOL> try : <EOL> backend . timing ( key , value , instance , tags , sample_rate ) <EOL> except Exception : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . exception ( '<STR_LIT>' ) <EOL> @ contextmanager <EOL> def timer ( key , instance = None , tags = None ) : <EOL> if tags is None : <EOL> tags = { } <EOL> start = time ( ) <EOL> try : <EOL> yield tags <EOL> except Exception : <EOL> tags [ '<STR_LIT:result>' ] = '<STR_LIT>' <EOL> raise <EOL> else : <EOL> tags [ '<STR_LIT:result>' ] = '<STR_LIT:success>' <EOL> finally : <EOL> timing ( key , time ( ) - start , instance , tags ) </s>
