|
| 1 | +# GenAI Instrumentation — Agent and Contributor Guidelines |
| 2 | + |
| 3 | +Instrumentation packages here wrap specific libraries (OpenAI, Anthropic, etc.) and bridge |
| 4 | +them to the shared telemetry layer in `util/opentelemetry-util-genai`. |
| 5 | + |
| 6 | +## 1. Instrumentation Layer Boundary |
| 7 | + |
| 8 | +Do not call OpenTelemetry APIs (`tracer`, `meter`, `span`, event APIs) directly. |
| 9 | +Always go through `TelemetryHandler` and the invocation objects it returns. |
| 10 | + |
| 11 | +This layer is responsible only for: |
| 12 | + |
| 13 | +- Patching the library |
| 14 | +- Parsing library-specific input/output into invocation fields |
| 15 | + |
| 16 | +Everything else (span creation, metric recording, event emission, context propagation) |
| 17 | +belongs in `util/opentelemetry-util-genai`. |
| 18 | + |
| 19 | +## 2. Invocation Pattern |
| 20 | + |
| 21 | +Use `start_*()` and control span lifetime manually: |
| 22 | + |
| 23 | +```python |
| 24 | +invocation = handler.start_inference(provider, request_model, server_address=..., server_port=...) |
| 25 | +invocation.temperature = ... |
| 26 | +try: |
| 27 | + response = client.call(...) |
| 28 | + invocation.response_model_name = response.model |
| 29 | + invocation.finish_reasons = response.finish_reasons |
| 30 | + invocation.stop() |
| 31 | +except Exception as exc: |
| 32 | + invocation.fail(exc) |
| 33 | + raise |
| 34 | +``` |
| 35 | + |
| 36 | +## 3. Exception Handling |
| 37 | + |
| 38 | +- Do not add `raise {Error}` statements in instrumentation/telemetry code — validation belongs in |
| 39 | + tests and callers, not in the instrumentation layer. |
| 40 | +- When catching exceptions from the underlying library to record telemetry, always re-raise |
| 41 | + the original exception unmodified. |
0 commit comments