diff --git a/.codespellrc b/.codespellrc index b81e62f640c..bfc15a7dd26 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,4 +1,4 @@ [codespell] # skipping auto generated folders -skip = ./.tox,./.mypy_cache,./docs/_build,./target,*/LICENSE,./venv,.git,./opentelemetry-semantic-conventions +skip = ./.tox,./.mypy_cache,./docs/_build,./target,*/LICENSE,./venv,.git,./opentelemetry-semantic-conventions,./_file_configuration/.nox ignore-words-list = ans,ue,ot,hist,ro diff --git a/.flake8 b/.flake8 index 8e17b0ab21a..c9912b62768 100644 --- a/.flake8 +++ b/.flake8 @@ -22,6 +22,7 @@ exclude = .hg .svn .tox + .nox CVS .venv*/ venv*/ diff --git a/.isort.cfg b/.isort.cfg index ab1ab74ee59..2bf4f988989 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -14,6 +14,6 @@ profile=black ; docs: https://github.com/timothycrosley/isort#multi-line-output-modes multi_line_output=3 skip=target -skip_glob=**/gen/*,.venv*/*,venv*/*,**/proto/*,.tox/* +skip_glob=**/gen/*,.venv*/*,venv*/*,**/proto/*,.tox/*,_file_configuration/.nox/* known_first_party=opentelemetry,opentelemetry_example_app known_third_party=psutil,pytest,redis,redis_opentracing diff --git a/.pylintrc b/.pylintrc index 976ef7f4730..0c1548e5790 100644 --- a/.pylintrc +++ b/.pylintrc @@ -7,7 +7,7 @@ extension-pkg-whitelist= # Add list of files or directories to be excluded. They should be base names, not # paths. -ignore=CVS,gen,proto +ignore=CVS,gen,proto,_file_configuration # Add files or directories matching the regex patterns to be excluded. The # regex matches against base names, not paths. diff --git a/_file_configuration/src/opentelemetry/file_configuration/__init__.py b/_file_configuration/src/opentelemetry/file_configuration/__init__.py index 369c13db0a7..0d2b17adb8c 100644 --- a/_file_configuration/src/opentelemetry/file_configuration/__init__.py +++ b/_file_configuration/src/opentelemetry/file_configuration/__init__.py @@ -19,6 +19,7 @@ from opentelemetry.file_configuration._internal import ( + SometimesMondaysOnSamplerPlugin, create_object, load_file_configuration, process_schema, @@ -26,7 +27,6 @@ resolve_schema, substitute_environment_variables, validate_file_configuration, - SometimesMondaysOnSamplerPlugin ) __all__ = [ @@ -37,5 +37,5 @@ "create_object", "load_file_configuration", "substitute_environment_variables", - "SometimesMondayOnSamplerPlugin" + "SometimesMondaysOnSamplerPlugin", ] diff --git a/_file_configuration/src/opentelemetry/file_configuration/_internal/__init__.py b/_file_configuration/src/opentelemetry/file_configuration/_internal/__init__.py index ef5ea077bef..8460ca72ac0 100644 --- a/_file_configuration/src/opentelemetry/file_configuration/_internal/__init__.py +++ b/_file_configuration/src/opentelemetry/file_configuration/_internal/__init__.py @@ -12,32 +12,33 @@ # See the License for the specific language governing permissions and # limitations under the License. +from abc import ABC, abstractmethod from collections import OrderedDict +from datetime import datetime +from logging import getLogger from os import environ from pathlib import Path -from re import compile as re_compile -from abc import ABC, abstractmethod -from logging import getLogger -from datetime import datetime from random import random - +from re import compile as re_compile from typing import Optional, Sequence -from opentelemetry.trace import Link, SpanKind -from opentelemetry.trace.span import TraceState -from opentelemetry.util.types import Attributes -from opentelemetry.context import Context +from black import Mode, format_str from ipdb import set_trace from jinja2 import Environment, FileSystemLoader from jsonref import JsonRef from jsonref import loads as jsonref_loads from jsonschema.validators import Draft202012Validator from yaml import safe_load -from black import format_str, Mode -from opentelemetry.util._importlib_metadata import entry_points -from opentelemetry.file_configuration._internal.path_function import path_function +from opentelemetry.context import Context +from opentelemetry.file_configuration._internal.path_function import ( + path_function, +) from opentelemetry.sdk.trace.sampling import Sampler, SamplingResult +from opentelemetry.trace import Link, SpanKind +from opentelemetry.trace.span import TraceState +from opentelemetry.util._importlib_metadata import entry_points +from opentelemetry.util.types import Attributes _logger = getLogger(__file__) @@ -66,7 +67,6 @@ def set_path_function(path_function: dict) -> None: class FileConfigurationPlugin(ABC): - @property @abstractmethod def schema(self) -> dict: @@ -122,7 +122,6 @@ def get_description(self) -> str: class SometimesMondaysOnSamplerPlugin(FileConfigurationPlugin): - @property def schema(self) -> tuple: """ @@ -134,11 +133,9 @@ def schema(self) -> tuple: "type": "object", "additionalProperties": False, "properties": { - "probability": { - "type": "number" - }, - } - } + "probability": {"type": "number"}, + }, + }, ) @property @@ -151,7 +148,7 @@ def schema_path(self) -> list: "tracer_provider", "properties", "sampler", - "properties" + "properties", ] @staticmethod @@ -187,7 +184,7 @@ def resolve_schema(json_file_path) -> dict: _logger.warning( "Unable to add plugin %s to schema: wrong schema path %s", entry_point.name, - ",".join(schema_path) + ",".join(schema_path), ) break else: @@ -201,16 +198,14 @@ def resolve_schema(json_file_path) -> dict: if schema_path_part == "properties": continue - sub_path_function = ( - sub_path_function[schema_path_part]["children"] - ) + sub_path_function = sub_path_function[schema_path_part]["children"] sub_path_function[plugin.schema[0]] = {} sub_path_function[plugin.schema[0]]["function"] = plugin.function sub_path_function[plugin.schema[0]]["children"] = {} - sub_path_function[plugin.schema[0]]["recursive_path"] = ( - plugin.recursive_path - ) + sub_path_function[plugin.schema[0]][ + "recursive_path" + ] = plugin.recursive_path set_path_function(original_path_function) @@ -225,8 +220,7 @@ def load_file_configuration(file_configuration_file_path: str) -> dict: def validate_file_configuration( - schema: dict, - file_configuration: dict + schema: dict, file_configuration: dict ) -> None: Draft202012Validator(schema).validate(file_configuration) @@ -270,19 +264,17 @@ def traverse( for positional_attribute in positional_attributes: - result_positional_attributes[positional_attribute] = ( - _type_type[ - schema_properties[positional_attribute]["type"] - ].__name__ - ) + result_positional_attributes[ + positional_attribute + ] = _type_type[ + schema_properties[positional_attribute]["type"] + ].__name__ for optional_attribute in optional_attributes: - result_optional_attributes[optional_attribute] = ( - _type_type[ - schema_properties[optional_attribute]["type"] - ].__name__ - ) + result_optional_attributes[optional_attribute] = _type_type[ + schema_properties[optional_attribute]["type"] + ].__name__ children = {} @@ -436,7 +428,7 @@ def create_object( file_configuration: dict, processed_schema: dict, object_name: str, - dry_run=False + dry_run=False, ) -> object: def create_object( file_configuration: dict, @@ -449,7 +441,10 @@ def create_object( positional_arguments = [] optional_arguments = {} - for file_configuration_key, file_configuration_value in file_configuration.items(): + for ( + file_configuration_key, + file_configuration_value, + ) in file_configuration.items(): if isinstance(file_configuration_value, dict): @@ -467,7 +462,9 @@ def create_object( new_processed_schema = new_processed_schema[ file_configuration_key ] - new_path_function = new_path_function[file_configuration_key] + new_path_function = new_path_function[ + file_configuration_key + ] else: new_processed_schema = processed_schema["children"][ file_configuration_key @@ -493,7 +490,9 @@ def create_object( object_.append( create_object( element, - processed_schema["children"][file_configuration_key], + processed_schema["children"][ + file_configuration_key + ], path_function["children"][file_configuration_key], original_processed_schema, original_path_function, @@ -539,7 +538,10 @@ def traverse( original_processed_schema: dict, ): - for file_configuration_key, file_configuration_value in file_configuration.items(): + for ( + file_configuration_key, + file_configuration_value, + ) in file_configuration.items(): if file_configuration_key not in processed_schema.keys(): continue @@ -558,10 +560,14 @@ def traverse( children = children[recursive_path]["children"] else: - children = processed_schema[file_configuration_key]["children"] + children = processed_schema[file_configuration_key][ + "children" + ] traverse( - file_configuration_value, children, original_processed_schema + file_configuration_value, + children, + original_processed_schema, ) elif isinstance(file_configuration_value, list): @@ -570,13 +576,17 @@ def traverse( if isinstance(element, dict): traverse( element, - processed_schema[file_configuration_key]["children"], + processed_schema[file_configuration_key][ + "children" + ], original_processed_schema, ) elif isinstance(file_configuration_value, str): - match = _environment_variable_regex.match(file_configuration_value) + match = _environment_variable_regex.match( + file_configuration_value + ) if match is not None: diff --git a/_file_configuration/src/opentelemetry/file_configuration/_internal/path_function.py b/_file_configuration/src/opentelemetry/file_configuration/_internal/path_function.py index 5a08aa5dd04..cf70a787aa3 100644 --- a/_file_configuration/src/opentelemetry/file_configuration/_internal/path_function.py +++ b/_file_configuration/src/opentelemetry/file_configuration/_internal/path_function.py @@ -43,7 +43,6 @@ class MockSampler(Mock): - def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self._args = args @@ -67,7 +66,7 @@ def set_resource(resource): def attribute_limits( attribute_count_limit: int = None, attribute_value_length_limit: int = None, - **kwargs + **kwargs, ): pass @@ -162,7 +161,7 @@ def meter_provider_readers_periodic_exporter( console: object = None, otlp: object = None, prometheus: object = None, - **kwargs + **kwargs, ): pass @@ -204,7 +203,7 @@ def meter_provider_readers_pull_exporter( console: object = None, otlp: object = None, prometheus: object = None, - **kwargs + **kwargs, ): pass @@ -352,7 +351,7 @@ def tracer_provider_processors_batch_exporter( console: object = None, otlp: object = None, zipkin: object = None, - **kwargs + **kwargs, ): return console or otlp or zipkin @@ -410,7 +409,7 @@ def tracer_provider_processors_simple_exporter( console: object = None, otlp: object = None, zipkin: object = None, - **kwargs + **kwargs, ): return console or otlp or zipkin @@ -483,7 +482,7 @@ def tracer_provider_sampler( jaeger_remote: object = None, parent_based: object = None, trace_id_ratio_based: object = None, - **kwargs + **kwargs, ): return MockSampler( type="Sampler", @@ -492,7 +491,7 @@ def tracer_provider_sampler( jaeger_remote=jaeger_remote, parent_based=parent_based, trace_id_ratio_based=trace_id_ratio_based, - **kwargs + **kwargs, ) @@ -521,7 +520,7 @@ def tracer_provider_sampler_jaeger_remote_initial_sampler( jaeger_remote: object = None, parent_based: object = None, trace_id_ratio_based: object = None, - **kwargs + **kwargs, ): return MockSampler( type="InitialSamplerSampler", @@ -530,7 +529,7 @@ def tracer_provider_sampler_jaeger_remote_initial_sampler( jaeger_remote=jaeger_remote, parent_based=parent_based, trace_id_ratio_based=trace_id_ratio_based, - **kwargs + **kwargs, ) @@ -557,7 +556,7 @@ def tracer_provider_sampler_parent_based_root( jaeger_remote: object = None, parent_based: object = None, trace_id_ratio_based: object = None, - **kwargs + **kwargs, ): return MockSampler( type="RootSampler", @@ -566,7 +565,7 @@ def tracer_provider_sampler_parent_based_root( jaeger_remote=jaeger_remote, parent_based=parent_based, trace_id_ratio_based=trace_id_ratio_based, - **kwargs + **kwargs, ) @@ -576,7 +575,7 @@ def tracer_provider_sampler_parent_based_remote_parent_sampled( jaeger_remote: object = None, parent_based: object = None, trace_id_ratio_based: object = None, - **kwargs + **kwargs, ): return MockSampler( type="RemoteParentSampledSampler", @@ -585,7 +584,7 @@ def tracer_provider_sampler_parent_based_remote_parent_sampled( jaeger_remote=jaeger_remote, parent_based=parent_based, trace_id_ratio_based=trace_id_ratio_based, - **kwargs + **kwargs, ) @@ -595,7 +594,7 @@ def tracer_provider_sampler_parent_based_remote_parent_not_sampled( jaeger_remote: object = None, parent_based: object = None, trace_id_ratio_based: object = None, - **kwargs + **kwargs, ): return MockSampler( type="RemoteParentNotSampledSampler", @@ -604,7 +603,7 @@ def tracer_provider_sampler_parent_based_remote_parent_not_sampled( jaeger_remote=jaeger_remote, parent_based=parent_based, trace_id_ratio_based=trace_id_ratio_based, - **kwargs + **kwargs, ) @@ -614,7 +613,7 @@ def tracer_provider_sampler_parent_based_local_parent_sampled( jaeger_remote: object = None, parent_based: object = None, trace_id_ratio_based: object = None, - **kwargs + **kwargs, ): return MockSampler( type="LocalParentSampledSampler", @@ -623,7 +622,7 @@ def tracer_provider_sampler_parent_based_local_parent_sampled( jaeger_remote=jaeger_remote, parent_based=parent_based, trace_id_ratio_based=trace_id_ratio_based, - **kwargs + **kwargs, ) @@ -633,7 +632,7 @@ def tracer_provider_sampler_parent_based_local_parent_not_sampled( jaeger_remote: object = None, parent_based: object = None, trace_id_ratio_based: object = None, - **kwargs + **kwargs, ): return MockSampler( type="LocalParentNotSampledSampler", @@ -642,7 +641,7 @@ def tracer_provider_sampler_parent_based_local_parent_not_sampled( jaeger_remote=jaeger_remote, parent_based=parent_based, trace_id_ratio_based=trace_id_ratio_based, - **kwargs + **kwargs, ) diff --git a/_file_configuration/tests/test_file_configuration.py b/_file_configuration/tests/test_file_configuration.py index 250d8c2a017..5336c6b6b66 100644 --- a/_file_configuration/tests/test_file_configuration.py +++ b/_file_configuration/tests/test_file_configuration.py @@ -41,8 +41,9 @@ def test_create_object(): file_configuration = load_file_configuration( - data_path.joinpath("file_configuration"). - joinpath("file_configuration_0.yaml") + data_path.joinpath("file_configuration").joinpath( + "file_configuration_0.yaml" + ) ) schema_path = data_path.joinpath("schema").joinpath( @@ -58,7 +59,9 @@ def test_create_object(): processed_schema = process_schema(resolved_schema) - set_resource(create_object(file_configuration, processed_schema, "resource")) + set_resource( + create_object(file_configuration, processed_schema, "resource") + ) tracer_provider = create_object( file_configuration, processed_schema, "tracer_provider" @@ -107,8 +110,9 @@ def test_create_object(): def test_substitute_environment_variables(): file_configuration = load_file_configuration( - data_path.joinpath("file_configuration"). - joinpath("file_configuration_1.yaml") + data_path.joinpath("file_configuration").joinpath( + "file_configuration_1.yaml" + ) ) schema_path = data_path.joinpath("schema").joinpath( @@ -155,8 +159,9 @@ def test_render(tmpdir): def test_dry_run(): file_configuration = load_file_configuration( - data_path.joinpath("file_configuration"). - joinpath("file_configuration_0.yaml") + data_path.joinpath("file_configuration").joinpath( + "file_configuration_0.yaml" + ) ) schema_path = data_path.joinpath("schema").joinpath( @@ -172,12 +177,17 @@ def test_dry_run(): processed_schema = process_schema(resolved_schema) - set_resource(create_object(file_configuration, processed_schema, "resource")) + set_resource( + create_object(file_configuration, processed_schema, "resource") + ) print() print( create_object( - file_configuration, processed_schema, "tracer_provider", dry_run=True + file_configuration, + processed_schema, + "tracer_provider", + dry_run=True, ) ) @@ -185,8 +195,9 @@ def test_dry_run(): def test_plugin(): file_configuration = load_file_configuration( - data_path.joinpath("file_configuration"). - joinpath("file_configuration_2.yaml") + data_path.joinpath("file_configuration").joinpath( + "file_configuration_2.yaml" + ) ) schema_path = data_path.joinpath("schema").joinpath( @@ -201,16 +212,11 @@ def test_plugin(): fail(f"Unexpected exception raised: {error}") assert ( - resolved_schema - ["properties"] - ["tracer_provider"] - ["properties"] - ["sampler"] - ["properties"] - ["sometimes_mondays_on"] - ["properties"] - ["probability"] - ["type"] + resolved_schema["properties"]["tracer_provider"]["properties"][ + "sampler" + ]["properties"]["sometimes_mondays_on"]["properties"]["probability"][ + "type" + ] ) == "number" processed_schema = process_schema(resolved_schema) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py index f2c1fe9f9d8..606a61623ae 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py @@ -198,7 +198,6 @@ def __init__( class Sampler(abc.ABC): - def __init__(self, *args, **kwargs) -> None: self._args_kwargs = [repr(arg) for arg in args] self._args_kwargs.extend( @@ -224,7 +223,7 @@ def get_description(self) -> str: pass def __repr__(self) -> str: - return f'{self.__class__.__name__}({self._args_kwargs})' + return f"{self.__class__.__name__}({self._args_kwargs})" class StaticSampler(Sampler): @@ -350,7 +349,7 @@ def __init__( remote_parent_sampled, remote_parent_not_sampled, local_parent_sampled, - local_parent_not_sampled + local_parent_not_sampled, ) self._root = root self._remote_parent_sampled = remote_parent_sampled diff --git a/pyproject.toml b/pyproject.toml index 01ae2999afc..589cf96e3a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,7 @@ exclude = ''' ( /( # generated files .tox| + .nox| venv| venv.*| .venv.*|