Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing query proxy #88

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
})