cancontech-tracing 0.0.1__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.
- cancontech_tracing-0.0.1/.env +1 -0
- cancontech_tracing-0.0.1/.env.example +7 -0
- cancontech_tracing-0.0.1/.gitignore +9 -0
- cancontech_tracing-0.0.1/LICENSE +201 -0
- cancontech_tracing-0.0.1/Makefile +24 -0
- cancontech_tracing-0.0.1/PKG-INFO +48 -0
- cancontech_tracing-0.0.1/README.md +30 -0
- cancontech_tracing-0.0.1/__init__.py +0 -0
- cancontech_tracing-0.0.1/docker-compose.yml +26 -0
- cancontech_tracing-0.0.1/pyproject.toml +36 -0
- cancontech_tracing-0.0.1/src/__init__.py +0 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/__init__.py +14 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/_with_span.py +70 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/add_span_attributes.py +67 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/ctracing_otl_span_exporter.py +24 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/helpers.py +69 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/init_tracing.py +166 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/traceable.py +441 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/utils/__init__.py +3 -0
- cancontech_tracing-0.0.1/src/cancontech_tracing/utils/build_traceparent_header.py +53 -0
- cancontech_tracing-0.0.1/src/tests/__init__.py +0 -0
- cancontech_tracing-0.0.1/src/tests/integration/__init__.py +0 -0
- cancontech_tracing-0.0.1/src/tests/integration/test_tracing.py +34 -0
- cancontech_tracing-0.0.1/src/tests/unit/__init__.py +0 -0
- cancontech_tracing-0.0.1/src/tests/unit/test_build_traceparent_header.py +25 -0
- cancontech_tracing-0.0.1/src/tests/unit/test_traceable.py +143 -0
- cancontech_tracing-0.0.1/uv.lock +788 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
PYPI_TOKEN=pypi-AgEIcHlwaS5vcmcCJGQ1MGVkNDI2LTczMjQtNGM0My04MWY0LWIxMDYzZTZjODA4YwACKlszLCJmMzdlMWU2Mi05NzBhLTQ1YzItODUwOC0wZWM4NWFjYjNkYjQiXQAABiCOzWKHmDZBmRsROGu8wGkV5TrU7zz7AnPsfHH1nEpOpQ
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
PYPI_REPOSITORY_URL ?= https://upload.pypi.org/legacy/
|
|
2
|
+
PYPI_TOKEN ?= $(TOKEN)
|
|
3
|
+
|
|
4
|
+
.PHONY: clean build_package check_package publish test
|
|
5
|
+
|
|
6
|
+
clean:
|
|
7
|
+
rm -rf dist build *.egg-info
|
|
8
|
+
|
|
9
|
+
build_package: clean
|
|
10
|
+
uvx --from build pyproject-build --installer uv
|
|
11
|
+
|
|
12
|
+
check_package: build_package
|
|
13
|
+
uvx twine check dist/*
|
|
14
|
+
|
|
15
|
+
publish: check_package
|
|
16
|
+
uvx twine upload \
|
|
17
|
+
--repository-url "$(PYPI_REPOSITORY_URL)" \
|
|
18
|
+
--username __token__ \
|
|
19
|
+
--password "$(PYPI_TOKEN)" \
|
|
20
|
+
dist/* \
|
|
21
|
+
--verbose
|
|
22
|
+
|
|
23
|
+
test:
|
|
24
|
+
uv run pytest
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cancontech_tracing
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: cancontech tracing library
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Requires-Dist: fastapi>=0.111.0
|
|
8
|
+
Requires-Dist: openinference-instrumentation-anthropic==0.1.20
|
|
9
|
+
Requires-Dist: openinference-instrumentation-google-genai>=0.1.12
|
|
10
|
+
Requires-Dist: openinference-instrumentation-openai==0.1.41
|
|
11
|
+
Requires-Dist: openinference-instrumentation==0.1.41
|
|
12
|
+
Requires-Dist: opentelemetry-api>=1.39.0
|
|
13
|
+
Requires-Dist: opentelemetry-exporter-otlp>=1.39.0
|
|
14
|
+
Requires-Dist: opentelemetry-instrumentation-aiohttp-client==0.60b0
|
|
15
|
+
Requires-Dist: opentelemetry-sdk>=1.39.0
|
|
16
|
+
Requires-Dist: pydantic>=2.12.3
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# **What is it?**
|
|
20
|
+
|
|
21
|
+
## CanConTech Tracing
|
|
22
|
+
|
|
23
|
+
Collection of tracing utility functions
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Installation
|
|
27
|
+
|
|
28
|
+
* Install **uv**:
|
|
29
|
+
* Windows: `powershell -c "irm https://astral.sh/uv/install.ps1 | iex"`
|
|
30
|
+
* macOS/Linux: `curl -LsSf https://astral.sh/uv/install.sh | sh`
|
|
31
|
+
* Set env var `UV_NATIVE_TLS=true` to avoid certificate errors
|
|
32
|
+
* Sync environment: `uv sync`. This will create a new default virtual environment (.venv) if it doesn't exist
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Testing
|
|
36
|
+
|
|
37
|
+
#### unit tests
|
|
38
|
+
`uv run pytest src/tests/unit`
|
|
39
|
+
|
|
40
|
+
#### integration tests
|
|
41
|
+
```
|
|
42
|
+
ARIZE_CONTAINER_ID=$(docker run --rm -d -p 6006:6006 -p 4317:4317 -i -t arizephoenix/phoenix:latest)
|
|
43
|
+
|
|
44
|
+
uv run pytest src/tests/integration
|
|
45
|
+
|
|
46
|
+
docker stop $ARIZE_CONTAINER_ID
|
|
47
|
+
|
|
48
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# **What is it?**
|
|
2
|
+
|
|
3
|
+
## CanConTech Tracing
|
|
4
|
+
|
|
5
|
+
Collection of tracing utility functions
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Installation
|
|
9
|
+
|
|
10
|
+
* Install **uv**:
|
|
11
|
+
* Windows: `powershell -c "irm https://astral.sh/uv/install.ps1 | iex"`
|
|
12
|
+
* macOS/Linux: `curl -LsSf https://astral.sh/uv/install.sh | sh`
|
|
13
|
+
* Set env var `UV_NATIVE_TLS=true` to avoid certificate errors
|
|
14
|
+
* Sync environment: `uv sync`. This will create a new default virtual environment (.venv) if it doesn't exist
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Testing
|
|
18
|
+
|
|
19
|
+
#### unit tests
|
|
20
|
+
`uv run pytest src/tests/unit`
|
|
21
|
+
|
|
22
|
+
#### integration tests
|
|
23
|
+
```
|
|
24
|
+
ARIZE_CONTAINER_ID=$(docker run --rm -d -p 6006:6006 -p 4317:4317 -i -t arizephoenix/phoenix:latest)
|
|
25
|
+
|
|
26
|
+
uv run pytest src/tests/integration
|
|
27
|
+
|
|
28
|
+
docker stop $ARIZE_CONTAINER_ID
|
|
29
|
+
|
|
30
|
+
```
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
services:
|
|
2
|
+
phoenix:
|
|
3
|
+
image: arizephoenix/phoenix:latest
|
|
4
|
+
container_name: arize_phoenix
|
|
5
|
+
restart: unless-stopped
|
|
6
|
+
environment:
|
|
7
|
+
PHOENIX_SQL_DATABASE_URL: ${PHOENIX_SQL_DATABASE_URL:-postgresql://phoenix:phoenix@phoenix_postgres:5432/phoenix}
|
|
8
|
+
PHOENIX_ENABLE_AUTH: "true"
|
|
9
|
+
PHOENIX_SECRET: ${PHOENIX_SECRET}
|
|
10
|
+
PHOENIX_DEFAULT_ADMIN_INITIAL_PASSWORD: ${PHOENIX_DEFAULT_ADMIN_INITIAL_PASSWORD}
|
|
11
|
+
PHOENIX_WORKING_DIR: /mnt/data
|
|
12
|
+
ports:
|
|
13
|
+
- "${PHOENIX_PORT:-6006}:6006" # UI + OTLP HTTP
|
|
14
|
+
- "${PHOENIX_GRPC_PORT:-4317}:4317" # OTLP gRPC
|
|
15
|
+
- "${PHOENIX_PROM_PORT:-9090}:9090" # Optional Prometheus
|
|
16
|
+
volumes:
|
|
17
|
+
- phoenix_data:/mnt/data
|
|
18
|
+
networks:
|
|
19
|
+
- dokploy-network
|
|
20
|
+
|
|
21
|
+
volumes:
|
|
22
|
+
phoenix_data:
|
|
23
|
+
|
|
24
|
+
networks:
|
|
25
|
+
dokploy-network:
|
|
26
|
+
external: true
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "cancontech_tracing"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "cancontech tracing library"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"openinference-instrumentation-openai==0.1.41",
|
|
9
|
+
"openinference-instrumentation-google-genai>=0.1.12",
|
|
10
|
+
"openinference-instrumentation-anthropic==0.1.20",
|
|
11
|
+
"openinference-instrumentation==0.1.41",
|
|
12
|
+
"opentelemetry-instrumentation-aiohttp-client==0.60b0",
|
|
13
|
+
"opentelemetry-api>=1.39.0",
|
|
14
|
+
"opentelemetry-exporter-otlp>=1.39.0",
|
|
15
|
+
"opentelemetry-sdk>=1.39.0",
|
|
16
|
+
"pydantic>=2.12.3",
|
|
17
|
+
"fastapi>=0.111.0",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["hatchling"]
|
|
22
|
+
build-backend = "hatchling.build"
|
|
23
|
+
|
|
24
|
+
[tool.ruff]
|
|
25
|
+
src = ["src"]
|
|
26
|
+
target-version = "py311"
|
|
27
|
+
line-length = 120
|
|
28
|
+
|
|
29
|
+
[tool.ruff.lint]
|
|
30
|
+
select = ["E", "F", "B", "I"]
|
|
31
|
+
[tool.uv]
|
|
32
|
+
dev-dependencies = [
|
|
33
|
+
"pytest==8.3.2",
|
|
34
|
+
"pytest-asyncio==1.2.0",
|
|
35
|
+
"ruff>=0.13.0",
|
|
36
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from openinference.semconv.trace import OpenInferenceSpanKindValues
|
|
2
|
+
|
|
3
|
+
from .add_span_attributes import add_current_span_attributes, add_span_attributes
|
|
4
|
+
from .init_tracing import init_tracing
|
|
5
|
+
from .traceable import ML_TRACEPARENT_HEADER_NAME, traceable
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"traceable",
|
|
9
|
+
"ML_TRACEPARENT_HEADER_NAME",
|
|
10
|
+
"add_span_attributes",
|
|
11
|
+
"add_current_span_attributes",
|
|
12
|
+
"init_tracing",
|
|
13
|
+
"OpenInferenceSpanKindValues",
|
|
14
|
+
]
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import traceback
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from opentelemetry import trace as trace_api
|
|
6
|
+
from opentelemetry.util.types import Attributes
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
logger.addHandler(logging.NullHandler())
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class _WithSpan:
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
span: trace_api.Span,
|
|
16
|
+
) -> None:
|
|
17
|
+
self._span = span
|
|
18
|
+
try:
|
|
19
|
+
self._is_finished = not self._span.is_recording()
|
|
20
|
+
except Exception:
|
|
21
|
+
logger.exception("Failed to check if span is recording")
|
|
22
|
+
self._is_finished = True
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def is_finished(self) -> bool:
|
|
26
|
+
return self._is_finished
|
|
27
|
+
|
|
28
|
+
def record_exception(self, exception: Exception) -> None:
|
|
29
|
+
if self._is_finished:
|
|
30
|
+
return
|
|
31
|
+
try:
|
|
32
|
+
self._span.record_exception(exception)
|
|
33
|
+
self._span.set_attribute("exception.stacktrace", traceback.format_exc())
|
|
34
|
+
except Exception:
|
|
35
|
+
logger.exception("Failed to record exception on span")
|
|
36
|
+
|
|
37
|
+
def add_event(self, name: str) -> None:
|
|
38
|
+
if self._is_finished:
|
|
39
|
+
return
|
|
40
|
+
try:
|
|
41
|
+
self._span.add_event(name)
|
|
42
|
+
except Exception:
|
|
43
|
+
logger.exception("Failed to add event to span")
|
|
44
|
+
|
|
45
|
+
def finish_tracing(
|
|
46
|
+
self,
|
|
47
|
+
status: Optional[trace_api.Status] = None,
|
|
48
|
+
attributes: Attributes = None,
|
|
49
|
+
) -> None:
|
|
50
|
+
if attributes is None:
|
|
51
|
+
attributes = {}
|
|
52
|
+
if self._is_finished:
|
|
53
|
+
return
|
|
54
|
+
for key, value in attributes.items():
|
|
55
|
+
if value is None:
|
|
56
|
+
continue
|
|
57
|
+
try:
|
|
58
|
+
self._span.set_attribute(key, value)
|
|
59
|
+
except Exception:
|
|
60
|
+
logger.exception("Failed to set attribute on span")
|
|
61
|
+
if status is not None:
|
|
62
|
+
try:
|
|
63
|
+
self._span.set_status(status=status)
|
|
64
|
+
except Exception:
|
|
65
|
+
logger.exception("Failed to set status code on span")
|
|
66
|
+
try:
|
|
67
|
+
self._span.end()
|
|
68
|
+
except Exception:
|
|
69
|
+
logger.exception("Failed to end span")
|
|
70
|
+
self._is_finished = True
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from typing import Any, Dict
|
|
2
|
+
|
|
3
|
+
from opentelemetry import trace as trace_api
|
|
4
|
+
from opentelemetry.sdk.trace import Span
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def add_span_attributes(span: Span, attributes: Dict[str, Any]):
|
|
8
|
+
"""
|
|
9
|
+
Adds the specified attributes to the given span.
|
|
10
|
+
|
|
11
|
+
This function sets key-value attributes to a tracing span.
|
|
12
|
+
If the span is invalid (i.e., not actively traced), the function does nothing.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
span : Span
|
|
17
|
+
The span to which the attributes will be added.
|
|
18
|
+
|
|
19
|
+
attributes : Dict[str, Any]
|
|
20
|
+
A dictionary containing the attributes to be added to the span.
|
|
21
|
+
Each key is the name of the attribute, and each value is the corresponding value to be set.
|
|
22
|
+
|
|
23
|
+
Example
|
|
24
|
+
-------
|
|
25
|
+
.. code-block:: python
|
|
26
|
+
|
|
27
|
+
from ytracing import add_span_attributes
|
|
28
|
+
|
|
29
|
+
span = get_current_span()
|
|
30
|
+
attributes = {"user_id": "12345", "request_id": "abcde"}
|
|
31
|
+
add_span_attributes(span, attributes)
|
|
32
|
+
"""
|
|
33
|
+
if span is not trace_api.INVALID_SPAN:
|
|
34
|
+
for key, value in attributes.items():
|
|
35
|
+
span.set_attribute(key, value)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def add_current_span_attributes(attributes: Dict[str, Any]):
|
|
39
|
+
"""
|
|
40
|
+
Adds the specified attributes to the current span.
|
|
41
|
+
|
|
42
|
+
This function retrieves the current active tracing span and assigns the given key-value attributes to it.
|
|
43
|
+
It internally uses the `add_span_attributes` function to set these attributes.
|
|
44
|
+
If there is no valid current span, the function does nothing.
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
----------
|
|
48
|
+
attributes : Dict[str, Any]
|
|
49
|
+
A dictionary containing the attributes to be added to the current span.
|
|
50
|
+
Each key is the name of the attribute, and each value is the corresponding value to be set.
|
|
51
|
+
|
|
52
|
+
Example
|
|
53
|
+
-------
|
|
54
|
+
.. code-block:: python
|
|
55
|
+
|
|
56
|
+
from ytracing import add_current_span_attributes
|
|
57
|
+
|
|
58
|
+
attributes = {"method": "GET", "status_code": 200}
|
|
59
|
+
add_current_span_attributes(attributes)
|
|
60
|
+
|
|
61
|
+
Notes
|
|
62
|
+
-----
|
|
63
|
+
It is a convenience wrapper around the `add_span_attributes` function,
|
|
64
|
+
which allows you to avoid manually fetching the current span.
|
|
65
|
+
|
|
66
|
+
"""
|
|
67
|
+
add_span_attributes(trace_api.get_current_span(), attributes)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from typing import Dict, Optional
|
|
2
|
+
|
|
3
|
+
import requests
|
|
4
|
+
from opentelemetry.exporter.otlp.proto.http import Compression
|
|
5
|
+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CtracingOTLSpanExporter(OTLPSpanExporter):
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
user_agent: str,
|
|
12
|
+
endpoint: Optional[str] = None,
|
|
13
|
+
certificate_file: Optional[str] = None,
|
|
14
|
+
headers: Optional[Dict[str, str]] = None,
|
|
15
|
+
timeout: Optional[int] = None,
|
|
16
|
+
compression: Optional[Compression] = None,
|
|
17
|
+
session: Optional[requests.Session] = None,
|
|
18
|
+
):
|
|
19
|
+
super().__init__(endpoint, certificate_file, headers, timeout, compression, session)
|
|
20
|
+
self._session.headers.update(
|
|
21
|
+
{
|
|
22
|
+
"User-Agent": user_agent,
|
|
23
|
+
}
|
|
24
|
+
)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
import dataclasses
|
|
3
|
+
import inspect
|
|
4
|
+
import json
|
|
5
|
+
from typing import Any, AsyncGenerator, Generator, Optional
|
|
6
|
+
|
|
7
|
+
from pydantic_core import to_jsonable_python
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _show_repr_str(obj: Any) -> Optional[str]:
|
|
11
|
+
if isinstance(obj, (Generator, AsyncGenerator)):
|
|
12
|
+
return f"<{obj.__class__.__qualname__} object>"
|
|
13
|
+
if callable(obj):
|
|
14
|
+
try:
|
|
15
|
+
return f"<{obj.__qualname__}{str(inspect.signature(obj))}>"
|
|
16
|
+
except BaseException:
|
|
17
|
+
return f"<{obj.__class__.__qualname__} object>"
|
|
18
|
+
return None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _asdict(obj: Any) -> Any:
|
|
22
|
+
"""
|
|
23
|
+
This is a copy of Python's `_asdict_inner` function (linked below) but modified primarily to
|
|
24
|
+
not throw exceptions for objects that cannot be deep-copied, e.g. Generators.
|
|
25
|
+
https://github.com/python/cpython/blob/b134f47574c36e842253266ecf0d144fb6f3b546/Lib/dataclasses.py#L1332
|
|
26
|
+
""" # noqa: E501
|
|
27
|
+
if dataclasses.is_dataclass(obj):
|
|
28
|
+
result = []
|
|
29
|
+
for f in dataclasses.fields(obj):
|
|
30
|
+
value = _asdict(getattr(obj, f.name))
|
|
31
|
+
result.append((f.name, value))
|
|
32
|
+
return dict(result)
|
|
33
|
+
elif isinstance(obj, tuple) and hasattr(obj, "_fields"):
|
|
34
|
+
return type(obj)(*[_asdict(v) for v in obj])
|
|
35
|
+
elif isinstance(obj, (list, tuple)):
|
|
36
|
+
return type(obj)(_asdict(v) for v in obj)
|
|
37
|
+
elif isinstance(obj, dict):
|
|
38
|
+
return type(obj)((_asdict(k), _asdict(v)) for k, v in obj.items())
|
|
39
|
+
else:
|
|
40
|
+
if repr_str := _show_repr_str(obj):
|
|
41
|
+
return repr_str
|
|
42
|
+
try:
|
|
43
|
+
return copy.deepcopy(obj)
|
|
44
|
+
except BaseException:
|
|
45
|
+
return repr(obj)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _encoder(obj: Any) -> Any:
|
|
49
|
+
if repr_str := _show_repr_str(obj):
|
|
50
|
+
return repr_str
|
|
51
|
+
if dataclasses.is_dataclass(obj):
|
|
52
|
+
return _asdict(obj)
|
|
53
|
+
try:
|
|
54
|
+
return to_jsonable_python(obj)
|
|
55
|
+
except BaseException:
|
|
56
|
+
return repr(obj)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class _Encoder(json.JSONEncoder):
|
|
60
|
+
def __init__(self, **kwargs: Any) -> None:
|
|
61
|
+
kwargs.pop("default", None)
|
|
62
|
+
super().__init__()
|
|
63
|
+
|
|
64
|
+
def default(self, obj: Any) -> Any:
|
|
65
|
+
return _encoder(obj)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def safe_json_dumps(obj: Any, **kwargs: Any) -> str:
|
|
69
|
+
return json.dumps(obj, default=str, ensure_ascii=False, **kwargs)
|