tokio-rs / valuable

MIT License
185 stars 19 forks source link

Feature request: Allow reading structable attributes #110

Open nhawkes opened 1 year ago

nhawkes commented 1 year ago

At the moment tracing-opentelemetry does a somewhat of a hack to know what fields are metrics. It requires the field name to be prefixed

To publish a new metric, add a key-value pair to your tracing::Event that contains following prefixes:

  • monotonic_counter. (non-negative numbers): Used when the counter should only ever increase
  • counter.: Used when the counter can go up or down
  • value.: Used for discrete data points (i.e., summing them does not make semantic sense)
async fn current_example() { 
     tracing::info!(histogram.racecar.speed = 80_f64);
}

Instead if attributes were exposed it would be possible to do something like this:

/// How fast the racecar is going
#[derive(Valuable)]
#[histogram(key="racecar.speed", unit="mph")]
struct RacecarSpeed(f64);

#[tokio::test]
async fn proposed_example() {
    tracing::info!("Going at {}mph", speed=RacecarSpeed(777.0012_f64));
}

I think there's two approaches here: expose the attributes directly, or expose the typeid and require subscribers to map that typeid to the attribute