> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Span Attributes

> Explains OpenTelemetry span attributes and best practices for attaching key value metadata to spans for filtering debugging aggregation and business analytics

In this lesson we cover span attributes: what they are, why they matter, and how to use them effectively with OpenTelemetry.

Span attributes are key-value pairs attached to individual spans to provide contextual metadata about the operation being tracked. Without attributes, a span is essentially a timestamped name; attributes make spans rich, searchable, and actionable for filtering, debugging, aggregation, and alerting.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-key-value-diagram.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=2784a7430571679a3e61badbe961e05f" alt="The image is a diagram about &#x22;Span Attributes,&#x22; describing them as key-value pairs attached to spans. It highlights two points: carrying metadata about the operation being tracked, and enriching spans with contextual information." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-key-value-diagram.jpg" />
</Frame>

Why use span attributes?

* Provide business context (order IDs, payment types) that makes traces searchable.
* Improve debugging by attaching relevant runtime details to an operation.
* Enable fine-grained alerting and aggregation (e.g., only alert on errors for a specific payment method).
* Support metric generation by aggregating attribute values across spans.

Example: e-commerce order processing

* Add `order_id`, `payment_method`, or `inventory_status` to the `process_order` span.
* Use these attributes to filter traces (for example, all spans where `payment_method` is `"Credit Card"`), or to group and analyze failures for specific products.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-example-order-id-customer-id.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=8e72016bed236acb6561fb464e38fa8a" alt="The image illustrates &#x22;Span Attributes&#x22; showing an example of attributes such as order ID, customer ID, payment method, and inventory status used to enrich spans for filtering, alerts, and analysis." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-example-order-id-customer-id.jpg" />
</Frame>

Span attributes power trace filtering, aggregation, and targeted alerts. For example, you can configure your observability backend to surface exceptions only for spans related to credit card payments if it supports slicing trace data by attributes.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-trace-filtering-aggregation.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=bda6a15886791874c67ab50bd57258f3" alt="The image describes the importance of span attributes, highlighting trace filtering, aggregation, and alerting with corresponding icons." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-trace-filtering-aggregation.jpg" />
</Frame>

Types of attributes to capture

* Business data: `order_id`, `payment_method`, `product_sku`
* User data: `user_id`, `account_tier`
* Technical data: `http.status_code`, `db.system`, cache `hit`/`miss`

Combining these categories provides a 360-degree view of what occurred in a span and enables both technical troubleshooting and business analytics.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/importance-of-span-attributes-metadata.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=ca8bcd258464a823ab83cbf30e89b9f9" alt="The image illustrates the concept of exposing business, user, and technical metadata with a graphic showing a person using a laptop, surrounded by a cloud and various devices. It is titled &#x22;Importance of Span Attributes.&#x22;" width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/importance-of-span-attributes-metadata.jpg" />
</Frame>

Attributes are especially valuable for modeling the lifecycle of business processes. For example, a `process_order` span that contains `order_id`, `payment_method`, and `inventory_status` lets you filter and debug fulfillment issues by order attributes.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/ecommerce-table-order-id-payment-inventory.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=5b2762f33b563711dd38389b1575d610" alt="The image shows a table with attributes related to e-commerce, including order ID, payment method, and inventory status with their corresponding values." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/ecommerce-table-order-id-payment-inventory.jpg" />
</Frame>

How to set attributes (Python example)

Manual instrumentation is how you add business-focused attributes. With OpenTelemetry Python you obtain a span from the tracer and call `set_attribute` on the span object:

```python theme={null}
# python
# Inside a span context
with tracer.start_as_current_span("process_order") as span:
    # Set order attributes using actual variables
    span.set_attribute("order_id", order_id)              # e.g., 12345
    span.set_attribute("payment_method", payment_method)  # e.g., "Credit Card"
    span.set_attribute("inventory_status", inventory_status)  # e.g., "In Stock"
    
    # Continue with your business logic
    process_order_logic()
```

Use the span object's `set_attribute` method (dot notation) to attach keys and values. Keys are strings and values must conform to supported types (see rules below). Add important attributes as early as possible so they are available for SDK-level sampling decisions.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-ecommerce-order-lifecycle-diagram.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=aadeb45a5582b75097214e3ed65b56ab" alt="The image illustrates the use of span attributes in e-commerce for tracing order life cycles, filtering orders by payment method, and debugging inventory fulfillment issues. The information is presented with a three-part circular diagram." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-ecommerce-order-lifecycle-diagram.jpg" />
</Frame>

Attribute rules and best practices

| Rule        | Recommendation                                                                              |
| ----------- | ------------------------------------------------------------------------------------------- |
| Key type    | Keys must be non-null strings. Use concise, consistent naming.                              |
| Value types | Values may be `string`, `boolean`, `integer`, `float`, or arrays of these primitives.       |
| Avoid nulls | Do not record null values; omit attributes when the value is unknown.                       |
| Key naming  | Prefer short, stable keys; use semantic conventions for common technical attributes.        |
| When to set | Add business-critical attributes early in execution to influence sampling and availability. |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/attribute-rules-key-value-checklist.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=498523d24093e0744f180d4c90bb202c" alt="The image shows a set of attribute rules for key-value pairs, emphasizing non-null keys, specific data types for values, and guidance on adding attributes. There's also an illustration of a person standing next to a checklist." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/attribute-rules-key-value-checklist.jpg" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  Use [semantic conventions](https://opentelemetry.io/docs/reference/specification/semantic_conventions/) (standardized attribute keys such as `http.method`, `db.system`, `messaging.operation`) where possible. These conventions promote consistency across services and make cross-system analysis easier.
</Callout>

Semantic attributes are predefined keys from the OpenTelemetry semantic conventions. Many auto-instrumentation libraries populate these automatically (for example, HTTP libraries add `http.method`, `http.url`, etc.), so you often do not need to add them manually. Use manual attributes for business-specific metadata (e.g., `order_id`, `payment_method`) to make traces searchable and filterable by business context.

Summary — key takeaways

* Span attributes are key-value metadata attached to spans to provide contextual, queryable information.
* Keys must be non-null strings; values must be primitives (`string`, `boolean`, `integer`, `float`) or arrays of primitives.
* Use semantic conventions for technical attributes; use manual instrumentation for business attributes.
* Add attributes early so they are available to sampling and analysis.
* Attributes enable trace filtering, aggregation, and more precise alerting based on business context.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-key-takeaways-guidelines.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=e7d1f7740fc62603dbde954b0bf9a382" alt="The image lists five key takeaways related to span attributes, including guidelines on metadata, keys, business context, SDK rules, and trace management. The takeaways are presented in a sequential, numbered format." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Span-Anatomy-and-Context-Propagation/Span-Attributes/span-attributes-key-takeaways-guidelines.jpg" />
</Frame>

Attributes are attached to individual spans and do not automatically propagate to child spans (unlike the trace ID). If you need to propagate key-value pairs across process boundaries or between spans, use baggage.

<Callout icon="warning" color="#FF6B6B">
  Span attributes are not propagated across spans. To propagate key-value pairs across process boundaries, use baggage instead.
</Callout>

Links and references

* [OpenTelemetry Python instrumentation](https://opentelemetry.io/docs/instrumentation/python/)
* [OpenTelemetry Semantic Conventions](https://opentelemetry.io/docs/reference/specification/semantic_conventions/)
* [OpenTelemetry Specification](https://opentelemetry.io/docs/reference/specification/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/prep-course-opentelemetry-certified-associate-certification-otca/module/2708459f-e4ca-4659-9878-5769d439a274/lesson/91d8fdff-0330-4591-b923-9cd10c509c3b" />
</CardGroup>
