Skip to content

Instantly share code, notes, and snippets.

@Duologic
Created August 23, 2016 08:51
Show Gist options
  • Select an option

  • Save Duologic/0c5e12fb3c602eb5b0adfc40d35082a0 to your computer and use it in GitHub Desktop.

Select an option

Save Duologic/0c5e12fb3c602eb5b0adfc40d35082a0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import statsd
import time
import commands
import logging
import re
import json
import requests
# Send a test event to Sentry every time_between_tests seconds and check if it is recorded.
# If so, increase the test.succes counter. Else, increase the test.fail counter.
def test_sentry(c):
#statsd client
stats = statsd.StatsClient(c['STATSD_HOST'], c['STATSD_PORT'])
statsd_prefix = c['STATSD_PREFIX']
#Sentry client
sentry_dsn = c['SENTRY_DSN']
#Sentry monitor events
sentry_api_call = c['SENTRY_API_CALL']
#Sentry auth token
sentry_api_token = c['SENTRY_API_TOKEN']
#Time between tests
interval = c['INTERVAL']
while(True):
test_succeeded = False
test_output = commands.getoutput('raven test ' + sentry_dsn)
#Regex to find all strings between single quotes in test_output. test_output should contain the event ID of the test event between single quotes.
print test_output
eventID = re.findall(r"'(.*?)'", test_output)[0]
# Wait for the event to propagate
time.sleep(interval)
# Check if event occured in Sentry
print '%s/%s' % (sentry_api_call, eventID)
r = requests.get('%s/%s/' % (sentry_api_call, eventID), headers={'Authorization': 'Bearer %s' % sentry_api_token})
print r.text
event = json.loads(r.text)
try:
if eventID == event['eventID']:
test_succeeded = True
except KeyError:
test_succeeded = False
if test_succeeded:
logging.info('%s.raven_test.succes' % statsd_prefix)
print '%s.raven_test.succes' % statsd_prefix
stats.incr('%s.raven_test.succes' % statsd_prefix)
else:
logging.info('%s.raven_test.fail' % statsd_prefix)
print '%s.raven_test.fail' % statsd_prefix
stats.incr('%s.raven_test.fail' % statsd_prefix)
if __name__ == "__main__":
import ast
import argparse
parser = argparse.ArgumentParser(description='Monitor a Sentry instance')
parser.add_argument("-c", dest="config", required=True,
help="Configuration file with environment variables")
# Example config:
#
# SENTRY_DSN="http://2345678uygfdfghjk:3456789uytfghjkl98765@sentry.example.com/1"
# SENTRY_API_EVENT_GROUP="http://sentry.example.com/api/0/groups/12345/events/"
# SENTRY_API_TOKEN="458ughjkytr56787"
# STATSD_HOST="graphite.yourcompany.com"
# STATSD_PREFIX="system.stats.sentry"
# INTERVAL=60
args = parser.parse_args()
c = {}
with open(args.config, 'rb') as file:
for line in file.readlines():
try:
key, value = line.split("=")
value = ast.literal_eval(value)
c[key] = value
except:
pass
test_sentry(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment