prometheus-haskell icon indicating copy to clipboard operation
prometheus-haskell copied to clipboard

Fancier types for labels

Open jml opened this issue 7 years ago • 2 comments

It'd be nice to have a type class for labels such that I could define in my app how a certain domain-specific value got translated to a Prometheus label.

e.g.

class ToLabel a where
  toLabel :: a -> String

data MyStatus = Good | Bad | Indifferent

instance ToLabel MyStatus where
  toLabel Good = "good"
  toLabel Bad = "bad"
  toLabel Indifferent = "indifferent"

withLabel Good incCounter someMetric

But then, as I write this, I realise this probably devolves into wanting extensible records such that you can declare the type of a label at metric creation and have the type system make sure you've go the right value in the right place, which is probably going overboard.

jml avatar Mar 23 '17 14:03 jml

While not perfect, I think you could do this today with the existing Label type class.

data MyStatus = Good | Bad | Indifferent

instance Label MyStatus where
  labelPairs (_, Good)        = ("MyStatus", "good")
  labelPairs (_, Bad)         = ("MyStatus", "bad")
  labelPairs (_, Indifferent) = ("MyStatus", "indifferent")

withLabel Good incCounter someMetric

The not perfect bit would be that you'd need to provide dummy labels while creating the counter.

someMetric = vector (Good) $ counter (Info "some_metric" "")

A real fix would probably entail making the Label class have two types, the type of the label an the types of the values.

fimad avatar Apr 23 '17 21:04 fimad

https://github.com/ocharles/prometheus-haskell/commit/70b07592ffb8e7cc847d7c15db7e94b80816d8ef is my take on what a Label class could look like.

It doesn't let you have "anonymous" labels, everything must be statically declared up front. Given the benefit of actually have much more explicit label assignment, I think it's worth it.

ocharles avatar Dec 14 '17 12:12 ocharles