def test_metrics(self): test_gauge = Gauge(group='test', name='gauge', title='Test gauge', description='total time to process timed events') test_gauge.set(500) gauge = [g for g in Gauge.get_gauges() if g.title == 'Test gauge'][0] self.assertGreaterEqual(gauge.value, 500) test_timer = Timer(group='test', name='timer', title='Test timer', description='total time to process timed events') recv_started = test_timer.start_timer() time.sleep(1) test_timer.stop_timer(recv_started) timer = [t for t in Timer.get_timers() if t.title == 'Test timer'][0] self.assertGreaterEqual(timer.count, 1) self.assertGreaterEqual(timer.total_time, 999)
def status(): total_alert_gauge.set(db.get_count()) metrics = Gauge.get_gauges(format='json') metrics.extend(Counter.get_counters(format='json')) metrics.extend(Timer.get_timers(format='json')) auto_refresh_allow = { "group": "switch", "name": "auto_refresh_allow", "type": "text", "title": "Alert console auto-refresh", "description": "Allows auto-refresh of alert consoles to be turned off remotely", "value": "ON" if Switch.get('auto-refresh-allow').is_on() else "OFF", } metrics.append(auto_refresh_allow) now = int(time.time() * 1000) return jsonify(application="alerta", version=__version__, time=now, uptime=int(now - started), metrics=metrics)
def prometheus_metrics(): total_alert_gauge.set(db.get_count()) output = Gauge.get_gauges(format='prometheus') output += Counter.get_counters(format='prometheus') output += Timer.get_timers(format='prometheus') return Response(output, content_type='text/plain; version=0.0.4; charset=utf-8')
def status(): total_alert_gauge.set(db.get_count()) metrics = Gauge.get_gauges() metrics.extend(Counter.get_counters()) metrics.extend(Timer.get_timers()) auto_refresh_allow = { "group": "switch", "name": "auto_refresh_allow", "type": "text", "title": "Alert console auto-refresh", "description": "Allows auto-refresh of alert consoles to be turned off remotely", "value": "ON" if Switch.get("auto-refresh-allow").is_on() else "OFF", } metrics.append(auto_refresh_allow) now = int(time.time() * 1000) return jsonify(application="alerta", version=__version__, time=now, uptime=int(now - started), metrics=metrics)
try: from urllib.parse import urljoin except ImportError: from urlparse import urljoin from alerta.app import app, db from alerta.app.metrics import Counter, Timer from alerta.plugins import Plugins, RejectException LOG = app.logger plugins = Plugins() reject_counter = Counter('alerts', 'rejected', 'Rejected alerts', 'Number of rejected alerts') error_counter = Counter('alerts', 'errored', 'Errored alerts', 'Number of errored alerts') duplicate_timer = Timer('alerts', 'duplicate', 'Duplicate alerts', 'Total time to process number of duplicate alerts') correlate_timer = Timer('alerts', 'correlate', 'Correlated alerts', 'Total time to process number of correlated alerts') create_timer = Timer('alerts', 'create', 'Newly created alerts', 'Total time to process number of new alerts') pre_plugin_timer = Timer('plugins', 'prereceive', 'Pre-receive plugins', 'Total number of pre-receive plugins') post_plugin_timer = Timer('plugins', 'postreceive', 'Post-receive plugins', 'Total number of post-receive plugins') def jsonp(func): """Wraps JSONified output for JSONP requests.""" @wraps(func) def decorated(*args, **kwargs): callback = request.args.get('callback', False) if callback: data = str(func(*args, **kwargs).data) content = str(callback) + '(' + data + ')' mimetype = 'application/javascript'
from alerta.app import app, db from alerta.app.switch import Switch from alerta.app.auth import permission, is_in_scope from alerta.app.utils import absolute_url, jsonp, parse_fields, process_alert, process_status, add_remote_ip from alerta.app.metrics import Timer from alerta.app.alert import Alert from alerta.app.exceptions import RejectException, RateLimit, BlackoutPeriod from alerta.app.heartbeat import Heartbeat from alerta.plugins import Plugins LOG = app.logger plugins = Plugins() # Set-up metrics gets_timer = Timer('alerts', 'queries', 'Alert queries', 'Total time to process number of alert queries') receive_timer = Timer('alerts', 'received', 'Received alerts', 'Total time to process number of received alerts') delete_timer = Timer('alerts', 'deleted', 'Deleted alerts', 'Total time to process number of deleted alerts') status_timer = Timer('alerts', 'status', 'Alert status change', 'Total time and number of alerts with status changed') tag_timer = Timer('alerts', 'tagged', 'Tagging alerts', 'Total time to tag number of alerts') attrs_timer = Timer('alerts', 'attributes', 'Alert attributes change', 'Total time and number of alerts with attributes changed') untag_timer = Timer('alerts', 'untagged', 'Removing tags from alerts', 'Total time to un-tag number of alerts') @app.route('/_', methods=['OPTIONS', 'PUT', 'POST', 'DELETE', 'GET'])
import datetime from copy import copy from flask import request from flask.ext.cors import cross_origin from dateutil.parser import parse as parse_date from alerta.app import app, db from alerta.alert import Alert from alerta.app.utils import absolute_url, jsonify, jsonp, process_alert from alerta.app.metrics import Timer from alerta.plugins import RejectException LOG = app.logger webhook_timer = Timer('alerts', 'webhook', 'Web hook alerts', 'Total time to process number of web hook alerts') duplicate_timer = Timer('alerts', 'duplicate', 'Duplicate alerts', 'Total time to process number of duplicate alerts') correlate_timer = Timer('alerts', 'correlate', 'Correlated alerts', 'Total time to process number of correlated alerts') create_timer = Timer('alerts', 'create', 'Newly created alerts', 'Total time to process number of new alerts') def cw_state_to_severity(state): if state == 'ALARM': return 'major' elif state == 'INSUFFICIENT_DATA': return 'warning' elif state == 'OK':
from collections import defaultdict from flask import request, render_template from alerta.app import app, db from alerta.app.switch import Switch from alerta.app.utils import jsonify, jsonp, auth_required, parse_fields, crossdomain, process_alert from alerta.app.metrics import Timer from alerta.alert import Alert from alerta.heartbeat import Heartbeat from alerta.plugins import RejectException LOG = app.logger # Set-up metrics gets_timer = Timer('alerts', 'queries', 'Alert queries', 'Total time to process number of alert queries') receive_timer = Timer('alerts', 'received', 'Received alerts', 'Total time to process number of received alerts') delete_timer = Timer('alerts', 'deleted', 'Deleted alerts', 'Total time to process number of deleted alerts') status_timer = Timer('alerts', 'status', 'Alert status change', 'Total time and number of alerts with status changed') tag_timer = Timer('alerts', 'tagged', 'Tagging alerts', 'Total time to tag number of alerts') untag_timer = Timer('alerts', 'untagged', 'Removing tags from alerts', 'Total time to un-tag number of alerts') @app.route('/_', methods=['OPTIONS', 'PUT', 'POST', 'DELETE', 'GET']) @crossdomain(origin='*', headers=['Origin', 'X-Requested-With', 'Content-Type', 'Accept']) @jsonp def test(): return jsonify( status="ok", method=request.method,
import json import datetime from flask import request from alerta.app import app from alerta.alert import Alert from alerta.app.utils import jsonify, jsonp, crossdomain, process_alert from alerta.app.metrics import Timer from alerta.plugins import RejectException LOG = app.logger webhook_timer = Timer('alerts', 'webhook', 'Web hook alerts', 'Total time to process number of web hook alerts') duplicate_timer = Timer('alerts', 'duplicate', 'Duplicate alerts', 'Total time to process number of duplicate alerts') correlate_timer = Timer('alerts', 'correlate', 'Correlated alerts', 'Total time to process number of correlated alerts') create_timer = Timer('alerts', 'create', 'Newly created alerts', 'Total time to process number of new alerts') def parse_notification(notification): notification = json.loads(notification) if notification['Type'] == 'SubscriptionConfirmation': return Alert( resource=notification['TopicArn'], event=notification['Type'], environment='Production', severity='informational', service=['Unknown'],
from alerta.app.metrics import Timer from flask import request, render_template, jsonify from flask_cors import cross_origin from werkzeug import urls from werkzeug.datastructures import ImmutableMultiDict try: from urllib.parse import urlparse except ImportError: from urlparse import urlparse LOG = app.logger oembed_timer = Timer('oEmbed', 'request', 'oEmbed request', 'Total time to process number of oEmbed requests') @app.route('/oembed', defaults={'format':'json'}, methods=['OPTIONS', 'GET']) @app.route('/oembed.<format>', methods=['OPTIONS', 'GET']) @cross_origin() @auth_required @jsonp def oembed(format): oembed_started = oembed_timer.start_timer() if format != 'json': oembed_timer.stop_timer(oembed_started) return jsonify(status="error", message="unsupported format: %s" % format), 400 if 'url' not in request.args or 'maxwidth' not in request.args \