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

Adding 'namespace' for prometheus metrics #13738

Merged
merged 3 commits into from Jun 28, 2018
Merged

Adding 'namespace' for prometheus metrics #13738

merged 3 commits into from Jun 28, 2018

Conversation

alexbarcelo
Copy link
Contributor

@alexbarcelo alexbarcelo commented Apr 7, 2018

Description:

Add a "namespace" for all the component metrics. This is a breaking change, motivated by "official best practices" see Prometheus documentation

A metric name [...] should have a (single-word) application prefix relevant to the domain the metric belongs to. The prefix is sometimes referred to as namespace by client libraries. For metrics specific to an application, the prefix is usually the application name itself.

The code could be tweaked to be non-breaking. IMHO, it is more sane and well-organized to force a top-level namespace and share that by all the metrics --instead of starting the metric name by the hass domain-- but it is arguably the best approach. Before adding the documentation I wanted to discuss that and listen to other opinions. If there is consensus in a different solution, I have no problem modifying this PR to follow the guidelines.

Example entry for configuration.yaml (if applicable):

prometheus:
  namespace: "hass"  # this happens to be the default namespace name

Result:

Going to http://hass.example.net:8123/api/prometheus shows:

...
# HELP hass_device_tracker_state State of the device tracker (0/1)
# TYPE hass_device_tracker_state gauge
hass_device_tracker_state{entity="device_tracker.04xxxxxxxx9d",friendly_name="04xxxxxxxx9d"} 1.0
hass_device_tracker_state{entity="device_tracker.androidffxxxxxxxxxxxx2f",friendly_name="android-ffxxxxxxxxxxxxxx2f"} 1.0
...

Checklist:

@homeassistant homeassistant added cla-signed integration: prometheus merging-to-master This PR is merging into the RC branch and should probably change the branch to `dev`. small-pr PRs with less than 30 lines. labels Apr 7, 2018
@@ -93,7 +101,8 @@ def _metric(self, metric, factory, documentation, labels=None):
try:
return self._metrics[metric]
except KeyError:
self._metrics[metric] = factory(metric, documentation, labels)
full_metric_name = "%s_%s" % (self.metrics_namespace, metric)
self._metrics[metric] = factory(full_metric_name, documentation, labels)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (84 > 79 characters)

@fabaff fabaff changed the base branch from master to dev April 7, 2018 16:32
@fabaff fabaff removed the merging-to-master This PR is merging into the RC branch and should probably change the branch to `dev`. label Apr 7, 2018
@fabaff fabaff changed the title adding 'namespace' for prometheus metrics Adding 'namespace' for prometheus metrics Apr 7, 2018
alexbarcelo added a commit to alexbarcelo/home-assistant.github.io that referenced this pull request Apr 11, 2018
This changes includes the information required about the new configuration parameter introduced in home-assistant/core#13738
CONFIG_SCHEMA = vol.Schema({
DOMAIN: recorder.FILTER_SCHEMA,
DOMAIN: recorder.FILTER_SCHEMA.extend({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that there was a helper for that :) I believe that the following commit addresses what you asked?

@alexbarcelo
Copy link
Contributor Author

alexbarcelo commented Apr 13, 2018

I messed up a little bit at the middle (I was not in my main development machine, and did some silly mistakes).

While looking into other components and seeing the entity_filter helper, I felt that moving the filter into its own entry in the settings is a better approach. That means more breaking changes, although the code is easier to mainteinance. The settings will be like this:

prometheus:
  filter:
    <filter settings here>
  namespace: "name"

Although the default

prometheus:

will keep working because everything else is optional.

I will update the documentation PR accordingly

@alexbarcelo
Copy link
Contributor Author

@pvizeli I think that the changes done were the ones you were requesting. Could you confirm that it looks good? At least that specific aspect.

@pvizeli
Copy link
Member

pvizeli commented Apr 25, 2018

No my change request are the same: https://github.com/home-assistant/home-assistant/blob/3fd61d8f456f14796e41dc05c77a7ab60861eb84/homeassistant/components/alexa/__init__.py#L34

You should not access to a other component from your componenent. That break the interface logic they we use. As next, with the helper, we can provide a config feature they work over multible component in same way. We need migrate all the old ways to this helper.

@alexbarcelo
Copy link
Contributor Author

I don't understand... Are you refering to an alexa component? I only touched Prometheus stuff... in fact, your change request was related to a stuff that was already there (but given that I was changing that part, it was the moment to fix it). I think that there is no reference to other component, or I am missing something.

CONF_PROM_NAMESPACE = 'namespace'
PROM_NAMESPACE = 'hass'

CONFIG_SCHEMA = vol.Schema({
DOMAIN: FILTER_SCHEMA.extend({
DOMAIN: vol.All({
vol.Optional(CONF_FILTER, default={}): entityfilter.FILTER_SCHEMA,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pvizeli This is what you asked, isn't it? Using the entityfilter.FILTER_SCHEMA at this point (instead of the previous other-component-stuff-that-may-break).

@fabaff
Copy link
Member

fabaff commented Apr 28, 2018

Could you please update the dependency (REQUIREMENTS)? You have a working setup and this would ensure that the upgrade works. Thanks.

@alexbarcelo
Copy link
Contributor Author

@fabaff Updated to prometheus_client=0.2.0 and restarted my local home assistant installation to test that everything was ok.

Main functionality is working as expected --let me remind that there are breaking changes on the naming of metrics, albeit I defend that there is "a greater good" behind the rename.

I don't know why coveralls is complaining now (or at all). Maybe it has to do with my f*** up when I started on the wrong branch? (shame on me).

@balloob
Copy link
Member

balloob commented May 1, 2018

You can ignore coveralls. It has already been fixed upstream.

You removed the PR instructions that you need to follow to get Travis to pass… run script/gen_requirements_all.py

DOMAIN: recorder.FILTER_SCHEMA,
DOMAIN: vol.All({
vol.Optional(CONF_FILTER, default={}): entityfilter.FILTER_SCHEMA,
vol.Optional(CONF_PROM_NAMESPACE, default=PROM_NAMESPACE): cv.string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make it have no default and not add it if not provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean that the metrics name, by default, should not have application prefix at all? The reason for that is to have the "previous" (current, actual) behaviour of prometheus component by default?

@fabaff fabaff mentioned this pull request May 17, 2018
@alexbarcelo
Copy link
Contributor Author

I redid the branch because there were so many mistakes on my part that commit history was a mess.

@pvizeli I think that I addressed your issues at a certain point but that was not clear. On the new commits, if 72351dc is not what you were asking, then please tell me so because that means that I still not know what to change :( That commit is in fact unrelated to the purpose of this PR, but it was a good moment to improve it.

@balloob I assumed that you wanted to have no default and, if no namespace is given, maintain the current behaviour. That is what is done (just tested it): the "metrics prefix" will be <namespace>_ or empty, depending if namespace settings is set.

The breaking changes are related to the use of filter settings. Again, not my main purpose of this PR, but collateral damageimprovements.

@alexbarcelo
Copy link
Contributor Author

Is this PR good to go? Or it still has issues?

@pvizeli pvizeli merged commit a277470 into home-assistant:dev Jun 28, 2018
frenck pushed a commit to home-assistant/home-assistant.io that referenced this pull request Jun 28, 2018
* Add information for the namespace option

This changes includes the information required about the new configuration parameter introduced in home-assistant/core#13738

* Nesting include/exclude into filter directive

* ✏️ Language tweaks

* Reflecting the optionality of namespace setting
@alexbarcelo alexbarcelo deleted the master branch June 28, 2018 20:20
@balloob balloob mentioned this pull request Jul 6, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
* Updating prometheus client version

* Using `entity_filter` as filter mechanism

* New optional `namespace` configuration
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants