Skip to content

Commit 123036e

Browse files
committed
Add setting of path function for plugins
1 parent e69a708 commit 123036e

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

_file_configuration/src/opentelemetry/file_configuration/_internal/__init__.py

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@
5353
"number": float,
5454
}
5555

56+
_path_function = path_function
57+
58+
59+
def get_path_function() -> dict:
60+
return _path_function
61+
62+
63+
def set_path_function(path_function: dict) -> None:
64+
global _path_function
65+
_path_function = path_function
66+
5667

5768
class FileConfigurationPlugin(ABC):
5869

@@ -77,6 +88,13 @@ def function(*args, **kwargs) -> object:
7788
The function that will instantiate the plugin object.
7889
"""
7990

91+
@property
92+
def recursive_path(self) -> list:
93+
"""
94+
The recursive path for the plugin object if any.
95+
"""
96+
return []
97+
8098

8199
class SometimesMondaysOnSampler(Sampler):
82100
"""
@@ -106,12 +124,13 @@ def get_description(self) -> str:
106124
class SometimesMondaysOnSamplerPlugin(FileConfigurationPlugin):
107125

108126
@property
109-
def schema(self) -> dict:
127+
def schema(self) -> tuple:
110128
"""
111129
Returns the plugin schema.
112130
"""
113-
return {
114-
"sometimes_mondays_on": {
131+
return (
132+
"sometimes_mondays_on",
133+
{
115134
"type": "object",
116135
"additionalProperties": False,
117136
"properties": {
@@ -120,7 +139,7 @@ def schema(self) -> dict:
120139
},
121140
}
122141
}
123-
}
142+
)
124143

125144
@property
126145
def schema_path(self) -> list:
@@ -148,34 +167,54 @@ def resolve_schema(json_file_path) -> dict:
148167
root_path = json_file_path.absolute()
149168

150169
with open(json_file_path, "r") as json_file:
151-
dictionary = jsonref_loads(
170+
resolved_schema = jsonref_loads(
152171
json_file.read(), base_uri=root_path.as_uri()
153172
)
154173

155174
for entry_point in entry_points(group="opentelemetry_file_configuration"):
156175

157176
plugin = entry_point.load()()
158177

159-
sub_dictionary = dictionary
178+
sub_resolved_schema = resolved_schema
160179

161180
schema_path = []
162181

163182
for schema_path_part in plugin.schema_path:
164183
schema_path.append(schema_path_part)
165184
try:
166-
sub_dictionary = sub_dictionary[schema_path_part]
185+
sub_resolved_schema = sub_resolved_schema[schema_path_part]
167186
except KeyError:
168187
_logger.warning(
169-
"Unable to add plugin %s to schema: wrong path %s",
188+
"Unable to add plugin %s to schema: wrong schema path %s",
170189
entry_point.name,
171190
",".join(schema_path)
172191
)
173192
break
174193
else:
175-
for key, value in plugin.schema.items():
176-
sub_dictionary[key] = value
194+
sub_resolved_schema[plugin.schema[0]] = plugin.schema[1]
177195

178-
return dictionary
196+
original_path_function = get_path_function()
197+
sub_path_function = original_path_function
198+
199+
for schema_path_part in plugin.schema_path:
200+
201+
if schema_path_part == "properties":
202+
continue
203+
204+
sub_path_function = (
205+
sub_path_function[schema_path_part]["children"]
206+
)
207+
208+
sub_path_function[plugin.schema[0]] = {}
209+
sub_path_function[plugin.schema[0]]["function"] = plugin.function
210+
sub_path_function[plugin.schema[0]]["children"] = {}
211+
sub_path_function[plugin.schema[0]]["recursive_path"] = (
212+
plugin.recursive_path
213+
)
214+
215+
set_path_function(original_path_function)
216+
217+
return resolved_schema
179218

180219

181220
def load_file_configuration(file_configuration_file_path: str) -> dict:
@@ -231,15 +270,15 @@ def traverse(
231270

232271
for positional_attribute in positional_attributes:
233272

234-
result_positional_attributes[positional_attribute] = str(
273+
result_positional_attributes[positional_attribute] = (
235274
_type_type[
236275
schema_properties[positional_attribute]["type"]
237276
].__name__
238277
)
239278

240279
for optional_attribute in optional_attributes:
241280

242-
result_optional_attributes[optional_attribute] = str(
281+
result_optional_attributes[optional_attribute] = (
243282
_type_type[
244283
schema_properties[optional_attribute]["type"]
245284
].__name__
@@ -477,6 +516,8 @@ def create_object(
477516
*positional_arguments, **optional_arguments
478517
)
479518

519+
path_function = get_path_function()
520+
480521
result = create_object(
481522
file_configuration[object_name],
482523
processed_schema[object_name],

_file_configuration/tests/test_file_configuration.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
substitute_environment_variables,
3030
validate_file_configuration,
3131
)
32-
from opentelemetry.file_configuration._internal.path_function import set_resource
32+
from opentelemetry.file_configuration._internal.path_function import (
33+
set_resource,
34+
)
3335

3436
set_trace
3537

@@ -210,3 +212,14 @@ def test_plugin():
210212
["probability"]
211213
["type"]
212214
) == "number"
215+
216+
processed_schema = process_schema(resolved_schema)
217+
218+
set_resource(
219+
create_object(file_configuration, processed_schema, "resource")
220+
)
221+
222+
tracer_provider = create_object(
223+
file_configuration, processed_schema, "tracer_provider"
224+
)
225+
assert tracer_provider.sampler.sometimes_mondays_on._probability == 0.8

0 commit comments

Comments
 (0)