Skip to content

Commit 98f7474

Browse files
committed
Add dry_run feature
1 parent 435e9d0 commit 98f7474

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

_configuration/src/opentelemetry/configuration/_internal/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from jsonschema.validators import Draft202012Validator
2727
from referencing import Registry, Resource
2828
from yaml import safe_load
29+
from black import Mode, format_str
2930

3031
from opentelemetry.configuration._internal.path_function import path_function
3132

@@ -69,7 +70,6 @@ def validate_configuration(schema_path: Path, configuration: dict):
6970
raise Exception(f"{schema_path} does not exist")
7071

7172
def retrieve_from_path(path: str):
72-
set_trace()
7373
return Resource.from_contents(json_loads(Path(path).read_text()))
7474

7575
Draft202012Validator(
@@ -279,14 +279,18 @@ def traverse(
279279

280280

281281
def create_object(
282-
configuration: dict, processed_schema: dict, object_name: str
282+
configuration: dict,
283+
processed_schema: dict,
284+
object_name: str,
285+
dry_run=False
283286
) -> object:
284287
def create_object(
285288
configuration: dict,
286289
processed_schema: dict,
287290
path_function: dict,
288291
original_processed_schema: dict,
289292
original_path_function: dict,
293+
dry_run=False
290294
) -> object:
291295

292296
positional_arguments = []
@@ -355,18 +359,29 @@ def create_object(
355359
else:
356360
optional_arguments[configuration_key] = object_
357361

358-
return path_function["function"](
362+
result = path_function["function"](
359363
*positional_arguments, **optional_arguments
360364
)
361-
362-
return create_object(
365+
if dry_run:
366+
return result[1]
367+
elif isinstance(result, tuple):
368+
return result[0]
369+
else:
370+
return result
371+
372+
result = create_object(
363373
configuration[object_name],
364374
processed_schema[object_name],
365375
path_function[object_name],
366376
processed_schema,
367377
path_function,
378+
dry_run=dry_run,
368379
)
369380

381+
if isinstance(result, str):
382+
return format_str(result, mode=Mode(line_length=1))
383+
return result
384+
370385

371386
def substitute_environment_variables(
372387
configuration: dict, processed_schema: dict

_configuration/src/opentelemetry/configuration/_internal/path_function.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OTLPSpanExporter as HTTPOTLPSpanExporter,
2323
)
2424
from opentelemetry.exporter.zipkin.proto.http import ZipkinExporter
25-
from opentelemetry.sdk.resources import Resource
25+
from opentelemetry.sdk.resources import Resource # noqa
2626
from opentelemetry.sdk.trace import (
2727
SpanLimits,
2828
SynchronousMultiSpanProcessor,
@@ -38,6 +38,9 @@
3838
ALWAYS_ON,
3939
ParentBasedTraceIdRatio,
4040
)
41+
from ipdb import set_trace
42+
43+
set_trace
4144

4245
_resource = None
4346

@@ -634,11 +637,19 @@ def tracer_provider_sampler_trace_id_ratio_based(ratio: float = None):
634637

635638

636639
def resource(attributes: object = None, schema_url: str = None):
637-
return Resource.create(attributes=attributes, schema_url=schema_url)
640+
command = (
641+
f'resource = Resource.create(attributes={attributes}, '
642+
f'schema_url="{schema_url}")'
643+
)
644+
exec(command)
645+
return locals()["resource"], command
638646

639647

640648
def resource_attributes(service_name: str = None, **kwargs):
641-
return {"service.name": service_name, **kwargs}
649+
command = str({"service.name": service_name, **kwargs})
650+
command = f'resource_attributes = {command}'
651+
exec(command)
652+
return locals()["resource_attributes"], command
642653

643654

644655
path_function = {

_configuration/tests/test_configuration.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
)
3333
from opentelemetry.configuration._internal.path_function import set_resource
3434

35+
set_trace
36+
3537
data_path = Path(__file__).parent.joinpath("data")
3638

3739

@@ -152,12 +154,34 @@ def test_subschemas():
152154
# dictionary the schema components of each plugin component sub schema then
153155
# use the resulting schema dictionary to do the validation.
154156

155-
set_trace()
156-
157157
configuration = load_configuration(
158158
data_path.joinpath("configuration").joinpath("configuration_0.yaml")
159159
)
160160

161161
# FIXME do the same for configuration components
162162

163163
Draft202012Validator(resolved_schema).validate(configuration)
164+
165+
166+
def test_dry_run():
167+
168+
configuration = load_configuration(
169+
data_path.joinpath("configuration").joinpath("configuration_0.yaml")
170+
)
171+
172+
schema_path = data_path.joinpath("schema").joinpath(
173+
"opentelemetry_configuration.json"
174+
)
175+
176+
try:
177+
validate_configuration(schema_path, configuration)
178+
except Exception as error:
179+
fail(f"Unexpected exception raised: {error}")
180+
181+
processed_schema = process_schema(resolve_schema(schema_path))
182+
183+
result = create_object(
184+
configuration, processed_schema, "resource", dry_run=True
185+
)
186+
print()
187+
print(result)

0 commit comments

Comments
 (0)