diff --git a/semantic-conventions/src/opentelemetry/semconv/main.py b/semantic-conventions/src/opentelemetry/semconv/main.py index 57dfabaa..ce144d94 100644 --- a/semantic-conventions/src/opentelemetry/semconv/main.py +++ b/semantic-conventions/src/opentelemetry/semconv/main.py @@ -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 } @@ -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": @@ -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( @@ -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",