nexogen-international / Nexogen.Libraries.Metrics

Library for collecting application metrics in .NET and exporting them to Prometheus
MIT License
61 stars 9 forks source link

Add an option to specify default label values #7

Closed endofcake closed 7 years ago

endofcake commented 7 years ago

The way it works currently is that you define label names statically at metric creation time:

            var planningTime = metrics.Gauge()
                .Name("planning_time").Help("The processing time in seconds").LabelNames("solver").Register();

And then you have to specify label values every time you do anything with this metric:

planningCount.Labels(solver).Increment();

There's no way to set label values once, which is a bit of a pain for things that don't change within the lifetime of a given instance (for example, environment or instance ID).

endofcake commented 7 years ago

Turns out it's possible to specify labels in the push URL:

/metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>}

This ties nicely with https://github.com/nexogen-international/Nexogen.Libraries.Metrics/issues/6.

farkasmark commented 7 years ago

Thanks for the feedback! Let me get back to you with this issue early next week!

endofcake commented 7 years ago

This looks like a simple fix, so I can submit a PR if you're willing to accept it.

farkasmark commented 7 years ago

Thanks! Yes, it is very much appreciated! Submit the PR and we can review & accept it on Monday!

ahoka commented 7 years ago

Hi Alexander,

Can't you use it like this?

            ILabelledGauge labelledGauge = metrics.Gauge()
                .Name("test")
                .Help("Test.")
                .LabelNames("robot")
                .Register();

            IGauge wallyGuage = labelledGauge.Labels("WALL-E");
            IGauge johnyGauge = labelledGauge.Labels("Johnny-5");

ILabelledGauge is a builder which assigns the label value to the metric and returns a IGauge .

Or do you have something different in mind?

endofcake commented 7 years ago

@ahoka , yes, I'm thinking about static labels which do not change during the lifetime of the instance (in our case this is a container running on AWS ECS) and should be applied seamlessly to all metrics. Some examples - AWS region, ECS task version, ECS task id, ECS host instance id. All these values can be grabbed from environment variables, but it's quite tedious to remember to do this for dozens of metrics, would be much better to just set them once. I think using the push URL is a good solution to this, I just need to find time to prepare the PR.

endofcake commented 7 years ago

This is roughly what I was thinking about: https://github.com/nexogen-international/Nexogen.Libraries.Metrics/pull/9