w4 / serde_prometheus

:pager: serde-based serializer for prometheus' text-based exposition format
Other
4 stars 5 forks source link

feat: explicit timestamps #5

Open kaspar030 opened 1 year ago

kaspar030 commented 1 year ago

I'm re-exporting an external metric (the watt minutes total of a shelly plug s' /status json. There's a timestamp for that metric in the json.

I'd like to be able to get that explicit timestamp into the generated prometheus string.

Is there a way to do this with serde_prometheus that I didn't find? If not, would that be hard to add?

w4 commented 1 year ago

You should be able to do something like this with just a u64:

#[derive(Serialize)]
pub struct Metrics {
    #[serde(serialize_with = "serialize_timestamp")]
    timestamp: DateTime<Utc>,
}

fn serialize_timestamp<S: Serialize>(serializer: S, val: &S) -> Result<S::Ok, S::Error> {
    timestamp.as_u64().serialize(serializer)
}