perf(sdk): cache Resource.__hash__; OTLP encoders re-serialize the Resource for every span and log record
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
- 領域
- backend, performance
調査の方向性
Resource.__hash__の実装とResourceのテストを読みます。issueにはテストファイルが指定されていないため、Resourceの等価性とハッシュ化を対象とするテストを探してください。等価性の動作を保ちながら、ハッシュ化を繰り返しても同じ値が返ることを確認するチェックを追加し、Resourceに絞ったテストを実行します。最初の計算後にハッシュが再利用され、既存のResourceの動作に関するテストが引き続き通れば完了です。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 2.7k
- フォーク
- 1.1k
- 平均マージ
- 3日 23時間
- マージ済み PR(30日)
- 20
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
open-telemetry/opentelemetry-python のほかの issue
-
References to Archived open-telemetry/oteps対応中かも @RjyavardhanSingh が 3 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
open-telemetry/opentelemetry-python#5984 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
Autoconfiguration rejects OTEL_EXPORTER_OTLP_PROTOCOL=http/json対応中かも @WendeelMarinho が 4 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
open-telemetry/opentelemetry-python#5981 ·
メンテナーはふだん 2 日以内に返信
-
MeterProvider raises ValueError on empty or uppercase OTEL_METRICS_EXEMPLAR_FILTER対応中かも @bishalde が 10 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
open-telemetry/opentelemetry-python#5948 · コメント 3 件 ·
メンテナーはふだん 2 日以内に返信
-
W3CBaggagePropagator drops a list-member over 4096 bytes that W3C Baggage requires it to propagate対応中かも @codedbyadiiii が 22 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
open-telemetry/opentelemetry-python#5913 ·
メンテナーはふだん 2 日以内に返信
-
otel.sdk.processor.{span,log}.queue.capacity uses synchronous UpDownCounter recorded once in __init__再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
open-telemetry/opentelemetry-python#5891 ·
メンテナーはふだん 2 日以内に返信
open-telemetry/opentelemetry-python の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
litestar-org/sqlspec#946 ·
メンテナーはふだん 1 日以内に返信
-
area:cli area:docs Platform vdr
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
NVIDIA-NeMo/Gym#7192 ·
メンテナーはふだん 2 日以内に返信
-
Bug: CaseInsensitiveDict (event.headers) returns True for both == and != against the same mappingオープンbug triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
aws-powertools/powertools-lambda-python#8948 ·
メンテナーはふだん 1 日以内に返信
-
SofyaBotオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
ai-robots-txt/ai.robots.txt#324 ·
-
[Resource]: Dashbarオープンresource-submission validation-passed
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
hesreallyhim/awesome-claude-code#3821 · コメント 1 件 ·