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

Allow multiple semconv in --only flag #157

Merged
merged 8 commits into from
Mar 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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