irimi-server 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- irimi_server/__init__.py +0 -0
- irimi_server/answers.py +9 -0
- irimi_server/app.py +120 -0
- irimi_server/assembly.py +210 -0
- irimi_server/diff_documents.py +278 -0
- irimi_server/diff_routes.py +77 -0
- irimi_server/document_reading.py +67 -0
- irimi_server/flags.py +9 -0
- irimi_server/json_diff.py +106 -0
- irimi_server/local_server.py +44 -0
- irimi_server/page_requests.py +35 -0
- irimi_server/pages.py +26 -0
- irimi_server/reader_documents.py +241 -0
- irimi_server/reader_routes.py +86 -0
- irimi_server/run_diff.py +182 -0
- irimi_server/run_pairing.py +83 -0
- irimi_server/sqlite_schema.py +216 -0
- irimi_server/sqlite_storage.py +266 -0
- irimi_server/storage.py +40 -0
- irimi_server/stored_fields.py +30 -0
- irimi_server/strict_json.py +12 -0
- irimi_server-0.2.0.dist-info/METADATA +25 -0
- irimi_server-0.2.0.dist-info/RECORD +24 -0
- irimi_server-0.2.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from typing import TypeVar
|
|
2
|
+
|
|
3
|
+
from irimi._wire import JsonValue
|
|
4
|
+
|
|
5
|
+
Optional = TypeVar("Optional", str, int, bool)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ReaderDocumentError(ValueError):
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def read_object(document: JsonValue, place: str) -> dict[str, JsonValue]:
|
|
13
|
+
if not isinstance(document, dict):
|
|
14
|
+
raise ReaderDocumentError(f"{place}: expected an object")
|
|
15
|
+
return document
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def read_string(document: dict[str, JsonValue], name: str, place: str) -> str:
|
|
19
|
+
value = document.get(name)
|
|
20
|
+
if not isinstance(value, str):
|
|
21
|
+
raise ReaderDocumentError(f"{place}.{name}: expected a string")
|
|
22
|
+
return value
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def read_list(document: dict[str, JsonValue], name: str, place: str) -> list[JsonValue]:
|
|
26
|
+
value = document.get(name)
|
|
27
|
+
if not isinstance(value, list):
|
|
28
|
+
raise ReaderDocumentError(f"{place}.{name}: expected a list")
|
|
29
|
+
return value
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def read_integer(document: dict[str, JsonValue], name: str, place: str) -> int:
|
|
33
|
+
value = document.get(name)
|
|
34
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
35
|
+
raise ReaderDocumentError(f"{place}.{name}: expected a whole number")
|
|
36
|
+
return value
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def read_boolean(document: dict[str, JsonValue], name: str, place: str) -> bool:
|
|
40
|
+
value = document.get(name)
|
|
41
|
+
if not isinstance(value, bool):
|
|
42
|
+
raise ReaderDocumentError(f"{place}.{name}: expected true or false")
|
|
43
|
+
return value
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def read_optional(
|
|
47
|
+
document: dict[str, JsonValue], name: str, place: str, expected: type[Optional]
|
|
48
|
+
) -> Optional | None:
|
|
49
|
+
value = document.get(name)
|
|
50
|
+
if value is None:
|
|
51
|
+
return None
|
|
52
|
+
if isinstance(value, expected) and isinstance(value, bool) == (expected is bool):
|
|
53
|
+
return value
|
|
54
|
+
raise ReaderDocumentError(f"{place}.{name}: expected {expected.__name__} or null")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def read_time(document: dict[str, JsonValue], name: str, place: str) -> int | None:
|
|
58
|
+
value = document.get(name)
|
|
59
|
+
if value is None:
|
|
60
|
+
return None
|
|
61
|
+
if not isinstance(value, str) or not (value.isascii() and value.isdigit()):
|
|
62
|
+
raise ReaderDocumentError(f"{place}.{name}: expected a string of digits or null")
|
|
63
|
+
return int(value)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def read_conditions(document: dict[str, JsonValue], name: str, place: str) -> tuple[str, ...]:
|
|
67
|
+
return tuple(str(condition) for condition in read_list(document, name, place))
|
irimi_server/flags.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
|
|
3
|
+
from irimi._wire import JsonValue
|
|
4
|
+
from irimi._wire.field_names import FLAGS
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def read_flags(fields: Mapping[str, JsonValue]) -> tuple[JsonValue, ...]:
|
|
8
|
+
flags = fields.get(FLAGS)
|
|
9
|
+
return tuple(flags) if isinstance(flags, list) else ()
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from collections.abc import Iterator
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from difflib import SequenceMatcher
|
|
7
|
+
from typing import Literal
|
|
8
|
+
|
|
9
|
+
from irimi._wire import JsonValue
|
|
10
|
+
|
|
11
|
+
JsonPath = tuple[str | int, ...]
|
|
12
|
+
ChangeKind = Literal["added", "removed", "replaced"]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class JsonTooDeepError(Exception):
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class Change:
|
|
21
|
+
kind: ChangeKind
|
|
22
|
+
base_path: JsonPath | None
|
|
23
|
+
candidate_path: JsonPath | None
|
|
24
|
+
base: JsonValue
|
|
25
|
+
candidate: JsonValue
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def diff_json(base: JsonValue, candidate: JsonValue) -> tuple[Change, ...]:
|
|
29
|
+
try:
|
|
30
|
+
return tuple(_diff_values(base, candidate, (), ()))
|
|
31
|
+
except RecursionError:
|
|
32
|
+
raise JsonTooDeepError("nested too deeply to compare") from None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _diff_values(
|
|
36
|
+
base: JsonValue, candidate: JsonValue, base_path: JsonPath, candidate_path: JsonPath
|
|
37
|
+
) -> Iterator[Change]:
|
|
38
|
+
if isinstance(base, dict) and isinstance(candidate, dict):
|
|
39
|
+
yield from _diff_objects(base, candidate, base_path, candidate_path)
|
|
40
|
+
elif isinstance(base, list) and isinstance(candidate, list):
|
|
41
|
+
yield from _diff_lists(base, candidate, base_path, candidate_path)
|
|
42
|
+
elif not _is_same_scalar(base, candidate):
|
|
43
|
+
yield Change("replaced", base_path, candidate_path, base, candidate)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _diff_objects(
|
|
47
|
+
base: dict[str, JsonValue],
|
|
48
|
+
candidate: dict[str, JsonValue],
|
|
49
|
+
base_path: JsonPath,
|
|
50
|
+
candidate_path: JsonPath,
|
|
51
|
+
) -> Iterator[Change]:
|
|
52
|
+
for name, value in base.items():
|
|
53
|
+
if name in candidate:
|
|
54
|
+
yield from _diff_values(
|
|
55
|
+
value, candidate[name], (*base_path, name), (*candidate_path, name)
|
|
56
|
+
)
|
|
57
|
+
else:
|
|
58
|
+
yield _removed(value, (*base_path, name))
|
|
59
|
+
for name, value in candidate.items():
|
|
60
|
+
if name not in base:
|
|
61
|
+
yield _added(value, (*candidate_path, name))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _diff_lists(
|
|
65
|
+
base: list[JsonValue],
|
|
66
|
+
candidate: list[JsonValue],
|
|
67
|
+
base_path: JsonPath,
|
|
68
|
+
candidate_path: JsonPath,
|
|
69
|
+
) -> Iterator[Change]:
|
|
70
|
+
aligned = SequenceMatcher(
|
|
71
|
+
None, _write_items(base), _write_items(candidate), autojunk=False
|
|
72
|
+
).get_opcodes()
|
|
73
|
+
for alignment, base_start, base_end, candidate_start, candidate_end in aligned:
|
|
74
|
+
if alignment == "equal":
|
|
75
|
+
continue
|
|
76
|
+
paired = min(base_end - base_start, candidate_end - candidate_start)
|
|
77
|
+
for offset in range(paired):
|
|
78
|
+
base_place, candidate_place = base_start + offset, candidate_start + offset
|
|
79
|
+
yield from _diff_values(
|
|
80
|
+
base[base_place],
|
|
81
|
+
candidate[candidate_place],
|
|
82
|
+
(*base_path, base_place),
|
|
83
|
+
(*candidate_path, candidate_place),
|
|
84
|
+
)
|
|
85
|
+
for place in range(base_start + paired, base_end):
|
|
86
|
+
yield _removed(base[place], (*base_path, place))
|
|
87
|
+
for place in range(candidate_start + paired, candidate_end):
|
|
88
|
+
yield _added(candidate[place], (*candidate_path, place))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _write_items(items: list[JsonValue]) -> list[str]:
|
|
92
|
+
return [json.dumps(item, sort_keys=True) for item in items]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _is_same_scalar(base: JsonValue, candidate: JsonValue) -> bool:
|
|
96
|
+
if isinstance(base, dict | list) or isinstance(candidate, dict | list):
|
|
97
|
+
return False
|
|
98
|
+
return isinstance(base, bool) == isinstance(candidate, bool) and base == candidate
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _removed(value: JsonValue, base_path: JsonPath) -> Change:
|
|
102
|
+
return Change("removed", base_path, None, value, None)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _added(value: JsonValue, candidate_path: JsonPath) -> Change:
|
|
106
|
+
return Change("added", None, candidate_path, None, value)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import socket
|
|
2
|
+
import sqlite3
|
|
3
|
+
import time
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import uvicorn
|
|
8
|
+
|
|
9
|
+
from irimi_server.app import create_app
|
|
10
|
+
from irimi_server.sqlite_schema import NewerSchemaError
|
|
11
|
+
from irimi_server.sqlite_storage import SqliteStorageEngine
|
|
12
|
+
|
|
13
|
+
LOCAL_HOST = "127.0.0.1"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class LocalServerError(Exception):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def serve_local_server(database_path: Path, port: int, announce: Callable[[str], None]) -> None:
|
|
21
|
+
listener = _listen_on(port)
|
|
22
|
+
try:
|
|
23
|
+
storage = SqliteStorageEngine(database_path, time.time_ns)
|
|
24
|
+
except (sqlite3.Error, OSError, NewerSchemaError) as error:
|
|
25
|
+
listener.close()
|
|
26
|
+
raise LocalServerError(f"cannot open the database {database_path}: {error}") from error
|
|
27
|
+
announce(f"http://{LOCAL_HOST}:{listener.getsockname()[1]}")
|
|
28
|
+
server = uvicorn.Server(uvicorn.Config(create_app(storage), log_level="warning"))
|
|
29
|
+
try:
|
|
30
|
+
server.run(sockets=[listener])
|
|
31
|
+
except KeyboardInterrupt:
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _listen_on(port: int) -> socket.socket:
|
|
36
|
+
listener = socket.socket()
|
|
37
|
+
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
38
|
+
try:
|
|
39
|
+
listener.bind((LOCAL_HOST, port))
|
|
40
|
+
except (OSError, OverflowError) as error:
|
|
41
|
+
listener.close()
|
|
42
|
+
raise LocalServerError(f"cannot listen on port {port}: {error}") from error
|
|
43
|
+
listener.listen()
|
|
44
|
+
return listener
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
|
|
4
|
+
LARGEST_EVENT_PAGE_LIMIT = 1000
|
|
5
|
+
EVENT_PAGE_SIZE_LIMIT = 8 * 1024 * 1024
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class PageRequest:
|
|
10
|
+
cursor: str | None
|
|
11
|
+
limit: int
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class InvalidPageRequestError(ValueError):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def read_page_request(
|
|
19
|
+
query: Mapping[str, str], default_limit: int, largest_limit: int
|
|
20
|
+
) -> PageRequest:
|
|
21
|
+
return PageRequest(
|
|
22
|
+
cursor=query.get("cursor"),
|
|
23
|
+
limit=_read_limit(query.get("limit"), default_limit, largest_limit),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _read_limit(text: str | None, default_limit: int, largest_limit: int) -> int:
|
|
28
|
+
if text is None:
|
|
29
|
+
return default_limit
|
|
30
|
+
is_short_whole_number = (
|
|
31
|
+
text.isascii() and text.isdigit() and len(text) <= len(str(largest_limit))
|
|
32
|
+
)
|
|
33
|
+
if not is_short_whole_number or not 1 <= int(text) <= largest_limit:
|
|
34
|
+
raise InvalidPageRequestError(f"limit: expected a whole number from 1 to {largest_limit}")
|
|
35
|
+
return int(text)
|
irimi_server/pages.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from collections.abc import Sequence
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Generic, TypeVar
|
|
4
|
+
|
|
5
|
+
Item = TypeVar("Item")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class Page(Generic[Item]):
|
|
10
|
+
items: tuple[Item, ...]
|
|
11
|
+
next_cursor: str | None
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class InvalidCursorError(ValueError):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def count_items_that_fit(sizes: Sequence[int], limit: int, size_limit: int) -> int:
|
|
19
|
+
count = 0
|
|
20
|
+
total_size = 0
|
|
21
|
+
for size in sizes[:limit]:
|
|
22
|
+
total_size += size
|
|
23
|
+
if count > 0 and total_size > size_limit:
|
|
24
|
+
return count
|
|
25
|
+
count += 1
|
|
26
|
+
return count
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
from irimi._wire import JsonValue
|
|
7
|
+
from irimi._wire.field_names import ENVIRONMENT, FLAGS, MODE, NEXT_CURSOR, STATUS
|
|
8
|
+
|
|
9
|
+
from irimi_server.document_reading import (
|
|
10
|
+
ReaderDocumentError,
|
|
11
|
+
read_conditions,
|
|
12
|
+
read_list,
|
|
13
|
+
read_object,
|
|
14
|
+
read_optional,
|
|
15
|
+
read_string,
|
|
16
|
+
read_time,
|
|
17
|
+
)
|
|
18
|
+
from irimi_server.pages import Page
|
|
19
|
+
|
|
20
|
+
READER_API_VERSION = "v1"
|
|
21
|
+
RUNS_PATH = f"/api/{READER_API_VERSION}/runs"
|
|
22
|
+
RUN_PATH = f"{RUNS_PATH}/{{trace_id}}"
|
|
23
|
+
CALL_PATH = f"{RUN_PATH}/calls/{{span_id}}"
|
|
24
|
+
UNATTACHED_CALLS_PATH = f"/api/{READER_API_VERSION}/unattached-calls"
|
|
25
|
+
|
|
26
|
+
REFUSAL_REASON = "error"
|
|
27
|
+
RUNS = "runs"
|
|
28
|
+
RUN = "run"
|
|
29
|
+
CALL = "call"
|
|
30
|
+
ROOT = "root"
|
|
31
|
+
DETACHED = "detached"
|
|
32
|
+
PARENT_SPAN_ID = "parent_span_id"
|
|
33
|
+
SEQ = "seq"
|
|
34
|
+
CHILDREN = "children"
|
|
35
|
+
EVENTS = "events"
|
|
36
|
+
ID = "id"
|
|
37
|
+
SPAN_ID = "span_id"
|
|
38
|
+
KIND = "kind"
|
|
39
|
+
NAME = "name"
|
|
40
|
+
START_TIME = "start_time_unix_nano"
|
|
41
|
+
END_TIME = "end_time_unix_nano"
|
|
42
|
+
CONDITIONS = "conditions"
|
|
43
|
+
RUN_SUMMARY_KEYS = frozenset(
|
|
44
|
+
{ID, SPAN_ID, KIND, NAME, START_TIME, END_TIME, STATUS, FLAGS, ENVIRONMENT, MODE, CONDITIONS}
|
|
45
|
+
)
|
|
46
|
+
NODE_KEYS = frozenset(
|
|
47
|
+
{
|
|
48
|
+
SPAN_ID,
|
|
49
|
+
PARENT_SPAN_ID,
|
|
50
|
+
KIND,
|
|
51
|
+
NAME,
|
|
52
|
+
SEQ,
|
|
53
|
+
START_TIME,
|
|
54
|
+
END_TIME,
|
|
55
|
+
STATUS,
|
|
56
|
+
FLAGS,
|
|
57
|
+
CHILDREN,
|
|
58
|
+
EVENTS,
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class RunSummary:
|
|
65
|
+
id: str
|
|
66
|
+
span_id: str
|
|
67
|
+
kind: str
|
|
68
|
+
name: str
|
|
69
|
+
start_time_unix_nano: int
|
|
70
|
+
end_time_unix_nano: int | None
|
|
71
|
+
status: JsonValue
|
|
72
|
+
flags: tuple[JsonValue, ...]
|
|
73
|
+
environment: JsonValue
|
|
74
|
+
mode: JsonValue
|
|
75
|
+
conditions: tuple[str, ...]
|
|
76
|
+
fields: Mapping[str, JsonValue]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@dataclass(frozen=True)
|
|
80
|
+
class Node:
|
|
81
|
+
span_id: str
|
|
82
|
+
parent_span_id: str | None
|
|
83
|
+
kind: str | None
|
|
84
|
+
name: str | None
|
|
85
|
+
seq: int | None
|
|
86
|
+
start_time_unix_nano: int | None
|
|
87
|
+
end_time_unix_nano: int | None
|
|
88
|
+
status: JsonValue
|
|
89
|
+
flags: tuple[JsonValue, ...]
|
|
90
|
+
fields: Mapping[str, JsonValue]
|
|
91
|
+
events: tuple[Mapping[str, JsonValue], ...]
|
|
92
|
+
children: tuple[Node, ...]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@dataclass(frozen=True)
|
|
96
|
+
class RunTree:
|
|
97
|
+
id: str
|
|
98
|
+
conditions: tuple[str, ...]
|
|
99
|
+
root: Node | None
|
|
100
|
+
detached: tuple[Node, ...]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def write_refusal(reason: str) -> dict[str, JsonValue]:
|
|
104
|
+
return {REFUSAL_REASON: reason}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def write_run(run: RunTree) -> dict[str, JsonValue]:
|
|
108
|
+
detached: list[JsonValue] = [_write_node(node) for node in run.detached]
|
|
109
|
+
return {
|
|
110
|
+
RUN: {
|
|
111
|
+
ID: run.id,
|
|
112
|
+
CONDITIONS: list(run.conditions),
|
|
113
|
+
ROOT: None if run.root is None else _write_node(run.root),
|
|
114
|
+
DETACHED: detached,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def parse_run(document: JsonValue) -> RunTree:
|
|
120
|
+
run = read_object(document.get(RUN) if isinstance(document, dict) else None, RUN)
|
|
121
|
+
root = run.get(ROOT)
|
|
122
|
+
return RunTree(
|
|
123
|
+
id=read_string(run, ID, RUN),
|
|
124
|
+
conditions=read_conditions(run, CONDITIONS, RUN),
|
|
125
|
+
root=None if root is None else _parse_node(root, f"{RUN}.{ROOT}"),
|
|
126
|
+
detached=_parse_nodes(run, DETACHED, RUN),
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def write_call(node: Node) -> dict[str, JsonValue]:
|
|
131
|
+
events: list[JsonValue] = [dict(event) for event in node.events]
|
|
132
|
+
return {CALL: _add_other_fields({**_write_core(node), EVENTS: events}, node.fields)}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def write_run_page(page: Page[RunSummary]) -> dict[str, JsonValue]:
|
|
136
|
+
summaries: list[JsonValue] = [_write_run_summary(summary) for summary in page.items]
|
|
137
|
+
return {RUNS: summaries, NEXT_CURSOR: page.next_cursor}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def parse_run_page(document: JsonValue) -> Page[RunSummary]:
|
|
141
|
+
page = read_object(document, "run page")
|
|
142
|
+
runs = page.get(RUNS)
|
|
143
|
+
next_cursor = page.get(NEXT_CURSOR)
|
|
144
|
+
if not isinstance(runs, list):
|
|
145
|
+
raise ReaderDocumentError(f"run page.{RUNS}: expected a list")
|
|
146
|
+
if not isinstance(next_cursor, str | None):
|
|
147
|
+
raise ReaderDocumentError(f"run page.{NEXT_CURSOR}: expected a string or null")
|
|
148
|
+
return Page(
|
|
149
|
+
items=tuple(_parse_run_summary(run, f"{RUNS}[{place}]") for place, run in enumerate(runs)),
|
|
150
|
+
next_cursor=next_cursor,
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def write_time(time_unix_nano: int | None) -> str | None:
|
|
155
|
+
return None if time_unix_nano is None else str(time_unix_nano)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _write_run_summary(summary: RunSummary) -> dict[str, JsonValue]:
|
|
159
|
+
core: dict[str, JsonValue] = {
|
|
160
|
+
ID: summary.id,
|
|
161
|
+
SPAN_ID: summary.span_id,
|
|
162
|
+
KIND: summary.kind,
|
|
163
|
+
NAME: summary.name,
|
|
164
|
+
START_TIME: write_time(summary.start_time_unix_nano),
|
|
165
|
+
END_TIME: write_time(summary.end_time_unix_nano),
|
|
166
|
+
STATUS: summary.status,
|
|
167
|
+
FLAGS: list(summary.flags),
|
|
168
|
+
ENVIRONMENT: summary.environment,
|
|
169
|
+
MODE: summary.mode,
|
|
170
|
+
CONDITIONS: list(summary.conditions),
|
|
171
|
+
}
|
|
172
|
+
return _add_other_fields(core, summary.fields)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _add_other_fields(
|
|
176
|
+
core: dict[str, JsonValue], fields: Mapping[str, JsonValue]
|
|
177
|
+
) -> dict[str, JsonValue]:
|
|
178
|
+
return {**core, **{name: value for name, value in fields.items() if name not in core}}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _parse_run_summary(document: JsonValue, place: str) -> RunSummary:
|
|
182
|
+
summary = read_object(document, place)
|
|
183
|
+
start_time = read_time(summary, START_TIME, place)
|
|
184
|
+
if start_time is None:
|
|
185
|
+
raise ReaderDocumentError(f"{place}.{START_TIME}: expected a string of digits")
|
|
186
|
+
return RunSummary(
|
|
187
|
+
id=read_string(summary, ID, place),
|
|
188
|
+
span_id=read_string(summary, SPAN_ID, place),
|
|
189
|
+
kind=read_string(summary, KIND, place),
|
|
190
|
+
name=read_string(summary, NAME, place),
|
|
191
|
+
start_time_unix_nano=start_time,
|
|
192
|
+
end_time_unix_nano=read_time(summary, END_TIME, place),
|
|
193
|
+
status=summary.get(STATUS),
|
|
194
|
+
flags=tuple(read_list(summary, FLAGS, place)),
|
|
195
|
+
environment=summary.get(ENVIRONMENT),
|
|
196
|
+
mode=summary.get(MODE),
|
|
197
|
+
conditions=read_conditions(summary, CONDITIONS, place),
|
|
198
|
+
fields={name: value for name, value in summary.items() if name not in RUN_SUMMARY_KEYS},
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _write_node(node: Node) -> dict[str, JsonValue]:
|
|
203
|
+
children: list[JsonValue] = [_write_node(child) for child in node.children]
|
|
204
|
+
return _add_other_fields({**_write_core(node), CHILDREN: children}, node.fields)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _write_core(node: Node) -> dict[str, JsonValue]:
|
|
208
|
+
return {
|
|
209
|
+
SPAN_ID: node.span_id,
|
|
210
|
+
PARENT_SPAN_ID: node.parent_span_id,
|
|
211
|
+
KIND: node.kind,
|
|
212
|
+
NAME: node.name,
|
|
213
|
+
SEQ: node.seq,
|
|
214
|
+
START_TIME: write_time(node.start_time_unix_nano),
|
|
215
|
+
END_TIME: write_time(node.end_time_unix_nano),
|
|
216
|
+
STATUS: node.status,
|
|
217
|
+
FLAGS: list(node.flags),
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _parse_node(document: JsonValue, place: str) -> Node:
|
|
222
|
+
node = read_object(document, place)
|
|
223
|
+
return Node(
|
|
224
|
+
span_id=read_string(node, SPAN_ID, place),
|
|
225
|
+
parent_span_id=read_optional(node, PARENT_SPAN_ID, place, str),
|
|
226
|
+
kind=read_optional(node, KIND, place, str),
|
|
227
|
+
name=read_optional(node, NAME, place, str),
|
|
228
|
+
seq=read_optional(node, SEQ, place, int),
|
|
229
|
+
start_time_unix_nano=read_time(node, START_TIME, place),
|
|
230
|
+
end_time_unix_nano=read_time(node, END_TIME, place),
|
|
231
|
+
status=node.get(STATUS),
|
|
232
|
+
flags=tuple(read_list(node, FLAGS, place)),
|
|
233
|
+
fields={name: value for name, value in node.items() if name not in NODE_KEYS},
|
|
234
|
+
events=(),
|
|
235
|
+
children=_parse_nodes(node, CHILDREN, place),
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _parse_nodes(document: dict[str, JsonValue], name: str, place: str) -> tuple[Node, ...]:
|
|
240
|
+
nodes = read_list(document, name, place)
|
|
241
|
+
return tuple(_parse_node(node, f"{place}.{name}[{index}]") for index, node in enumerate(nodes))
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from starlette.concurrency import run_in_threadpool
|
|
2
|
+
from starlette.requests import Request
|
|
3
|
+
from starlette.routing import Route
|
|
4
|
+
|
|
5
|
+
from irimi_server.answers import JsonAnswer
|
|
6
|
+
from irimi_server.assembly import assemble_call, assemble_run, summarise_root
|
|
7
|
+
from irimi_server.page_requests import read_page_request
|
|
8
|
+
from irimi_server.pages import Page
|
|
9
|
+
from irimi_server.reader_documents import (
|
|
10
|
+
CALL_PATH,
|
|
11
|
+
READER_API_VERSION,
|
|
12
|
+
RUN_PATH,
|
|
13
|
+
RUNS_PATH,
|
|
14
|
+
UNATTACHED_CALLS_PATH,
|
|
15
|
+
RunSummary,
|
|
16
|
+
write_call,
|
|
17
|
+
write_refusal,
|
|
18
|
+
write_run,
|
|
19
|
+
write_run_page,
|
|
20
|
+
)
|
|
21
|
+
from irimi_server.storage import DEFAULT_PROJECT, NotHeldError, RootSet, StorageEngine
|
|
22
|
+
|
|
23
|
+
ACCEPTED_READER_API_VERSIONS = (READER_API_VERSION,)
|
|
24
|
+
DEFAULT_LIST_PAGE_LIMIT = 50
|
|
25
|
+
LARGEST_LIST_PAGE_LIMIT = 200
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def create_reader_routes(storage: StorageEngine) -> list[Route]:
|
|
29
|
+
async def list_summaries(request: Request, root_set: RootSet) -> Page[RunSummary]:
|
|
30
|
+
page_request = read_page_request(
|
|
31
|
+
request.query_params, DEFAULT_LIST_PAGE_LIMIT, LARGEST_LIST_PAGE_LIMIT
|
|
32
|
+
)
|
|
33
|
+
page = await run_in_threadpool(
|
|
34
|
+
storage.list_roots, DEFAULT_PROJECT, root_set, page_request.cursor, page_request.limit
|
|
35
|
+
)
|
|
36
|
+
return Page(
|
|
37
|
+
items=tuple(summarise_root(root) for root in page.items), next_cursor=page.next_cursor
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
async def list_runs(request: Request) -> JsonAnswer:
|
|
41
|
+
return JsonAnswer(write_run_page(await list_summaries(request, "runs")))
|
|
42
|
+
|
|
43
|
+
async def list_unattached_calls(request: Request) -> JsonAnswer:
|
|
44
|
+
return JsonAnswer(write_run_page(await list_summaries(request, "unattached_calls")))
|
|
45
|
+
|
|
46
|
+
async def read_run(request: Request) -> JsonAnswer:
|
|
47
|
+
trace_id: str = request.path_params["trace_id"]
|
|
48
|
+
events = await run_in_threadpool(storage.read_run_core_events, DEFAULT_PROJECT, trace_id)
|
|
49
|
+
if not events:
|
|
50
|
+
raise NotHeldError(f"no run with id {trace_id}")
|
|
51
|
+
return JsonAnswer(write_run(assemble_run(trace_id, events)))
|
|
52
|
+
|
|
53
|
+
async def read_call(request: Request) -> JsonAnswer:
|
|
54
|
+
trace_id: str = request.path_params["trace_id"]
|
|
55
|
+
span_id: str = request.path_params["span_id"]
|
|
56
|
+
events = await run_in_threadpool(
|
|
57
|
+
storage.read_call_events, DEFAULT_PROJECT, trace_id, span_id
|
|
58
|
+
)
|
|
59
|
+
call = assemble_call(events)
|
|
60
|
+
if call is None:
|
|
61
|
+
raise NotHeldError(f"no call with id {span_id} in run {trace_id}")
|
|
62
|
+
return JsonAnswer(write_call(call))
|
|
63
|
+
|
|
64
|
+
return [
|
|
65
|
+
Route(RUNS_PATH, list_runs, methods=["GET"]),
|
|
66
|
+
Route(RUN_PATH, read_run, methods=["GET"]),
|
|
67
|
+
Route(CALL_PATH, read_call, methods=["GET"]),
|
|
68
|
+
Route(UNATTACHED_CALLS_PATH, list_unattached_calls, methods=["GET"]),
|
|
69
|
+
Route(
|
|
70
|
+
"/api/{version}/{rest:path}", refuse_unknown_reader_api_path, methods=["GET", "POST"]
|
|
71
|
+
),
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
async def refuse_unknown_reader_api_path(request: Request) -> JsonAnswer:
|
|
76
|
+
version = request.path_params["version"]
|
|
77
|
+
if version in ACCEPTED_READER_API_VERSIONS:
|
|
78
|
+
refusal = write_refusal(f"nothing is served at {request.url.path}")
|
|
79
|
+
return JsonAnswer(refusal, status_code=404)
|
|
80
|
+
return JsonAnswer(
|
|
81
|
+
{
|
|
82
|
+
**write_refusal(f"unknown reader API version: {version}"),
|
|
83
|
+
"accepted_versions": list(ACCEPTED_READER_API_VERSIONS),
|
|
84
|
+
},
|
|
85
|
+
status_code=404,
|
|
86
|
+
)
|