fastx-opentelemetry 0.1.0__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.
@@ -0,0 +1,22 @@
1
+ .vscode
2
+
3
+ otelcol-fastx/otelcol-fastx
4
+
5
+ output/
6
+
7
+ ./nohup.out
8
+
9
+ *.log
10
+
11
+ **/node_modules
12
+
13
+ .otel-build
14
+
15
+ demo
16
+
17
+ __pycache__
18
+
19
+ dist
20
+
21
+ .coverage
22
+
@@ -0,0 +1,329 @@
1
+ Metadata-Version: 2.4
2
+ Name: fastx-opentelemetry
3
+ Version: 0.1.0
4
+ Summary: Code-based OpenTelemetry auto-instrumentation for Python
5
+ Author-email: jimo <jimo@foxmail.com>
6
+ License: Apache-2.0
7
+ Keywords: auto-instrumentation,observability,opentelemetry,tracing
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.7
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: System :: Monitoring
19
+ Requires-Python: >=3.7
20
+ Requires-Dist: importlib-metadata>=1.0; python_version < '3.8'
21
+ Requires-Dist: opentelemetry-api>=1.22
22
+ Requires-Dist: opentelemetry-instrumentation>=0.43
23
+ Requires-Dist: opentelemetry-sdk>=1.22
24
+ Provides-Extra: all
25
+ Requires-Dist: opentelemetry-exporter-otlp-proto-grpc>=1.22; extra == 'all'
26
+ Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.22; extra == 'all'
27
+ Provides-Extra: otlp-grpc
28
+ Requires-Dist: opentelemetry-exporter-otlp-proto-grpc>=1.22; extra == 'otlp-grpc'
29
+ Provides-Extra: otlp-http
30
+ Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.22; extra == 'otlp-http'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # fastx-opentelemetry
34
+
35
+ 基于代码的 OpenTelemetry 自动增强 SDK 封装。一行代码完成 OpenTelemetry 初始化,自动发现并加载所有已安装的 instrumentation 库。
36
+
37
+ ## 特性
38
+
39
+ - **一行初始化** — `fastx_opentelemetry.init(service_name="my-app")` 完成全部配置
40
+ - **自动发现增强** — 自动检测并加载所有已安装的 `opentelemetry-instrumentation-*` 包
41
+ - **智能建议** — 检测已安装但未增强的库,提示安装对应 instrumentation 包
42
+ - **上下游服务串联** — 自动传播 Baggage 中的服务调用链(`parent.service.name` / `parent.service.names` / `current.service.name`)
43
+ - **Baggage → Span 属性** — 自动将 Baggage 条目复制为 Span 属性,无需手动操作
44
+ - **内置采样器** — 提供 `RulesBasedSampler`(按规则采样)和 `RateLimitingSampler`(限流采样)
45
+ - **自动资源检测** — 自动采集 hostname、IP、PID、instance ID 以及云平台属性(`cloud.platform`、`cloud.region` 等)
46
+ - **环境变量兼容** — 完全兼容 OpenTelemetry 标准环境变量
47
+ - **信号级端点** — 自动为 traces/metrics/logs 拼接 `/v1/traces`、`/v1/metrics`、`/v1/logs` 路径
48
+
49
+ ## 安装
50
+
51
+ ```bash
52
+ # 基础安装
53
+ pip install fastx-opentelemetry
54
+
55
+ # 带 OTLP gRPC 导出器
56
+ pip install fastx-opentelemetry[otlp-grpc]
57
+
58
+ # 带 OTLP HTTP 导出器
59
+ pip install fastx-opentelemetry[otlp-http]
60
+
61
+ # 全部安装
62
+ pip install fastx-opentelemetry[all]
63
+ ```
64
+
65
+ ## 快速开始
66
+
67
+ ### 最简用法
68
+
69
+ ```python
70
+ import fastx_opentelemetry
71
+
72
+ fastx_opentelemetry.init()
73
+ ```
74
+
75
+ 配合环境变量:
76
+
77
+ ```bash
78
+ export OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4318
79
+ export OTEL_SERVICE_NAME=my-app
80
+ python my_app.py
81
+ ```
82
+
83
+ ### Flask 应用
84
+
85
+ ```python
86
+ import fastx_opentelemetry
87
+
88
+ # 必须在创建 Flask app 之前初始化
89
+ fastx_opentelemetry.init(service_name="my-flask-app")
90
+
91
+ from flask import Flask
92
+ app = Flask(__name__)
93
+
94
+ @app.route("/hello")
95
+ def hello():
96
+ return {"message": "Hello, World!"}
97
+ ```
98
+
99
+ ### Django 应用
100
+
101
+ 在 `settings.py` 中添加:
102
+
103
+ ```python
104
+ import fastx_opentelemetry
105
+ fastx_opentelemetry.init(service_name="my-django-app")
106
+ ```
107
+
108
+ ### FastAPI 应用
109
+
110
+ ```python
111
+ import fastx_opentelemetry
112
+
113
+ fastx_opentelemetry.init(service_name="my-fastapi-app")
114
+
115
+ from fastapi import FastAPI
116
+ app = FastAPI()
117
+
118
+ @app.get("/hello")
119
+ async def hello():
120
+ return {"message": "Hello, World!"}
121
+ ```
122
+
123
+ ## 高级用法
124
+
125
+ ### 自定义采样器
126
+
127
+ ```python
128
+ import fastx_opentelemetry
129
+ from fastx_opentelemetry import RulesBasedSampler, SamplingRule, RateLimitingSampler
130
+ from opentelemetry.sdk.trace.sampling import ALWAYS_ON, ALWAYS_OFF, TraceIdRatioBased
131
+
132
+ sampler = RulesBasedSampler(
133
+ rules=[
134
+ SamplingRule(match_name="/health.*", sampler=ALWAYS_ON),
135
+ SamplingRule(match_name="/static/.*", sampler=ALWAYS_OFF),
136
+ SamplingRule(match_name="/api/.*", sampler=TraceIdRatioBased(0.1)),
137
+ ],
138
+ default_sampler=RateLimitingSampler(max_spans_per_second=50),
139
+ )
140
+
141
+ fastx_opentelemetry.init(
142
+ service_name="my-app",
143
+ sampler=sampler,
144
+ )
145
+ ```
146
+
147
+ ### 完整配置
148
+
149
+ ```python
150
+ import fastx_opentelemetry
151
+ from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
152
+
153
+ console_processor = SimpleSpanProcessor(ConsoleSpanExporter())
154
+
155
+ loaded = fastx_opentelemetry.init(
156
+ service_name="my-app",
157
+ endpoint="http://collector:4317", # 自动拼接 /v1/traces, /v1/metrics, /v1/logs
158
+ protocol="grpc",
159
+ compression="gzip",
160
+ headers={"Authorization": "Bearer token123"},
161
+ extra_resource_attributes={
162
+ "deployment.environment": "production",
163
+ "service.version": "1.2.3",
164
+ },
165
+ span_processors=[console_processor],
166
+ disabled_instrumentations=["sqlite3"],
167
+ )
168
+
169
+ print(f"Loaded instrumentors: {loaded}")
170
+ ```
171
+
172
+ ### 禁用特定增强
173
+
174
+ ```python
175
+ fastx_opentelemetry.init(
176
+ service_name="my-app",
177
+ disabled_instrumentations=["sqlite3", "urllib"],
178
+ )
179
+ ```
180
+
181
+ 或通过环境变量:
182
+
183
+ ```bash
184
+ export OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=sqlite3,urllib
185
+ ```
186
+
187
+ 禁用所有增强(仅初始化 SDK,不加载任何 instrumentor):
188
+
189
+ ```python
190
+ fastx_opentelemetry.init(
191
+ service_name="my-app",
192
+ disabled_instrumentations=["*"],
193
+ )
194
+ ```
195
+
196
+ ### 上下游服务串联
197
+
198
+ `FastxBaggagePropagator` 和 `FastxSpanProcessor` 在 `init()` 时自动启用,无需额外配置。它们实现了与 Go 端 `fastx_otel.go` 一致的 Baggage 传播逻辑:
199
+
200
+ - **`FastxBaggagePropagator`** — 包装 W3C Baggage 传播器,提取时自动转换服务链:
201
+ - 将 `current.service.name` 移入 `parent.service.name`
202
+ - 将 `current.service.name` 追加到 `parent.service.names`(以 `++` 分隔,去重)
203
+ - 将 `current.service.name` 设置为本地服务名
204
+ - **`FastxSpanProcessor`** — 在 Span 开始时,自动将所有 Baggage 条目复制为 Span 属性
205
+
206
+ 这意味着在微服务调用链中,每个服务的 Span 都会携带完整的上游服务链信息,便于在后端(如 Jaeger、Grafana)中追踪调用路径。
207
+
208
+ ## 环境变量
209
+
210
+ | 变量 | 说明 | 默认值 |
211
+ |------|------|--------|
212
+ | `FASTX_PYTHON_INSTRUMENTATION_ENABLED` | 全局开关,`false` 时跳过全部初始化 | `true` |
213
+ | `OTEL_SERVICE_NAME` | 服务名称 | `unknown_service` |
214
+ | `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP Collector 地址(自动拼接信号路径) | — |
215
+ | `OTEL_EXPORTER_OTLP_PROTOCOL` | 传输协议 `http/protobuf` 或 `grpc` | `http/protobuf` |
216
+ | `OTEL_EXPORTER_OTLP_COMPRESSION` | 压缩方式 `gzip`/`deflate`/`none` | `gzip` |
217
+ | `OTEL_EXPORTER_OTLP_TRACES_HEADERS` | 导出器请求头 | — |
218
+ | `OTEL_TRACES_SAMPLER` | 采样器名称 | `parentbased_always_on` |
219
+ | `OTEL_PYTHON_DISABLED_INSTRUMENTATIONS` | 禁用的增强列表(逗号分隔,`*` 禁用全部) | — |
220
+ | `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` | 是否启用日志自动增强 | `true` |
221
+ | `IP_ADDR` | 自动检测为 `host.ip` 资源属性 | 本地非回环 IP |
222
+ | `CLOUD_TYPE` | 自动检测为 `cloud.platform` 资源属性 | — |
223
+ | `REGION_CODE` | 自动检测为 `cloud.region` 资源属性 | — |
224
+ | `AZ_CODE` | 自动检测为 `cloud.availability` 资源属性 | — |
225
+ | `env_cd` | 自动检测为 `deployment.environment.name` 资源属性 | — |
226
+
227
+ ## 安装增强库
228
+
229
+ `fastx-opentelemetry` 会自动检测已安装的库并加载对应的 instrumentation。你需要手动安装需要的 instrumentation 包:
230
+
231
+ ```bash
232
+ # Web 框架
233
+ pip install opentelemetry-instrumentation-flask
234
+ pip install opentelemetry-instrumentation-django
235
+ pip install opentelemetry-instrumentation-fastapi
236
+
237
+ # HTTP 客户端
238
+ pip install opentelemetry-instrumentation-requests
239
+ pip install opentelemetry-instrumentation-httpx
240
+ pip install opentelemetry-instrumentation-urllib
241
+
242
+ # 数据库
243
+ pip install opentelemetry-instrumentation-sqlalchemy
244
+ pip install opentelemetry-instrumentation-psycopg2
245
+ pip install opentelemetry-instrumentation-redis
246
+ pip install opentelemetry-instrumentation-pymongo
247
+
248
+ # 消息队列
249
+ pip install opentelemetry-instrumentation-celery
250
+ pip install opentelemetry-instrumentation-kafka-python
251
+ ```
252
+
253
+ 检查已安装的增强库:
254
+
255
+ ```bash
256
+ pip list | grep opentelemetry-instrumentation
257
+ ```
258
+
259
+ ## API 参考
260
+
261
+ ### `fastx_opentelemetry.init(**kwargs)`
262
+
263
+ 初始化 OpenTelemetry SDK 并加载所有已安装的 instrumentor。
264
+
265
+ **参数:**
266
+
267
+ | 参数 | 类型 | 说明 |
268
+ |------|------|------|
269
+ | `service_name` | `str \| None` | 服务名称,回退到 `OTEL_SERVICE_NAME` |
270
+ | `endpoint` | `str \| None` | OTLP Collector 地址(自动拼接信号路径) |
271
+ | `protocol` | `str \| None` | `http/protobuf` 或 `grpc` |
272
+ | `compression` | `str \| None` | `gzip`、`deflate` 或 `none` |
273
+ | `headers` | `dict \| None` | OTLP 导出器请求头 |
274
+ | `extra_resource_attributes` | `dict \| None` | 额外的资源属性 |
275
+ | `span_processors` | `list \| None` | 自定义 SpanProcessor 列表 |
276
+ | `sampler` | `Sampler \| None` | 自定义采样器实例 |
277
+ | `disabled_instrumentations` | `list \| None` | 禁用的 instrumentor 名称列表,`"*"` 禁用全部 |
278
+
279
+ **返回值:** `list[str]` — 成功加载的 instrumentor 名称列表。
280
+
281
+ ### `fastx_opentelemetry.list_available_instrumentors()`
282
+
283
+ 返回所有已安装的 instrumentor 名称列表。
284
+
285
+ ### `fastx_opentelemetry.suggest_missing_instrumentations(loaded)`
286
+
287
+ 检查已安装但未增强的库,返回建议安装的 instrumentation 包名列表。
288
+
289
+ ### 采样器
290
+
291
+ #### `RulesBasedSampler(rules, default_sampler=None)`
292
+
293
+ 按规则匹配 span 名称或属性,委托给不同的采样器。
294
+
295
+ #### `SamplingRule(sampler, match_name=None, match_attributes=None)`
296
+
297
+ 单条采样规则。`match_name` 为正则表达式匹配 span 名称,`match_attributes` 为属性键值对匹配。
298
+
299
+ #### `RateLimitingSampler(max_spans_per_second)`
300
+
301
+ 基于令牌桶算法的限流采样器,限制每秒采样的 span 数量。
302
+
303
+ ### Baggage 传播与 Span 处理
304
+
305
+ #### `FastxBaggagePropagator`
306
+
307
+ 包装 W3C Baggage 传播器,在 `extract` 时自动转换服务调用链。`init()` 时自动设置为全局传播器。
308
+
309
+ #### `FastxSpanProcessor`
310
+
311
+ 在 Span `on_start` 时将所有 Baggage 条目复制为 Span 属性。`init()` 时自动添加到 TracerProvider。
312
+
313
+ ## 本地开发
314
+
315
+ ```bash
316
+ # 安装依赖
317
+ pip install -e ".[all]"
318
+
319
+ # 运行示例
320
+ export OTEL_LOG_LEVEL=debug
321
+ export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318
322
+ export OTEL_SERVICE_NAME=my-app
323
+
324
+ python examples/app.py
325
+ ```
326
+
327
+ ## License
328
+
329
+ Apache-2.0
@@ -0,0 +1,297 @@
1
+ # fastx-opentelemetry
2
+
3
+ 基于代码的 OpenTelemetry 自动增强 SDK 封装。一行代码完成 OpenTelemetry 初始化,自动发现并加载所有已安装的 instrumentation 库。
4
+
5
+ ## 特性
6
+
7
+ - **一行初始化** — `fastx_opentelemetry.init(service_name="my-app")` 完成全部配置
8
+ - **自动发现增强** — 自动检测并加载所有已安装的 `opentelemetry-instrumentation-*` 包
9
+ - **智能建议** — 检测已安装但未增强的库,提示安装对应 instrumentation 包
10
+ - **上下游服务串联** — 自动传播 Baggage 中的服务调用链(`parent.service.name` / `parent.service.names` / `current.service.name`)
11
+ - **Baggage → Span 属性** — 自动将 Baggage 条目复制为 Span 属性,无需手动操作
12
+ - **内置采样器** — 提供 `RulesBasedSampler`(按规则采样)和 `RateLimitingSampler`(限流采样)
13
+ - **自动资源检测** — 自动采集 hostname、IP、PID、instance ID 以及云平台属性(`cloud.platform`、`cloud.region` 等)
14
+ - **环境变量兼容** — 完全兼容 OpenTelemetry 标准环境变量
15
+ - **信号级端点** — 自动为 traces/metrics/logs 拼接 `/v1/traces`、`/v1/metrics`、`/v1/logs` 路径
16
+
17
+ ## 安装
18
+
19
+ ```bash
20
+ # 基础安装
21
+ pip install fastx-opentelemetry
22
+
23
+ # 带 OTLP gRPC 导出器
24
+ pip install fastx-opentelemetry[otlp-grpc]
25
+
26
+ # 带 OTLP HTTP 导出器
27
+ pip install fastx-opentelemetry[otlp-http]
28
+
29
+ # 全部安装
30
+ pip install fastx-opentelemetry[all]
31
+ ```
32
+
33
+ ## 快速开始
34
+
35
+ ### 最简用法
36
+
37
+ ```python
38
+ import fastx_opentelemetry
39
+
40
+ fastx_opentelemetry.init()
41
+ ```
42
+
43
+ 配合环境变量:
44
+
45
+ ```bash
46
+ export OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4318
47
+ export OTEL_SERVICE_NAME=my-app
48
+ python my_app.py
49
+ ```
50
+
51
+ ### Flask 应用
52
+
53
+ ```python
54
+ import fastx_opentelemetry
55
+
56
+ # 必须在创建 Flask app 之前初始化
57
+ fastx_opentelemetry.init(service_name="my-flask-app")
58
+
59
+ from flask import Flask
60
+ app = Flask(__name__)
61
+
62
+ @app.route("/hello")
63
+ def hello():
64
+ return {"message": "Hello, World!"}
65
+ ```
66
+
67
+ ### Django 应用
68
+
69
+ 在 `settings.py` 中添加:
70
+
71
+ ```python
72
+ import fastx_opentelemetry
73
+ fastx_opentelemetry.init(service_name="my-django-app")
74
+ ```
75
+
76
+ ### FastAPI 应用
77
+
78
+ ```python
79
+ import fastx_opentelemetry
80
+
81
+ fastx_opentelemetry.init(service_name="my-fastapi-app")
82
+
83
+ from fastapi import FastAPI
84
+ app = FastAPI()
85
+
86
+ @app.get("/hello")
87
+ async def hello():
88
+ return {"message": "Hello, World!"}
89
+ ```
90
+
91
+ ## 高级用法
92
+
93
+ ### 自定义采样器
94
+
95
+ ```python
96
+ import fastx_opentelemetry
97
+ from fastx_opentelemetry import RulesBasedSampler, SamplingRule, RateLimitingSampler
98
+ from opentelemetry.sdk.trace.sampling import ALWAYS_ON, ALWAYS_OFF, TraceIdRatioBased
99
+
100
+ sampler = RulesBasedSampler(
101
+ rules=[
102
+ SamplingRule(match_name="/health.*", sampler=ALWAYS_ON),
103
+ SamplingRule(match_name="/static/.*", sampler=ALWAYS_OFF),
104
+ SamplingRule(match_name="/api/.*", sampler=TraceIdRatioBased(0.1)),
105
+ ],
106
+ default_sampler=RateLimitingSampler(max_spans_per_second=50),
107
+ )
108
+
109
+ fastx_opentelemetry.init(
110
+ service_name="my-app",
111
+ sampler=sampler,
112
+ )
113
+ ```
114
+
115
+ ### 完整配置
116
+
117
+ ```python
118
+ import fastx_opentelemetry
119
+ from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
120
+
121
+ console_processor = SimpleSpanProcessor(ConsoleSpanExporter())
122
+
123
+ loaded = fastx_opentelemetry.init(
124
+ service_name="my-app",
125
+ endpoint="http://collector:4317", # 自动拼接 /v1/traces, /v1/metrics, /v1/logs
126
+ protocol="grpc",
127
+ compression="gzip",
128
+ headers={"Authorization": "Bearer token123"},
129
+ extra_resource_attributes={
130
+ "deployment.environment": "production",
131
+ "service.version": "1.2.3",
132
+ },
133
+ span_processors=[console_processor],
134
+ disabled_instrumentations=["sqlite3"],
135
+ )
136
+
137
+ print(f"Loaded instrumentors: {loaded}")
138
+ ```
139
+
140
+ ### 禁用特定增强
141
+
142
+ ```python
143
+ fastx_opentelemetry.init(
144
+ service_name="my-app",
145
+ disabled_instrumentations=["sqlite3", "urllib"],
146
+ )
147
+ ```
148
+
149
+ 或通过环境变量:
150
+
151
+ ```bash
152
+ export OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=sqlite3,urllib
153
+ ```
154
+
155
+ 禁用所有增强(仅初始化 SDK,不加载任何 instrumentor):
156
+
157
+ ```python
158
+ fastx_opentelemetry.init(
159
+ service_name="my-app",
160
+ disabled_instrumentations=["*"],
161
+ )
162
+ ```
163
+
164
+ ### 上下游服务串联
165
+
166
+ `FastxBaggagePropagator` 和 `FastxSpanProcessor` 在 `init()` 时自动启用,无需额外配置。它们实现了与 Go 端 `fastx_otel.go` 一致的 Baggage 传播逻辑:
167
+
168
+ - **`FastxBaggagePropagator`** — 包装 W3C Baggage 传播器,提取时自动转换服务链:
169
+ - 将 `current.service.name` 移入 `parent.service.name`
170
+ - 将 `current.service.name` 追加到 `parent.service.names`(以 `++` 分隔,去重)
171
+ - 将 `current.service.name` 设置为本地服务名
172
+ - **`FastxSpanProcessor`** — 在 Span 开始时,自动将所有 Baggage 条目复制为 Span 属性
173
+
174
+ 这意味着在微服务调用链中,每个服务的 Span 都会携带完整的上游服务链信息,便于在后端(如 Jaeger、Grafana)中追踪调用路径。
175
+
176
+ ## 环境变量
177
+
178
+ | 变量 | 说明 | 默认值 |
179
+ |------|------|--------|
180
+ | `FASTX_PYTHON_INSTRUMENTATION_ENABLED` | 全局开关,`false` 时跳过全部初始化 | `true` |
181
+ | `OTEL_SERVICE_NAME` | 服务名称 | `unknown_service` |
182
+ | `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP Collector 地址(自动拼接信号路径) | — |
183
+ | `OTEL_EXPORTER_OTLP_PROTOCOL` | 传输协议 `http/protobuf` 或 `grpc` | `http/protobuf` |
184
+ | `OTEL_EXPORTER_OTLP_COMPRESSION` | 压缩方式 `gzip`/`deflate`/`none` | `gzip` |
185
+ | `OTEL_EXPORTER_OTLP_TRACES_HEADERS` | 导出器请求头 | — |
186
+ | `OTEL_TRACES_SAMPLER` | 采样器名称 | `parentbased_always_on` |
187
+ | `OTEL_PYTHON_DISABLED_INSTRUMENTATIONS` | 禁用的增强列表(逗号分隔,`*` 禁用全部) | — |
188
+ | `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` | 是否启用日志自动增强 | `true` |
189
+ | `IP_ADDR` | 自动检测为 `host.ip` 资源属性 | 本地非回环 IP |
190
+ | `CLOUD_TYPE` | 自动检测为 `cloud.platform` 资源属性 | — |
191
+ | `REGION_CODE` | 自动检测为 `cloud.region` 资源属性 | — |
192
+ | `AZ_CODE` | 自动检测为 `cloud.availability` 资源属性 | — |
193
+ | `env_cd` | 自动检测为 `deployment.environment.name` 资源属性 | — |
194
+
195
+ ## 安装增强库
196
+
197
+ `fastx-opentelemetry` 会自动检测已安装的库并加载对应的 instrumentation。你需要手动安装需要的 instrumentation 包:
198
+
199
+ ```bash
200
+ # Web 框架
201
+ pip install opentelemetry-instrumentation-flask
202
+ pip install opentelemetry-instrumentation-django
203
+ pip install opentelemetry-instrumentation-fastapi
204
+
205
+ # HTTP 客户端
206
+ pip install opentelemetry-instrumentation-requests
207
+ pip install opentelemetry-instrumentation-httpx
208
+ pip install opentelemetry-instrumentation-urllib
209
+
210
+ # 数据库
211
+ pip install opentelemetry-instrumentation-sqlalchemy
212
+ pip install opentelemetry-instrumentation-psycopg2
213
+ pip install opentelemetry-instrumentation-redis
214
+ pip install opentelemetry-instrumentation-pymongo
215
+
216
+ # 消息队列
217
+ pip install opentelemetry-instrumentation-celery
218
+ pip install opentelemetry-instrumentation-kafka-python
219
+ ```
220
+
221
+ 检查已安装的增强库:
222
+
223
+ ```bash
224
+ pip list | grep opentelemetry-instrumentation
225
+ ```
226
+
227
+ ## API 参考
228
+
229
+ ### `fastx_opentelemetry.init(**kwargs)`
230
+
231
+ 初始化 OpenTelemetry SDK 并加载所有已安装的 instrumentor。
232
+
233
+ **参数:**
234
+
235
+ | 参数 | 类型 | 说明 |
236
+ |------|------|------|
237
+ | `service_name` | `str \| None` | 服务名称,回退到 `OTEL_SERVICE_NAME` |
238
+ | `endpoint` | `str \| None` | OTLP Collector 地址(自动拼接信号路径) |
239
+ | `protocol` | `str \| None` | `http/protobuf` 或 `grpc` |
240
+ | `compression` | `str \| None` | `gzip`、`deflate` 或 `none` |
241
+ | `headers` | `dict \| None` | OTLP 导出器请求头 |
242
+ | `extra_resource_attributes` | `dict \| None` | 额外的资源属性 |
243
+ | `span_processors` | `list \| None` | 自定义 SpanProcessor 列表 |
244
+ | `sampler` | `Sampler \| None` | 自定义采样器实例 |
245
+ | `disabled_instrumentations` | `list \| None` | 禁用的 instrumentor 名称列表,`"*"` 禁用全部 |
246
+
247
+ **返回值:** `list[str]` — 成功加载的 instrumentor 名称列表。
248
+
249
+ ### `fastx_opentelemetry.list_available_instrumentors()`
250
+
251
+ 返回所有已安装的 instrumentor 名称列表。
252
+
253
+ ### `fastx_opentelemetry.suggest_missing_instrumentations(loaded)`
254
+
255
+ 检查已安装但未增强的库,返回建议安装的 instrumentation 包名列表。
256
+
257
+ ### 采样器
258
+
259
+ #### `RulesBasedSampler(rules, default_sampler=None)`
260
+
261
+ 按规则匹配 span 名称或属性,委托给不同的采样器。
262
+
263
+ #### `SamplingRule(sampler, match_name=None, match_attributes=None)`
264
+
265
+ 单条采样规则。`match_name` 为正则表达式匹配 span 名称,`match_attributes` 为属性键值对匹配。
266
+
267
+ #### `RateLimitingSampler(max_spans_per_second)`
268
+
269
+ 基于令牌桶算法的限流采样器,限制每秒采样的 span 数量。
270
+
271
+ ### Baggage 传播与 Span 处理
272
+
273
+ #### `FastxBaggagePropagator`
274
+
275
+ 包装 W3C Baggage 传播器,在 `extract` 时自动转换服务调用链。`init()` 时自动设置为全局传播器。
276
+
277
+ #### `FastxSpanProcessor`
278
+
279
+ 在 Span `on_start` 时将所有 Baggage 条目复制为 Span 属性。`init()` 时自动添加到 TracerProvider。
280
+
281
+ ## 本地开发
282
+
283
+ ```bash
284
+ # 安装依赖
285
+ pip install -e ".[all]"
286
+
287
+ # 运行示例
288
+ export OTEL_LOG_LEVEL=debug
289
+ export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318
290
+ export OTEL_SERVICE_NAME=my-app
291
+
292
+ python examples/app.py
293
+ ```
294
+
295
+ ## License
296
+
297
+ Apache-2.0