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

Compress rules, jobs for cos-proxy #634

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Conversation

sed-i
Copy link
Contributor

@sed-i sed-i commented Jul 19, 2024

Issue

Some productions deployments have several megs of relation data just for alert rules, scrape jobs.
Contributing factors to the high volume of reldata:

As a result, every event cos-proxy gets from nrpe relation-changed, results in reading/writing several megs of data, taking several seconds to complete. When many units are at play, model settling takes a long time.

In addition, relation data limit is 16M, and in common envs we already approach 25% of that.

Solution

  • LZMA-compress rules, jobs before storing in reldata, but only in MetricsEndpointAggregator.
  • Decode in MetricsEndpointConsumer.
  • Use encode/decode methods from grafana.

TODO:

  • Since we're bumping LIBAPI here, could consider addressing some of the TODO/FIXMEs sprinkled in the code.

Context

Testing Instructions

Upgrade Notes

@sed-i
Copy link
Contributor Author

sed-i commented Jul 24, 2024

As of writing, the diff between v0 and v1 is:
image

This was prompted by charmcraft pack failing with
"error: can't find Rust compiler".
Comment on lines +412 to +418
def _decode_content(encoded_content: str) -> str:
try:
# Assuming content is encoded, try decoding it.
return lzma.decompress(base64.b64decode(encoded_content.encode("utf-8"))).decode()
except (binascii.Error, lzma.LZMAError):
# Failed to base64-decode or decompress, so probably not encoded. Return as is.
return encoded_content
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are assuming the content is encoded, shouldn't we split this except so we can log meaningful messages to juju debug-log in case our assumption is False?

RELATION_INTERFACE_NAME = "prometheus_scrape"

DEFAULT_ALERT_RULES_RELATIVE_PATH = "./src/prometheus_alert_rules"

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ENCODING = "utf-8"


def _encode_content(content: Union[str, bytes]) -> str:
if isinstance(content, str):
content = bytes(content, "utf-8")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
content = bytes(content, "utf-8")
content = bytes(content, ENCODING)

if isinstance(content, str):
content = bytes(content, "utf-8")

return base64.b64encode(lzma.compress(content)).decode("utf-8")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return base64.b64encode(lzma.compress(content)).decode("utf-8")
return base64.b64encode(lzma.compress(content)).decode(ENCODING)

def _decode_content(encoded_content: str) -> str:
try:
# Assuming content is encoded, try decoding it.
return lzma.decompress(base64.b64decode(encoded_content.encode("utf-8"))).decode()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return lzma.decompress(base64.b64decode(encoded_content.encode("utf-8"))).decode()
return lzma.decompress(base64.b64decode(encoded_content.encode(ENCODING))).decode()


def _exec(self, cmd) -> str:
result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return result.stdout.decode("utf-8").strip()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return result.stdout.decode("utf-8").strip()
return result.stdout.decode(ENCODING).strip()

@Abuelodelanada Abuelodelanada marked this pull request as ready for review July 25, 2024 15:15
@Abuelodelanada Abuelodelanada requested a review from a team as a code owner July 25, 2024 15:15
@Abuelodelanada Abuelodelanada marked this pull request as draft July 25, 2024 15:15
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.

3 participants