Skip to content

Compress rules, jobs for cos-proxy - #634

Closed
sed-i wants to merge 5 commits into
mainfrom
feature/compress-rules-jobs
Closed

sed-i wants to merge 5 commits into
mainfrom
feature/compress-rules-jobs

Conversation

@sed-i

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

Copy link
Copy Markdown
Contributor

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

sed-i commented Jul 24, 2024

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@lucabello

Copy link
Copy Markdown
Contributor

We should probably not move forward with this. Currently, we only compress dashboards, and for now it's probably better to keep everything in plaintext, so we don't fracture the way we handle relation data across our charms.

@sed-i

sed-i commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #864.

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.

4 participants