opentelemetry-rust icon indicating copy to clipboard operation
opentelemetry-rust copied to clipboard

[Feature]: add derives for Key/Value conversions

Open Sufflope opened this issue 9 months ago • 0 comments

Related Problems?

In my projects I have multiple lightweight types that I want to trace with opentelemetry as key-values, where implementing the necessary conversions feels boilerplatey, and is actually longer than the types and their business logic themselves.

Think something like:

#[derive(strum::Display)]
#[strum(serialize_all = "UPPERCASE")]
enum Method {
    Get,
    Post,
}

impl Method {
    const KEY: &'static str = "method";
}

impl From<Method> for opentelemetry::Value {
    fn from(value: Method) -> Self {
        Self::String(value.to_string().into())
    }
}

impl From<Method> for opentelemetry::KeyValue {
    fn from(value: Method) -> Self {
        Self::new(Method::KEY, value)
    }
}

Describe the solution you'd like:

I would like to suggest derive macros to derive Key, Value, StringValue and KeyValue conversions. Above example would look like:

#[derive(strum::Display, Key, KeyValue, StringValue, Value)]
#[otel(variant = StringValue)]
// or override the key with
// #[otel(key = "custom", variant = StringValue)]
#[strum(serialize_all = "UPPERCASE")]
enum Method {
    Get,
    Post,
}

Considered Alternatives

No response

Additional Context

No response

Sufflope avatar Apr 28 '24 16:04 Sufflope