-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need &'static str
s from semantic-conventions
for tracing
field recording
#1320
Comments
Can you use the |
I forked the repo and used my local path to make these modifications in /// Returns a reference to the underlying key name
pub const fn as_str(&self) -> &str {
self.0.as_str()
} impl OtelString {
const fn as_str(&self) -> &str {
match self {
OtelString::Owned(s) => panic!(),
OtelString::Static(s) => s,
OtelString::RefCounted(s) => panic!(),
}
}
} The above
Even with the changes compiled the following statement: event!(Level::INFO, { semconv::trace::CODE_FUNCTION.as_str() } = "do_work", "hello_world");
Even if that worked I'd probably protest a bit because of the copy-and-pasted Please see the following example to test usage. Its output should look something like:
[package]
name = "tracing-example"
version = "0.1.0"
edition = "2021"
[dependencies]
opentelemetry-semantic-conventions = { version = "0.12.0", default-features = false }
tracing = { version = "0.1.37", default-features = false }
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["fmt"] } use tracing::{event, span, Level};
use tracing_subscriber::{prelude::*};
pub mod semconv {
pub const CODE_FUNCTION: &str = "code.function";
}
pub mod api_methods {
pub const DO_WORK_METHOD: &str = "do_work";
}
pub mod api_attrs {
pub const USER_NAME: &str = "com.tracing-example.user-name";
}
fn do_work(user_name: &str) {
let span = span!(
Level::INFO,
api_methods::DO_WORK_METHOD,
{ semconv::CODE_FUNCTION } = api_methods::DO_WORK_METHOD
);
let _enter = span.enter();
event!(
Level::INFO,
{ semconv::CODE_FUNCTION } = api_methods::DO_WORK_METHOD,
{ api_attrs::USER_NAME } = user_name,
"Preparing to print greeting to console"
);
println!("Hello, {user_name}!");
}
fn main() {
let fmt = tracing_subscriber::fmt::layer()
.with_level(true)
.with_target(true)
.with_thread_ids(true)
.with_thread_names(true)
.with_ansi(false)
.compact();
tracing_subscriber::registry().with(fmt).init();
event!(Level::INFO, "Initialized telemetry");
do_work("Anonymous");
} |
I'd be in favor of changing this and all the others from: pub const CODE_FUNCTION: Key = Key::from_static_str("code.function"); to pub const CODE_FUNCTION = "code.function"; and relying on the existing I see no fundamental reason why the semantic convention consts need to be bound to If other @open-telemetry/rust-approvers are not opposed, would @kriswuollett be up for submitting a PR? |
I think it's a valid use cases.
I think it's easier for users to just import a Key and attach it but if it's blocking other use case we don't have to maintain this behavior. A simple static string maybe a better choice |
@shaun-cox, yes I'd be happy to put together a PR for this. |
Just to confirm, these changes are solely applicable to the semantic conventions of traces and do not extend to resources, correct? |
No. Although the title of the issue says I think for something like this we'd need 2 PRs? One to add/deprecate w/ experimental feature to remove, and the second that removes the experimental feature and just removes deprecated definitions. I'll introduce myself on the community chat channel. |
I'm not a fan of any approach where we end up with two sets of constants. I think it's better to have whole bunch of |
+1 |
Yes, we generally don't try very hard to keep compatibility. |
We're attempting to get to stable, but for now breaking changes more need to be noted in the Changelog for users to know. |
I'd like to do something like the following with
tracing
:The
tracing
crate requires values that look like Rust identifiers, string literals, or constant expressions. But the string for values like code function are wrapped up in aKey
which althoughconst
does not satisfy theformat_args
that is used bytracing
:I'd think that the easiest solution would be to create another submodule that is code generated with the just primary source of the key strings from which the module with the
const Key
s are derived, e.g.,:That way something like the above example would work for
tracing
as well as any other context in whichconst
/&'static str
is required.Although the code generation can be on my end, automatically or manually, this seems basic enough and useful to others that I'd think it should be implemented here? Or is there some Rust technique or library I haven't learned of yet to make use of the existing constants instead of copy and pasting raw
str
values like"code.function"
everywhere?The text was updated successfully, but these errors were encountered: