Skip to content

Commit

Permalink
[IMPROVEMENT] Add /api/v1/query proxy
Browse files Browse the repository at this point in the history
Merge pull request #88 from kfdm/proxy-query
  • Loading branch information
kfdm authored Jun 19, 2018
2 parents 0639bd2 + 79a6162 commit 729a250
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions promgen/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
url(r'^graph', views.ProxyGraph.as_view()),
url(r'^api/v1/label/(.+)/values', views.ProxyLabel.as_view(), name='proxy-label'),
url(r'^api/v1/query_range', views.ProxyQueryRange.as_view()),
url(r'^api/v1/query', views.ProxyQuery.as_view()),
url(r'^api/v1/series', views.ProxySeries.as_view()),
]

Expand Down
32 changes: 32 additions & 0 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,3 +1362,35 @@ def get(self, request):
'result': data,
}
})


class ProxyQuery(PrometheusProxy):
def get(self, request):
data = []
futures = []
resultType = None
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
for host in models.Shard.objects.filter(proxy=True):
futures.append(executor.submit(util.get, '{}/api/v1/query?{}'.format(host.url, request.META['QUERY_STRING']), headers=self.headers))
for future in concurrent.futures.as_completed(futures):
try:
result = future.result()
# Need to try to decode the json BEFORE we raise_for_status
# so that we can pass back the error message from Prometheus
_json = result.json()
result.raise_for_status()
logger.debug('Appending data from %s', result.request.url)
data += _json['data']['result']
resultType = _json['data']['resultType']
except:
logger.exception('Error with response')
_json['promgen_proxy_request'] = result.request.url
return JsonResponse(_json, status=result.status_code)

return JsonResponse({
'status': 'success',
'data': {
'resultType': resultType,
'result': data,
}
})

0 comments on commit 729a250

Please sign in to comment.