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

Enum: custom label for state #416

Closed
jmsantorum opened this issue May 29, 2019 · 6 comments
Closed

Enum: custom label for state #416

jmsantorum opened this issue May 29, 2019 · 6 comments

Comments

@jmsantorum
Copy link

jmsantorum commented May 29, 2019

We're using the Enum metric to represent the state of different critical processes in the following way:

registry = CollectorRegistry()

deadline_start = Enum(
  'deadline_start', 'Start of a time-critical process',
  labelnames=['customer'], states=['waiting', 'late', 'started'],
  registry=registry
)

deadline_end = Enum(
  'deadline_end', 'Completion of a time-critical process',
  labelnames=['customer'], states=['waiting', 'late', 'started'],
  registry=registry
)

and the result we obtain is:

# HELP deadline_start Start of a time-critical process
# TYPE deadline_start gauge
deadline_start{customer="customerA",deadline_start="waiting"} 1.0
deadline_start{customer="customerA",deadline_start="late"} 0.0
deadline_start{customer="customerA",deadline_start="started"} 0.0
# HELP deadline_end Completion of a time-critical process
# TYPE deadline_end gauge
deadline_end{customer="customerA",deadline_end="waiting"} 1.0
deadline_end{customer="customerA",deadline_end="late"} 0.0
deadline_end{customer="customerA",deadline_end="started"} 0.0

but we would like to have a custom_label for the state instead of the metric name as we need to have the same label for all the metrics.

# HELP deadline_start Start of a time-critical process
# TYPE deadline_start gauge
deadline_start{customer="customerA",custom_label="waiting"} 1.0
deadline_start{customer="customerA",custom_label="late"} 0.0
deadline_start{customer="customerA",custom_label="started"} 0.0
# HELP deadline_end Completion of a time-critical process
# TYPE deadline_end gauge
deadline_end{customer="customerA",custom_label="waiting"} 1.0
deadline_end{customer="customerA",custom_label="late"} 0.0
deadline_end{customer="customerA",custom_label="started"} 0.0

the same happens with StateSetMetricFamily.

Would be possible to allow to pass a custom label? I can make a PR with the improvement

@brian-brazil
Copy link
Contributor

The OpenMetrics draft standard does not permit this. Can you explain more about your use case?

@jmsantorum
Copy link
Author

Of course!

We have different scheduled processes that usually start on time and depending on the data to process take more or less time. Those processes emit different events like (started, completed, failed) that are processed by an external watcher (the one that exposes the metrics, the above ones and more metrics).

Then on Prometheus and Grafana, we want to define several alerts and graphs based on those metrics where most of them are based on a couple of different state metrics. And here is where we have the problem because the metrics have different labels and is quite complex to define them.

Right now we're using a Gauge that we set with the different states but we would like to use an Enum as it's easier and better for our use case.

deadline_start = Gauge(
  'deadline_start', 'Start of a time-critical process',
  labelnames=['customer', 'state'],
  registry=registry
)

for state in ['waiting', 'late', 'started']:
  metric_value = 1 if current_state == state else 0
  deadline_start.labels(customer=customer, state=state).set(metric_value)

@brian-brazil
Copy link
Contributor

Hmm, personally I'd expose timestamps of the starts/deadlines and work from there rather than putting that logic into an enum.

@brian-brazil
Copy link
Contributor

This is defined by OpenMetrics, so won't be changing on our own.

@drinks5
Copy link

drinks5 commented Jan 7, 2020

Hi, I use Enum to describe a task's state, when I use the label of taskId, the result is not what I excepted.
Am I use the right way in Enum?

from prometheus_client import (CollectorRegistry, Counter, Enum, Gauge, push_to_gateway, write_to_textfile,
                               pushadd_to_gateway)

registry = CollectorRegistry()

def push():
    pushadd_to_gateway("127.0.0.1:9091", job="test1", registry=registry)
    write_to_textfile('raid.prom', registry)
    with open('raid.prom', 'r') as fd:
        print(fd.read())

e = Enum(
    "my_task_state",
    "Description of enum",
    states=["starting", "running", "stopped"],
    labelnames=('taskId', ),
    #  labelvalues=('35', ),
    registry=registry,
)
e.state('running')
e.labels(taskId=35)
e.state('running')
push()

the result is follow

# HELP my_task_state Description of enum
# TYPE my_task_state gauge
my_task_state{my_task_state="starting",taskId="35"} 1.0
my_task_state{my_task_state="running",taskId="35"} 0.0
my_task_state{my_task_state="stopped",taskId="35"} 0.0

what I excepted is

# HELP my_task_state Description of enum
# TYPE my_task_state gauge
my_task_state{my_task_state="starting",taskId="35"} 0.0
my_task_state{my_task_state="running",taskId="35"} 1.0
my_task_state{my_task_state="stopped",taskId="35"} 0.0

@brian-brazil
Copy link
Contributor

Please don't ask questions on closed issues. It makes more sense to ask questions like this on the prometheus-users mailing list rather than in a GitHub issue. On the mailing list, more people are available to potentially respond to your question, and the whole community can benefit from the answers provided.

@prometheus prometheus locked as off-topic and limited conversation to collaborators Jan 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants