Skip to content

Commit

Permalink
fixes bug 1240107 - Expose a /healthcheck endpoint for ELB and Pingdo…
Browse files Browse the repository at this point in the history
…m, r=adngdb
  • Loading branch information
peterbe committed Jan 20, 2016
1 parent 895b3ae commit d472309
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
44 changes: 43 additions & 1 deletion webapp-django/crashstats/crashstats/tests/test_views.py
Expand Up @@ -2,13 +2,13 @@
import csv
import datetime
import json
import mock
import re
import urllib
import random
import urlparse

import pyquery
import mock

from cStringIO import StringIO
from nose.tools import eq_, ok_, assert_raises
Expand Down Expand Up @@ -571,6 +571,48 @@ def test_contribute_json(self):
ok_(json.loads(''.join(response.streaming_content)))
eq_(response['Content-Type'], 'application/json')

def test_healthcheck_elb(self):
response = self.client.get('/healthcheck', {'elb': 'true'})
eq_(response.status_code, 200)
eq_(json.loads(response.content)['ok'], True)

# This time, ignoring the results, make sure that running
# this does not cause an DB queries.
self.assertNumQueries(
0,
self.client.get,
'/healthcheck',
{'elb': 'true'}
)

@mock.patch('crashstats.crashstats.views.elasticsearch')
def test_healthcheck(self, mocked_elasticsearch):

es_instance = mock.MagicMock()

def fake_es_instance(**config):
eq_(
config['hosts'],
settings.SOCORRO_IMPLEMENTATIONS_CONFIG
['elasticsearch']['elasticsearch_urls']
)
return es_instance

mocked_elasticsearch.Elasticsearch.side_effect = fake_es_instance
response = self.client.get('/healthcheck')
eq_(response.status_code, 200)
eq_(json.loads(response.content)['ok'], True)

es_instance.info.assert_called_with()

self.assertNumQueries(
1,
self.client.get,
'/healthcheck',
)

eq_(es_instance.info.call_count, 2)

@mock.patch('requests.get')
def test_handler500(self, rget):
root_urlconf = __import__(
Expand Down
3 changes: 3 additions & 0 deletions webapp-django/crashstats/crashstats/urls.py
Expand Up @@ -26,6 +26,9 @@
url('^robots\.txt$',
views.robots_txt,
name='robots_txt'),
url('^healthcheck$',
views.healthcheck,
name='healthcheck'),
url('^home' + products + '$',
views.home,
name='home'),
Expand Down
24 changes: 24 additions & 0 deletions webapp-django/crashstats/crashstats/views.py
@@ -1,6 +1,7 @@
import copy
import json
import datetime
import elasticsearch
import logging
import math
import urllib
Expand All @@ -17,6 +18,7 @@
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import permission_required
from django.core.cache import cache
from django.contrib.auth.models import Permission
from django.utils.http import urlquote
from django.contrib import messages

Expand Down Expand Up @@ -100,6 +102,28 @@ def robots_txt(request):
)


def healthcheck(request):
if not request.GET.get('elb') in ('1', 'true'):
# Perform some really basic DB queries.
# There will always be permissions created
assert Permission.objects.all().count() > 0

# We should also be able to set and get a cache value
cache_key = '__healthcheck__'
cache.set(cache_key, 1, 10)
assert cache.get(cache_key)
cache.delete(cache_key)

# Do a really basic Elasticsearch query
es_settings = settings.SOCORRO_IMPLEMENTATIONS_CONFIG['elasticsearch']
es = elasticsearch.Elasticsearch(
hosts=es_settings['elasticsearch_urls']
)
es.info() # will raise an error if there's a problem with the cluster

return http.JsonResponse({'ok': True})


def has_builds(product, versions):
contains_builds = False
prod_versions = []
Expand Down

0 comments on commit d472309

Please sign in to comment.