Skip to content

Commit

Permalink
Add dry_run feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jan 23, 2024
1 parent 435e9d0 commit 98f7474
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -279,14 +279,18 @@ 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,
processed_schema: dict,
path_function: dict,
original_processed_schema: dict,
original_path_function: dict,
dry_run=False
) -> object:

positional_arguments = []
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,6 +38,9 @@
ALWAYS_ON,
ParentBasedTraceIdRatio,
)
from ipdb import set_trace

set_trace

_resource = None

Expand Down Expand Up @@ -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 = {
Expand Down
28 changes: 26 additions & 2 deletions _configuration/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
)
from opentelemetry.configuration._internal.path_function import set_resource

set_trace

data_path = Path(__file__).parent.joinpath("data")


Expand Down Expand Up @@ -152,12 +154,34 @@ 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")
)

# 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)

0 comments on commit 98f7474

Please sign in to comment.