telemetry-dev 0.3.2__tar.gz → 0.3.3__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.
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/PKG-INFO +82 -26
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/README.md +81 -25
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/pyproject.toml +1 -1
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/pyproject.toml.orig +1 -1
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/__init__.py +2 -0
- telemetry_dev-0.3.3/src/telemetry_dev/_capture.py +390 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_client.py +88 -19
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_experiments.py +33 -10
- telemetry_dev-0.3.3/src/telemetry_dev/_export.py +641 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_metrics.py +28 -26
- telemetry_dev-0.3.3/src/telemetry_dev/_process_lock.py +41 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_processor.py +26 -2
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_prompts.py +86 -33
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_serialize.py +75 -1
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_spans.py +5 -4
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/otel.py +50 -15
- telemetry_dev-0.3.2/src/telemetry_dev/_capture.py +0 -189
- telemetry_dev-0.3.2/src/telemetry_dev/_export.py +0 -100
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/LICENSE +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_api.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_ci.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_config.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_context.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_logs.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_observe.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_scores.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/src/telemetry_dev/_semconv.py +0 -0
- {telemetry_dev-0.3.2 → telemetry_dev-0.3.3}/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.
|
|
3
|
+
Version: 0.3.3
|
|
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
|
|
@@ -128,10 +128,6 @@ metric readers, so supplying `metric_reader=` disables this histogram. Duration,
|
|
|
128
128
|
time-to-first-chunk metrics remain available; the first accepted streamed span with two or more
|
|
129
129
|
recorded chunks reports this limitation once through `on_error` and the SDK logger.
|
|
130
130
|
|
|
131
|
-
Provider integrations feature-detect `record_output_chunk`. Older core SDKs without that method
|
|
132
|
-
continue delivering streams and tracing normally, but do not emit chunk-interval metrics. Upgrade
|
|
133
|
-
the core SDK with provider integrations to enable the new metric.
|
|
134
|
-
|
|
135
131
|
### Span types
|
|
136
132
|
|
|
137
133
|
`type=` maps to `gen_ai.operation.name`:
|
|
@@ -183,14 +179,27 @@ metadata, identifiers, resource attributes, or events written directly through r
|
|
|
183
179
|
| `log_level` | `"warn"` | SDK diagnostics level: `debug`/`info`/`warn`/`error`/`silent`. |
|
|
184
180
|
| `capture_input`, `capture_output` | `True` | Global content-capture defaults. |
|
|
185
181
|
| `mask` | `None` | `Callable[[Any, MaskContext], Any]` redaction hook, runs before JSON serialization on input/output/log messages and `SpanHandle.serialize_content` (`MaskContext.key` is the attribute being written). Not applied to correlation identifiers. |
|
|
186
|
-
| `max_attribute_length` | `
|
|
182
|
+
| `max_attribute_length` | `None` | Per-content-attribute cap in UTF-16 units, the marker included: `None` (no cap, each value is sent whole) or a non-negative integer. Invalid values call `on_error`, do not raise into application code, and install a disabled client; truncated values use the ASCII `...[truncated]` marker, or its prefix when the cap is shorter than the marker. The cap also applies, with the marker, to string metadata and log attributes; any other string attribute is cut at the cap without a marker, except that a cap of `0` leaves those whole. |
|
|
183
|
+
| `max_stream_bytes` | `67108864` (64 MiB) | Maximum output, across the process, that provider integrations keep for streams that have not ended, in UTF-8 bytes of JSON. Each stream keeps at most `MAX_RECORD_BYTES` (16 MiB); while no other stream holds bytes in it, not even the few bytes a started stream holds before any output, a stream may grow to 16 MiB even past this bound. Output past either bound is not kept, and its span gets `telemetry.dev.capture.truncated`. `None` selects the default; other values that are not non-negative integers or `math.inf` call `on_error` and install a disabled client. The bound counts UTF-8 bytes, not Python memory, so each streamed text buffer holds more than it counts: up to 255 of its latest deltas as separate strings, at most about 22 KB, and either about 1.5% of its text on Python 3.10 and 3.11 or, on 3.12 and later, the room `StringIO` reserves to grow, up to about 25% of its text (50% on Windows). Text with a character outside Latin-1, such as an emoji, is stored at up to 4 bytes per character, so its buffer can hold up to about 4 times the bytes counted. |
|
|
187
184
|
| `span_filter` | `None` | Export predicate `Callable[[ReadableSpan], bool]`. |
|
|
188
185
|
| `sampler` | OTel environment configuration | OTel `Sampler` that overrides environment settings. Session roots use its root policy. Real parents keep their sampling decisions. |
|
|
189
|
-
| `on_error` | `None` | Receives internal SDK errors; repeated instrumentation failures from one stream may be coalesced into one report. The SDK never raises them into application code. A series of failed exports, for example during an ingest outage, is one report to `on_error` and the `telemetry_dev` logger. After an export reaches the ingest, the next failure is a new report. |
|
|
186
|
+
| `on_error` | `None` | Receives internal SDK errors; repeated instrumentation failures from one stream may be coalesced into one report. The SDK never raises them into application code. A series of failed exports, for example during an ingest outage, is one report to `on_error` and the `telemetry_dev` logger. After an export reaches the ingest, the next failure is a new report. A `flush()` or `shutdown()` that `on_error` calls during an export returns at once, before it sends anything, and runs on a new thread, because a batch processor or the metric reader holds a lock during its export that the call needs; the thread waits for that export to end. With `export_mode="immediate"` the span and log exports hold no lock, so the call starts at once, but it still returns before it sends anything: in a serverless handler, call `flush()` after the work, outside `on_error`. The process does not wait for a deferred flush before it exits; the shutdown at exit sends what is left, unless `disable_atexit=True`, where the application must call `shutdown()`. After a deferred `shutdown()`, the client is already gone, and the process waits for the shutdown before it exits; if its thread cannot start, it runs when the process exits, unless the process is already exiting. |
|
|
190
187
|
| `disable_atexit` | `False` | Skip the automatic atexit shutdown. |
|
|
191
|
-
| `timeout` | `10.0` | OTLP HTTP
|
|
188
|
+
| `timeout` | `10.0` | Timeout of each OTLP HTTP export request in seconds. No wait before a retry starts after it, but a request whose connection fails is sent again at once with the same limit, so an ingest that does not answer can take up to twice this time. The limit applies to each wait for the server, so a response that keeps arriving can also take longer. A batch split into several requests gives each request this timeout. |
|
|
192
189
|
| `span_exporter`, `log_exporter`, `metric_reader` | `None` | Test seams / offline mode; any of them enables the client without an API key. |
|
|
193
190
|
|
|
191
|
+
Each export request is at most 16 MiB, the ingest's limit, so a larger batch is sent in several
|
|
192
|
+
requests. A span or log record that is larger than 16 MiB by itself is still sent: its longest
|
|
193
|
+
string values (attributes, or a log record's message) are cut to fit and end with `...[truncated]`,
|
|
194
|
+
the record gets `telemetry.dev.capture.truncated = true`, and `on_error` receives a report. A record
|
|
195
|
+
that still does not fit, because its events or array attributes hold the excess, is dropped and
|
|
196
|
+
reported. `create_telemetry_span_exporter` and `TelemetrySpanProcessor` follow the same rules.
|
|
197
|
+
|
|
198
|
+
In `"batched"` mode, the spans and the log records that wait for export are each bounded to 2048
|
|
199
|
+
records (for logs, `OTEL_BLRP_MAX_QUEUE_SIZE` when it is set) and 64 MiB of string values, each character counted as one byte. A record that arrives
|
|
200
|
+
when its queue is full is dropped, and the first drop of a series is reported to `on_error`. A
|
|
201
|
+
single record larger than the bound is still queued when its queue is empty.
|
|
202
|
+
|
|
194
203
|
`capture_input=False` and `capture_output=False` omit captured content. Provider integrations may
|
|
195
204
|
still record non-content metadata such as response IDs and models, finish reasons, usage, timing,
|
|
196
205
|
and error types. Capture flags do not gate stop sequences, tool descriptions and definitions
|
|
@@ -246,13 +255,13 @@ pinned = telemetry_dev.get_prompt("support/reply", version=13)
|
|
|
246
255
|
call returns it at once and fetches a new copy in a background thread. Only a call with no copy
|
|
247
256
|
waits for a fetch, for 10 seconds at most, also when the network stops. Calls for the same
|
|
248
257
|
prompt share one fetch. `get_prompt_async` runs the same function in a worker thread.
|
|
249
|
-
- A process
|
|
250
|
-
`multiprocessing` with the `fork` start method
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
258
|
+
- A forked process keeps the copies of its parent: a process that `os.fork()` makes, one that
|
|
259
|
+
`multiprocessing` makes with the `fork` start method, or one that C code forks without running
|
|
260
|
+
fork hooks. A call that starts in the child does not wait for a fetch of the parent, or for a
|
|
261
|
+
lock that a thread of the parent held. If the call must fetch, it starts a new fetch.
|
|
262
|
+
- If a signal handler forks while a call waits for a fetch, that call continues in the child,
|
|
263
|
+
where no thread completes that fetch. Thus the call looks at the cache again, and starts a new
|
|
264
|
+
fetch if it must, which it waits for up to 10 seconds more.
|
|
256
265
|
- If a fetch fails, `get_prompt` returns the last copy that it fetched and reports the error
|
|
257
266
|
through `on_error`. With no copy, it returns the `fallback` (`version` is `None` and
|
|
258
267
|
`is_fallback` is `True`). With no fallback, it raises `PromptError` whose `code` tells why, for
|
|
@@ -276,7 +285,10 @@ pinned = telemetry_dev.get_prompt("support/reply", version=13)
|
|
|
276
285
|
`td.prompt.name`, `td.prompt.version`, and `td.prompt.label` (only for a prompt fetched by label).
|
|
277
286
|
When input capture is on, `td.prompt.variables` holds the JSON of the variables of the last
|
|
278
287
|
`compile()` of that prompt object before `with_prompt` starts, or of the last `compile()` inside
|
|
279
|
-
it, after `mask
|
|
288
|
+
it, after `mask`. Past `max_attribute_length` the longest values are cut one by one, each to a text
|
|
289
|
+
that ends with `...[truncated]`, when the keys and a marker for each cut value fit; the JSON then
|
|
290
|
+
keeps every variable. Otherwise the whole attribute is cut, so it can lose variables and stop being
|
|
291
|
+
valid JSON. If threads or tasks that run at the same time share one prompt
|
|
280
292
|
object, call `compile()` in `with_prompt`, or call `get_prompt` in each thread or task.
|
|
281
293
|
`get_prompt` returns a new object from the cache. If you do not, a thread can record the variables
|
|
282
294
|
of a different thread. Inside one `with_prompt`, an asyncio task records the last `compile()` that
|
|
@@ -384,9 +396,12 @@ result = telemetry_dev.run_experiment(
|
|
|
384
396
|
- `run_experiment` sends the outputs in batches of 100 items at most, and then waits up to 30
|
|
385
397
|
minutes for the server evaluators. Then it prints the scores and returns an `ExperimentResult`:
|
|
386
398
|
`status`, `check_status`, item counts, `scores` with the value, the baseline value, the change,
|
|
387
|
-
|
|
399
|
+
the pass mark, and the failed runs (`items_failed`) of each score, and the `url` of the
|
|
400
|
+
experiment page.
|
|
388
401
|
- `min_scores` sets a pass mark for each score name: the lowest pass rate (0 to 1) of a pass/fail
|
|
389
|
-
score, or the lowest mean of a number score. A score under its mark fails the check.
|
|
402
|
+
score, or the lowest mean of a number score. A score under its mark fails the check. A run whose
|
|
403
|
+
task raises has no score, so the value of a score leaves it out, and with pass marks a failed
|
|
404
|
+
run fails the check.
|
|
390
405
|
- A failed request raises `ExperimentError` whose `code` tells why, for example
|
|
391
406
|
`dataset_not_found`, `dataset_empty`, `evaluator_not_found`, `insufficient_scope`,
|
|
392
407
|
`missing_api_key`, or `timeout`. A read of the dataset or the result, a post of outputs, and the
|
|
@@ -396,7 +411,8 @@ result = telemetry_dev.run_experiment(
|
|
|
396
411
|
made the experiment, also after Ctrl-C or a cancellation, `run_experiment` sends the completion
|
|
397
412
|
before it raises: telemetry.dev then ends the experiment with the item runs that it got, or fails
|
|
398
413
|
it when it got none.
|
|
399
|
-
- The key needs the `datasets:read` and
|
|
414
|
+
- The key needs Run experiments in its Access on the API keys page (the `datasets:read` and
|
|
415
|
+
`experiments:write` scopes). `run_experiment` uses the same
|
|
400
416
|
API key and base URL as `get_prompt`, or its own `api_key` and `base_url`. With
|
|
401
417
|
`environment="experiment"`, the key of the telemetry client must be for all environments or for
|
|
402
418
|
`experiment`: a key for another environment cannot send the traces of the items, and the items
|
|
@@ -419,7 +435,9 @@ The [experiment action](../../actions/experiment) runs your eval script in a wor
|
|
|
419
435
|
the result on the pull request. It gives `run_experiment` the `dataset` input in
|
|
420
436
|
`TELEMETRY_DEV_EXPERIMENT_DATASET`, which must agree with the `dataset` of the script, and the
|
|
421
437
|
`min-scores` input in `TELEMETRY_DEV_EXPERIMENT_MIN_SCORES`, which wins over `min_scores` for the
|
|
422
|
-
same name.
|
|
438
|
+
same name. Use the action as `telemetry-dev/telemetry/actions/experiment@experiment-action-v1`, a
|
|
439
|
+
tag that moves to each 1.x release of the action. In the action, a failed experiment with `ci=False`
|
|
440
|
+
shows its result but does not fail the job.
|
|
423
441
|
|
|
424
442
|
## Scores
|
|
425
443
|
|
|
@@ -429,13 +447,16 @@ user on a reply:
|
|
|
429
447
|
```python
|
|
430
448
|
import telemetry_dev
|
|
431
449
|
|
|
432
|
-
# When the app replies, keep the trace id of the reply with the message:
|
|
450
|
+
# When the app replies, keep the trace id and the span id of the reply with the message:
|
|
433
451
|
with telemetry_dev.start_span("reply") as span:
|
|
434
452
|
text = reply_to_customer(message)
|
|
435
|
-
|
|
453
|
+
context = span.span.get_span_context()
|
|
454
|
+
trace_id, span_id = f"{context.trace_id:032x}", f"{context.span_id:016x}"
|
|
436
455
|
|
|
437
456
|
# Later, when the user clicks thumbs down:
|
|
438
|
-
telemetry_dev.score(
|
|
457
|
+
telemetry_dev.score(
|
|
458
|
+
name="user_feedback", trace_id=trace_id, span_id=span_id, value=0, comment="Wrong amount"
|
|
459
|
+
)
|
|
439
460
|
```
|
|
440
461
|
|
|
441
462
|
- Give `trace_id`, `session_id`, or the two, and a `span_id` of the trace for a score on one span.
|
|
@@ -445,8 +466,16 @@ telemetry_dev.score(name="user_feedback", trace_id=trace_id, value=0, comment="W
|
|
|
445
466
|
after it sends a `ScoreError` to `on_error`. It checks the score before it sends it
|
|
446
467
|
(`invalid_score`). The trace id of a span from a client that is not enabled is all zeros, and
|
|
447
468
|
`score` reports it as `invalid_score`.
|
|
469
|
+
- Give the `span_id` of the reply for feedback on one reply. The SDK puts all the turns of a session
|
|
470
|
+
in one trace, so feedback with only a `trace_id` counts for the turn that started last before the
|
|
471
|
+
feedback came, which can be a later turn than the reply.
|
|
472
|
+
- The rates of the dashboard count one `user_feedback` vote for each span, turn, or session: a
|
|
473
|
+
later `user_feedback` score on the same span (on the same turn, for feedback with only a trace,
|
|
474
|
+
or on the same session, for feedback with no trace) replaces the earlier one. The SDK sends
|
|
475
|
+
feedback with no author, so all the feedback that the SDK sends on one span or one turn counts as
|
|
476
|
+
one vote: the newest.
|
|
448
477
|
- `score` makes one request, with no retry. `score_async` runs it in a worker thread. The key
|
|
449
|
-
needs the `scores:write` scope.
|
|
478
|
+
needs Run experiments in its Access on the API keys page (the `scores:write` scope).
|
|
450
479
|
|
|
451
480
|
## Auto-metrics
|
|
452
481
|
|
|
@@ -474,6 +503,27 @@ telemetry_dev.init(export_mode="immediate")
|
|
|
474
503
|
telemetry_dev.flush() # force-flush traces + logs + metrics
|
|
475
504
|
```
|
|
476
505
|
|
|
506
|
+
## Forking servers
|
|
507
|
+
|
|
508
|
+
A server that forks its workers from the process that called `init()`, as gunicorn,
|
|
509
|
+
`multiprocessing` with the `fork` start method, or `os.fork()`, needs no setup. Python runs the
|
|
510
|
+
fork hooks of the SDK and of OpenTelemetry in each worker, and the SDK does not wait in a worker
|
|
511
|
+
for a lock that a thread of the parent held at the fork.
|
|
512
|
+
|
|
513
|
+
uWSGI loads the app before it forks its workers, and by default it runs no Python fork hooks in
|
|
514
|
+
them. Traces and logs still go out, but the OpenTelemetry metric reader starts its thread again
|
|
515
|
+
only in a fork hook, so a worker sends metrics only when it calls `telemetry_dev.flush()`. Set one
|
|
516
|
+
of these uWSGI options:
|
|
517
|
+
|
|
518
|
+
- `lazy-apps = true`: each worker loads the app, and thus calls `init()` itself.
|
|
519
|
+
- `py-call-uwsgi-fork-hooks = true` (uWSGI 2.0.21 and later): uWSGI runs the Python fork hooks
|
|
520
|
+
around each fork. Before 2.0.27, it runs only the hooks of the child, which is enough for the
|
|
521
|
+
SDK.
|
|
522
|
+
|
|
523
|
+
Before uWSGI 2.0.27, Python threads run only with `enable-threads = true` or more than one
|
|
524
|
+
`threads`. The SDK sends telemetry from its own threads, so set one of them too, also with
|
|
525
|
+
`lazy-apps`.
|
|
526
|
+
|
|
477
527
|
## Bring your own OTel (`telemetry_dev.otel`)
|
|
478
528
|
|
|
479
529
|
If you already run an OpenTelemetry SDK, attach the telemetry.dev processor to your provider
|
|
@@ -495,6 +545,10 @@ span_exporter = create_telemetry_span_exporter()
|
|
|
495
545
|
your_tracer_provider.add_span_processor(BatchSpanProcessor(span_exporter))
|
|
496
546
|
```
|
|
497
547
|
|
|
548
|
+
`create_telemetry_span_exporter` takes `api_key`, `base_url`, and `timeout`, in seconds (default
|
|
549
|
+
`10.0`). The timeout works as the `timeout` option of `init` describes, also for each request of a
|
|
550
|
+
batch that is split.
|
|
551
|
+
|
|
498
552
|
`TelemetrySpanProcessor` remains available from the package root for compatibility:
|
|
499
553
|
|
|
500
554
|
```python
|
|
@@ -509,12 +563,14 @@ from telemetry_dev import TelemetrySpanProcessor
|
|
|
509
563
|
| `max_export_batch_size` | `64` | BatchSpanProcessor export batch size. |
|
|
510
564
|
| `schedule_delay_millis` | `1000` | BatchSpanProcessor schedule delay. |
|
|
511
565
|
| `max_queue_size` | `2048` | BatchSpanProcessor queue size. |
|
|
512
|
-
| `
|
|
566
|
+
| `max_queue_bytes` | `67108864` (64 MiB) | Maximum size of the queued spans, counting each character of their string attribute values as one byte. A span is still queued when the queue is empty. Spans over the bound are dropped and reported to `on_error`. |
|
|
567
|
+
| `max_stream_bytes` | `67108864` (64 MiB) | Maximum output that provider integrations keep, across the process, for streams that have not ended (UTF-8 bytes of JSON). Each stream keeps at most `MAX_RECORD_BYTES` (16 MiB); while no other stream holds bytes in it, not even the few bytes a started stream holds before any output, a stream may grow to 16 MiB even past this bound. The bound is process-wide, so the span processor started last sets it. `None` selects the default; other values that are not non-negative integers or `math.inf` are reported to `on_error`, and the processor exports nothing. Output past either bound is not kept, and its span gets `telemetry.dev.capture.truncated`. The bound counts UTF-8 bytes, not Python memory, so each streamed text buffer holds more than it counts: up to 255 of its latest deltas as separate strings, at most about 22 KB, and either about 1.5% of its text on Python 3.10 and 3.11 or, on 3.12 and later, the room `StringIO` reserves to grow, up to about 25% of its text (50% on Windows). Text with a character outside Latin-1, such as an emoji, is stored at up to 4 bytes per character, so its buffer can hold up to about 4 times the bytes counted. |
|
|
568
|
+
| `export_timeout_millis` | `30000` | Passed to BatchSpanProcessor, which does not apply it. The telemetry.dev exporter applies a 10-second `timeout` to each request, as the `init` option describes. |
|
|
513
569
|
| `span_filter` | `None` | Export predicate `Callable[[ReadableSpan], bool]`; failures export the span. |
|
|
514
570
|
| `metrics` | `True` | Auto-record GenAI duration/token histograms for exported spans when an API key is available. |
|
|
515
571
|
| `service_name` | `OTEL_SERVICE_NAME` or `unknown_service` | `service.name` on the metrics resource. |
|
|
516
572
|
| `environment` | `TELEMETRY_DEV_ENVIRONMENT` or `production` | `deployment.environment.name` on the metrics resource. |
|
|
517
|
-
| `on_error` | `None` | Receives export errors of the telemetry.dev exporters and internal processor errors; errors are never raised into your code. A series of failed exports is one report. After an export reaches the ingest, the next failure is a new report. |
|
|
573
|
+
| `on_error` | `None` | Receives export errors of the telemetry.dev exporters and internal processor errors; errors are never raised into your code. A series of failed exports is one report. After an export reaches the ingest, the next failure is a new report. A `force_flush()` or `shutdown()` of the processor that `on_error` calls during an export returns at once (`force_flush()` returns `False`) and runs on a new thread. The process waits for a deferred `shutdown()` before it exits, and runs it at exit if its thread cannot start, unless the process is already exiting; it does not wait for a deferred `force_flush()`. |
|
|
518
574
|
| `span_exporter` | `None` | Advanced/test seam replacing the telemetry.dev OTLP trace exporter. |
|
|
519
575
|
|
|
520
576
|
Without an API key and without `span_exporter`, `TelemetrySpanProcessor()` is an inert no-op:
|
|
@@ -103,10 +103,6 @@ metric readers, so supplying `metric_reader=` disables this histogram. Duration,
|
|
|
103
103
|
time-to-first-chunk metrics remain available; the first accepted streamed span with two or more
|
|
104
104
|
recorded chunks reports this limitation once through `on_error` and the SDK logger.
|
|
105
105
|
|
|
106
|
-
Provider integrations feature-detect `record_output_chunk`. Older core SDKs without that method
|
|
107
|
-
continue delivering streams and tracing normally, but do not emit chunk-interval metrics. Upgrade
|
|
108
|
-
the core SDK with provider integrations to enable the new metric.
|
|
109
|
-
|
|
110
106
|
### Span types
|
|
111
107
|
|
|
112
108
|
`type=` maps to `gen_ai.operation.name`:
|
|
@@ -158,14 +154,27 @@ metadata, identifiers, resource attributes, or events written directly through r
|
|
|
158
154
|
| `log_level` | `"warn"` | SDK diagnostics level: `debug`/`info`/`warn`/`error`/`silent`. |
|
|
159
155
|
| `capture_input`, `capture_output` | `True` | Global content-capture defaults. |
|
|
160
156
|
| `mask` | `None` | `Callable[[Any, MaskContext], Any]` redaction hook, runs before JSON serialization on input/output/log messages and `SpanHandle.serialize_content` (`MaskContext.key` is the attribute being written). Not applied to correlation identifiers. |
|
|
161
|
-
| `max_attribute_length` | `
|
|
157
|
+
| `max_attribute_length` | `None` | Per-content-attribute cap in UTF-16 units, the marker included: `None` (no cap, each value is sent whole) or a non-negative integer. Invalid values call `on_error`, do not raise into application code, and install a disabled client; truncated values use the ASCII `...[truncated]` marker, or its prefix when the cap is shorter than the marker. The cap also applies, with the marker, to string metadata and log attributes; any other string attribute is cut at the cap without a marker, except that a cap of `0` leaves those whole. |
|
|
158
|
+
| `max_stream_bytes` | `67108864` (64 MiB) | Maximum output, across the process, that provider integrations keep for streams that have not ended, in UTF-8 bytes of JSON. Each stream keeps at most `MAX_RECORD_BYTES` (16 MiB); while no other stream holds bytes in it, not even the few bytes a started stream holds before any output, a stream may grow to 16 MiB even past this bound. Output past either bound is not kept, and its span gets `telemetry.dev.capture.truncated`. `None` selects the default; other values that are not non-negative integers or `math.inf` call `on_error` and install a disabled client. The bound counts UTF-8 bytes, not Python memory, so each streamed text buffer holds more than it counts: up to 255 of its latest deltas as separate strings, at most about 22 KB, and either about 1.5% of its text on Python 3.10 and 3.11 or, on 3.12 and later, the room `StringIO` reserves to grow, up to about 25% of its text (50% on Windows). Text with a character outside Latin-1, such as an emoji, is stored at up to 4 bytes per character, so its buffer can hold up to about 4 times the bytes counted. |
|
|
162
159
|
| `span_filter` | `None` | Export predicate `Callable[[ReadableSpan], bool]`. |
|
|
163
160
|
| `sampler` | OTel environment configuration | OTel `Sampler` that overrides environment settings. Session roots use its root policy. Real parents keep their sampling decisions. |
|
|
164
|
-
| `on_error` | `None` | Receives internal SDK errors; repeated instrumentation failures from one stream may be coalesced into one report. The SDK never raises them into application code. A series of failed exports, for example during an ingest outage, is one report to `on_error` and the `telemetry_dev` logger. After an export reaches the ingest, the next failure is a new report. |
|
|
161
|
+
| `on_error` | `None` | Receives internal SDK errors; repeated instrumentation failures from one stream may be coalesced into one report. The SDK never raises them into application code. A series of failed exports, for example during an ingest outage, is one report to `on_error` and the `telemetry_dev` logger. After an export reaches the ingest, the next failure is a new report. A `flush()` or `shutdown()` that `on_error` calls during an export returns at once, before it sends anything, and runs on a new thread, because a batch processor or the metric reader holds a lock during its export that the call needs; the thread waits for that export to end. With `export_mode="immediate"` the span and log exports hold no lock, so the call starts at once, but it still returns before it sends anything: in a serverless handler, call `flush()` after the work, outside `on_error`. The process does not wait for a deferred flush before it exits; the shutdown at exit sends what is left, unless `disable_atexit=True`, where the application must call `shutdown()`. After a deferred `shutdown()`, the client is already gone, and the process waits for the shutdown before it exits; if its thread cannot start, it runs when the process exits, unless the process is already exiting. |
|
|
165
162
|
| `disable_atexit` | `False` | Skip the automatic atexit shutdown. |
|
|
166
|
-
| `timeout` | `10.0` | OTLP HTTP
|
|
163
|
+
| `timeout` | `10.0` | Timeout of each OTLP HTTP export request in seconds. No wait before a retry starts after it, but a request whose connection fails is sent again at once with the same limit, so an ingest that does not answer can take up to twice this time. The limit applies to each wait for the server, so a response that keeps arriving can also take longer. A batch split into several requests gives each request this timeout. |
|
|
167
164
|
| `span_exporter`, `log_exporter`, `metric_reader` | `None` | Test seams / offline mode; any of them enables the client without an API key. |
|
|
168
165
|
|
|
166
|
+
Each export request is at most 16 MiB, the ingest's limit, so a larger batch is sent in several
|
|
167
|
+
requests. A span or log record that is larger than 16 MiB by itself is still sent: its longest
|
|
168
|
+
string values (attributes, or a log record's message) are cut to fit and end with `...[truncated]`,
|
|
169
|
+
the record gets `telemetry.dev.capture.truncated = true`, and `on_error` receives a report. A record
|
|
170
|
+
that still does not fit, because its events or array attributes hold the excess, is dropped and
|
|
171
|
+
reported. `create_telemetry_span_exporter` and `TelemetrySpanProcessor` follow the same rules.
|
|
172
|
+
|
|
173
|
+
In `"batched"` mode, the spans and the log records that wait for export are each bounded to 2048
|
|
174
|
+
records (for logs, `OTEL_BLRP_MAX_QUEUE_SIZE` when it is set) and 64 MiB of string values, each character counted as one byte. A record that arrives
|
|
175
|
+
when its queue is full is dropped, and the first drop of a series is reported to `on_error`. A
|
|
176
|
+
single record larger than the bound is still queued when its queue is empty.
|
|
177
|
+
|
|
169
178
|
`capture_input=False` and `capture_output=False` omit captured content. Provider integrations may
|
|
170
179
|
still record non-content metadata such as response IDs and models, finish reasons, usage, timing,
|
|
171
180
|
and error types. Capture flags do not gate stop sequences, tool descriptions and definitions
|
|
@@ -221,13 +230,13 @@ pinned = telemetry_dev.get_prompt("support/reply", version=13)
|
|
|
221
230
|
call returns it at once and fetches a new copy in a background thread. Only a call with no copy
|
|
222
231
|
waits for a fetch, for 10 seconds at most, also when the network stops. Calls for the same
|
|
223
232
|
prompt share one fetch. `get_prompt_async` runs the same function in a worker thread.
|
|
224
|
-
- A process
|
|
225
|
-
`multiprocessing` with the `fork` start method
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
233
|
+
- A forked process keeps the copies of its parent: a process that `os.fork()` makes, one that
|
|
234
|
+
`multiprocessing` makes with the `fork` start method, or one that C code forks without running
|
|
235
|
+
fork hooks. A call that starts in the child does not wait for a fetch of the parent, or for a
|
|
236
|
+
lock that a thread of the parent held. If the call must fetch, it starts a new fetch.
|
|
237
|
+
- If a signal handler forks while a call waits for a fetch, that call continues in the child,
|
|
238
|
+
where no thread completes that fetch. Thus the call looks at the cache again, and starts a new
|
|
239
|
+
fetch if it must, which it waits for up to 10 seconds more.
|
|
231
240
|
- If a fetch fails, `get_prompt` returns the last copy that it fetched and reports the error
|
|
232
241
|
through `on_error`. With no copy, it returns the `fallback` (`version` is `None` and
|
|
233
242
|
`is_fallback` is `True`). With no fallback, it raises `PromptError` whose `code` tells why, for
|
|
@@ -251,7 +260,10 @@ pinned = telemetry_dev.get_prompt("support/reply", version=13)
|
|
|
251
260
|
`td.prompt.name`, `td.prompt.version`, and `td.prompt.label` (only for a prompt fetched by label).
|
|
252
261
|
When input capture is on, `td.prompt.variables` holds the JSON of the variables of the last
|
|
253
262
|
`compile()` of that prompt object before `with_prompt` starts, or of the last `compile()` inside
|
|
254
|
-
it, after `mask
|
|
263
|
+
it, after `mask`. Past `max_attribute_length` the longest values are cut one by one, each to a text
|
|
264
|
+
that ends with `...[truncated]`, when the keys and a marker for each cut value fit; the JSON then
|
|
265
|
+
keeps every variable. Otherwise the whole attribute is cut, so it can lose variables and stop being
|
|
266
|
+
valid JSON. If threads or tasks that run at the same time share one prompt
|
|
255
267
|
object, call `compile()` in `with_prompt`, or call `get_prompt` in each thread or task.
|
|
256
268
|
`get_prompt` returns a new object from the cache. If you do not, a thread can record the variables
|
|
257
269
|
of a different thread. Inside one `with_prompt`, an asyncio task records the last `compile()` that
|
|
@@ -359,9 +371,12 @@ result = telemetry_dev.run_experiment(
|
|
|
359
371
|
- `run_experiment` sends the outputs in batches of 100 items at most, and then waits up to 30
|
|
360
372
|
minutes for the server evaluators. Then it prints the scores and returns an `ExperimentResult`:
|
|
361
373
|
`status`, `check_status`, item counts, `scores` with the value, the baseline value, the change,
|
|
362
|
-
|
|
374
|
+
the pass mark, and the failed runs (`items_failed`) of each score, and the `url` of the
|
|
375
|
+
experiment page.
|
|
363
376
|
- `min_scores` sets a pass mark for each score name: the lowest pass rate (0 to 1) of a pass/fail
|
|
364
|
-
score, or the lowest mean of a number score. A score under its mark fails the check.
|
|
377
|
+
score, or the lowest mean of a number score. A score under its mark fails the check. A run whose
|
|
378
|
+
task raises has no score, so the value of a score leaves it out, and with pass marks a failed
|
|
379
|
+
run fails the check.
|
|
365
380
|
- A failed request raises `ExperimentError` whose `code` tells why, for example
|
|
366
381
|
`dataset_not_found`, `dataset_empty`, `evaluator_not_found`, `insufficient_scope`,
|
|
367
382
|
`missing_api_key`, or `timeout`. A read of the dataset or the result, a post of outputs, and the
|
|
@@ -371,7 +386,8 @@ result = telemetry_dev.run_experiment(
|
|
|
371
386
|
made the experiment, also after Ctrl-C or a cancellation, `run_experiment` sends the completion
|
|
372
387
|
before it raises: telemetry.dev then ends the experiment with the item runs that it got, or fails
|
|
373
388
|
it when it got none.
|
|
374
|
-
- The key needs the `datasets:read` and
|
|
389
|
+
- The key needs Run experiments in its Access on the API keys page (the `datasets:read` and
|
|
390
|
+
`experiments:write` scopes). `run_experiment` uses the same
|
|
375
391
|
API key and base URL as `get_prompt`, or its own `api_key` and `base_url`. With
|
|
376
392
|
`environment="experiment"`, the key of the telemetry client must be for all environments or for
|
|
377
393
|
`experiment`: a key for another environment cannot send the traces of the items, and the items
|
|
@@ -394,7 +410,9 @@ The [experiment action](../../actions/experiment) runs your eval script in a wor
|
|
|
394
410
|
the result on the pull request. It gives `run_experiment` the `dataset` input in
|
|
395
411
|
`TELEMETRY_DEV_EXPERIMENT_DATASET`, which must agree with the `dataset` of the script, and the
|
|
396
412
|
`min-scores` input in `TELEMETRY_DEV_EXPERIMENT_MIN_SCORES`, which wins over `min_scores` for the
|
|
397
|
-
same name.
|
|
413
|
+
same name. Use the action as `telemetry-dev/telemetry/actions/experiment@experiment-action-v1`, a
|
|
414
|
+
tag that moves to each 1.x release of the action. In the action, a failed experiment with `ci=False`
|
|
415
|
+
shows its result but does not fail the job.
|
|
398
416
|
|
|
399
417
|
## Scores
|
|
400
418
|
|
|
@@ -404,13 +422,16 @@ user on a reply:
|
|
|
404
422
|
```python
|
|
405
423
|
import telemetry_dev
|
|
406
424
|
|
|
407
|
-
# When the app replies, keep the trace id of the reply with the message:
|
|
425
|
+
# When the app replies, keep the trace id and the span id of the reply with the message:
|
|
408
426
|
with telemetry_dev.start_span("reply") as span:
|
|
409
427
|
text = reply_to_customer(message)
|
|
410
|
-
|
|
428
|
+
context = span.span.get_span_context()
|
|
429
|
+
trace_id, span_id = f"{context.trace_id:032x}", f"{context.span_id:016x}"
|
|
411
430
|
|
|
412
431
|
# Later, when the user clicks thumbs down:
|
|
413
|
-
telemetry_dev.score(
|
|
432
|
+
telemetry_dev.score(
|
|
433
|
+
name="user_feedback", trace_id=trace_id, span_id=span_id, value=0, comment="Wrong amount"
|
|
434
|
+
)
|
|
414
435
|
```
|
|
415
436
|
|
|
416
437
|
- Give `trace_id`, `session_id`, or the two, and a `span_id` of the trace for a score on one span.
|
|
@@ -420,8 +441,16 @@ telemetry_dev.score(name="user_feedback", trace_id=trace_id, value=0, comment="W
|
|
|
420
441
|
after it sends a `ScoreError` to `on_error`. It checks the score before it sends it
|
|
421
442
|
(`invalid_score`). The trace id of a span from a client that is not enabled is all zeros, and
|
|
422
443
|
`score` reports it as `invalid_score`.
|
|
444
|
+
- Give the `span_id` of the reply for feedback on one reply. The SDK puts all the turns of a session
|
|
445
|
+
in one trace, so feedback with only a `trace_id` counts for the turn that started last before the
|
|
446
|
+
feedback came, which can be a later turn than the reply.
|
|
447
|
+
- The rates of the dashboard count one `user_feedback` vote for each span, turn, or session: a
|
|
448
|
+
later `user_feedback` score on the same span (on the same turn, for feedback with only a trace,
|
|
449
|
+
or on the same session, for feedback with no trace) replaces the earlier one. The SDK sends
|
|
450
|
+
feedback with no author, so all the feedback that the SDK sends on one span or one turn counts as
|
|
451
|
+
one vote: the newest.
|
|
423
452
|
- `score` makes one request, with no retry. `score_async` runs it in a worker thread. The key
|
|
424
|
-
needs the `scores:write` scope.
|
|
453
|
+
needs Run experiments in its Access on the API keys page (the `scores:write` scope).
|
|
425
454
|
|
|
426
455
|
## Auto-metrics
|
|
427
456
|
|
|
@@ -449,6 +478,27 @@ telemetry_dev.init(export_mode="immediate")
|
|
|
449
478
|
telemetry_dev.flush() # force-flush traces + logs + metrics
|
|
450
479
|
```
|
|
451
480
|
|
|
481
|
+
## Forking servers
|
|
482
|
+
|
|
483
|
+
A server that forks its workers from the process that called `init()`, as gunicorn,
|
|
484
|
+
`multiprocessing` with the `fork` start method, or `os.fork()`, needs no setup. Python runs the
|
|
485
|
+
fork hooks of the SDK and of OpenTelemetry in each worker, and the SDK does not wait in a worker
|
|
486
|
+
for a lock that a thread of the parent held at the fork.
|
|
487
|
+
|
|
488
|
+
uWSGI loads the app before it forks its workers, and by default it runs no Python fork hooks in
|
|
489
|
+
them. Traces and logs still go out, but the OpenTelemetry metric reader starts its thread again
|
|
490
|
+
only in a fork hook, so a worker sends metrics only when it calls `telemetry_dev.flush()`. Set one
|
|
491
|
+
of these uWSGI options:
|
|
492
|
+
|
|
493
|
+
- `lazy-apps = true`: each worker loads the app, and thus calls `init()` itself.
|
|
494
|
+
- `py-call-uwsgi-fork-hooks = true` (uWSGI 2.0.21 and later): uWSGI runs the Python fork hooks
|
|
495
|
+
around each fork. Before 2.0.27, it runs only the hooks of the child, which is enough for the
|
|
496
|
+
SDK.
|
|
497
|
+
|
|
498
|
+
Before uWSGI 2.0.27, Python threads run only with `enable-threads = true` or more than one
|
|
499
|
+
`threads`. The SDK sends telemetry from its own threads, so set one of them too, also with
|
|
500
|
+
`lazy-apps`.
|
|
501
|
+
|
|
452
502
|
## Bring your own OTel (`telemetry_dev.otel`)
|
|
453
503
|
|
|
454
504
|
If you already run an OpenTelemetry SDK, attach the telemetry.dev processor to your provider
|
|
@@ -470,6 +520,10 @@ span_exporter = create_telemetry_span_exporter()
|
|
|
470
520
|
your_tracer_provider.add_span_processor(BatchSpanProcessor(span_exporter))
|
|
471
521
|
```
|
|
472
522
|
|
|
523
|
+
`create_telemetry_span_exporter` takes `api_key`, `base_url`, and `timeout`, in seconds (default
|
|
524
|
+
`10.0`). The timeout works as the `timeout` option of `init` describes, also for each request of a
|
|
525
|
+
batch that is split.
|
|
526
|
+
|
|
473
527
|
`TelemetrySpanProcessor` remains available from the package root for compatibility:
|
|
474
528
|
|
|
475
529
|
```python
|
|
@@ -484,12 +538,14 @@ from telemetry_dev import TelemetrySpanProcessor
|
|
|
484
538
|
| `max_export_batch_size` | `64` | BatchSpanProcessor export batch size. |
|
|
485
539
|
| `schedule_delay_millis` | `1000` | BatchSpanProcessor schedule delay. |
|
|
486
540
|
| `max_queue_size` | `2048` | BatchSpanProcessor queue size. |
|
|
487
|
-
| `
|
|
541
|
+
| `max_queue_bytes` | `67108864` (64 MiB) | Maximum size of the queued spans, counting each character of their string attribute values as one byte. A span is still queued when the queue is empty. Spans over the bound are dropped and reported to `on_error`. |
|
|
542
|
+
| `max_stream_bytes` | `67108864` (64 MiB) | Maximum output that provider integrations keep, across the process, for streams that have not ended (UTF-8 bytes of JSON). Each stream keeps at most `MAX_RECORD_BYTES` (16 MiB); while no other stream holds bytes in it, not even the few bytes a started stream holds before any output, a stream may grow to 16 MiB even past this bound. The bound is process-wide, so the span processor started last sets it. `None` selects the default; other values that are not non-negative integers or `math.inf` are reported to `on_error`, and the processor exports nothing. Output past either bound is not kept, and its span gets `telemetry.dev.capture.truncated`. The bound counts UTF-8 bytes, not Python memory, so each streamed text buffer holds more than it counts: up to 255 of its latest deltas as separate strings, at most about 22 KB, and either about 1.5% of its text on Python 3.10 and 3.11 or, on 3.12 and later, the room `StringIO` reserves to grow, up to about 25% of its text (50% on Windows). Text with a character outside Latin-1, such as an emoji, is stored at up to 4 bytes per character, so its buffer can hold up to about 4 times the bytes counted. |
|
|
543
|
+
| `export_timeout_millis` | `30000` | Passed to BatchSpanProcessor, which does not apply it. The telemetry.dev exporter applies a 10-second `timeout` to each request, as the `init` option describes. |
|
|
488
544
|
| `span_filter` | `None` | Export predicate `Callable[[ReadableSpan], bool]`; failures export the span. |
|
|
489
545
|
| `metrics` | `True` | Auto-record GenAI duration/token histograms for exported spans when an API key is available. |
|
|
490
546
|
| `service_name` | `OTEL_SERVICE_NAME` or `unknown_service` | `service.name` on the metrics resource. |
|
|
491
547
|
| `environment` | `TELEMETRY_DEV_ENVIRONMENT` or `production` | `deployment.environment.name` on the metrics resource. |
|
|
492
|
-
| `on_error` | `None` | Receives export errors of the telemetry.dev exporters and internal processor errors; errors are never raised into your code. A series of failed exports is one report. After an export reaches the ingest, the next failure is a new report. |
|
|
548
|
+
| `on_error` | `None` | Receives export errors of the telemetry.dev exporters and internal processor errors; errors are never raised into your code. A series of failed exports is one report. After an export reaches the ingest, the next failure is a new report. A `force_flush()` or `shutdown()` of the processor that `on_error` calls during an export returns at once (`force_flush()` returns `False`) and runs on a new thread. The process waits for a deferred `shutdown()` before it exits, and runs it at exit if its thread cannot start, unless the process is already exiting; it does not wait for a deferred `force_flush()`. |
|
|
493
549
|
| `span_exporter` | `None` | Advanced/test seam replacing the telemetry.dev OTLP trace exporter. |
|
|
494
550
|
|
|
495
551
|
Without an API key and without `span_exporter`, `TelemetrySpanProcessor()` is an inert no-op:
|
|
@@ -29,6 +29,7 @@ from ._experiments import (
|
|
|
29
29
|
run_experiment,
|
|
30
30
|
run_experiment_async,
|
|
31
31
|
)
|
|
32
|
+
from ._export import MAX_RECORD_BYTES
|
|
32
33
|
from ._logs import log
|
|
33
34
|
from ._observe import observe
|
|
34
35
|
from ._prompts import (
|
|
@@ -55,6 +56,7 @@ from ._spans import (
|
|
|
55
56
|
from .otel import TelemetrySpanProcessor
|
|
56
57
|
|
|
57
58
|
__all__ = [
|
|
59
|
+
"MAX_RECORD_BYTES",
|
|
58
60
|
"NOT_GIVEN",
|
|
59
61
|
"AsyncScorer",
|
|
60
62
|
"AttributeValue",
|