Beispiel #1
0
    def init_app(self, app):
        self.config = app.config

        enabled = self.config.setdefault("GRAPHITE_ENABLED", False)
        self.app = app

        if not enabled:
            return

        from greplin import scales
        from greplin.scales.graphite import GraphitePeriodicPusher
        from greplin.scales.meter import MeterStat

        host = self.config.setdefault("GRAPHITE_HOST", "localhost")
        port = self.config.setdefault("GRAPHITE_PORT", 2003)
        prefix = self.config.setdefault("GRAPHITE_PREFIX", "doorman")
        period = self.config.setdefault("GRAPHITE_REPORTING_INTERVAL", 60)

        app.metrics = {}
        for rule in app.url_map.iter_rules():
            app.metrics[rule.endpoint] = scales.collection(
                rule.endpoint,
                MeterStat('count'),
                scales.PmfStat('latency'),
            )

        app.graphite = GraphitePeriodicPusher(
            host, port, period=period, prefix=prefix,
        )

        for rule in self.config.setdefault("GRAPHITE_ALLOW", ['*']):
            app.graphite.allow(rule)

        app.graphite.start()
        return
Beispiel #2
0
def init(config={}):
    """Set up the GraphitePusher instance from a config dict.

    :param config: A dict in the form::

        config = {
            "metrics.enabled": "yes" | "no",
            "metrics.host": 'localhost',
            "metrics.port": 2003,
            "metrics.prefix": 'pp',
            # seconds:
            "metrics.period": 60,
            # no restrictions on the prefix all will be logged:
            "metrics.allow": '*',
        }

        If metrics.enabled = "no" then the GraphitePeriodicPusher
        won't be created and started.

    """
    log = logging.getLogger("{}.init".format(__name__))

    enabled = (config.get("metrics.enabled", "no")).lower().strip()

    if enabled != "yes":
        log.warn("Metrics are disabled (metrics.enabled = 'no').")
        return

    d = dict(
        host=config.get("metrics.host", 'localhost'),
        port=int(config.get("metrics.port", 2003)),
        prefix=config.get("metrics.prefix", 'api.audience'),
        period=float(config.get("metrics.period", 60)),
        allow=config.get("metrics.allow", '*'),
    )
    log.info((
        "GraphitePeriodicPusher configuration = "
        "host:{host} port:{port} prefix:'{prefix}' period:{period}"
    ).format(**d))

    g = GraphitePeriodicPusher(d['host'], d['port'], d['prefix'], d['period'])
    g.allow(d['allow'])
    g.start()

    _graphitepusher.append(g)
Beispiel #3
0
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

# Initialize app
# --------------
app = Flask(__name__)

# Initialize metrics backend
# --------------------------
if config.debug or not config.graphite.enable:
    import greplin.scales.flaskhandler as statserver
    statserver.registerStatsHandler(app, __name__, prefix='/status/')
else:
    from greplin.scales.graphite import GraphitePeriodicPusher
    graphitePeriodicPusher = GraphitePeriodicPusher(config.graphite.host, config.graphite.port, None, period=10)
    graphitePeriodicPusher.allow("*")
    graphitePeriodicPusher.start()

if config.debug:
    app.config["DEBUG"] = config.debug

HomeView.register(app)
MediasView.register(app)
UsersView.register(app)
ProfileView.register(app)