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

Break the Rules monolith. Add support for recording rules. #433

Closed
wants to merge 5 commits into from

Conversation

rbarry82
Copy link
Contributor

Issue

prometheus_scrape doesn't currently support recording rules via any intentional mechanism -- they can be accidentally loaded via the AlertRules pathway, but none of the log messages or databags will be meaningful. We should support this use case, separated.

Solution

The actual handling of the rules is mostly identical, outside of keys to check for if we're validating single rule formats, and a string to use in log messages/alerts.

Make AlertRules into Rules(ABC) with an @abstractmethod+@property which returns a Literal _RulesType, which child classes implement to indicate the string to interpolate where needed. Similarly, extract the handling for alerts() into a more generalized method which takes the type as an argument in addition and maps it to a databag. All of the handling there is also identical (for now).

Remove every reference I could find to alert in docstrings, docs, and variable names where it should not be present, substituting the interpolated value or appending/amending docstrings to speak about both types. Rename CosTool.validate_alert_rules() to CosTool.validate_rules() (it isn't specific). Rename exceptions to more generic names.

Add constructor args and events for recording rules.

In this way, if/when we ever do need different handling (such as if we do not want to support "single rule file" formats for recording rules, or Loki, or other) or the relation data needs/APIs for either type diverge, they are already cleanly separated without breaking the current API.

In addition, this restructuring makes it possible to, once pydeps for charmcraft is implemented, move the [T]Rules machinery elsewhere, rather than trying to keep prometheus_scrape, prometheus_remote_write, loki_push_api, and others in sync, with independent patches needed any time a bug or feature comes up in any of them (with a little handling to smash metadata into scrape_metadata, even as just an optional metadata_key arg in the constructor for Rules())

Closes #428

Context

Also, mock.create_autospec() exists, and it's great. _dedupe_... was moved back into the class it belongs to (the only place that uses it), and test_functions uses create_autospec to test that function in an isolated way. We don't need to extract methods to the top level to make writing unit tests easier, and, as a common refrain, doing so makes new readers/authors or anyone writing a patch juggle more mental state while trying to keep track of whether it's really doing something generalized, used elsewhere, etc.

The problem domain of _dedupe_... is in the only class which ever calls it, and create_autospec() makes unit testing it in an isolated fashion just as easy as importing it directly from the top-level.

Testing Instructions

Deploy. Make sure there aren't regressions.

Release Notes

Add support for recording rules.

@@ -36,6 +37,7 @@
)
from ops.framework import EventBase, EventSource, Object, ObjectEvents
from ops.model import Relation
from typing_extensions import Literal
Copy link
Contributor

Choose a reason for hiding this comment

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

Literal is a 3.8 feature.
Perhaps this was added before changing mypy --python-version to 3.8?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably. I'll re-check...

_rule_type = "alert" # type: _RuleType

@property
def rule_type(self) -> _RuleType:
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably wouldn't need a property if we use ClassVar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it would, because there's no similar ClassVar on the parent, so self.rule_type won't resolve on AlertRules(ABC) without at least an @abstractmethod, hence the property. Parents can't use the attributes of their children unless they're defined, and the ABC parent has no reason to have a _rule_type

lib/charms/prometheus_k8s/v0/prometheus_remote_write.py Outdated Show resolved Hide resolved
lib/charms/prometheus_k8s/v0/prometheus_remote_write.py Outdated Show resolved Hide resolved
@sed-i
Copy link
Contributor

sed-i commented Jan 25, 2023

The plot thickens?
Federated rule groups:

name: MyGroupName
source_tenants: ["tenant-a", "tenant-b"]
rules:
  - record: sum:metric
    expr: sum(metric)

@rbarry82
Copy link
Contributor Author

Federated rule groups

Probably only if we ever get to the point of multi-tenancy, but at least cos-tool already supports this, and it's nominally easier to add (excluding Prometheus itself) if we get there.

Copy link
Contributor

@dstathis dstathis left a comment

Choose a reason for hiding this comment

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

This looks good to me. What do people think about making a rules lib once pydeps is a thing?

Copy link
Contributor

@lucabello lucabello left a comment

Choose a reason for hiding this comment

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

Looks good; make sure the CI passes before merging! :)

@rbarry82
Copy link
Contributor Author

rbarry82 commented Mar 5, 2023

Closing to move this to cosl instead, with unification between all the variants (mostly the same as this patch, just small tweaks to meet in the middle for Prom+Loki)

@rbarry82 rbarry82 closed this Mar 5, 2023
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

Successfully merging this pull request may close these issues.

Add explicit support for recording rules
5 participants