diff --git a/_configuration/src/opentelemetry/configuration/_internal/__init__.py b/_configuration/src/opentelemetry/configuration/_internal/__init__.py index 891fbd10599..f082fc549ad 100644 --- a/_configuration/src/opentelemetry/configuration/_internal/__init__.py +++ b/_configuration/src/opentelemetry/configuration/_internal/__init__.py @@ -26,6 +26,7 @@ from jsonschema.validators import Draft202012Validator from referencing import Registry, Resource from yaml import safe_load +from black import Mode, format_str from opentelemetry.configuration._internal.path_function import path_function @@ -69,7 +70,6 @@ def validate_configuration(schema_path: Path, configuration: dict): raise Exception(f"{schema_path} does not exist") def retrieve_from_path(path: str): - set_trace() return Resource.from_contents(json_loads(Path(path).read_text())) Draft202012Validator( @@ -279,7 +279,10 @@ def traverse( def create_object( - configuration: dict, processed_schema: dict, object_name: str + configuration: dict, + processed_schema: dict, + object_name: str, + dry_run=False ) -> object: def create_object( configuration: dict, @@ -287,6 +290,7 @@ def create_object( path_function: dict, original_processed_schema: dict, original_path_function: dict, + dry_run=False ) -> object: positional_arguments = [] @@ -355,18 +359,29 @@ def create_object( else: optional_arguments[configuration_key] = object_ - return path_function["function"]( + result = path_function["function"]( *positional_arguments, **optional_arguments ) - - return create_object( + if dry_run: + return result[1] + elif isinstance(result, tuple): + return result[0] + else: + return result + + result = create_object( configuration[object_name], processed_schema[object_name], path_function[object_name], processed_schema, path_function, + dry_run=dry_run, ) + if isinstance(result, str): + return format_str(result, mode=Mode(line_length=1)) + return result + def substitute_environment_variables( configuration: dict, processed_schema: dict diff --git a/_configuration/src/opentelemetry/configuration/_internal/path_function.py b/_configuration/src/opentelemetry/configuration/_internal/path_function.py index ca9da03f549..bccf9231f2f 100644 --- a/_configuration/src/opentelemetry/configuration/_internal/path_function.py +++ b/_configuration/src/opentelemetry/configuration/_internal/path_function.py @@ -22,7 +22,7 @@ OTLPSpanExporter as HTTPOTLPSpanExporter, ) from opentelemetry.exporter.zipkin.proto.http import ZipkinExporter -from opentelemetry.sdk.resources import Resource +from opentelemetry.sdk.resources import Resource # noqa from opentelemetry.sdk.trace import ( SpanLimits, SynchronousMultiSpanProcessor, @@ -38,6 +38,9 @@ ALWAYS_ON, ParentBasedTraceIdRatio, ) +from ipdb import set_trace + +set_trace _resource = None @@ -634,11 +637,19 @@ def tracer_provider_sampler_trace_id_ratio_based(ratio: float = None): def resource(attributes: object = None, schema_url: str = None): - return Resource.create(attributes=attributes, schema_url=schema_url) + command = ( + f'resource = Resource.create(attributes={attributes}, ' + f'schema_url="{schema_url}")' + ) + exec(command) + return locals()["resource"], command def resource_attributes(service_name: str = None, **kwargs): - return {"service.name": service_name, **kwargs} + command = str({"service.name": service_name, **kwargs}) + command = f'resource_attributes = {command}' + exec(command) + return locals()["resource_attributes"], command path_function = { diff --git a/_configuration/tests/test_configuration.py b/_configuration/tests/test_configuration.py index 95c53c18b83..253cae28bd4 100644 --- a/_configuration/tests/test_configuration.py +++ b/_configuration/tests/test_configuration.py @@ -32,6 +32,8 @@ ) from opentelemetry.configuration._internal.path_function import set_resource +set_trace + data_path = Path(__file__).parent.joinpath("data") @@ -152,8 +154,6 @@ def test_subschemas(): # dictionary the schema components of each plugin component sub schema then # use the resulting schema dictionary to do the validation. - set_trace() - configuration = load_configuration( data_path.joinpath("configuration").joinpath("configuration_0.yaml") ) @@ -161,3 +161,27 @@ def test_subschemas(): # FIXME do the same for configuration components Draft202012Validator(resolved_schema).validate(configuration) + + +def test_dry_run(): + + configuration = load_configuration( + data_path.joinpath("configuration").joinpath("configuration_0.yaml") + ) + + schema_path = data_path.joinpath("schema").joinpath( + "opentelemetry_configuration.json" + ) + + try: + validate_configuration(schema_path, configuration) + except Exception as error: + fail(f"Unexpected exception raised: {error}") + + processed_schema = process_schema(resolve_schema(schema_path)) + + result = create_object( + configuration, processed_schema, "resource", dry_run=True + ) + print() + print(result)