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

Add prometheus metrics endpoint for nfregexes #11

Merged
merged 2 commits into from
Dec 15, 2024

Conversation

Minei3oat
Copy link
Contributor

This PR adds a prometheus metrics endpoint for exposing the stats of nfregexes to prometheus.

The added endpoint can be used to visualize the blocking over time or configure alerts.

@domysh
Copy link
Member

domysh commented Dec 15, 2024

Hi, thank you for the PR!

I think the feature looks great overall. There's just one suggestion: instead of the custom sanitize function, it might be better to use json.dumps() to encode the strings. This will ensure the output includes brackets and is properly escaped.

If you could make this change, I'd be happy to merge your PR and release a new version.

Thanks again for your contribution!

Example:

metrics = []
sanitize = lambda s : json.dumps(s)
for stat in stats:
    props = f'service_name={sanitize(stat["name"])},regex={sanitize(b64decode(stat["regex"]).decode())},mode={sanitize(stat["mode"])},is_case_sensitive={sanitize(stat["is_case_sensitive"])}'
    metrics.append(f'firegex_blocked_packets{{{props}}} {stat["blocked_packets"]}')
    metrics.append(f'firegex_active{{{props}}} {int(stat["active"] and stat["status"] == "active")}')
return "\n".join(metrics)

@Minei3oat
Copy link
Contributor Author

Minei3oat commented Dec 15, 2024

I don't think this will work, as json.dumps escapes significantly more characters than new line, double quote and backslash. While this is probably not so bad for special ASCII characters in the regex (e.g. tab -> \t), this will completely destroy non ASCII characters like emojis (🤔 -> \ud83e\udd14) since the open metrics format does not define escapes for arbitrary unicode characters: https://github.com/prometheus/OpenMetrics/blob/main/specification/OpenMetrics.md#abnf

@domysh
Copy link
Member

domysh commented Dec 15, 2024

Reading it I agree with you, thank you again for the support to the project!

@domysh domysh merged commit 9ed5e1e into Pwnzer0tt1:main Dec 15, 2024
@Minei3oat Minei3oat deleted the metrics branch December 15, 2024 22:33
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.

2 participants