From 86a8796319d7c79aae6ebeb42c439db469812bf3 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 14 Nov 2023 17:25:50 -0600 Subject: [PATCH] Successfully created a resource object --- .../configuration/_internal/configurator.py | 90 +++++++++++-------- .../_internal/schema_paths_functions.py | 6 +- prototypes/python/tests/test_configuration.py | 11 ++- 3 files changed, 64 insertions(+), 43 deletions(-) diff --git a/prototypes/python/src/opentelemetry/configuration/_internal/configurator.py b/prototypes/python/src/opentelemetry/configuration/_internal/configurator.py index d8e1952..b3f4522 100644 --- a/prototypes/python/src/opentelemetry/configuration/_internal/configurator.py +++ b/prototypes/python/src/opentelemetry/configuration/_internal/configurator.py @@ -15,6 +15,7 @@ from os.path import exists from pathlib import Path from os import getcwd +from collections import OrderedDict from json import loads, dumps from jsonschema.validators import Draft202012Validator from referencing import Registry, Resource @@ -215,7 +216,8 @@ def traverse(schema_dictionary, schema_path, schema_paths): positional_attributes = sorted(list(positional_attributes)) optional_attributes = sorted(list(optional_attributes)) - result_positional_attributes = [] + result_positional_attributes = OrderedDict() + result_optional_attributes = OrderedDict() for positional_attribute in positional_attributes: @@ -237,15 +239,10 @@ def traverse(schema_dictionary, schema_path, schema_paths): else: type_ = "object" - result_positional_attributes.append( - { - "attribute": positional_attribute, - "type": type_ - } + result_positional_attributes[positional_attribute] = ( + type_ ) - result_optional_attributes = [] - for optional_attribute in optional_attributes: optional_attribute_value = ( @@ -266,12 +263,7 @@ def traverse(schema_dictionary, schema_path, schema_paths): else: type_ = "object" - result_optional_attributes.append( - { - "attribute": optional_attribute, - "type": type_ - } - ) + result_optional_attributes[optional_attribute] = type_ schema_paths[schema_path_joined] = { "function_name": schema_path_joined.replace("/", "_"), @@ -366,19 +358,19 @@ def write_schema_paths_functions( f"def {schema_path_value['function_name']}(\n" ) attributes = [] - for positional_attribute in ( - schema_path_value["positional_attributes"] + for positional_attribute_name, positional_attribute_type in ( + schema_path_value["positional_attributes"].items() ): attributes.append( - f" {positional_attribute['attribute']}: " - f"{positional_attribute['type']}," + f" {positional_attribute_name}: " + f"{positional_attribute_type}," ) - for optional_attribute in ( - schema_path_value["optional_attributes"] + for optional_attribute_name, optional_attribute_type in ( + schema_path_value["optional_attributes"].items() ): attributes.append( - f" {optional_attribute['attribute']}: " - f"{optional_attribute['type']} = None," + f" {optional_attribute_name}: " + f"{optional_attribute_type} = None," ) if schema_path_value["additional_properties"]: attributes.append(" **kwargs") @@ -396,38 +388,58 @@ def write_schema_paths_functions( ) -def create_configuration_objects(configuration_dictionary, schema_paths): - - def traverse(dictionary, path, schema_paths): +def create_configuration_object( + configuration_dictionary, schema_paths, object_name +): - print(path) + def create_object(configuration_dictionary, path, schema_paths) -> object: - for dictionary_key, dictionary_value in dictionary.items(): + positional_arguments = [] + optional_arguments = {} - if isinstance(dictionary_value, dict): + schema_path = "/".join(path) - path.append(dictionary_key.lower()) + for configuration_dictionary_key, configuration_dictionary_value in ( + configuration_dictionary.items() + ): - schema_path = "/".join(path) + if isinstance(configuration_dictionary_value, dict): - if schema_path in path_function.keys(): - set_trace() + path.append(configuration_dictionary_key.lower()) - traverse( - dictionary_value, + object_ = create_object( + configuration_dictionary_value, path, schema_paths ) path.pop() - elif isinstance(dictionary_value, list): + elif isinstance(configuration_dictionary_value, list): - path.append(dictionary_key.lower()) + path.append(configuration_dictionary_key.lower()) - for element in dictionary_value: + for element in configuration_dictionary_value: if isinstance(element, dict): - traverse(element, path, schema_paths) + object_ = create_object(element, path, schema_paths) path.pop() - traverse(configuration_dictionary, [], schema_paths) + else: + + object_ = configuration_dictionary_value + + if configuration_dictionary_key in ( + schema_paths[schema_path]["positional_attributes"].keys() + ): + positional_arguments.append(object_) + + else: + optional_arguments[configuration_dictionary_key] = object_ + + return path_function[schema_path]( + *positional_arguments, **optional_arguments + ) + + return create_object( + configuration_dictionary, object_name, schema_paths + ) diff --git a/prototypes/python/src/opentelemetry/configuration/_internal/schema_paths_functions.py b/prototypes/python/src/opentelemetry/configuration/_internal/schema_paths_functions.py index 2be49b9..98034a9 100644 --- a/prototypes/python/src/opentelemetry/configuration/_internal/schema_paths_functions.py +++ b/prototypes/python/src/opentelemetry/configuration/_internal/schema_paths_functions.py @@ -1,3 +1,5 @@ +from opentelemetry.sdk.resources import Resource + # flake8: noqa: E501 @@ -342,14 +344,14 @@ def resource( attributes: object = None, schema_url: str = None, ): - pass + return Resource.create(attributes=attributes, schema_url=schema_url) def resource_attributes( service_name: str = None, **kwargs ): - pass + result = {"service.name": service_name, **kwargs} path_function = { diff --git a/prototypes/python/tests/test_configuration.py b/prototypes/python/tests/test_configuration.py index 59a00c3..969a1b9 100644 --- a/prototypes/python/tests/test_configuration.py +++ b/prototypes/python/tests/test_configuration.py @@ -20,7 +20,7 @@ get_schema_paths, Configurator, write_schema_paths_functions, - create_configuration_objects + create_configuration_object ) from opentelemetry.configuration._internal.yaml_parser import YAMLParser from pathlib import Path @@ -134,12 +134,19 @@ def test_create_configuration_objects(): configuration = YAMLParser().parse(data_path.joinpath("kitchen-sink.yaml")) + set_trace() + result = resolve_schema( data_path.joinpath("opentelemetry_configuration.json") ) result = get_schema_paths(result) - create_configuration_objects(configuration, result) + tracer_provider = create_configuration_object( + # configuration["tracer_provider"], result, ["tracer_provider"] + configuration["resource"], result, ["resource"] + ) + + tracer_provider def test_bad_attribute_limits():