This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
forked from italux/sentry-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.py
77 lines (62 loc) · 2.67 KB
/
exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
from os import getenv
from time import sleep
from wsgiref.simple_server import make_server
from flask import Flask
from prometheus_client import make_wsgi_app, start_http_server
from prometheus_client.core import REGISTRY
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple
from helpers.prometheus import SentryCollector, clean_registry
from libs.sentry import SentryAPI
DEFAULT_BASE_URL = "https://sentry.io/api/0/"
BASE_URL = getenv("SENTRY_BASE_URL") or DEFAULT_BASE_URL
AUTH_TOKEN = getenv("SENTRY_AUTH_TOKEN")
ORG_SLUG = getenv("SENTRY_EXPORTER_ORG")
PROJECTS_SLUG = getenv("SENTRY_EXPORTER_PROJECTS")
LOG_LEVEL = getenv("LOG_LEVEL", "INFO")
log = logging.getLogger("exporter")
level = logging.getLevelName(LOG_LEVEL)
logging.basicConfig(
level=logging.getLevelName(level),
format="%(asctime)s - %(process)d - %(levelname)s - %(name)s - %(message)s",
)
app = Flask(__name__)
def get_metric_config():
"""Get metric scraping options."""
scrape_issue_metrics = getenv("SENTRY_SCRAPE_ISSUE_METRICS") or "True"
scrape_events_metrics = getenv("SENTRY_SCRAPE_EVENT_METRICS") or "True"
scrape_rate_limit_metrics = getenv("SENTRY_SCRAPE_RATE_LIMIT_METRICS") or "False"
default_for_time_metrics = "True" if scrape_issue_metrics == "True" else "False"
get_1h_metrics = getenv("SENTRY_ISSUES_1H") or default_for_time_metrics
get_24h_metrics = getenv("SENTRY_ISSUES_24H") or default_for_time_metrics
get_14d_metrics = getenv("SENTRY_ISSUES_14D") or default_for_time_metrics
return [
scrape_issue_metrics,
scrape_events_metrics,
scrape_rate_limit_metrics,
get_1h_metrics,
get_24h_metrics,
get_14d_metrics,
]
@app.route("/")
def hello_world():
return "<h1>Sentry Issues & Events Exporter</h1>\
<h3>Go to <a href=/metrics/>/metrics</a></h3>\
"
@app.route("/metrics/")
def sentry_exporter():
sentry = SentryAPI(BASE_URL, AUTH_TOKEN)
log.info("exporter: cleaning registry collectors...")
clean_registry()
REGISTRY.register(SentryCollector(sentry, ORG_SLUG, get_metric_config(), PROJECTS_SLUG))
exporter = DispatcherMiddleware(app.wsgi_app, {"/metrics": make_wsgi_app()})
return exporter
if __name__ == "__main__":
if not ORG_SLUG or not AUTH_TOKEN:
log.error("ENVs: SENTRY_AUTH_TOKEN or SENTRY_EXPORTER_ORG was not found!")
exit(1)
log.info("Starting simple wsgi server...")
# The binding port was picked from the Default port allocations documentation:
# https://github.com/prometheus/prometheus/wiki/Default-port-allocations
run_simple(hostname="0.0.0.0", port=9790, application=app.wsgi_app)