-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathspan.py
More file actions
253 lines (212 loc) · 6.74 KB
/
span.py
File metadata and controls
253 lines (212 loc) · 6.74 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
# SPDX-License-Identifier: MIT
import types
from abc import ABC, abstractmethod
from typing import Dict, Any
from datetime import datetime
from cozeloop.entities.prompt import Prompt
from cozeloop.spec.tracespec import Runtime
class SpanContext(ABC):
"""
Interface for Span Context operations.
"""
@property
@abstractmethod
def span_id(self) -> str:
"""
Get the Span ID.
"""
@property
@abstractmethod
def trace_id(self) -> str:
"""
Get the Trace ID.
"""
@property
@abstractmethod
def baggage(self) -> Dict[str, str]:
"""
Get the baggage as a dictionary of key-value pairs.
"""
class CommonSpanSetter(ABC):
"""
Interface for setting system-defined fields in a span.
"""
@abstractmethod
def set_input(self, input: Any) -> None:
"""
Set input information, serialized into a JSON string.
Key: `input`.
The recommended standard format is ModelInput of spec package, but custom fields can also be used.
"""
@abstractmethod
def set_output(self, output: Any) -> None:
"""
Set output information, serialized into a JSON string.
Key: `output`.
The recommended standard format is ModelOutput of spec package, but custom fields can also be used.
"""
@abstractmethod
def set_error(self, err: Exception) -> None:
"""
Set error message.
Key: `error`.
"""
@abstractmethod
def set_status_code(self, code: int) -> None:
"""
Set status code. A non-zero code is considered an exception.
Key: `_status_code`.
"""
@abstractmethod
def set_user_id(self, user_id: str) -> None:
"""
Set user ID.
Key: `user_id`.
"""
@abstractmethod
def set_user_id_baggage(self, user_id: str) -> None:
"""
Set user ID as baggage.
"""
@abstractmethod
def set_message_id(self, message_id: str) -> None:
"""
Set message ID.
Key: `message_id`.
"""
@abstractmethod
def set_message_id_baggage(self, message_id: str) -> None:
"""
Set message ID as baggage.
"""
@abstractmethod
def set_thread_id(self, thread_id: str) -> None:
"""
Set thread ID for correlating multiple requests.
Key: `thread_id`.
"""
@abstractmethod
def set_thread_id_baggage(self, thread_id: str) -> None:
"""
Set thread ID as baggage.
"""
@abstractmethod
def set_prompt(self, prompt: Prompt) -> None:
"""
Set the PromptKey and PromptVersion tags associated with the prompt.
Key: `prompt`.
"""
@abstractmethod
def set_model_provider(self, model_provider: str) -> None:
"""
Set the LLM provider, such as OpenAI.
Key: `model_provider`.
"""
@abstractmethod
def set_model_name(self, model_name: str) -> None:
"""
Set the name of the LLM model, such as gpt-4-1106-preview.
Key: `model_name`.
"""
@abstractmethod
def set_model_call_options(self, model_call_options: Any) -> None:
"""
Set the model call options
Key: `call_options`.
"""
@abstractmethod
def set_input_tokens(self, input_tokens: int) -> None:
"""
Set the usage of input tokens.
Key: `input_tokens`.
It will be automatically summed with output_tokens to calculate the tokens tag.
"""
@abstractmethod
def set_output_tokens(self, output_tokens: int) -> None:
"""
Set the usage of output tokens.
Key: `output_tokens`.
It will be automatically summed with input_tokens to calculate the tokens tag.
"""
@abstractmethod
def set_start_time_first_resp(self, start_time_first_resp: int) -> None:
"""
Set the timestamp of the first packet return from LLM in microseconds.
Key: `start_time_first_resp`.
"""
@abstractmethod
def set_runtime(self, runtime: Runtime) -> None:
"""
Set the runtime of the span. Only used for integration.
Key: `runtime`.
"""
@abstractmethod
def set_service_name(self, service_name: str) -> None:
"""
set the custom service name, identify different services.
"""
@abstractmethod
def set_log_id(self, log_id: str) -> None:
"""
Set the custom log id, identify different query.
"""
@abstractmethod
def set_system_tags(self, system_tags: Dict[str, Any]) -> None:
"""
Set system tags. DO NOT use this method unless you know what you are doing.
"""
@abstractmethod
def set_deployment_env(self, deployment_env: str) -> None:
"""
Set the deployment environment of the span, identify custom environments.
"""
@abstractmethod
def set_finish_time(self, finish_time: datetime) -> None:
"""
Set the finish time of the span. DO NOT use this method unless you know what you are doing.
"""
class Span(CommonSpanSetter, SpanContext):
"""
Interface for Span operations with tracing and tagging capabilities.
"""
@abstractmethod
def set_tags(self, tag_kvs: Dict[str, Any]) -> None:
"""
Set business custom tags. Tag value should be JSON-serializable,str,int,float,bool,Sequence[str],Sequence[bool],Sequence[int],Sequence[float].
"""
@abstractmethod
def set_baggage(self, baggage_items: Dict[str, str]) -> None:
"""
Set tags and also pass these tags to other downstream spans (assuming the user uses
`to_header` and `from_header` to handle header passing between services).
Value should be JSON-serializable,str,int,float,bool,Sequence[str],Sequence[bool],Sequence[int],Sequence[float].
"""
@abstractmethod
def finish(self) -> None:
"""
The span will be reported only after an explicit call to Finish.
Under the hood, it is actually placed in an asynchronous queue waiting to be reported.
"""
@abstractmethod
def discard(self) -> None:
"""
The span will be discarded, not be reported.
"""
@property
@abstractmethod
def start_time(self) -> datetime:
"""
Returns the start time as a timestamp of the Span.
"""
@abstractmethod
def to_header(self) -> Dict[str, str]:
"""
Convert the span to headers. Used for cross-process correlation.
"""
@abstractmethod
def __enter__(self):
return self
@abstractmethod
def __exit__(self, exc, value, tb):
self.finish()