Skip to content

Adopt opentelemetry-semantic-conventions for OTel attribute names#3510

Open
ChihweiLHBird wants to merge 3 commits into
spinframework:mainfrom
ChihweiLHBird:zhiwei/otel-semantic-conventions
Open

Adopt opentelemetry-semantic-conventions for OTel attribute names#3510
ChihweiLHBird wants to merge 3 commits into
spinframework:mainfrom
ChihweiLHBird:zhiwei/otel-semantic-conventions

Conversation

@ChihweiLHBird
Copy link
Copy Markdown
Contributor

@ChihweiLHBird ChihweiLHBird commented May 14, 2026

Description

Adopt opentelemetry-semantic-conventions 0.28 for stable runtime span attribute names on outbound HTTP (factor-outbound-http) and the HTTP trigger (trigger-http), replacing string-literal field keys with crate constants where available.

Bump workspace tracing from 0.1.41 to 0.1.44 so #[instrument(fields(...))] accepts braced semconv constants (older tracing-attributes only parsed dotted field names there).

otel.kind, otel.name, and incubating http.response.body.size stay literals (with a short comment on body size). Spin-specific attributes (e.g. error.blame) are unchanged.

@lann
Copy link
Copy Markdown
Collaborator

lann commented May 14, 2026

FYI: https://docs.rs/tracing/latest/tracing/index.html#recording-fields:~:text=Constant%20expressions%20can,the%20constant%E2%80%99s%20name.

Constant expressions can also be used as field names. Constants must be enclosed in curly braces ({}) to indicate that the value of the constant is to be used as the field name, rather than the constant’s name.

@ChihweiLHBird ChihweiLHBird force-pushed the zhiwei/otel-semantic-conventions branch from f8bb3c1 to 900797a Compare May 14, 2026 16:42
@ChihweiLHBird
Copy link
Copy Markdown
Contributor Author

FYI: https://docs.rs/tracing/latest/tracing/index.html#recording-fields:~:text=Constant%20expressions%20can,the%20constant%E2%80%99s%20name.

Constant expressions can also be used as field names. Constants must be enclosed in curly braces ({}) to indicate that the value of the constant is to be used as the field name, rather than the constant’s name.

Thanks! Let me try that. Do we want to include migrations for them in this PR as well?

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
@ChihweiLHBird ChihweiLHBird force-pushed the zhiwei/otel-semantic-conventions branch from 900797a to e3e19ef Compare May 14, 2026 17:13
@lann
Copy link
Copy Markdown
Collaborator

lann commented May 14, 2026

Do we want to include migrations for them in this PR as well?

Up to you.

@lann lann requested a review from calebschoepp May 14, 2026 17:29
@ChihweiLHBird
Copy link
Copy Markdown
Contributor Author

ChihweiLHBird commented May 14, 2026

Note that there are other experimental or deprecated semantic conventions that I left as string literals and didn't migrate to the constants in this PR. And it should be ready for review now. Let me know if anything else is needed.

@lann
Copy link
Copy Markdown
Collaborator

lann commented May 14, 2026

I probably should have started with this: looking at the diff now I'm not sure this is a net improvement. Avoiding typos is good, but there is also a downside here in that the constants make it harder to see the actual field names. That wouldn't be so bad if they were simply UPPERCASED versions of the field names but they also flatten periods into underscores... 🤔

@ChihweiLHBird
Copy link
Copy Markdown
Contributor Author

ChihweiLHBird commented May 14, 2026

@lann thanks for the thought! That's totally fair and I actually had a similar concern about it because this introduces another dependency.

The upside is the omitted key is still the dotted conventions and benefits of safer compile-time typo checks.
I personally feel it's easy to view the value of the constants too.

Another benefit is that the opentelemetry-semantic-conventions crate also makes us aware of the status of each semantic contention, like whether it's deprecated or experimental; so, we can avoid adding a deprecated key when building some new stuff.

I am okay if the team decide to not accept this, and feel free to close the PR in that case.

@itowlson itowlson requested a review from lann May 15, 2026 00:29
@ChihweiLHBird
Copy link
Copy Markdown
Contributor Author

Another benefit is that the opentelemetry-semantic-conventions crate also makes us aware of the status of each semantic contention, like whether it's deprecated or experimental; so, we can avoid adding a deprecated key when building some new stuff.

An example of this is db.system, which is still widely used in spin but is actually deprecated. And it's marked as deprecated in the crate so triggers warnings in compile time, is used without #![allow(deprecated)].

Copy link
Copy Markdown
Collaborator

@calebschoepp calebschoepp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you make sure you found all possible attributes to replace?

Overall the advantages of getting warnings when things are deprecated seems worth it.

Comment thread crates/trigger-http/src/instrument.rs Outdated
@@ -1,5 +1,8 @@
use anyhow::Result;
use http::Response;
use opentelemetry_semantic_conventions::attribute::{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could just import all of opentelemetry_semantic_conventions::attribute with an alias of otel_attribute or something like that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Let me try that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting an alias to work in a macro is more complex than I thought. It would require use ::opentelemetry_semantic_conventions::attribute as otel_attribute; in the macro block and also in the modules where the macro is used. Can I leave this as is?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this pattern for all others seem to be fine and I pushed it here: 0b5917b

@@ -397,12 +404,12 @@ impl p2::WasiHttpHooks for InstanceHttpHooks {
skip_all,
fields(
otel.kind = "client",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this one not a convention?

Copy link
Copy Markdown
Contributor Author

@ChihweiLHBird ChihweiLHBird May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, otel.kind is not an official convention and is a spin specific thing...
You can see all otel.* keys in the conventions on this page and it doesn't contain otel.kind.

Copy link
Copy Markdown
Contributor Author

@ChihweiLHBird ChihweiLHBird May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say another benefit of using the official convention crate is we can realize things like this earlier

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lann Thanks! Nice to know it

Copy link
Copy Markdown
Collaborator

@lann lann May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're switching to constants for standard keys it would probably be wise for us to create constants for these too, e.g. OTEL_KIND, OTEL_NAME. (tracing-opentelemetry doesn't seem to define them)

Scope creepily: I wouldn't be sad to have macros for these. We use otel.name and otel.kind quite a bit so e.g.:

#[instrument(
  otel_kind!("client"),
  // Lots of this pattern in crates/factor-outbound-redis/src/host.rs
  otel_name!("GET {key}"),
)]

expanding to

#[instrument(
  {$crate::something_goes_here::OTEL_KIND} = "client",
  // Lots of this pattern in crates/factor-outbound-redis/src/host.rs
  {$crate::something_goes_here::OTEL_NAME} = format!("GET {path}"),
)]

Note: if you haven't seen it, $crate is a magic macro variable: https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.meta.dollar-crate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope creepily: I wouldn't be sad to have macros for the otel.* fields. We use otel.name and otel.kind quite a bit

This looks like something for tracing-opentelemetry to add.

@ChihweiLHBird
Copy link
Copy Markdown
Contributor Author

ChihweiLHBird commented May 15, 2026

@calebschoepp thanks for the review!

Did you make sure you found all possible attributes to replace?

There are some experimental keys and some deprecated key I didn't replace with constants. The experimental keys require an additional feature of the crate, and the deprecate keys requir #![allow(deprecated)] in lib.rs. I can add them if you think that's fine. Personally I lean to leave it as is OR only add experimental feature. We can migrate the deprecated keys in the future then replace them with constants; so, we won't have to declare allowing any deprecated item in lib.rs

@lann
Copy link
Copy Markdown
Collaborator

lann commented May 15, 2026

I was on the fence so @calebschoepp can be the tie-breaker; I delegate my review to him. 🙂

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
@ChihweiLHBird ChihweiLHBird force-pushed the zhiwei/otel-semantic-conventions branch from 7f046d0 to 2424006 Compare May 15, 2026 15:56
Updated the usage of OpenTelemetry semantic conventions in the outbound HTTP crate to utilize the new aliasing for attribute names.

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants