System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) · 课时

跨度属性、事件与状态

学习使用有意义的属性丰富跨度、记录定时事件并设置跨度状态,让追踪数据具有诊断价值,而不只是计时数据。

第 4 / 4 课13 个步骤

跨度属性、事件与状态 是 CoddyKit 上的免费 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Beyond Timing

A span knows its duration automatically, but raw timing is rarely enough. Enriching spans with attributes, events, and status makes traces tell a story.

Span Attributes

Attributes are key-value pairs that add context to a span, like the route handled or the number of rows returned.

span.set_attribute("http.route", "/orders/:id")
span.set_attribute("db.rows", 42)

Use Conventions

Prefer standard semantic-convention keys for common data so backends understand them. Use custom keys only for domain-specific context.

standard: http.response.status_code
custom: order.tier

Keep Attributes Bounded

Like metric labels, avoid high-cardinality or huge values. Do not stuff entire payloads into an attribute.

  • Good: order count, region
  • Bad: full request body, raw stack trace as attribute

Span Events

An event is a timestamped annotation inside a span marking something that happened at a moment, like a retry or cache miss.

span.add_event("cache.miss", { "key": "user:42" })

Recording Exceptions

Exceptions are recorded as a special event capturing the type, message, and stack, so failures show up in the trace timeline.

try:
    do_work()
except Exception as e:
    span.record_exception(e)

Span Status

Status tells whether the operation succeeded. The default is Unset; set Error on failure so backends can highlight broken spans.

span.set_status(StatusCode.ERROR, "payment declined")

Status vs Recording Exceptions

Recording an exception adds detail but does not by itself mark the span failed. Always also set the status to Error.

span.record_exception(e)
span.set_status(StatusCode.ERROR)

Attributes vs Events

Use attributes for facts true for the whole span; use events for things that happen at a point in time within it.

Naming Spans Well

Span names should be low cardinality, describing the operation type, not the specific instance. Put the variable part in attributes.

name: "GET /orders/:id"   (good)
name: "GET /orders/48213" (bad)

A Well-Formed Span

A diagnostic span has a clean name, a few bounded attributes, events for notable moments, and an accurate status.

Quick Check

Pick the correct statement about span status.

Recap

You learned to enrich spans with bounded attributes (preferring semantic conventions), to mark point-in-time moments and failures with events and record_exception, and to set status to Error explicitly. Low-cardinality span names plus rich context turn traces into real diagnostics.

免费开始

用 AI 导师学习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「跨度属性、事件与状态」课时是免费的吗?

是的 — 「跨度属性、事件与状态」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程的其余内容,请升级到 CoddyKit PRO。 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程共包含 4 节课。

「跨度属性、事件与状态」这节课中我会学到什么?

学习使用有意义的属性丰富跨度、记录定时事件并设置跨度状态,让追踪数据具有诊断价值,而不只是计时数据。 你通过在浏览器中直接运行的动手代码来练习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「跨度属性、事件与状态」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课中编写并运行代码吗?

能。每节 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 自动检测技术
  2. 手动检测最佳实践
  3. 上下文传播与行李
  4. 跨度属性、事件与状态
← 返回 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry)