#!/usr/bin/env python3 import base64 import json import urllib.parse import urllib.request from pathlib import Path from client import ROOT, arguments, check, run GRAFANA = 'http://127.0.0.1:3000' def call(method, path, body=None): password = (ROOT / 'secrets/grafana_admin_password').read_text().strip() request = urllib.request.Request(GRAFANA + path, data=json.dumps(body).encode() if body is not None else None, method=method, headers={'Content-Type': 'application/json', 'Authorization': 'Basic ' + base64.b64encode(('admin:' + password).encode()).decode()}) with urllib.request.urlopen(request, timeout=30) as response: return json.loads(response.read()) def main(): arguments('Provisioned datasources and dashboard answer their queries through Grafana') sources = {d['uid']: d['type'] for d in call('GET', '/api/datasources')} check(sources == {'prometheus': 'prometheus', 'loki': 'loki', 'tempo': 'tempo'}, 'datasources not provisioned: %s' % sources) found = call('GET', '/api/search?query=Messenger') check([d['uid'] for d in found] == ['messenger-observability'], 'dashboard not provisioned') dashboard = call('GET', '/api/dashboards/uid/messenger-observability')['dashboard'] on_disk = json.loads((ROOT / 'infra/grafana/dashboards/messenger.json').read_text()) check(len(dashboard['panels']) == len(on_disk['panels']), 'dashboard differs from the file in Git') checked = 0 for panel in dashboard['panels']: for target in panel.get('targets', []): if panel['datasource']['type'] == 'tempo': found = call('GET', '/api/datasources/proxy/uid/tempo/api/search?q=' + urllib.parse.quote(target['query']) + '&limit=5') check('traces' in found, 'panel "%s" tempo search failed' % panel['title']) checked += 1 continue query = dict(target, intervalMs=15000, maxDataPoints=500) result = call('POST', '/api/ds/query', {'queries': [query], 'from': 'now-30m', 'to': 'now'})['results'][target['refId']] check('error' not in result and result.get('status', 200) == 200, 'panel "%s" query failed: %s' % (panel['title'], result.get('error'))) checked += 1 print('grafana: 3 datasources and dashboard "%s" provisioned from Git, %d panels, %d queries executed without errors' % (dashboard['title'], len(dashboard['panels']), checked)) print('PASS: provisioning and dashboard queries') if __name__ == '__main__': run(main)