Skip to content

Commit

Permalink
Allow multiple semconv in --only flag (#157)
Browse files Browse the repository at this point in the history
* allowing multiple only flags to generate multiple conventions

* oops

* review comments

* lint

* lint

* validate --only

* lint
  • Loading branch information
lmolkova committed Mar 15, 2023
1 parent 92855e2 commit 5da2fe5
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions semantic-conventions/src/opentelemetry/semconv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def exclude_file_list(folder: str, pattern: str) -> List[str]:
return file_names


def filter_semconv(semconv, type_filter):
if type_filter:
def filter_semconv(semconv, types):
if types:
semconv.models = {
id: model
for id, model in semconv.models.items()
if model.GROUP_TYPE_NAME == type_filter
if model.GROUP_TYPE_NAME in types
}


Expand All @@ -65,7 +65,8 @@ def main():
args = parser.parse_args()
check_args(args, parser)
semconv = parse_semconv(args, parser)
filter_semconv(semconv, args.only)
semconv_filter = parse_only_filter(args, parser)
filter_semconv(semconv, semconv_filter)
if len(semconv.models) == 0:
parser.error("No semantic convention model found!")
if args.flavor == "code":
Expand Down Expand Up @@ -109,6 +110,20 @@ def check_args(arguments, parser):
parser.error("Either --yaml-root or YAML_FILE must be present")


def parse_only_filter(arguments, parser):
if not arguments.only:
return None

types = [t.strip() for t in arguments.only.split(",")]
unknown_types = [t for t in types if t not in CONVENTION_CLS_BY_GROUP_TYPE.keys()]
if unknown_types:
parser.error(
f"Unknown semconv names in `--only` option: '{', '.join(unknown_types)}'"
)
sys.exit(1)
return types


def add_code_parser(subparsers):
parser = subparsers.add_parser("code")
parser.add_argument(
Expand Down Expand Up @@ -210,8 +225,18 @@ def setup_parser():
)
parser.add_argument(
"--only",
choices=list(CONVENTION_CLS_BY_GROUP_TYPE.keys()),
help="Process only semantic conventions of the specified type.",
type=str,
help=f"""Generates semantic conventions of the specified types only
({", ".join(CONVENTION_CLS_BY_GROUP_TYPE.keys())}).
To generate multiple conventions at once, pass comma-separated list of
convention names, e.g. '--only span,event'.
The `--only` flag filters the output and does not apply to input.
Resolution of referenced attributes (using `ref`), or semantic conventions
(using `exclude` or `include`) is done against all semantic convention
files provided as input using `--yaml-root` or `YAML_FILE` options.
""",
)
parser.add_argument(
"--yaml-root",
Expand Down

0 comments on commit 5da2fe5

Please sign in to comment.