Skip to content

Commit

Permalink
Add flag to suppress validation errors to simplify codegen and back c…
Browse files Browse the repository at this point in the history
…ompat checks (#300)
  • Loading branch information
lmolkova committed Mar 19, 2024
1 parent 157bb4b commit 761b9f0
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 188 deletions.
4 changes: 4 additions & 0 deletions semantic-conventions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Please update the changelog as part of any significant pull request.
([#266](https://github.com/open-telemetry/build-tools/pull/266))
- Sort attribute tables by requirement level and attribute name
([#260](https://github.com/open-telemetry/build-tools/pull/260))
- Support suppressing all validation errors via flag that allows to
parse previous versions of semantic conventions for backward compatibility checks
and use code generation improvements on older semantic convention version.
([#300](https://github.com/open-telemetry/build-tools/pull/300))

## v0.23.0

Expand Down
10 changes: 8 additions & 2 deletions semantic-conventions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ The following checks are performed:
This check does not take into account opt-in attributes. Adding new attributes to metric is not always breaking,
so it's considered non-critical and it's possible to suppress it with `--ignore-warnings`

Previous versions of semantic conventions are not always compatible with newer versions of build-tools. You can suppress validation errors by adding `--continue-on-validation-errors` flag:

```bash
docker run --rm otel/semconvgen --yaml-root {yaml_folder} --continue-on-validation-errors compatibility --previous-version {semconv version}
```

## Code Generator

The image supports [Jinja](https://jinja.palletsprojects.com/en/2.11.x/) templates to generate code from the models.
Expand Down Expand Up @@ -171,13 +177,13 @@ Finally, additional value can be passed to the template in form of `key=value` p
comma using the `--parameters [{key=value},]+` or `-D` flag.

Generating code from older versions of semantic conventions with new tooling is, in general, not supported.
However in some cases minor incompatibilities in semantic conventions can be ignored by setting `--strict-validation` flag to `false`
However in some cases minor incompatibilities in semantic conventions can be suppressed by adding `--continue-on-validation-errors` flag:

```bash
docker run --rm \
otel/semconvgen:$GENERATOR_VERSION \
--yaml-root /source \
`--strict-validation false`
--continue-on-validation-errors \
code \
...other parameters...
```
Expand Down
38 changes: 22 additions & 16 deletions semantic-conventions/src/opentelemetry/semconv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
CONVENTION_CLS_BY_GROUP_TYPE,
SemanticConventionSet,
)
from opentelemetry.semconv.model.utils import ValidationContext
from opentelemetry.semconv.templating.code import CodeRenderer
from opentelemetry.semconv.templating.compatibility import CompatibilityChecker
from opentelemetry.semconv.templating.markdown import MarkdownRenderer
Expand All @@ -42,7 +43,7 @@ def parse_semconv(
for file in sorted(files):
if not file.endswith(".yaml") and not file.endswith(".yml"):
parser.error(f"{file} is not a yaml file.")
semconv.parse(file, strict_validation)
semconv.parse(file, ValidationContext(file, strict_validation))
semconv.finish()
if semconv.has_error():
sys.exit(1)
Expand Down Expand Up @@ -73,7 +74,11 @@ def main():
args = parser.parse_args()
check_args(args, parser)
semconv = parse_semconv(
args.yaml_root, args.exclude, args.debug, args.strict_validation, parser
args.yaml_root,
args.exclude,
args.debug,
not args.continue_on_validation_errors,
parser,
)
semconv_filter = parse_only_filter(args.only, parser)
filter_semconv(semconv, semconv_filter)
Expand All @@ -93,9 +98,9 @@ def main():
def process_markdown(semconv, args):
options = MarkdownOptions(
check_only=args.md_check,
disable_stable_badge=args.md_disable_stable,
disable_experimental_badge=args.md_disable_experimental,
disable_deprecated_badge=args.md_disable_deprecated,
disable_stable_badge=args.md_disable_stable_badge,
disable_experimental_badge=args.md_disable_experimental_badge,
disable_deprecated_badge=args.md_disable_deprecated_badge,
break_count=args.md_break_conditional,
exclude_files=exclude_file_list(args.markdown_root, args.exclude),
)
Expand All @@ -106,7 +111,11 @@ def process_markdown(semconv, args):
def check_compatibility(semconv, args, parser):
prev_semconv_path = download_previous_version(args.previous_version)
prev_semconv = parse_semconv(
prev_semconv_path, args.exclude, args.debug, args.strict_validation, parser
prev_semconv_path,
args.exclude,
args.debug,
not args.continue_on_validation_errors,
parser,
)
compatibility_checker = CompatibilityChecker(semconv, prev_semconv)
problems = compatibility_checker.check()
Expand Down Expand Up @@ -220,12 +229,6 @@ def add_md_parser(subparsers):
required=False,
action="store_true",
)
parser.add_argument(
"--check-compat",
help="Check backward compatibility with previous version of semantic conventions.",
type=str,
required=False,
)
parser.add_argument(
"--md-disable-stable-badge",
help="Removes badges from attributes marked as stable.",
Expand Down Expand Up @@ -306,11 +309,14 @@ def setup_parser():
help="YAML file containing a Semantic Convention",
)
parser.add_argument(
"--strict-validation",
help="Fail on non-critical yaml validation issues.",
"--continue-on-validation-errors",
help="""Continue parsing on yaml validation issues.
Should not be used to generate or validate semantic conventions.
Useful when running backward compatibility checks or using newer
tooling version to generate code for released semantic conventions.""",
required=False,
default=True,
action="store_false",
default=False,
action="store_true",
)
subparsers = parser.add_subparsers(dest="flavor")
add_code_parser(subparsers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from ruamel.yaml.comments import CommentedSeq

from opentelemetry.semconv.model.exceptions import ValidationError
from opentelemetry.semconv.model.semantic_attribute import SemanticAttribute
from opentelemetry.semconv.model.utils import validate_values

Expand Down Expand Up @@ -64,20 +63,20 @@ class Include:
semconv_id: str


def parse_constraints(yaml_constraints):
def parse_constraints(yaml_constraints, validation_ctx):
"""This method parses the yaml representation for semantic convention attributes
creating a list of Constraint objects.
"""
constraints = ()
allowed_keys = ("include", "any_of")
for constraint in yaml_constraints:
validate_values(constraint, allowed_keys)
validate_values(constraint, allowed_keys, validation_ctx)
if len(constraint.keys()) > 1:
position = constraint.lc.data[list(constraint)[1]]
msg = (
"Invalid entry in constraint array - multiple top-level keys in entry."
)
raise ValidationError.from_yaml_pos(position, msg)
validation_ctx.raise_or_warn(position, msg, None)
if "include" in constraint:
constraints += (Include(constraint.get("include")),)
elif "any_of" in constraint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ class ValidationError(Exception):
line -- line in the file where the error occurred
column -- column in the file where the error occurred
message -- reason of the error
fqn -- identifier of the node that contains the error
"""

@classmethod
def from_yaml_pos(cls, pos, msg):
# the yaml parser starts counting from 0
# while in document is usually reported starting from 1
return cls(pos[0] + 1, pos[1] + 1, msg)

def __init__(self, line, column, message):
super().__init__(line, column, message)
def __init__(self, line, column, message, fqn):
super().__init__(line, column, message, fqn)
self.message = message
self.line = line
self.column = column
self.fqn = fqn

def __str__(self):
return f"{self.message} - @{self.line}:{self.column}"
return f"{self.message} - @{self.line}:{self.column} ('{self.fqn}')"
Loading

0 comments on commit 761b9f0

Please sign in to comment.