Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fancier types for labels #11

Open
jml opened this issue Mar 23, 2017 · 2 comments
Open

Fancier types for labels #11

jml opened this issue Mar 23, 2017 · 2 comments

Comments

@jml
Copy link
Collaborator

jml commented Mar 23, 2017

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.

@fimad
Copy link
Owner

fimad commented Apr 23, 2017

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.

@ocharles
Copy link
Collaborator

ocharles@70b0759 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants