|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Emit machine artifacts from docs/protocol.md. |
| 3 | +
|
| 4 | +The protocol doc carries a fenced ```yaml block tagged `# o2r-attributes` |
| 5 | +that lists the canonical span attributes. This script parses that block |
| 6 | +and writes: |
| 7 | +
|
| 8 | +- docs/generated/o2r-attributes.schema.json - JSON Schema describing |
| 9 | + the attribute set. Useful as a contract for span-validating tooling. |
| 10 | +- docs/generated/o2r-semconv.yaml - OTel-semantic-conventions-shaped |
| 11 | + YAML so downstream OTel tooling can consume the same names. |
| 12 | +
|
| 13 | +Doc is the source of truth. Run after editing the attribute block. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import argparse |
| 19 | +import json |
| 20 | +import pathlib |
| 21 | +import re |
| 22 | +import sys |
| 23 | + |
| 24 | +import yaml |
| 25 | + |
| 26 | +DOC = pathlib.Path("docs/protocol.md") |
| 27 | +OUT_DIR = pathlib.Path("docs/generated") |
| 28 | +SCHEMA_OUT = OUT_DIR / "o2r-attributes.schema.json" |
| 29 | +SEMCONV_OUT = OUT_DIR / "o2r-semconv.yaml" |
| 30 | + |
| 31 | +REQUIREMENT_VALUES = {"required", "recommended", "optional"} |
| 32 | + |
| 33 | + |
| 34 | +def extract_attributes(doc_text: str) -> list[dict[str, object]]: |
| 35 | + """Find the fenced yaml block tagged `# o2r-attributes` and parse it.""" |
| 36 | + pattern = re.compile( |
| 37 | + r"```yaml\s*\n#\s*o2r-attributes\s*\n(.*?)\n```", |
| 38 | + re.DOTALL, |
| 39 | + ) |
| 40 | + m = pattern.search(doc_text) |
| 41 | + if not m: |
| 42 | + raise SystemExit("docs/protocol.md missing the o2r-attributes yaml block") |
| 43 | + parsed = yaml.safe_load(m.group(1)) |
| 44 | + attrs = parsed.get("attributes") |
| 45 | + if not isinstance(attrs, list): |
| 46 | + raise SystemExit("o2r-attributes block must contain a list under `attributes`") |
| 47 | + return attrs |
| 48 | + |
| 49 | + |
| 50 | +def validate(attrs: list[dict[str, object]]) -> None: |
| 51 | + for a in attrs: |
| 52 | + for key in ("id", "type", "requirement", "brief"): |
| 53 | + if key not in a: |
| 54 | + raise SystemExit(f"attribute {a!r} missing required key {key}") |
| 55 | + if a["requirement"] not in REQUIREMENT_VALUES: |
| 56 | + raise SystemExit( |
| 57 | + f"attribute {a['id']} has invalid requirement {a['requirement']!r}; " |
| 58 | + f"expected one of {sorted(REQUIREMENT_VALUES)}" |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def render_schema(attrs: list[dict[str, object]]) -> dict[str, object]: |
| 63 | + properties = {} |
| 64 | + required = [] |
| 65 | + for a in attrs: |
| 66 | + prop = {"type": a["type"], "description": a["brief"]} |
| 67 | + if "enum" in a: |
| 68 | + prop["enum"] = a["enum"] |
| 69 | + properties[a["id"]] = prop |
| 70 | + if a["requirement"] == "required": |
| 71 | + required.append(a["id"]) |
| 72 | + return { |
| 73 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 74 | + "$id": "https://coilysiren.me/otel-a2a-relay/o2r-attributes.schema.json", |
| 75 | + "title": "o2r span attribute registry", |
| 76 | + "description": "Generated from docs/protocol.md. Do not hand-edit.", |
| 77 | + "type": "object", |
| 78 | + "properties": properties, |
| 79 | + "required": required, |
| 80 | + "additionalProperties": True, |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +def render_semconv(attrs: list[dict[str, object]]) -> dict[str, object]: |
| 85 | + return { |
| 86 | + "groups": [ |
| 87 | + { |
| 88 | + "id": "registry.o2r", |
| 89 | + "type": "attribute_group", |
| 90 | + "brief": "o2r span attribute registry. Generated from docs/protocol.md.", |
| 91 | + "attributes": [ |
| 92 | + { |
| 93 | + "id": a["id"], |
| 94 | + "type": a["type"], |
| 95 | + "requirement_level": a["requirement"], |
| 96 | + "brief": a["brief"], |
| 97 | + **({"members": a["enum"]} if "enum" in a else {}), |
| 98 | + } |
| 99 | + for a in attrs |
| 100 | + ], |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | +def main() -> int: |
| 107 | + p = argparse.ArgumentParser(description=__doc__) |
| 108 | + p.add_argument("--repo-root", default=".", help="repo root (default: cwd)") |
| 109 | + p.add_argument("--check", action="store_true", help="exit 1 if outputs would change") |
| 110 | + args = p.parse_args() |
| 111 | + |
| 112 | + root = pathlib.Path(args.repo_root).resolve() |
| 113 | + doc_path = root / DOC |
| 114 | + if not doc_path.exists(): |
| 115 | + print(f"missing {doc_path}", file=sys.stderr) |
| 116 | + return 2 |
| 117 | + |
| 118 | + attrs = extract_attributes(doc_path.read_text()) |
| 119 | + validate(attrs) |
| 120 | + |
| 121 | + schema = render_schema(attrs) |
| 122 | + semconv = render_semconv(attrs) |
| 123 | + |
| 124 | + schema_str = json.dumps(schema, indent=2, sort_keys=False) + "\n" |
| 125 | + semconv_str = yaml.safe_dump(semconv, sort_keys=False) |
| 126 | + |
| 127 | + schema_path = root / SCHEMA_OUT |
| 128 | + semconv_path = root / SEMCONV_OUT |
| 129 | + |
| 130 | + if args.check: |
| 131 | + existing_schema = schema_path.read_text() if schema_path.exists() else "" |
| 132 | + existing_semconv = semconv_path.read_text() if semconv_path.exists() else "" |
| 133 | + if existing_schema != schema_str or existing_semconv != semconv_str: |
| 134 | + print( |
| 135 | + "generated protocol artifacts are stale - regenerate with" |
| 136 | + " `make protocol-artifacts`", |
| 137 | + file=sys.stderr, |
| 138 | + ) |
| 139 | + return 1 |
| 140 | + return 0 |
| 141 | + |
| 142 | + (root / OUT_DIR).mkdir(parents=True, exist_ok=True) |
| 143 | + schema_path.write_text(schema_str) |
| 144 | + semconv_path.write_text(semconv_str) |
| 145 | + print(f"wrote {SCHEMA_OUT} and {SEMCONV_OUT} ({len(attrs)} attributes)") |
| 146 | + return 0 |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + raise SystemExit(main()) |
0 commit comments