telemetry-dev 0.3.7__tar.gz → 0.3.9__tar.gz

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.
Files changed (26) hide show
  1. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/PKG-INFO +9 -4
  2. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/README.md +8 -3
  3. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/pyproject.toml +1 -1
  4. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/pyproject.toml.orig +1 -1
  5. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_semconv.py +3 -2
  6. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_spans.py +175 -3
  7. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/LICENSE +0 -0
  8. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/__init__.py +0 -0
  9. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_api.py +0 -0
  10. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_capture.py +0 -0
  11. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_ci.py +0 -0
  12. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_client.py +0 -0
  13. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_config.py +0 -0
  14. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_context.py +0 -0
  15. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_experiments.py +0 -0
  16. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_export.py +0 -0
  17. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_logs.py +0 -0
  18. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_metrics.py +0 -0
  19. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_observe.py +0 -0
  20. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_process_lock.py +0 -0
  21. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_processor.py +0 -0
  22. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_prompts.py +0 -0
  23. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_scores.py +0 -0
  24. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/_serialize.py +0 -0
  25. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/otel.py +0 -0
  26. {telemetry_dev-0.3.7 → telemetry_dev-0.3.9}/src/telemetry_dev/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: telemetry-dev
3
- Version: 0.3.7
3
+ Version: 0.3.9
4
4
  Summary: telemetry.dev SDK for Python — OpenTelemetry-native GenAI tracing, logs, and metrics
5
5
  Keywords: telemetry,opentelemetry,llm,genai,tracing,observability
6
6
  Author: telemetry.dev
@@ -209,9 +209,14 @@ single record larger than the bound is still queued when its queue is empty.
209
209
 
210
210
  `capture_input=False` and `capture_output=False` omit captured content. Provider integrations may
211
211
  still record non-content metadata such as response IDs and models, finish reasons, usage, timing,
212
- and error types. Capture flags do not gate stop sequences, tool descriptions and definitions
213
- (`gen_ai.tool.description`, `gen_ai.tool.definitions`), caller-supplied metadata or raw attributes;
214
- redact those separately when needed.
212
+ and error types. Capture flags do not gate stop sequences, a tool span's description
213
+ (`gen_ai.tool.description`), or caller-supplied metadata or raw attributes; redact those separately
214
+ when needed. The one exception is `gen_ai.tool.definitions`, the tools offered to a model: with
215
+ input capture off it keeps only each tool's type and name, and with it on it passes through the
216
+ mask. Tool descriptions and input schemas are prompt content. Like other content, a list longer
217
+ than `max_attribute_length`, 0 included, keeps only the types and names, so that it stays JSON. The
218
+ names follow the limit of raw attributes, where 0 sets no limit, and are left out when they do
219
+ not fit.
215
220
 
216
221
  `mask` receives input and output content, system instructions, log messages, captured exception
217
222
  messages and stack traces, and content that
@@ -182,9 +182,14 @@ single record larger than the bound is still queued when its queue is empty.
182
182
 
183
183
  `capture_input=False` and `capture_output=False` omit captured content. Provider integrations may
184
184
  still record non-content metadata such as response IDs and models, finish reasons, usage, timing,
185
- and error types. Capture flags do not gate stop sequences, tool descriptions and definitions
186
- (`gen_ai.tool.description`, `gen_ai.tool.definitions`), caller-supplied metadata or raw attributes;
187
- redact those separately when needed.
185
+ and error types. Capture flags do not gate stop sequences, a tool span's description
186
+ (`gen_ai.tool.description`), or caller-supplied metadata or raw attributes; redact those separately
187
+ when needed. The one exception is `gen_ai.tool.definitions`, the tools offered to a model: with
188
+ input capture off it keeps only each tool's type and name, and with it on it passes through the
189
+ mask. Tool descriptions and input schemas are prompt content. Like other content, a list longer
190
+ than `max_attribute_length`, 0 included, keeps only the types and names, so that it stays JSON. The
191
+ names follow the limit of raw attributes, where 0 sets no limit, and are left out when they do
192
+ not fit.
188
193
 
189
194
  `mask` receives input and output content, system instructions, log messages, captured exception
190
195
  messages and stack traces, and content that
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "telemetry-dev"
3
- version = "0.3.7"
3
+ version = "0.3.9"
4
4
  description = "telemetry.dev SDK for Python — OpenTelemetry-native GenAI tracing, logs, and metrics"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "telemetry-dev"
3
- version = "0.3.7"
3
+ version = "0.3.9"
4
4
  description = "telemetry.dev SDK for Python — OpenTelemetry-native GenAI tracing, logs, and metrics"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -7,8 +7,8 @@ SCOPE_NAME = "telemetry_dev"
7
7
  SpanType = Literal["span", "generation", "tool", "agent", "embedding"]
8
8
  LogLevel = Literal["debug", "info", "warn", "warning", "error"]
9
9
 
10
- # Plain spans ALWAYS map to "function" — an operation-less span carrying a propagated
11
- # gen_ai.conversation.id would otherwise be classified as "llm" by the ingest heuristic.
10
+ # Plain spans ALWAYS map to "function": without one, the ingest guesses "llm" from other gen_ai.*
11
+ # keys on the span, such as a gen_ai.agent.name the caller set.
12
12
  SPAN_TYPE_TO_OPERATION: dict[str, str] = {
13
13
  "span": "function",
14
14
  "generation": "chat",
@@ -71,6 +71,7 @@ ATTR_OUTPUT_MESSAGES = "gen_ai.output.messages"
71
71
  ATTR_TOOL_NAME = "gen_ai.tool.name"
72
72
  ATTR_TOOL_CALL_ID = "gen_ai.tool.call.id"
73
73
  ATTR_TOOL_DESCRIPTION = "gen_ai.tool.description"
74
+ ATTR_TOOL_DEFINITIONS = "gen_ai.tool.definitions"
74
75
  ATTR_TOOL_ARGUMENTS = "gen_ai.tool.call.arguments"
75
76
  ATTR_TOOL_RESULT = "gen_ai.tool.call.result"
76
77
  ATTR_AGENT_NAME = "gen_ai.agent.name"
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import json
3
4
  import math
4
5
  import sys
5
6
  import threading
@@ -10,7 +11,7 @@ from collections.abc import Callable, Mapping, Sequence
10
11
  from contextvars import Token
11
12
  from dataclasses import dataclass
12
13
  from datetime import datetime
13
- from typing import Any, Literal, TypedDict
14
+ from typing import Any, Literal, TypedDict, cast
14
15
  from weakref import WeakKeyDictionary, WeakValueDictionary
15
16
 
16
17
  from opentelemetry import context as otel_context
@@ -48,6 +49,7 @@ from ._semconv import (
48
49
  ATTR_SYSTEM_INSTRUCTIONS,
49
50
  ATTR_TIME_TO_FIRST_CHUNK,
50
51
  ATTR_TOOL_CALL_ID,
52
+ ATTR_TOOL_DEFINITIONS,
51
53
  ATTR_TOOL_DESCRIPTION,
52
54
  ATTR_TOOL_NAME,
53
55
  DURATION_BUCKETS,
@@ -61,7 +63,13 @@ from ._semconv import (
61
63
  input_key,
62
64
  output_key,
63
65
  )
64
- from ._serialize import AttributeValue, coerce_attr_value
66
+ from ._serialize import (
67
+ AttributeValue,
68
+ coerce_attr_value,
69
+ json_dumps,
70
+ serialize_content,
71
+ utf16_length,
72
+ )
65
73
 
66
74
 
67
75
  class _NotGiven:
@@ -151,10 +159,102 @@ def _to_ns(value: TimeInput) -> int | None:
151
159
  return int(value)
152
160
 
153
161
 
162
+ # Error classes that provider SDKs raise for any failed request, whose name says nothing about the
163
+ # cause: openai's and anthropic's APIStatusError (anthropic's for a stream's error event), and
164
+ # google-genai's APIError, ClientError, and ServerError. botocore's ClientError, and its
165
+ # EventStreamError for a Bedrock stream's exception, carry their code in `response`. Plain
166
+ # Exception is here for errors that carry a code, such as the failures that the integrations build
167
+ # from a response body with `code` and `type` attributes. The TypeScript SDK keeps the same rule in
168
+ # `errorTypeOf` (packages/otel/src/errors.ts); change both together.
169
+ _GENERIC_ERROR_NAMES = frozenset(
170
+ {"Exception", "APIError", "APIStatusError", "ClientError", "ServerError", "EventStreamError"}
171
+ )
172
+
173
+
174
+ def _text(value: object) -> str | None:
175
+ return value if isinstance(value, str) and value else None
176
+
177
+
178
+ def _error_code(value: object) -> str | None:
179
+ # Some providers send numeric codes, such as Z.ai's "1302" for a rate limit, and openai's SDK
180
+ # turns OpenRouter's numeric code into a string. A number names no cause the way a class does.
181
+ code = _text(value)
182
+ return None if code is None or code.isdigit() else code
183
+
184
+
185
+ def _http_status(value: object) -> int | None:
186
+ return (
187
+ value
188
+ if isinstance(value, int) and not isinstance(value, bool) and 400 <= value <= 599
189
+ else None
190
+ )
191
+
192
+
193
+ def _nested(value: object, *keys: str) -> object:
194
+ for key in keys:
195
+ if not isinstance(value, Mapping):
196
+ return None
197
+ value = cast("Mapping[str, object]", value).get(key)
198
+ return value
199
+
200
+
201
+ def _attribute(error: BaseException, name: str) -> object:
202
+ # A property of a third-party exception can raise anything; recording the error must not.
203
+ try:
204
+ return getattr(error, name, None)
205
+ except Exception:
206
+ return None
207
+
208
+
209
+ def _error_type(error: BaseException) -> str:
210
+ """The error.type of a failure: the provider's error code when the error carries one.
211
+
212
+ - An error with an HTTP status and a code that is not only digits (openai's
213
+ `insufficient_quota`, `context_length_exceeded`) gives the code.
214
+ - A generic provider error class gives its code, else its HTTP status (`429`), else the
215
+ provider's error type (anthropic's `overloaded_error` from a stream).
216
+ - Any other error gives its class name.
217
+ """
218
+ name = type(error).__name__
219
+ body = _attribute(error, "body")
220
+ code = _error_code(_attribute(error, "code")) or _error_code(_nested(body, "error", "code"))
221
+ status = next(
222
+ (
223
+ status
224
+ for status in (
225
+ _http_status(_attribute(error, attribute))
226
+ for attribute in ("status_code", "code", "status")
227
+ )
228
+ if status is not None
229
+ ),
230
+ None,
231
+ )
232
+ if code is not None and status is not None:
233
+ return code
234
+ if name not in _GENERIC_ERROR_NAMES:
235
+ return name
236
+ if code is not None:
237
+ return code
238
+ if status is not None:
239
+ return str(status)
240
+ aws_code = _text(_nested(_attribute(error, "response"), "Error", "Code"))
241
+ if aws_code is not None:
242
+ # botocore names a Bedrock stream's exception by its event, such as
243
+ # modelStreamErrorException; the TypeScript SDK records its class,
244
+ # ModelStreamErrorException.
245
+ aws_code = aws_code[0].upper() + aws_code[1:]
246
+ return (
247
+ _text(_attribute(error, "type"))
248
+ or _text(_nested(body, "error", "type"))
249
+ or aws_code
250
+ or name
251
+ )
252
+
253
+
154
254
  def _record_error_on_span(
155
255
  client: Client, span: Span, state: _SpanState, error: BaseException
156
256
  ) -> None:
157
- error_type = type(error).__name__
257
+ error_type = _error_type(error)
158
258
  span.set_attribute(ATTR_ERROR_TYPE, error_type)
159
259
  span.set_status(Status(StatusCode.ERROR))
160
260
  attributes: dict[str, otel_types.AttributeValue] = {
@@ -285,6 +385,15 @@ def _fields_to_attributes(
285
385
  # The variables are input, also when the app passes them from prompt_attributes_for().
286
386
  if key == ATTR_PROMPT_VARIABLES and not state.capture_input:
287
387
  continue
388
+ if key == ATTR_TOOL_DEFINITIONS:
389
+ definitions = _tool_list(value)
390
+ if definitions is not None:
391
+ kept = _tool_definitions(client, state, value, definitions)
392
+ if kept is not None:
393
+ attrs[key] = kept
394
+ continue
395
+ if not state.capture_input or client.mask is not None:
396
+ continue
288
397
  attr = coerce_attr_value(
289
398
  value,
290
399
  max_len=sys.maxsize,
@@ -297,6 +406,69 @@ def _fields_to_attributes(
297
406
  return attrs
298
407
 
299
408
 
409
+ def _tool_list(value: Any) -> list[Any] | None:
410
+ """The tools of a ``gen_ai.tool.definitions`` value: JSON text, a list of one JSON text per
411
+ tool, or a list of tool objects. None for any other value."""
412
+ try:
413
+ if isinstance(value, str):
414
+ parsed = json.loads(value)
415
+ return cast("list[Any]", parsed) if isinstance(parsed, list) else None
416
+ if isinstance(value, (list, tuple)):
417
+ items = cast("Sequence[Any]", value)
418
+ if all(isinstance(item, str) for item in items):
419
+ return [json.loads(item) for item in items]
420
+ return list(items)
421
+ except (ValueError, RecursionError):
422
+ pass
423
+ return None
424
+
425
+
426
+ def _tool_definitions(
427
+ client: Client, state: _SpanState, value: Any, definitions: list[Any]
428
+ ) -> str | None:
429
+ """The ``gen_ai.tool.definitions`` that a span keeps. Tool descriptions and schemas are prompt
430
+ content: with input capture off only each tool's type and name stay, and with a mask the
431
+ definitions go through it as input does. Like other content, a list longer than
432
+ ``max_attribute_length``, 0 included, keeps only the names, since a cut list is not JSON and
433
+ readers would lose every tool. The names follow the limit of raw attributes, where 0 sets no
434
+ limit; names that do not fit leave the attribute out."""
435
+ if state.capture_input:
436
+ if client.mask is None:
437
+ try:
438
+ full = value if isinstance(value, str) else json_dumps(definitions)
439
+ except (TypeError, ValueError, RecursionError):
440
+ full = None
441
+ else:
442
+ full = serialize_content(
443
+ definitions,
444
+ key=ATTR_TOOL_DEFINITIONS,
445
+ mask=client.mask,
446
+ max_len=sys.maxsize,
447
+ on_error=client.on_error,
448
+ )
449
+ if full is not None and utf16_length(full) <= client.max_attribute_length:
450
+ return full
451
+ names: list[dict[str, str]] = []
452
+ for definition in definitions:
453
+ if not isinstance(definition, dict):
454
+ continue
455
+ fields = cast("dict[str, Any]", definition)
456
+ kept = {key: fields[key] for key in ("type", "name") if isinstance(fields.get(key), str)}
457
+ # OpenAI's Chat Completions form nests the name under ``function``.
458
+ function = fields.get("function")
459
+ if "name" not in kept and isinstance(function, dict):
460
+ nested = cast("dict[str, Any]", function).get("name")
461
+ if isinstance(nested, str):
462
+ kept["name"] = nested
463
+ if kept:
464
+ names.append(kept)
465
+ if not names:
466
+ return None
467
+ text = json_dumps(names)
468
+ raw_limit = client.max_attribute_length or sys.maxsize
469
+ return text if utf16_length(text) <= raw_limit else None
470
+
471
+
300
472
  def _apply_fields(client: Client, span: Span, state: _SpanState, fields: dict[str, Any]) -> None:
301
473
  name = fields.get("name")
302
474
  if name is not None:
File without changes