Description
Why?
As an example, is
metrics::Unit
actually sufficient for all users as-is
And it's most definitely not: off the top of my mind, it's missing temperature, voltage, electric current, power - just to name a few used to monitor computer hardware.
And while yes, metrics
is probably intended mostly as a library to measure the performance of software, if it gets popular enough at some point it will inevitably be used with more physical quantities.
Hence, this issue, because the current design of metrics::Unit
is definitely not satisfactory.
Questions to answer?
There are two questions to answer here:
- Should
metrics
deal with units at all? Or just pass them through? - If so how should it be done?
Possible designs
Just plain strings
There's so many units of measurement that simply passing them through is a very tempting option. So much so that the current implementation of OpenTelemetry protocol does exactly this.
Internally
Another option is to have some enumeration in metrics
itself, with an escape hatch for more unusual units. This would still be a decently large undertatking. A quick estimate is that just the SI system has around a hundred different units.
A quick sketch of an API would be something like below, with the understanding that UnitName
, UnitPrefix
and Unit
all implement Display
, or a similar trait, to allow Recorders to easily convert the units to, at least, a ShardString
.
enum UnitName {
Seconds,
Bytes,
Bits,
Volts,
Amperes,
// etc - probably close to a hundred entries
Custom(SharedString)
}
enum UnitPrefix {
None,
// decimal prefixes
Micro,
Milli,
Deca,
Kilo,
// binary prefixes
Kibi,
Mibi,
// and so on
Custom(SharedString)
}
struct Unit {
name: UnitName,
prefix: UnitPrefix,
}
Use an external crate
Option I admittedly have not looked much into, use an existing crate to deal with this. A few I found with a quick search: