perf(sdk): cache Resource.__hash__; OTLP encoders re-serialize the Resource for every span and log record
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- backend, performance
Hướng nghiên cứu
Đọc phần triển khai Resource.__hash__ và các bài kiểm thử Resource; issue không nêu tên tệp kiểm thử, vì vậy hãy tìm các bài kiểm thử bao quát tính bằng nhau và việc băm của Resource. Thêm một kiểm tra xác nhận việc băm lặp lại trả về cùng một giá trị mà vẫn giữ nguyên hành vi so sánh bằng, rồi chạy các bài kiểm thử tập trung vào Resource. Hoàn thành khi giá trị hash được tái sử dụng sau lần tính đầu tiên và các bài kiểm thử hành vi hiện có của Resource vẫn đạt.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Is your feature request related to a problem?
Resource.__hash__ recomputes its hash from a JSON dump of all attributes on every call:
def __hash__(self) -> int:
return hash(
f"{dumps(self._attributes.copy(), sort_keys=True, default=_json_default)}"
f"|{self._schema_url}|{self._schema_url_conflict}"
)
The OTLP encoders group records by resource, using the Resource as a dict key once per record:
_encode_resource_spans:sdk_resource_spans[sdk_span.resource][sdk_instrumentation].append(pb2_span)_encode_resource_logs:sdk_resource_logs[readable_log.resource][sdk_instrumentation].append(pb2_log)
So every exported span and log record copies, sorts and JSON-encodes the entire resource, even though a process almost always has a single, unchanging Resource. With the usual resource attributes (service, host, process, k8s/cloud detectors), this is a noticeable share of export CPU in high-throughput services.
Observed on opentelemetry-sdk 1.41.0; main has the same uncached implementation. No existing issue found (related but distinct: #5576/#5577, #5591).
Describe the solution you'd like
I'd like Resource to compute its hash once and reuse it.
A Resource is effectively immutable after construction (its attributes are a BoundedAttributes, there is no public mutator, and merge returns a new Resource), so the hash can be computed eagerly in __init__ or lazily on the first __hash__ call. Lazy caching keeps construction cost unchanged:
def __hash__(self) -> int:
cached = self.__dict__.get("_hash")
if cached is None:
cached = self._hash = hash(
f"{dumps(self._attributes.copy(), sort_keys=True, default=_json_default)}"
f"|{self._schema_url}|{self._schema_url_conflict}"
)
return cached
Describe alternatives you've considered
- Cache in the OTLP encoders instead, e.g. group by
id(resource)or keep a per-batch resource → key map. That fixes only the OTLP path; other exporters and processors that hash aResourcestill pay the cost, so fixing it inResourceitself is simpler and benefits every consumer. - Eagerly compute the hash in
Resource.__init__. It works too, but it charges every construction, including the temporary resources made during detector merging, which may never be hashed. - Leave it to users to monkeypatch
Resource.__hash__. That's what we do today; it works but depends on private attributes and could break on upgrades.
Additional Context
- Similar fix in the Collector: open-telemetry/opentelemetry-collector-contrib#50955 caches the resource attribute hash per resource in the signaltometrics connector instead of rehashing on every signal.
- Related Resource hashing issues in this repo (correctness, not performance): #5576 / #5577 (bytes-valued attributes), #5591.
- Versions: observed on opentelemetry-sdk 1.41.0;
mainhas the same uncached__hash__.
Would you like to implement a fix?
None
Tip
React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.
- Ngôn ngữ chính
- Python
- Star
- 2.7k
- Fork
- 1.1k
- Merge trung bình
- 3 ngày 23 giờ
- Pull request đã merge (30 ngày)
- 20
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của open-telemetry/opentelemetry-python
-
References to Archived open-telemetry/otepsCó thể đã có người làm @RjyavardhanSingh đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
open-telemetry/opentelemetry-python#5984 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Autoconfiguration rejects OTEL_EXPORTER_OTLP_PROTOCOL=http/jsonCó thể đã có người làm @WendeelMarinho đã nhận 4 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
open-telemetry/opentelemetry-python#5981 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
MeterProvider raises ValueError on empty or uppercase OTEL_METRICS_EXEMPLAR_FILTERCó thể đã có người làm @bishalde đã nhận 10 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
open-telemetry/opentelemetry-python#5948 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
W3CBaggagePropagator drops a list-member over 4096 bytes that W3C Baggage requires it to propagateCó thể đã có người làm @codedbyadiiii đã nhận 22 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
open-telemetry/opentelemetry-python#5913 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
otel.sdk.processor.{span,log}.queue.capacity uses synchronous UpDownCounter recorded once in __init__Có thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
open-telemetry/opentelemetry-python#5891 ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của open-telemetry/opentelemetry-python
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
litestar-org/sqlspec#946 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area:cli area:docs Platform vdr
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
NVIDIA-NeMo/Gym#7192 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Bug: CaseInsensitiveDict (event.headers) returns True for both == and != against the same mappingĐang mởbug triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
aws-powertools/powertools-lambda-python#8948 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
SofyaBotĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
ai-robots-txt/ai.robots.txt#324 ·
-
[Resource]: DashbarĐang mởresource-submission validation-passed
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
hesreallyhim/awesome-claude-code#3821 · 1 bình luận ·