-
Notifications
You must be signed in to change notification settings - Fork 39
Add dimension, metric and namespace validation #91
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4edde22
add dimension, metric and namespace validation
5584ce5
add docstring for validator
bda35d0
update readme with validation errors
4c1f618
Merge branch 'master' into validation
f6151b0
change dimension key to dimension name in err msg
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright 2019 Amazon.com, Inc. or its affiliates. | ||
# Licensed under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import math | ||
import re | ||
from typing import Dict, Optional | ||
from aws_embedded_metrics.unit import Unit | ||
from aws_embedded_metrics.exceptions import DimensionSetExceededError, InvalidDimensionError, InvalidMetricError, InvalidNamespaceError | ||
import aws_embedded_metrics.constants as constants | ||
|
||
|
||
def validate_dimension_set(dimension_set: Dict[str, str]) -> None: | ||
""" | ||
Validates a dimension set | ||
|
||
Parameters: | ||
dimension_set (Dict[str, str]): The dimension set to validate | ||
|
||
Raises: | ||
DimensionSetExceededError: If the dimension set is too large | ||
InvalidDimensionError: If a dimension is invalid | ||
""" | ||
if len(dimension_set) > constants.MAX_DIMENSION_SET_SIZE: | ||
raise DimensionSetExceededError( | ||
f"Maximum number of dimensions per dimension set allowed are {constants.MAX_DIMENSION_SET_SIZE}") | ||
|
||
for name, value in dimension_set.items(): | ||
if not name or len(name.strip()) == 0: | ||
raise InvalidDimensionError("Dimension name must include at least one non-whitespace character") | ||
|
||
if not value or len(value.strip()) == 0: | ||
raise InvalidDimensionError("Dimension value must include at least one non-whitespace character") | ||
|
||
if len(name) > constants.MAX_DIMENSION_NAME_LENGTH: | ||
raise InvalidDimensionError(f"Dimension name cannot be longer than {constants.MAX_DIMENSION_NAME_LENGTH} characters") | ||
|
||
if len(value) > constants.MAX_DIMENSION_VALUE_LENGTH: | ||
raise InvalidDimensionError(f"Dimension value cannot be longer than {constants.MAX_DIMENSION_VALUE_LENGTH} characters") | ||
|
||
if not name.isascii(): | ||
raise InvalidDimensionError(f"Dimension name contains invalid characters: {name}") | ||
|
||
if not value.isascii(): | ||
raise InvalidDimensionError(f"Dimension value contains invalid characters: {value}") | ||
|
||
if name.startswith(":"): | ||
raise InvalidDimensionError("Dimension name cannot start with ':'") | ||
|
||
|
||
def validate_metric(name: str, value: float, unit: Optional[str]) -> None: | ||
""" | ||
Validates a metric | ||
|
||
Parameters: | ||
name (str): The name of the metric | ||
value (float): The value of the metric | ||
unit (Optional[str]): The unit of the metric | ||
|
||
Raises: | ||
InvalidMetricError: If the metric is invalid | ||
""" | ||
if not name or len(name.strip()) == 0: | ||
raise InvalidMetricError("Metric name must include at least one non-whitespace character") | ||
|
||
if len(name) > constants.MAX_DIMENSION_NAME_LENGTH: | ||
raise InvalidMetricError(f"Metric name cannot be longer than {constants.MAX_DIMENSION_NAME_LENGTH} characters") | ||
|
||
if not math.isfinite(value): | ||
raise InvalidMetricError("Metric value must be finite") | ||
|
||
if unit is not None and unit not in Unit: | ||
raise InvalidMetricError(f"Metric unit is not valid: {unit}") | ||
|
||
|
||
def validate_namespace(namespace: str) -> None: | ||
""" | ||
Validates a namespace | ||
|
||
Parameters: | ||
namespace (str): The namespace to validate | ||
|
||
Raises: | ||
InvalidNamespaceError: If the namespace is invalid | ||
""" | ||
if not namespace or len(namespace.strip()) == 0: | ||
raise InvalidNamespaceError("Namespace must include at least one non-whitespace character") | ||
|
||
if len(namespace) > constants.MAX_NAMESPACE_LENGTH: | ||
raise InvalidNamespaceError(f"Namespace cannot be longer than {constants.MAX_NAMESPACE_LENGTH} characters") | ||
|
||
if not re.match(constants.VALID_NAMESPACE_REGEX, namespace): | ||
raise InvalidNamespaceError(f"Namespace contains invalid characters: {namespace}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.