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

feat(metrics): Limit metric name to 150 chars #3628

Merged
merged 12 commits into from
May 22, 2024

Conversation

iambriccardo
Copy link
Member

@iambriccardo iambriccardo commented May 21, 2024

This PR limits the metric name to 150 characters, which is added to the limit of 15 characters in the metric unit. Since we have a total of 200 characters supported by the indexer, we now have 35 chars for separators, type and namespace of the MRI.

Closes: #3614

@iambriccardo iambriccardo marked this pull request as ready for review May 21, 2024 13:37
@iambriccardo iambriccardo requested a review from a team as a code owner May 21, 2024 13:37
relay-base-schema/src/metrics/mod.rs Show resolved Hide resolved
relay-base-schema/src/metrics/mod.rs Outdated Show resolved Hide resolved
relay-base-schema/src/metrics/mod.rs Outdated Show resolved Hide resolved
@@ -464,6 +464,23 @@ mod tests {
);
}

#[test]
fn test_normalize_name_length() {
let long_mri = "c:custom/ThisIsACharacterLongStringForTestingPurposesToEnsureThatWeHaveEnoughCharactersToWorkWithAndToCheckIfOurFunctionProperlyHandlesSlicingAndNormalizationWithoutErrors";
Copy link
Member

Choose a reason for hiding this comment

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

makes sense to also have a test on how it behaves when we have replacement characters in the name which get replaced with a single underscore

Comment on lines +45 to +47
Cow::Borrowed(value) => Cow::Borrowed(&value[..CUSTOM_METRIC_NAME_MAX_SIZE]),
Cow::Owned(mut value) => {
value.truncate(CUSTOM_METRIC_NAME_MAX_SIZE);
Copy link
Member

Choose a reason for hiding this comment

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

Note that both &value[..CUSTOM_METRIC_NAME_MAX_SIZE] and String::truncate would panic if it wasn't for normalize_re and the .len() check. I don't have a better idea right now, but it's something to keep in mind if we ever refactor this function.

Maybe we can turn the early return of the length check into a if normalized.len() > CUSTOM_METRIC_NAME_MAX_SIZE { ... } block to clarify that these belong together, and add a debug_assert!(value.is_ascii());

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this was considered, in my opinion our best bet is to put enough good tests so that a malicious refactor will be noticed.

@@ -474,6 +474,14 @@ mod tests {
"ThisIsACharacterLongStringForTestingPurposesToEnsureThatWeHaveEnoughCharactersToWorkWithAndToCheckIfOurFunctionProperlyHandlesSlicingAndNormalizationW"
);

let long_mri_with_replacement = "c:custom/ThisIsÄCharacterLongStringForŤestingPurposesToEnsureThatWeHaveEnoughCharactersToWorkWithAndToCheckIfOurFunctionProperlyHandlesSlicingAndNormalizationWithoutErrors";
Copy link
Member

Choose a reason for hiding this comment

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

The test is supposed to show what happens if multiple characters get replaced with a single underscore. Also possibly makes sense to have a separate test case for exactly this behaviour.

@iambriccardo iambriccardo merged commit 4e61bcc into master May 22, 2024
22 checks passed
@iambriccardo iambriccardo deleted the riccardo/feat/metric-name-limit branch May 22, 2024 08:09
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.

Enforce limit of 200 chars per MRI
3 participants