agnara-telemetry 0.1.0a8__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.
- agnara_telemetry-0.1.0a8/.gitignore +41 -0
- agnara_telemetry-0.1.0a8/LICENSE +202 -0
- agnara_telemetry-0.1.0a8/PKG-INFO +195 -0
- agnara_telemetry-0.1.0a8/README.md +175 -0
- agnara_telemetry-0.1.0a8/pyproject.toml +28 -0
- agnara_telemetry-0.1.0a8/src/agnara_telemetry/__init__.py +6 -0
- agnara_telemetry-0.1.0a8/src/agnara_telemetry/metrics.py +45 -0
- agnara_telemetry-0.1.0a8/src/agnara_telemetry/py.typed +0 -0
- agnara_telemetry-0.1.0a8/src/agnara_telemetry/tracing.py +99 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.eggs/
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
|
|
9
|
+
# Environments
|
|
10
|
+
.venv/
|
|
11
|
+
venv/
|
|
12
|
+
|
|
13
|
+
# Tooling caches
|
|
14
|
+
.pytest_cache/
|
|
15
|
+
.pytest-*/
|
|
16
|
+
.ruff_cache/
|
|
17
|
+
.ty_cache/
|
|
18
|
+
.mypy_cache/
|
|
19
|
+
.coverage
|
|
20
|
+
coverage.xml
|
|
21
|
+
htmlcov/
|
|
22
|
+
test-results/
|
|
23
|
+
|
|
24
|
+
# uv
|
|
25
|
+
uv.lock.bak
|
|
26
|
+
.uv-cache/
|
|
27
|
+
|
|
28
|
+
# Locally generated release and validation artifacts. These directories
|
|
29
|
+
# hold built wheels and sdists; several are created by tooling that drops
|
|
30
|
+
# its own `*` .gitignore inside them, which only protects the repository
|
|
31
|
+
# for as long as that file survives. Name them here as well, so a
|
|
32
|
+
# hand-recreated directory cannot commit distributions.
|
|
33
|
+
.artifacts/
|
|
34
|
+
.dist-test/
|
|
35
|
+
.release-validation/
|
|
36
|
+
|
|
37
|
+
# Editors / OS
|
|
38
|
+
.idea/
|
|
39
|
+
.vscode/
|
|
40
|
+
.DS_Store
|
|
41
|
+
Thumbs.db
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: agnara-telemetry
|
|
3
|
+
Version: 0.1.0a8
|
|
4
|
+
Summary: OpenTelemetry bridge for Agnara capability execution.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Blandskron/agnara
|
|
6
|
+
Project-URL: Repository, https://github.com/Blandskron/agnara
|
|
7
|
+
Author-email: Agnara Maintainers <maintainers@agnara.dev>
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
16
|
+
Requires-Python: >=3.14
|
|
17
|
+
Requires-Dist: agnara==0.1.0a8
|
|
18
|
+
Requires-Dist: opentelemetry-api<2,>=1.44
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# agnara-telemetry
|
|
22
|
+
|
|
23
|
+
Explicit OpenTelemetry metrics and span bridges for Agnara's execution hooks.
|
|
24
|
+
This distribution is built and versioned with the synchronized workspace
|
|
25
|
+
set. Which versions exist on an index is answered by its PyPI project page,
|
|
26
|
+
not by this file: a README ships inside the artifact and cannot describe
|
|
27
|
+
the state of a publication that happens after it is built.
|
|
28
|
+
|
|
29
|
+
It imports `agnara` and `opentelemetry-api`, never a sibling adapter or the SDK.
|
|
30
|
+
|
|
31
|
+
## Composition
|
|
32
|
+
|
|
33
|
+
The application installs/configures its chosen SDK and supplies a meter:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from opentelemetry.sdk.metrics import MeterProvider
|
|
37
|
+
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
|
|
38
|
+
|
|
39
|
+
from agnara.execution import ExecutionPlan
|
|
40
|
+
from agnara_telemetry import OpenTelemetryMetricsHook
|
|
41
|
+
|
|
42
|
+
# In-memory example: no network exporter or global provider installation.
|
|
43
|
+
reader = InMemoryMetricReader()
|
|
44
|
+
provider = MeterProvider(metric_readers=[reader], shutdown_on_exit=False)
|
|
45
|
+
try:
|
|
46
|
+
hook = OpenTelemetryMetricsHook(provider.get_meter("agnara_telemetry"))
|
|
47
|
+
plan = ExecutionPlan.compile(definition, registry, hooks=[hook])
|
|
48
|
+
# Invoke plans during your application's owned lifecycle.
|
|
49
|
+
# provider.force_flush() may be called by the application when needed.
|
|
50
|
+
finally:
|
|
51
|
+
provider.shutdown()
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`definition` and `registry` are the application's declared capability and DI
|
|
55
|
+
registry. Reuse one hook across plans; duplicate hook registration intentionally
|
|
56
|
+
duplicates measurements. A supplied `NoOpMeterProvider().get_meter(...)` works
|
|
57
|
+
without SDK setup. The package neither selects a global provider nor owns
|
|
58
|
+
flush, shutdown, background workers, network endpoints or exporter credentials.
|
|
59
|
+
|
|
60
|
+
## Measurements
|
|
61
|
+
|
|
62
|
+
| Instrument | Kind | Unit |
|
|
63
|
+
| --- | --- | --- |
|
|
64
|
+
| `agnara.invocation.count` | Counter | `1` |
|
|
65
|
+
| `agnara.invocation.duration` | Histogram | `s` |
|
|
66
|
+
|
|
67
|
+
Each terminal callback records one count and the core elapsed duration,
|
|
68
|
+
converted from monotonic nanoseconds to seconds. The start callback stores
|
|
69
|
+
nothing. Attributes are restricted to `agnara.capability.id` and
|
|
70
|
+
`agnara.invocation.outcome`. Outcomes are `success`, `failure`, `timeout` and
|
|
71
|
+
`cancellation`; unrecognized direct events map to `unknown`. Negative elapsed
|
|
72
|
+
durations are rejected before recording. Use static capability IDs to avoid
|
|
73
|
+
unbounded metric cardinality.
|
|
74
|
+
|
|
75
|
+
No tracking IDs, arguments, return values, exception details or principal
|
|
76
|
+
data are added. Applications still control SDK resource attributes, exemplars,
|
|
77
|
+
readers and exporters, so review their full export independently. Metric names
|
|
78
|
+
are custom Agnara names, not a protocol or GenAI semantic-convention claim.
|
|
79
|
+
|
|
80
|
+
Counts describe terminal deliveries, not every attempted invocation. Calls
|
|
81
|
+
rejected before core emits a start event are excluded. An explicitly returned
|
|
82
|
+
canonical `Failure` counts as a successful Python return in the existing core
|
|
83
|
+
event semantics. Ordinary instrument errors are isolated by the core runtime;
|
|
84
|
+
a failed instrument may leave partial measurements. There are no retries or
|
|
85
|
+
exactly-once export promises.
|
|
86
|
+
|
|
87
|
+
## Ownership and validation
|
|
88
|
+
|
|
89
|
+
The hook retains only instrument handles; nested/overlapping invocations and
|
|
90
|
+
repeated tracking IDs need no correlation map. The supplied meter must support
|
|
91
|
+
concurrent, synchronous, non-blocking recording. Do not mutate instruments
|
|
92
|
+
while plans are active. A custom blocking instrument blocks invocation.
|
|
93
|
+
|
|
94
|
+
Development tests use the pinned OpenTelemetry API/SDK 1.44.0 with an in-memory
|
|
95
|
+
reader and an in-memory span exporter:
|
|
96
|
+
`uv run pytest tests/telemetry tests/architecture`. Tests cover exact
|
|
97
|
+
values/units, attributes, span names, status mapping, parent/child nesting,
|
|
98
|
+
released correlation state, redaction, runtime outcomes, nested and concurrent
|
|
99
|
+
execution, thread sharing, API no-op behavior and application-owned lifecycle.
|
|
100
|
+
They do not establish free-threading compatibility, network exporter
|
|
101
|
+
conformance, screening of application-added SDK data or performance
|
|
102
|
+
superiority.
|
|
103
|
+
|
|
104
|
+
## Spans
|
|
105
|
+
|
|
106
|
+
`OpenTelemetryTracingHook(tracer)` opens one span per invocation and ends it on
|
|
107
|
+
the matching terminal event:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from opentelemetry.sdk.trace import TracerProvider
|
|
111
|
+
|
|
112
|
+
from agnara_telemetry import OpenTelemetryTracingHook
|
|
113
|
+
|
|
114
|
+
provider = TracerProvider(shutdown_on_exit=False)
|
|
115
|
+
# The application adds its own span processor and exporter here.
|
|
116
|
+
try:
|
|
117
|
+
hook = OpenTelemetryTracingHook(provider.get_tracer("agnara_telemetry"))
|
|
118
|
+
plan = ExecutionPlan.compile(definition, registry, hooks=[hook])
|
|
119
|
+
finally:
|
|
120
|
+
provider.shutdown()
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The span is named after the capability, is `INTERNAL`, and carries only
|
|
124
|
+
`agnara.capability.id` and `agnara.invocation.outcome`. `success` sets `OK`;
|
|
125
|
+
`failure` and `timeout` set `ERROR` with the outcome word as the description;
|
|
126
|
+
`cancellation` is recorded but left `UNSET`, because the caller withdrew rather
|
|
127
|
+
than the capability failing. No exception text, argument, result, principal,
|
|
128
|
+
transport field or tracking ID is attached, and no span events are recorded.
|
|
129
|
+
|
|
130
|
+
Pairing uses the runtime's `invocation_id`, never a caller `tracking_id`. That
|
|
131
|
+
identity is not exported: it would be unbounded cardinality on every span, and
|
|
132
|
+
the span ID already identifies the span.
|
|
133
|
+
|
|
134
|
+
A nested invocation becomes a child span, because the started span is attached
|
|
135
|
+
to the OpenTelemetry context; invocations in sibling tasks are unrelated,
|
|
136
|
+
because each task holds its own copy of that context. Register the hook at most
|
|
137
|
+
once per plan — a second registration opens a second span for one invocation.
|
|
138
|
+
Delivering events to this hook by hand, out of order, is outside the runtime's
|
|
139
|
+
nesting guarantee: the spans still end, but the previous context is not
|
|
140
|
+
restored.
|
|
141
|
+
|
|
142
|
+
Metrics and span hooks compose on one plan and can be registered together.
|
|
143
|
+
|
|
144
|
+
## Joining a caller's trace
|
|
145
|
+
|
|
146
|
+
Agnara reads no propagation header. A capability span is parented by whatever
|
|
147
|
+
OpenTelemetry context is current when the invocation starts, so it joins a
|
|
148
|
+
caller's distributed trace exactly when the application has propagated that
|
|
149
|
+
caller's context — typically with the ASGI or client instrumentation the
|
|
150
|
+
application already runs.
|
|
151
|
+
|
|
152
|
+
The consequence worth stating plainly: a request carrying `traceparent` still
|
|
153
|
+
produces an unlinked root span if nothing extracted it. Linking is opt-in.
|
|
154
|
+
|
|
155
|
+
This is a deliberate boundary, not an omission. Honouring a caller-supplied
|
|
156
|
+
`traceparent` lets that caller choose the trace identity their operation is
|
|
157
|
+
recorded under, which is reasonable inside a trusted perimeter and rarely so at
|
|
158
|
+
an untrusted edge. That judgment belongs to a deployment, not to a framework
|
|
159
|
+
default. See [proposed ADR 0056](../../docs/adr/0056-transport-span-linking.md).
|
|
160
|
+
|
|
161
|
+
Verified over the real HTTP dispatcher and the real MCP invoker: with
|
|
162
|
+
propagation the capability span carries the caller's trace ID and parent span;
|
|
163
|
+
without it, a root span. Malformed headers are inert rather than fatal, and an
|
|
164
|
+
unknown `traceparent` version links, because W3C requires forward compatibility
|
|
165
|
+
and forbids only `ff`.
|
|
166
|
+
|
|
167
|
+
## Semantic conventions
|
|
168
|
+
|
|
169
|
+
Agnara emits `agnara.`-namespaced names plus exactly one OpenTelemetry
|
|
170
|
+
convention attribute: `error.type`, on a span whose outcome is `failure` or
|
|
171
|
+
`timeout`, carrying that same outcome word. It is the only stable convention
|
|
172
|
+
attribute that applies to a capability invocation. It is not an exception type:
|
|
173
|
+
exception text is never exported, and the convention asks for a low-cardinality
|
|
174
|
+
identifier. A `cancellation` carries none, because the caller withdrew rather
|
|
175
|
+
than the capability failing. Metric attributes are unchanged.
|
|
176
|
+
|
|
177
|
+
The GenAI and MCP vocabularies are **not** emitted. `gen_ai.operation.name` with
|
|
178
|
+
the value `execute_tool` would assert that every capability is a tool, which is
|
|
179
|
+
false for an HTTP request made by a human, and the tool call argument and result
|
|
180
|
+
attributes are payloads this package never exports. Both vocabularies are also
|
|
181
|
+
still incubating.
|
|
182
|
+
|
|
183
|
+
If your application genuinely serves capabilities as MCP tools and you want a
|
|
184
|
+
backend's GenAI views, add `mcp.*` and `gen_ai.*` at your MCP layer, where a
|
|
185
|
+
tool identity and a method name actually exist. A transport-neutral capability
|
|
186
|
+
span is the wrong place to claim them.
|
|
187
|
+
|
|
188
|
+
Two tests check every attribute both hooks emit against the installed
|
|
189
|
+
`opentelemetry-semantic-conventions` package: each must be `agnara.`-namespaced
|
|
190
|
+
or stable, and no incubating name may appear. See
|
|
191
|
+
[proposed ADR 0057](../../docs/adr/0057-semantic-convention-compatibility.md).
|
|
192
|
+
|
|
193
|
+
No-op benchmarks remain E9.6. See
|
|
194
|
+
[proposed ADR 0054](../../docs/adr/0054-opentelemetry-metrics-bridge.md)
|
|
195
|
+
and [proposed ADR 0055](../../docs/adr/0055-capability-invocation-spans.md).
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# agnara-telemetry
|
|
2
|
+
|
|
3
|
+
Explicit OpenTelemetry metrics and span bridges for Agnara's execution hooks.
|
|
4
|
+
This distribution is built and versioned with the synchronized workspace
|
|
5
|
+
set. Which versions exist on an index is answered by its PyPI project page,
|
|
6
|
+
not by this file: a README ships inside the artifact and cannot describe
|
|
7
|
+
the state of a publication that happens after it is built.
|
|
8
|
+
|
|
9
|
+
It imports `agnara` and `opentelemetry-api`, never a sibling adapter or the SDK.
|
|
10
|
+
|
|
11
|
+
## Composition
|
|
12
|
+
|
|
13
|
+
The application installs/configures its chosen SDK and supplies a meter:
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from opentelemetry.sdk.metrics import MeterProvider
|
|
17
|
+
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
|
|
18
|
+
|
|
19
|
+
from agnara.execution import ExecutionPlan
|
|
20
|
+
from agnara_telemetry import OpenTelemetryMetricsHook
|
|
21
|
+
|
|
22
|
+
# In-memory example: no network exporter or global provider installation.
|
|
23
|
+
reader = InMemoryMetricReader()
|
|
24
|
+
provider = MeterProvider(metric_readers=[reader], shutdown_on_exit=False)
|
|
25
|
+
try:
|
|
26
|
+
hook = OpenTelemetryMetricsHook(provider.get_meter("agnara_telemetry"))
|
|
27
|
+
plan = ExecutionPlan.compile(definition, registry, hooks=[hook])
|
|
28
|
+
# Invoke plans during your application's owned lifecycle.
|
|
29
|
+
# provider.force_flush() may be called by the application when needed.
|
|
30
|
+
finally:
|
|
31
|
+
provider.shutdown()
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`definition` and `registry` are the application's declared capability and DI
|
|
35
|
+
registry. Reuse one hook across plans; duplicate hook registration intentionally
|
|
36
|
+
duplicates measurements. A supplied `NoOpMeterProvider().get_meter(...)` works
|
|
37
|
+
without SDK setup. The package neither selects a global provider nor owns
|
|
38
|
+
flush, shutdown, background workers, network endpoints or exporter credentials.
|
|
39
|
+
|
|
40
|
+
## Measurements
|
|
41
|
+
|
|
42
|
+
| Instrument | Kind | Unit |
|
|
43
|
+
| --- | --- | --- |
|
|
44
|
+
| `agnara.invocation.count` | Counter | `1` |
|
|
45
|
+
| `agnara.invocation.duration` | Histogram | `s` |
|
|
46
|
+
|
|
47
|
+
Each terminal callback records one count and the core elapsed duration,
|
|
48
|
+
converted from monotonic nanoseconds to seconds. The start callback stores
|
|
49
|
+
nothing. Attributes are restricted to `agnara.capability.id` and
|
|
50
|
+
`agnara.invocation.outcome`. Outcomes are `success`, `failure`, `timeout` and
|
|
51
|
+
`cancellation`; unrecognized direct events map to `unknown`. Negative elapsed
|
|
52
|
+
durations are rejected before recording. Use static capability IDs to avoid
|
|
53
|
+
unbounded metric cardinality.
|
|
54
|
+
|
|
55
|
+
No tracking IDs, arguments, return values, exception details or principal
|
|
56
|
+
data are added. Applications still control SDK resource attributes, exemplars,
|
|
57
|
+
readers and exporters, so review their full export independently. Metric names
|
|
58
|
+
are custom Agnara names, not a protocol or GenAI semantic-convention claim.
|
|
59
|
+
|
|
60
|
+
Counts describe terminal deliveries, not every attempted invocation. Calls
|
|
61
|
+
rejected before core emits a start event are excluded. An explicitly returned
|
|
62
|
+
canonical `Failure` counts as a successful Python return in the existing core
|
|
63
|
+
event semantics. Ordinary instrument errors are isolated by the core runtime;
|
|
64
|
+
a failed instrument may leave partial measurements. There are no retries or
|
|
65
|
+
exactly-once export promises.
|
|
66
|
+
|
|
67
|
+
## Ownership and validation
|
|
68
|
+
|
|
69
|
+
The hook retains only instrument handles; nested/overlapping invocations and
|
|
70
|
+
repeated tracking IDs need no correlation map. The supplied meter must support
|
|
71
|
+
concurrent, synchronous, non-blocking recording. Do not mutate instruments
|
|
72
|
+
while plans are active. A custom blocking instrument blocks invocation.
|
|
73
|
+
|
|
74
|
+
Development tests use the pinned OpenTelemetry API/SDK 1.44.0 with an in-memory
|
|
75
|
+
reader and an in-memory span exporter:
|
|
76
|
+
`uv run pytest tests/telemetry tests/architecture`. Tests cover exact
|
|
77
|
+
values/units, attributes, span names, status mapping, parent/child nesting,
|
|
78
|
+
released correlation state, redaction, runtime outcomes, nested and concurrent
|
|
79
|
+
execution, thread sharing, API no-op behavior and application-owned lifecycle.
|
|
80
|
+
They do not establish free-threading compatibility, network exporter
|
|
81
|
+
conformance, screening of application-added SDK data or performance
|
|
82
|
+
superiority.
|
|
83
|
+
|
|
84
|
+
## Spans
|
|
85
|
+
|
|
86
|
+
`OpenTelemetryTracingHook(tracer)` opens one span per invocation and ends it on
|
|
87
|
+
the matching terminal event:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from opentelemetry.sdk.trace import TracerProvider
|
|
91
|
+
|
|
92
|
+
from agnara_telemetry import OpenTelemetryTracingHook
|
|
93
|
+
|
|
94
|
+
provider = TracerProvider(shutdown_on_exit=False)
|
|
95
|
+
# The application adds its own span processor and exporter here.
|
|
96
|
+
try:
|
|
97
|
+
hook = OpenTelemetryTracingHook(provider.get_tracer("agnara_telemetry"))
|
|
98
|
+
plan = ExecutionPlan.compile(definition, registry, hooks=[hook])
|
|
99
|
+
finally:
|
|
100
|
+
provider.shutdown()
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The span is named after the capability, is `INTERNAL`, and carries only
|
|
104
|
+
`agnara.capability.id` and `agnara.invocation.outcome`. `success` sets `OK`;
|
|
105
|
+
`failure` and `timeout` set `ERROR` with the outcome word as the description;
|
|
106
|
+
`cancellation` is recorded but left `UNSET`, because the caller withdrew rather
|
|
107
|
+
than the capability failing. No exception text, argument, result, principal,
|
|
108
|
+
transport field or tracking ID is attached, and no span events are recorded.
|
|
109
|
+
|
|
110
|
+
Pairing uses the runtime's `invocation_id`, never a caller `tracking_id`. That
|
|
111
|
+
identity is not exported: it would be unbounded cardinality on every span, and
|
|
112
|
+
the span ID already identifies the span.
|
|
113
|
+
|
|
114
|
+
A nested invocation becomes a child span, because the started span is attached
|
|
115
|
+
to the OpenTelemetry context; invocations in sibling tasks are unrelated,
|
|
116
|
+
because each task holds its own copy of that context. Register the hook at most
|
|
117
|
+
once per plan — a second registration opens a second span for one invocation.
|
|
118
|
+
Delivering events to this hook by hand, out of order, is outside the runtime's
|
|
119
|
+
nesting guarantee: the spans still end, but the previous context is not
|
|
120
|
+
restored.
|
|
121
|
+
|
|
122
|
+
Metrics and span hooks compose on one plan and can be registered together.
|
|
123
|
+
|
|
124
|
+
## Joining a caller's trace
|
|
125
|
+
|
|
126
|
+
Agnara reads no propagation header. A capability span is parented by whatever
|
|
127
|
+
OpenTelemetry context is current when the invocation starts, so it joins a
|
|
128
|
+
caller's distributed trace exactly when the application has propagated that
|
|
129
|
+
caller's context — typically with the ASGI or client instrumentation the
|
|
130
|
+
application already runs.
|
|
131
|
+
|
|
132
|
+
The consequence worth stating plainly: a request carrying `traceparent` still
|
|
133
|
+
produces an unlinked root span if nothing extracted it. Linking is opt-in.
|
|
134
|
+
|
|
135
|
+
This is a deliberate boundary, not an omission. Honouring a caller-supplied
|
|
136
|
+
`traceparent` lets that caller choose the trace identity their operation is
|
|
137
|
+
recorded under, which is reasonable inside a trusted perimeter and rarely so at
|
|
138
|
+
an untrusted edge. That judgment belongs to a deployment, not to a framework
|
|
139
|
+
default. See [proposed ADR 0056](../../docs/adr/0056-transport-span-linking.md).
|
|
140
|
+
|
|
141
|
+
Verified over the real HTTP dispatcher and the real MCP invoker: with
|
|
142
|
+
propagation the capability span carries the caller's trace ID and parent span;
|
|
143
|
+
without it, a root span. Malformed headers are inert rather than fatal, and an
|
|
144
|
+
unknown `traceparent` version links, because W3C requires forward compatibility
|
|
145
|
+
and forbids only `ff`.
|
|
146
|
+
|
|
147
|
+
## Semantic conventions
|
|
148
|
+
|
|
149
|
+
Agnara emits `agnara.`-namespaced names plus exactly one OpenTelemetry
|
|
150
|
+
convention attribute: `error.type`, on a span whose outcome is `failure` or
|
|
151
|
+
`timeout`, carrying that same outcome word. It is the only stable convention
|
|
152
|
+
attribute that applies to a capability invocation. It is not an exception type:
|
|
153
|
+
exception text is never exported, and the convention asks for a low-cardinality
|
|
154
|
+
identifier. A `cancellation` carries none, because the caller withdrew rather
|
|
155
|
+
than the capability failing. Metric attributes are unchanged.
|
|
156
|
+
|
|
157
|
+
The GenAI and MCP vocabularies are **not** emitted. `gen_ai.operation.name` with
|
|
158
|
+
the value `execute_tool` would assert that every capability is a tool, which is
|
|
159
|
+
false for an HTTP request made by a human, and the tool call argument and result
|
|
160
|
+
attributes are payloads this package never exports. Both vocabularies are also
|
|
161
|
+
still incubating.
|
|
162
|
+
|
|
163
|
+
If your application genuinely serves capabilities as MCP tools and you want a
|
|
164
|
+
backend's GenAI views, add `mcp.*` and `gen_ai.*` at your MCP layer, where a
|
|
165
|
+
tool identity and a method name actually exist. A transport-neutral capability
|
|
166
|
+
span is the wrong place to claim them.
|
|
167
|
+
|
|
168
|
+
Two tests check every attribute both hooks emit against the installed
|
|
169
|
+
`opentelemetry-semantic-conventions` package: each must be `agnara.`-namespaced
|
|
170
|
+
or stable, and no incubating name may appear. See
|
|
171
|
+
[proposed ADR 0057](../../docs/adr/0057-semantic-convention-compatibility.md).
|
|
172
|
+
|
|
173
|
+
No-op benchmarks remain E9.6. See
|
|
174
|
+
[proposed ADR 0054](../../docs/adr/0054-opentelemetry-metrics-bridge.md)
|
|
175
|
+
and [proposed ADR 0055](../../docs/adr/0055-capability-invocation-spans.md).
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "agnara-telemetry"
|
|
3
|
+
version = "0.1.0a8"
|
|
4
|
+
description = "OpenTelemetry bridge for Agnara capability execution."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
authors = [{name = "Agnara Maintainers", email = "maintainers@agnara.dev"}]
|
|
8
|
+
license = {text = "Apache-2.0"}
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 3 - Alpha",
|
|
11
|
+
"Intended Audience :: Developers",
|
|
12
|
+
"License :: OSI Approved :: Apache Software License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Programming Language :: Python :: 3.14",
|
|
15
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
16
|
+
]
|
|
17
|
+
dependencies = ["agnara==0.1.0a8", "opentelemetry-api>=1.44,<2"]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/Blandskron/agnara"
|
|
21
|
+
Repository = "https://github.com/Blandskron/agnara"
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["hatchling"]
|
|
25
|
+
build-backend = "hatchling.build"
|
|
26
|
+
|
|
27
|
+
[tool.hatch.build.targets.wheel]
|
|
28
|
+
packages = ["src/agnara_telemetry"]
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"""Application-owned OpenTelemetry bridges for Agnara's execution port."""
|
|
2
|
+
|
|
3
|
+
from agnara_telemetry.metrics import OpenTelemetryMetricsHook
|
|
4
|
+
from agnara_telemetry.tracing import OpenTelemetryTracingHook
|
|
5
|
+
|
|
6
|
+
__all__ = ["OpenTelemetryMetricsHook", "OpenTelemetryTracingHook"]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Terminal-event metrics using application-owned OpenTelemetry instruments."""
|
|
2
|
+
|
|
3
|
+
from opentelemetry.metrics import Meter
|
|
4
|
+
|
|
5
|
+
from agnara.execution import InvocationStartEvent, InvocationTerminalEvent
|
|
6
|
+
|
|
7
|
+
__all__ = ["OpenTelemetryMetricsHook"]
|
|
8
|
+
|
|
9
|
+
_OUTCOMES = frozenset({"success", "failure", "timeout", "cancellation"})
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class OpenTelemetryMetricsHook:
|
|
13
|
+
"""Record terminal deliveries without retaining invocation state.
|
|
14
|
+
|
|
15
|
+
Supply a meter at application composition time. The caller owns the SDK,
|
|
16
|
+
exporter configuration and shutdown; this hook never installs a provider.
|
|
17
|
+
Instruments must support synchronous, non-blocking concurrent recording.
|
|
18
|
+
Reuse this hook across plans and register it once on each plan.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
__slots__ = ("_count", "_duration")
|
|
22
|
+
|
|
23
|
+
def __init__(self, meter: Meter) -> None:
|
|
24
|
+
if not isinstance(meter, Meter):
|
|
25
|
+
raise TypeError("meter must be an OpenTelemetry Meter")
|
|
26
|
+
self._count = meter.create_counter(
|
|
27
|
+
"agnara.invocation.count", unit="1", description="Terminal invocation hook deliveries"
|
|
28
|
+
)
|
|
29
|
+
self._duration = meter.create_histogram(
|
|
30
|
+
"agnara.invocation.duration", unit="s", description="Core invocation elapsed duration"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
def on_invocation_start(self, event: InvocationStartEvent) -> None:
|
|
34
|
+
"""No correlation state is needed for terminal-only measurements."""
|
|
35
|
+
|
|
36
|
+
def on_invocation_terminal(self, event: InvocationTerminalEvent) -> None:
|
|
37
|
+
"""Record elapsed seconds and the core outcome, without caller metadata."""
|
|
38
|
+
if event.duration_ns < 0:
|
|
39
|
+
raise ValueError("invocation duration must be non-negative")
|
|
40
|
+
attributes = {
|
|
41
|
+
"agnara.capability.id": str(event.capability_id),
|
|
42
|
+
"agnara.invocation.outcome": event.outcome if event.outcome in _OUTCOMES else "unknown",
|
|
43
|
+
}
|
|
44
|
+
self._count.add(1, attributes)
|
|
45
|
+
self._duration.record(event.duration_ns / 1_000_000_000, attributes)
|
|
File without changes
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Capability spans over application-owned OpenTelemetry tracers."""
|
|
2
|
+
|
|
3
|
+
import threading
|
|
4
|
+
from contextvars import Token
|
|
5
|
+
|
|
6
|
+
from opentelemetry.context import Context, attach, detach
|
|
7
|
+
from opentelemetry.trace import Span, SpanKind, Tracer, set_span_in_context
|
|
8
|
+
from opentelemetry.trace.status import Status, StatusCode
|
|
9
|
+
|
|
10
|
+
from agnara.execution import InvocationStartEvent, InvocationTerminalEvent
|
|
11
|
+
|
|
12
|
+
__all__ = ["OpenTelemetryTracingHook"]
|
|
13
|
+
|
|
14
|
+
_OUTCOMES = frozenset({"success", "failure", "timeout", "cancellation"})
|
|
15
|
+
|
|
16
|
+
#: Outcomes that describe the capability itself going wrong. A cancellation is
|
|
17
|
+
#: the caller withdrawing, so it is recorded but left ``UNSET`` rather than
|
|
18
|
+
#: reported as an error the capability caused.
|
|
19
|
+
_ERROR_OUTCOMES = frozenset({"failure", "timeout"})
|
|
20
|
+
|
|
21
|
+
#: The one stable OpenTelemetry convention attribute that applies to a
|
|
22
|
+
#: capability invocation, from ``opentelemetry.semconv.attributes``. Its value
|
|
23
|
+
#: is Agnara's own closed outcome vocabulary, not an exception type: ADR 0055
|
|
24
|
+
#: forbids exporting exception text, and the convention asks for a
|
|
25
|
+
#: low-cardinality identifier rather than a message. See ADR 0057.
|
|
26
|
+
_ERROR_TYPE = "error.type"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class OpenTelemetryTracingHook:
|
|
30
|
+
"""Open one span per invocation and end it on the matching terminal event.
|
|
31
|
+
|
|
32
|
+
Supply a tracer at application composition time. The caller owns the SDK,
|
|
33
|
+
processor, exporter, flush and shutdown; this hook never installs or reads
|
|
34
|
+
a global provider. Spans are paired through the runtime's
|
|
35
|
+
``invocation_id``, never through caller-supplied tracking metadata.
|
|
36
|
+
|
|
37
|
+
The started span is attached to the OpenTelemetry context, so a capability
|
|
38
|
+
that invokes another capability produces a child span. Attachment is
|
|
39
|
+
per-context: sibling invocations running in separate tasks do not become
|
|
40
|
+
each other's parents. Both callbacks of one invocation run in the same
|
|
41
|
+
task, which is what makes the paired attach and detach balance.
|
|
42
|
+
|
|
43
|
+
Reuse this hook across plans, but register it at most once per plan: a
|
|
44
|
+
second registration would open a second span for one identity, and only
|
|
45
|
+
the first is tracked. Concurrent recording safety belongs to the supplied
|
|
46
|
+
tracer's implementation; no free-threading claim is made here.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
__slots__ = ("_lock", "_spans", "_tracer")
|
|
50
|
+
|
|
51
|
+
def __init__(self, tracer: Tracer) -> None:
|
|
52
|
+
if not isinstance(tracer, Tracer):
|
|
53
|
+
raise TypeError("tracer must be an OpenTelemetry Tracer")
|
|
54
|
+
self._tracer = tracer
|
|
55
|
+
# Maps a runtime invocation identity to its open span and the context
|
|
56
|
+
# token that restores the previous span. Bounded by concurrently open
|
|
57
|
+
# invocations: the runtime emits the terminal event from a ``finally``
|
|
58
|
+
# block, and that callback always removes the entry first.
|
|
59
|
+
self._spans: dict[str, tuple[Span, Token[Context]]] = {}
|
|
60
|
+
self._lock = threading.Lock()
|
|
61
|
+
|
|
62
|
+
def on_invocation_start(self, event: InvocationStartEvent) -> None:
|
|
63
|
+
"""Start a span and make it the current one for nested invocations."""
|
|
64
|
+
span = self._tracer.start_span(
|
|
65
|
+
str(event.capability_id),
|
|
66
|
+
kind=SpanKind.INTERNAL,
|
|
67
|
+
attributes={"agnara.capability.id": str(event.capability_id)},
|
|
68
|
+
)
|
|
69
|
+
token = attach(set_span_in_context(span))
|
|
70
|
+
with self._lock:
|
|
71
|
+
tracked = self._spans.setdefault(event.invocation_id, (span, token))
|
|
72
|
+
if tracked[0] is not span:
|
|
73
|
+
# A duplicate registration of this hook on one plan. Undo this
|
|
74
|
+
# span rather than replacing the tracked one, whose token must be
|
|
75
|
+
# detached in the reverse order it was attached.
|
|
76
|
+
detach(token)
|
|
77
|
+
span.end()
|
|
78
|
+
|
|
79
|
+
def on_invocation_terminal(self, event: InvocationTerminalEvent) -> None:
|
|
80
|
+
"""End the paired span, or ignore a terminal event with no start."""
|
|
81
|
+
with self._lock:
|
|
82
|
+
tracked = self._spans.pop(event.invocation_id, None)
|
|
83
|
+
if tracked is None:
|
|
84
|
+
# The start callback was suppressed by the runtime, or this hook
|
|
85
|
+
# was registered after the invocation began.
|
|
86
|
+
return
|
|
87
|
+
span, token = tracked
|
|
88
|
+
try:
|
|
89
|
+
outcome = event.outcome if event.outcome in _OUTCOMES else "unknown"
|
|
90
|
+
span.set_attribute("agnara.invocation.outcome", outcome)
|
|
91
|
+
if outcome in _ERROR_OUTCOMES:
|
|
92
|
+
# The outcome name is a fixed vocabulary, not error text.
|
|
93
|
+
span.set_attribute(_ERROR_TYPE, outcome)
|
|
94
|
+
span.set_status(Status(StatusCode.ERROR, outcome))
|
|
95
|
+
elif outcome == "success":
|
|
96
|
+
span.set_status(Status(StatusCode.OK))
|
|
97
|
+
finally:
|
|
98
|
+
detach(token)
|
|
99
|
+
span.end()
|