-
Notifications
You must be signed in to change notification settings - Fork 165
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
cleanup(sinsp/metrics): assorted cleanups for robustness #1950
Conversation
Signed-off-by: Melissa Kilby <[email protected]>
/milestone 0.18.0 |
Perf diff from master - unit tests
Perf diff from master - scap file
Heap diff from master - unit tests
Heap diff from master - scap file
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good cleanup !
if (!sanitized_name.empty() && !(std::isalnum(sanitized_name.front()) || sanitized_name.front() == '_')) | ||
{ | ||
sanitized_name = "_" + sanitized_name; | ||
} | ||
if (!sanitized_name.empty() && sanitized_name.back() == '_') | ||
{ | ||
sanitized_name.pop_back(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure to follow the logic:
if not empty, an underscore is prepended if the first char is not alpha numeric or if it is already an underscore.
The former should not happen based on the RegEx and for the latter, the documentation state that double underscore starting names are reserved for internal use.
Also, if the metric name is empty, then nothing happen, shouldn't it scream about it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misread the regex and thought for the metric name also numbers are allowed in front. Plus yes the !sanitized_name.empty()
was an error here. Re-pushed to 100% strictly stay with the prometheus mandates.
Right now we would replace and empty metrics name with _
. We chatted a bit over slack and are unsure how to best handle it. Logs could be too noisy and since metrics are not that critical, we certainly don't want to error out or anything. Maybe simply not adding the metric is the right solution.
std::string prometheus_sanitize_metric_name(const std::string& name) | ||
{ | ||
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | ||
static const RE2 invalid_chars("[^a-zA-Z0-9_]"); | ||
std::string sanitized_name = name; | ||
RE2::GlobalReplace(&sanitized_name, invalid_chars, "_"); | ||
RE2::GlobalReplace(&sanitized_name, "_+", "_"); | ||
if (!sanitized_name.empty() && !(std::isalnum(sanitized_name.front()) || sanitized_name.front() == '_')) | ||
{ | ||
sanitized_name = "_" + sanitized_name; | ||
} | ||
if (!sanitized_name.empty() && sanitized_name.back() == '_') | ||
{ | ||
sanitized_name.pop_back(); | ||
} | ||
return sanitized_name; | ||
} | ||
|
||
std::string prometheus_sanitize_label_name(const std::string& name) | ||
{ | ||
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | ||
static const RE2 invalid_chars("[^a-zA-Z0-9_]"); | ||
std::string sanitized_label = name; | ||
RE2::GlobalReplace(&sanitized_label, invalid_chars, "_"); | ||
RE2::GlobalReplace(&sanitized_label, "_+", "_"); | ||
|
||
// Ensure the label starts with a letter or underscore (if empty after sanitizing, set to "_") | ||
if (sanitized_label.empty() || (!std::isalpha(sanitized_label.front()) && sanitized_label.front() != '_')) | ||
{ | ||
sanitized_label = "_" + sanitized_label; | ||
} | ||
|
||
return sanitized_label; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to the logic question, I would factor out the common part to avoid code duplication since both are using exactly the same cleanup starting phase.
std::string prometheus_sanitize_metric_name(const std::string& name) | |
{ | |
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | |
static const RE2 invalid_chars("[^a-zA-Z0-9_]"); | |
std::string sanitized_name = name; | |
RE2::GlobalReplace(&sanitized_name, invalid_chars, "_"); | |
RE2::GlobalReplace(&sanitized_name, "_+", "_"); | |
if (!sanitized_name.empty() && !(std::isalnum(sanitized_name.front()) || sanitized_name.front() == '_')) | |
{ | |
sanitized_name = "_" + sanitized_name; | |
} | |
if (!sanitized_name.empty() && sanitized_name.back() == '_') | |
{ | |
sanitized_name.pop_back(); | |
} | |
return sanitized_name; | |
} | |
std::string prometheus_sanitize_label_name(const std::string& name) | |
{ | |
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | |
static const RE2 invalid_chars("[^a-zA-Z0-9_]"); | |
std::string sanitized_label = name; | |
RE2::GlobalReplace(&sanitized_label, invalid_chars, "_"); | |
RE2::GlobalReplace(&sanitized_label, "_+", "_"); | |
// Ensure the label starts with a letter or underscore (if empty after sanitizing, set to "_") | |
if (sanitized_label.empty() || (!std::isalpha(sanitized_label.front()) && sanitized_label.front() != '_')) | |
{ | |
sanitized_label = "_" + sanitized_label; | |
} | |
return sanitized_label; | |
} | |
std::string _sanitize_name(const std::string& name) | |
{ | |
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | |
static const RE2 invalid_chars("[^a-zA-Z0-9_]"); | |
std::string sanitized_name = name; | |
// RE2::Extract(name, invalid_chars, "_", &sanitized_name); | |
RE2::GlobalReplace(&sanitized_name, invalid_chars, "_"); | |
RE2::GlobalReplace(&sanitized_name, "_+", "_"); | |
return sanitized_name; | |
} | |
std::string prometheus_sanitize_metric_name(const std::string& name) | |
{ | |
std::string sanitized_name = _sanitize_name(name); | |
if (!sanitized_name.empty()) | |
{ | |
if (!(std::isalnum(sanitized_name.front()) || sanitized_name.front() == '_')) | |
{ | |
sanitized_name.insert(0, "_"); | |
} | |
if (sanitized_name.back() == '_') | |
{ | |
sanitized_name.pop_back(); | |
} | |
} | |
return sanitized_name; | |
} | |
std::string prometheus_sanitize_label_name(const std::string& name) | |
{ | |
std::string sanitized_label = _sanitize_name(name); | |
// Ensure the label starts with a letter or underscore (if empty after sanitizing, set to "_") | |
if (sanitized_label.empty() || (!std::isalpha(sanitized_label.front()) && sanitized_label.front() != '_')) | |
{ | |
sanitized_label.insert(0, "_"); | |
} | |
return sanitized_label; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added an alternative solution as second co-authored commit, let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sgaist kind bump, ty!
{ | ||
prometheus_text += "," + key + "=\"" + value + "\"" ; | ||
prometheus_text += " "; // white space at the end important! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prometheus_text += " "; // white space at the end important! | |
prometheus_text += " "; // the white space at the end is important! |
} | ||
prometheus_text += prometheus_sanitize_label_name(key) + "=\"" + value + "\""; | ||
} | ||
prometheus_text += "} "; // white space at the end important! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prometheus_text += "} "; // white space at the end important! | |
prometheus_text += "} "; // the white space at the end is important! |
@sgaist: changing LGTM is restricted to collaborators In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
…ation methods Signed-off-by: Melissa Kilby <[email protected]>
Co-authored-by: Samuel Gaist <[email protected]> Signed-off-by: Melissa Kilby <[email protected]>
Perf diff from master - unit tests
Perf diff from master - scap file
Heap diff from master - unit tests
Heap diff from master - scap file
|
LGTM label has been added. Git tree hash: 6d68da1fdf4ccef12e2abfe2ead351d23db0bb43
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: Andreagit97, FedeDP, incertum The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind cleanup
Any specific area of the project related to this PR?
/area libsinsp
Does this PR require a change in the driver versions?
What this PR does / why we need it:
metrics
framework future refactors / cleanups falco#3194 (comment)Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?: