logging: StructuredLogHandler writes invalid JSON when trace or span_id contains " or \
維護者通常 2 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
該 issue 指向 packages/google-cloud-logging/google/cloud/logging_v2/handlers/structured_log.py 和 handlers.py;先閱讀 StructuredLogHandler 如何格式化及篩選 trace 與 span 值。使用提供的 filter() 和 format() 範例重現失敗,接著檢查相關的 handler 測試。完成標準是格式化後的輸出可解析為 JSON,並保留包含引號或反斜線的 trace 和 span 字串。
由索引模型根據 Issue 內容生成。
描述
Determine this is the right repository
- I determined this is the correct repository in which to report this bug.
Summary of the issue
StructuredLogHandler writes invalid JSON when a trace or span id contains " or \. Its format string puts %(_trace_str)s and %(_span_id_str)s inside JSON quotes (structured_log.py#L33), and the filter copies the raw values (handlers.py#L111) without JSON escaping. The handler's own X-Cloud-Trace-Context parser accepts only [\w-]+ (_helpers.py#L195), so the default path is safe. Values set explicitly (extra={"trace": ...}, a logging filter, a framework integration) go into the line as-is, and Cloud Logging receives one broken text line instead of a structured entry.
Expected: valid JSON with both strings preserved. Actual: json.loads fails (Expecting ',' delimiter for the quote, Invalid \escape for the backslash).
API client name and version
google-cloud-logging 3.17.0 (also 3.12.1)
Reproduction steps: code
No client, credentials or network: only filter() and format() run.
"""Explicit trace/span strings must round-trip through structured JSON logging."""
import json
import logging
import sys
import traceback
from importlib.metadata import version
control = True
try:
print("google-cloud-logging", version("google-cloud-logging"), flush=True)
from google.cloud.logging_v2.handlers import StructuredLogHandler
handler = StructuredLogHandler()
def encode(trace_value, span_value):
record = logging.LogRecord("repro", logging.INFO, "repro.py", 1, "hello", (), None)
record.trace = trace_value
record.span_id = span_value
assert handler.filter(record)
return handler.format(record) # No emit(), client, credentials, or network.
trace_key = "logging.googleapis.com/trace"
span_key = "logging.googleapis.com/spanId"
decoded = json.loads(encode("projects/p/traces/abc", "123"))
assert decoded[trace_key] == "projects/p/traces/abc" and decoded[span_key] == "123"
print("CONTROL PASSED: plain trace/span values round-trip through valid JSON")
control = False
failures = []
for label, trace_value, span_value in (
("trace quote", 'projects/p/traces/a"b', "123"),
("span backslash", "projects/p/traces/abc", r"bad\span"),
):
try:
decoded = json.loads(encode(trace_value, span_value))
except json.JSONDecodeError as error:
failures.append(f"{label}: {error.msg}")
else:
if decoded[trace_key] != trace_value or decoded[span_key] != span_value:
failures.append(f"{label}: value corrupted")
handler.close()
prefix = "BUG REPRODUCED: " if failures else "NOT REPRODUCED: "
print(prefix + f"escaping failures={failures}; expected valid JSON preserving both strings")
except Exception:
traceback.print_exc()
if control:
print("CONTROL FAILED")
sys.exit(2 if control else 1)
Output:
google-cloud-logging 3.17.0
CONTROL PASSED: plain trace/span values round-trip through valid JSON
BUG REPRODUCED: escaping failures=["trace quote: Expecting ',' delimiter", 'span backslash: Invalid \\escape']; expected valid JSON preserving both strings
OS & version + platform
Linux
Python environment
Python 3.11
Additional context
Escaping these two fields like the message (json.dumps(value)[1:-1]) would fix it, or validating them against the same pattern the header parser uses.
- 主要語言
- Python
- 星號
- 5.4k
- 分支
- 1.8k
- 平均合併
- 2 天 20 小時
- 30 天內合併 PR
- 140
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
googleapis/google-cloud-python 的其他 Issue
-
firestore: field path with a quoted name ending in a backslash is misparsed可能已有人在做 @sebxt 於 28 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
googleapis/google-cloud-python#18771 ·
維護者通常 2 天內回覆
-
api: spanner type: feature request
難度 2/5 1-3 小時 新手友好度 68/100
googleapis/google-cloud-python#18584 ·
維護者通常 2 天內回覆
-
auth: `impersonated_credentials` loses `subject` on copy, so `with_scopes()` and `with_quota_project()` turn off domain wide delegation可能已有人在做 @Om-singhaI 於 57 天前認領。 未關閉auth priority: p2 type: bug
難度 2/5 1-3 小時 新手友好度 88/100
googleapis/google-cloud-python#18428 ·
維護者通常 2 天內回覆
-
A client called `run_in_transaction` be stuck if Spanner is down可能已有人在做 @founderinaction 於 10 天前認領。 未關閉priority: p2 type: bug
難度 2/5 1-3 小時 新手友好度 72/100
googleapis/google-cloud-python#18375 · 1 則留言 ·
維護者通常 2 天內回覆
-
ci: enable --strict flag in presubmit twine_check可能重新可做 @ohmayr 於 47 天前認領,目前沒有進行中的 PR。 未關閉
難度 1/5 1 小時以內 新手友好度 76/100
googleapis/google-cloud-python#18339 · 1 則留言 · 已指派 1 人 ·
維護者通常 2 天內回覆
查看 googleapis/google-cloud-python 的全部 Issue
相似的 Issue
-
area-templates triage:bot-seen triage:needs-human
難度 2/5 1-3 小時 新手友好度 75/100
microsoft/aspire#22356 · 1 則留言 ·
維護者通常 1 天內回覆
-
[Train] An elastic ScalingConfig(num_workers=(0, 0)) is accepted and the run waits forever可能已有人在做 @wguo7 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 75/100
ray-project/ray#68217 ·
維護者通常 2 天內回覆
-
PairwiseComparisonEvaluator records A when a judge's final verdict is B or C after mentioning [[A]]未關閉
難度 2/5 1-3 小時 新手友好度 88/100
run-llama/llama_index#24279 ·
維護者通常 2 天內回覆
-
area:docs good first issue
難度 2/5 1-3 小時 新手友好度 68/100
-
On PyPy, _has_type against an abstract base class lets a metaclass's own exception out of a refusal未關閉
難度 2/5 1-3 小時 新手友好度 78/100
btclib-org/btclib#3139 ·
維護者通常 1 天內回覆