How to Apply Semantic Conventions to Spans
Name spans and label attributes with OpenTelemetry semantic conventions so traces are consistent and queryable across services and languages.
On this page
Introduction
Two services that both “instrument with tracing” can still produce traces that cannot be compared, because one names spans GET /orders and the other calls them fetchOrders, one records http.status_code and the other calls it status. Semantic conventions fix this with an agreed vocabulary for span names, attribute keys and values. Following them is not bureaucracy; it is what makes a trace backend able to group, filter and alert uniformly, and it is what lets a dashboard built for one service work for the rest.
Why a Shared Vocabulary Matters
A trace is only as useful as the labels on its spans. If every team invents its own attribute names, cross-service queries break: you cannot ask “show me all spans with http.status_code 500” if half the fleet calls the field status and the other half response_code. Semantic conventions give one name for one concept across languages and vendors.
Conventions also encode value types and formats. An HTTP status is a number, a URL is a string without credentials, an error is carried by a status field rather than by the span name. Adopting the format means instrumenting libraries, dashboards and alerts all agree without per-service mapping.
Naming Spans by Operation, Not by Detail
A span name should identify the operation and be low-cardinality. For HTTP, the convention is the method plus the route template, not the full URL: GET /orders/{id}, not GET /orders/12345. The path parameters belong in attributes such as url.path and url.query, which keep the span name stable so aggregations do not explode into one series per id.
For database calls, name the span with the operation and the target, such as SELECT orders, and put the statement and rows affected in attributes. The rule is the same: the name is a class of operation, and the attributes carry the instance details. High-cardinality names make every span its own metric and destroy aggregation.
import { trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
const tracer = trace.getTracer('orders-service');
export async function getOrder(id: string) {
// Low-cardinality span name: method + route template, not the raw path.
return tracer.startActiveSpan('GET /orders/{id}', { kind: SpanKind.SERVER }, async (span) => {
span.setAttribute('http.request.method', 'GET');
span.setAttribute('url.path', '/orders/' + id); // instance detail here
span.setAttribute('http.route', '/orders/{id}');
try {
const order = await loadOrder(id);
span.setAttribute('http.response.status_code', 200);
return order;
} catch (err) {
span.setAttribute('http.response.status_code', 500);
span.setStatus({ code: SpanStatusCode.ERROR });
throw err;
} finally {
span.end();
}
});
}
Required Attributes for Common Signals
Most conventions define required and recommended attributes per span kind. For a server span, the method, route, status code and scheme are expected. For a client span, the peer service and the target. For messaging, the system, destination and message id. Required attributes are what upstream dashboards assume exist, so omitting them breaks tooling that was never tested against your service.
Set the attributes the convention requires even when your own dashboards do not use them yet, because a shared dashboard or an auto-generated service map may. Use the instrumentation libraries where possible: they already emit convention-compliant attributes, so hand-rolled spans are where drift creeps in.
# Server span (required)
http.request.method = "GET"
url.path = "/orders/12345"
http.route = "/orders/{id}"
http.response.status_code = 200
url.scheme = "https"
# Client span (required)
server.address = "payments.internal"
server.port = 443
rpc.system = "grpc"
rpc.service = "Payments"
rpc.method = "Charge"
Resource Attributes and Service Identity
Span attributes describe a single operation; resource attributes describe the entity emitting it and are set once per process. service.name is required and is how the backend groups spans into a service. Also set service.version, deployment.environment and, in a fleet, host or container identity. Without service.name the trace backend cannot build a service map at all.
Keep resource attributes consistent across a deployment, ideally injected from the build and the orchestrator rather than hard-coded. A service that sometimes reports service.name=orders and sometimes orders-api is two services to the backend, which fragments every query and alert that relies on the name.
import { resourceFromAttributes } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
export const resource = resourceFromAttributes({
[SemanticResourceAttributes.SERVICE_NAME]: 'orders-api',
[SemanticResourceAttributes.SERVICE_VERSION]: process.env.GIT_SHA ?? 'dev',
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: process.env.ENV ?? 'local',
});
Recording Errors the Convention Way
Do not encode the error in the span name. Use the span status and the exception convention: set span status to error, and record the exception with recordException so the type, message and stack travel with the span. This keeps the span name stable and makes error queries mechanical: filter spans by status=ERROR.
Distinguish expected errors from failures. A validation rejection that is a normal outcome should not mark the span as a failure, or your error rate becomes the sum of things working as designed. Set error status when the operation failed to do its job, and record the reason in an attribute so the distinction is queryable.
Enforcing Conventions in a Fleet
Conventions only help if everyone follows them, so make enforcement part of the toolchain. Prefer auto-instrumentation libraries that already comply, add a lint rule or a test that checks span names against the convention, and use a collector processor to normalise or reject non-conforming attribute keys during rollout.
Document the subset of conventions your organisation actually checks, because the full specification is large. A small, enforced subset beats a large, ignored one. Review it when a new signal is added, and keep the mapping from legacy attribute names during migrations so dashboards do not break on the switchover.
Common Pitfalls and Conclusion
The classic mistake is putting high-cardinality data in the span name, which turns one operation into millions of series. The second is inconsistency: two spellings of the same attribute across services. The third is missing service.name, which orphans spans from the service map. The fourth is using span names to signal errors instead of the status field.
Semantic conventions are what turn a pile of spans into a queryable system. Name spans by low-cardinality operation, put instance detail in attributes, set the required attributes for each span kind, identity your service with resource attributes, and record errors through status and exceptions. Follow the conventions and a dashboard written once works everywhere.
Sources: OpenTelemetry Semantic Conventions, HTTP span conventions, Resource semantic conventions.
FAQ
- Why not put the full URL in the span name?
- Because path parameters make the name high-cardinality: every order id becomes a distinct name, so aggregations explode into millions of series. Use the route template as the name and put the concrete path in url.path.
- Is service.name really required?
- Yes. The tracing backend uses service.name to group spans into a service and to build the service map. Without it, spans are orphaned and most cross-service queries and alerts cannot work.
Report a problem with this articleOpens a pre-filled issue on GitHub.