-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
54 lines (39 loc) · 1.66 KB
/
Copy pathserver.py
File metadata and controls
54 lines (39 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""host_tracing — OTel ConsoleSpanExporter on both sides; trace stitches across the wire."""
from __future__ import annotations
import asyncio
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from arcp import RuntimeInfo, serve_websocket
from arcp.middleware.otel import with_tracing
from arcp.runtime import ARCPRuntime, JobContext, StaticBearerVerifier
PORT = int(os.environ.get("ARCP_DEMO_PORT", "7895"))
TOKEN = os.environ.get("ARCP_DEMO_TOKEN", "demo-token")
def configure_tracer() -> trace.Tracer:
provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)
return trace.get_tracer("arcp.examples.host_tracing.server")
async def echo(input: dict, ctx: JobContext) -> dict:
await ctx.log("info", "echo started")
return {"echoed": input}
async def main() -> None:
tracer = configure_tracer()
runtime = ARCPRuntime(
runtime=RuntimeInfo(name="host-tracing-server", version="1.0.0"),
bearer=StaticBearerVerifier({TOKEN: "demo-principal"}),
)
runtime.register_agent("echo", echo)
async def traced_accept(transport):
await runtime.accept(with_tracing(transport, tracer=tracer))
server = await serve_websocket(traced_accept, host="127.0.0.1", port=PORT, path="/arcp")
print(f"listening on ws://127.0.0.1:{PORT}/arcp")
try:
await asyncio.Future()
finally:
server.close()
await server.wait_closed()
await runtime.close()
if __name__ == "__main__":
asyncio.run(main())