Skip to content

The design of metrics::Unit #360

Open
@jaskij

Description

@jaskij

Why?

In #337 @tobz asks:

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-coreComponent: core functionality such as traits, etc.E-intermediateEffort: intermediate.T-enhancementType: enhancement.T-ergonomicsType: ergonomics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions