118 lines
4.9 KiB
Python
118 lines
4.9 KiB
Python
"""Synthetic local experiment clients; credentials remain only in memory."""
|
|
import base64
|
|
import json
|
|
import subprocess
|
|
import time
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
API = 'http://127.0.0.1:8086'
|
|
PROM = 'http://127.0.0.1:19096'
|
|
LOKI = 'http://127.0.0.1:13106'
|
|
TEMPO = 'http://127.0.0.1:13206'
|
|
|
|
|
|
def get(url):
|
|
with urllib.request.urlopen(url, timeout=8) as response:
|
|
return json.load(response)
|
|
|
|
|
|
def compose(*args):
|
|
result = subprocess.run(['docker', 'compose', *args], cwd=ROOT, capture_output=True, text=True)
|
|
if result.returncode:
|
|
raise RuntimeError('Compose action failed: ' + ' '.join(args[:3]))
|
|
return result.stdout
|
|
|
|
|
|
def query(expression):
|
|
data = get(PROM + '/api/v1/query?' + urllib.parse.urlencode({'query': expression}))
|
|
assert data['status'] == 'success', data
|
|
return data['data']['result']
|
|
|
|
|
|
def scalar(expression):
|
|
values = query(expression)
|
|
return float(values[0]['value'][1]) if values else 0.0
|
|
|
|
|
|
def wait_for(check, timeout=90, interval=2):
|
|
deadline = time.monotonic() + timeout
|
|
last = None
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
last = check()
|
|
if last:
|
|
return last
|
|
except (urllib.error.URLError, TimeoutError, AssertionError):
|
|
pass
|
|
time.sleep(interval)
|
|
raise AssertionError('Condition did not become true; last=' + str(last)[:100])
|
|
|
|
|
|
def cgroups(services):
|
|
result = []
|
|
for service in services:
|
|
for container in compose('ps', '-q', service).split():
|
|
inspect = json.loads(subprocess.check_output(['docker', 'inspect', container]))[0]
|
|
# Read only this container's cgroup through a nonprivileged helper;
|
|
# Collector's minimal image intentionally has no shell/python.
|
|
pid = str(inspect['State']['Pid'])
|
|
script = 'p=$(cut -d: -f3 /proc/' + pid + '/cgroup); cat /sys/fs/cgroup$p/memory.current /sys/fs/cgroup$p/memory.peak; cat /sys/fs/cgroup$p/memory.events'
|
|
stats = subprocess.check_output(['docker', 'run', '--rm', '--network=none', '--read-only', '--cap-drop=ALL', '--pid=host', '--cgroupns=host', '--entrypoint', 'sh', 'postgres:17.10-alpine', '-c', script], text=True)
|
|
lines = stats.splitlines()
|
|
result.append({'service': service, 'container': container,
|
|
'current_bytes': int(lines[0]), 'peak_bytes': int(lines[1]),
|
|
'events': dict(line.split() for line in lines[2:]),
|
|
'limit_bytes': inspect['HostConfig']['Memory'],
|
|
'oom_killed': inspect['State']['OOMKilled']})
|
|
return result
|
|
|
|
|
|
class Client:
|
|
def __init__(self):
|
|
self.token = None
|
|
self.username = 'obs_' + uuid4().hex[:20]
|
|
password = 'Synthetic-' + uuid4().hex
|
|
status, user, _ = self.request('POST', '/api/v1/users', {'username': self.username, 'display_name': 'Observation fixture', 'password': password})
|
|
assert status == 201
|
|
basic = base64.b64encode((self.username + ':' + password).encode()).decode()
|
|
status, session, _ = self.request('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + basic})
|
|
assert status == 201
|
|
self.token = session['token']
|
|
status, chat, _ = self.request('POST', '/api/v1/chats', {'title': 'Observation fixture', 'member_ids': [user['id']]})
|
|
assert status == 201
|
|
self.chat = chat['id']
|
|
|
|
def request(self, method, path, body=None, headers=None):
|
|
headers = dict(headers or {})
|
|
if self.token:
|
|
headers.setdefault('Authorization', 'Bearer ' + self.token)
|
|
if isinstance(body, (dict, list)):
|
|
body = json.dumps(body).encode()
|
|
headers['Content-Type'] = 'application/json'
|
|
request = urllib.request.Request(API + path, data=body, headers=headers, method=method)
|
|
try:
|
|
response = urllib.request.urlopen(request, timeout=12)
|
|
except urllib.error.HTTPError as error:
|
|
response = error
|
|
with response:
|
|
raw = response.read()
|
|
value = json.loads(raw) if raw and 'json' in response.headers.get('Content-Type', '') else raw
|
|
return response.status, value, dict(response.headers)
|
|
|
|
def upload(self, trace_id=None):
|
|
headers = {'Content-Type': 'image/png', 'X-Filename': 'fixture.png'}
|
|
if trace_id:
|
|
headers['traceparent'] = '00-' + trace_id + '-1234567890abcdef-01'
|
|
status, body, _ = self.request('POST', f'/api/v1/chats/{self.chat}/attachments?filename=fixture.png', (ROOT/'tests/fixtures/sample.png').read_bytes(), headers)
|
|
assert status == 202, (status, body)
|
|
return body['id']
|
|
|
|
def ready(self, attachment):
|
|
status, body, _ = self.request('GET', '/api/v1/attachments/' + attachment)
|
|
return status == 200 and body['status'] == 'ready'
|