Skip to content

Commit

Permalink
Successfully created a resource object
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Nov 14, 2023
1 parent 9ca78b1 commit 86a8796
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand All @@ -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 = (
Expand All @@ -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("/", "_"),
Expand Down Expand Up @@ -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")
Expand All @@ -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
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from opentelemetry.sdk.resources import Resource

# flake8: noqa: E501


Expand Down Expand Up @@ -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 = {
Expand Down
11 changes: 9 additions & 2 deletions prototypes/python/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 86a8796

Please sign in to comment.