| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from datetime import datetime |
| 8 | from enum import Enum |
| 9 | from typing import Any, Dict, Literal, Optional, Protocol, Union |
| 10 | |
| 11 | from llama_models.schema_utils import json_schema_type, webmethod |
| 12 | from pydantic import BaseModel, Field |
| 13 | from typing_extensions import Annotated |
| 14 | |
| 15 | |
| 16 | @json_schema_type |
| 17 | class SpanStatus(Enum): |
| 18 | OK = "ok" |
| 19 | ERROR = "error" |
| 20 | |
| 21 | |
| 22 | @json_schema_type |
| 23 | class Span(BaseModel): |
| 24 | span_id: str |
| 25 | trace_id: str |
| 26 | parent_span_id: Optional[str] = None |
| 27 | name: str |
| 28 | start_time: datetime |
| 29 | end_time: Optional[datetime] = None |
| 30 | attributes: Optional[Dict[str, Any]] = Field(default_factory=dict) |
| 31 | |
| 32 | |
| 33 | @json_schema_type |
| 34 | class Trace(BaseModel): |
| 35 | trace_id: str |
| 36 | root_span_id: str |
| 37 | start_time: datetime |
| 38 | end_time: Optional[datetime] = None |
| 39 | |
| 40 | |
| 41 | @json_schema_type |
| 42 | class EventType(Enum): |
| 43 | UNSTRUCTURED_LOG = "unstructured_log" |
| 44 | STRUCTURED_LOG = "structured_log" |
| 45 | METRIC = "metric" |
| 46 | |
| 47 | |
| 48 | @json_schema_type |
| 49 | class LogSeverity(Enum): |
| 50 | VERBOSE = "verbose" |
| 51 | DEBUG = "debug" |
| 52 | INFO = "info" |
| 53 | WARN = "warn" |
| 54 | ERROR = "error" |
| 55 | CRITICAL = "critical" |
| 56 | |
| 57 | |
| 58 | class EventCommon(BaseModel): |
| 59 | trace_id: str |
| 60 | span_id: str |
| 61 | timestamp: datetime |
| 62 | attributes: Optional[Dict[str, Any]] = Field(default_factory=dict) |
| 63 | |
| 64 | |
| 65 | @json_schema_type |
| 66 | class UnstructuredLogEvent(EventCommon): |
| 67 | type: Literal[EventType.UNSTRUCTURED_LOG.value] = EventType.UNSTRUCTURED_LOG.value |
| 68 | message: str |
| 69 | severity: LogSeverity |
| 70 | |
| 71 | |
| 72 | @json_schema_type |
| 73 | class MetricEvent(EventCommon): |
| 74 | type: Literal[EventType.METRIC.value] = EventType.METRIC.value |
| 75 | metric: str # this would be an enum |
| 76 | value: Union[int, float] |
| 77 | unit: str |
| 78 | |
| 79 | |
| 80 | @json_schema_type |
| 81 | class StructuredLogType(Enum): |
| 82 | SPAN_START = "span_start" |
| 83 | SPAN_END = "span_end" |
| 84 | |
| 85 | |
| 86 | @json_schema_type |
| 87 | class SpanStartPayload(BaseModel): |
| 88 | type: Literal[StructuredLogType.SPAN_START.value] = ( |
| 89 | StructuredLogType.SPAN_START.value |
| 90 | ) |
| 91 | name: str |
| 92 | parent_span_id: Optional[str] = None |
| 93 | |
| 94 | |
| 95 | @json_schema_type |
| 96 | class SpanEndPayload(BaseModel): |
| 97 | type: Literal[StructuredLogType.SPAN_END.value] = StructuredLogType.SPAN_END.value |
| 98 | status: SpanStatus |
| 99 | |
| 100 | |
| 101 | StructuredLogPayload = Annotated[ |
| 102 | Union[ |
| 103 | SpanStartPayload, |
| 104 | SpanEndPayload, |
| 105 | ], |
| 106 | Field(discriminator="type"), |
| 107 | ] |
| 108 | |
| 109 | |
| 110 | @json_schema_type |
| 111 | class StructuredLogEvent(EventCommon): |
| 112 | type: Literal[EventType.STRUCTURED_LOG.value] = EventType.STRUCTURED_LOG.value |
| 113 | payload: StructuredLogPayload |
| 114 | |
| 115 | |
| 116 | Event = Annotated[ |
| 117 | Union[ |
| 118 | UnstructuredLogEvent, |
| 119 | MetricEvent, |
| 120 | StructuredLogEvent, |
| 121 | ], |
| 122 | Field(discriminator="type"), |
| 123 | ] |
| 124 | |
| 125 | |
| 126 | class Telemetry(Protocol): |
| 127 | @webmethod(route="/telemetry/log_event") |
| 128 | async def log_event(self, event: Event) -> None: ... |
| 129 | |
| 130 | @webmethod(route="/telemetry/get_trace", method="GET") |
| 131 | async def get_trace(self, trace_id: str) -> Trace: ... |