Skip to content

Commit eb4baea

Browse files
committed
Add schema validation for plugin
1 parent f450321 commit eb4baea

File tree

5 files changed

+211
-21
lines changed

5 files changed

+211
-21
lines changed

_file_configuration/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ dependencies = [
4141
[project.urls]
4242
Homepage = "https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-file-configuration"
4343

44-
[project.entry-points.opentelemetry_id_generator]
45-
sometimes_monday_on_sampler = "opentelemetry.sdk.trace.id_generator:RandomIdGenerator"
44+
[project.entry-points.opentelemetry_file_configuration]
45+
sometimes_mondays_on_sampler = "opentelemetry.file_configuration:SometimesMondaysOnSamplerPlugin"
4646

4747
[tool.hatch.version]
4848
path = "src/opentelemetry/file_configuration/version.py"

_file_configuration/src/opentelemetry/file_configuration/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
resolve_schema,
2727
substitute_environment_variables,
2828
validate_file_configuration,
29+
SometimesMondaysOnSamplerPlugin
2930
)
3031

3132
__all__ = [
@@ -36,4 +37,5 @@
3637
"create_object",
3738
"load_file_configuration",
3839
"substitute_environment_variables",
40+
"SometimesMondayOnSamplerPlugin"
3941
]

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
# limitations under the License.
1414

1515
from collections import OrderedDict
16-
from json import loads as json_loads
1716
from os import environ
18-
from os.path import exists
1917
from pathlib import Path
2018
from re import compile as re_compile
2119
from abc import ABC, abstractmethod
@@ -34,7 +32,6 @@
3432
from jsonref import JsonRef
3533
from jsonref import loads as jsonref_loads
3634
from jsonschema.validators import Draft202012Validator
37-
from referencing import Registry, Resource
3835
from yaml import safe_load
3936
from black import format_str, Mode
4037
from opentelemetry.util._importlib_metadata import entry_points
@@ -81,7 +78,7 @@ def function(*args, **kwargs) -> object:
8178
"""
8279

8380

84-
class SometimesMondayOnSampler(Sampler):
81+
class SometimesMondaysOnSampler(Sampler):
8582
"""
8683
A sampler that samples only on Mondays, but sometimes.
8784
"""
@@ -114,7 +111,7 @@ def schema(self) -> dict:
114111
Returns the plugin schema.
115112
"""
116113
return {
117-
"sometimes_monday_on": {
114+
"sometimes_mondays_on": {
118115
"type": "object",
119116
"additionalProperties": False,
120117
"properties": {
@@ -139,10 +136,11 @@ def schema_path(self) -> list:
139136
]
140137

141138
@staticmethod
142-
def function(probability: float) -> object:
139+
def function(probability: float) -> SometimesMondaysOnSampler:
143140
"""
144141
The function that will instantiate the plugin object.
145142
"""
143+
return SometimesMondaysOnSampler(probability)
146144

147145

148146
def resolve_schema(json_file_path) -> dict:
@@ -173,6 +171,9 @@ def resolve_schema(json_file_path) -> dict:
173171
",".join(schema_path)
174172
)
175173
break
174+
else:
175+
for key, value in plugin.schema.items():
176+
sub_dictionary[key] = value
176177

177178
return dictionary
178179

@@ -184,19 +185,11 @@ def load_file_configuration(file_configuration_file_path: str) -> dict:
184185
return safe_load(file_configuration_file)
185186

186187

187-
def validate_file_configuration(schema_path: Path, file_configuration: dict):
188-
189-
schema_path = str(schema_path)
190-
191-
if not exists(schema_path):
192-
raise Exception(f"{schema_path} does not exist")
193-
194-
def retrieve_from_path(path: str):
195-
return Resource.from_contents(json_loads(Path(path).read_text()))
196-
197-
Draft202012Validator(
198-
{"$ref": schema_path}, registry=Registry(retrieve=retrieve_from_path)
199-
).validate(file_configuration)
188+
def validate_file_configuration(
189+
schema: dict,
190+
file_configuration: dict
191+
) -> None:
192+
Draft202012Validator(schema).validate(file_configuration)
200193

201194

202195
def process_schema(schema: dict) -> dict:
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# kitchen-sink.yaml demonstrates all configurable surface area, including explanatory comments.
2+
#
3+
# It DOES NOT represent expected real world file configuration, as it makes strange file configuration
4+
# choices in an effort to exercise the full surface area.
5+
#
6+
# Configuration values are set to their defaults when default values are defined.
7+
8+
# The file format version
9+
file_format: "0.1"
10+
11+
# Configure if the SDK is disabled or not. This is not required to be provided
12+
# to ensure the SDK isn't disabled, the default value when this is not provided
13+
# is for the SDK to be enabled.
14+
#
15+
# Environment variable: OTEL_SDK_DISABLED
16+
disabled: false
17+
18+
# Configure general attribute limits. See also tracer_provider.limits, logger_provider.limits.
19+
attribute_limits:
20+
# Configure max attribute value size.
21+
#
22+
# Environment variable: OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT
23+
attribute_value_length_limit: 4096
24+
# Configure max attribute count.
25+
#
26+
# Environment variable: OTEL_ATTRIBUTE_COUNT_LIMIT
27+
attribute_count_limit: 128
28+
29+
# Configure text map context propagators.
30+
#
31+
# Environment variable: OTEL_PROPAGATORS
32+
propagator:
33+
composite: [tracecontext, baggage, b3, b3multi, jaeger, xray, ottrace]
34+
35+
# Configure tracer provider.
36+
tracer_provider:
37+
# Configure span processors.
38+
processors:
39+
# Configure a batch span processor.
40+
- batch:
41+
# Configure delay interval (in milliseconds) between two consecutive exports.
42+
#
43+
# Environment variable: OTEL_BSP_SCHEDULE_DELAY
44+
schedule_delay: 5000
45+
# Configure maximum allowed time (in milliseconds) to export data.
46+
#
47+
# Environment variable: OTEL_BSP_EXPORT_TIMEOUT
48+
export_timeout: 30000
49+
# Configure maximum queue size.
50+
#
51+
# Environment variable: OTEL_BSP_MAX_QUEUE_SIZE
52+
max_queue_size: 2048
53+
# Configure maximum batch size.
54+
#
55+
# Environment variable: OTEL_BSP_MAX_EXPORT_BATCH_SIZE
56+
max_export_batch_size: 512
57+
# Configure exporter.
58+
#
59+
# Environment variable: OTEL_TRACES_EXPORTER
60+
exporter:
61+
# Configure exporter to be OTLP.
62+
otlp:
63+
# Configure protocol.
64+
#
65+
# Environment variable: OTEL_EXPORTER_OTLP_PROTOCOL, OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
66+
protocol: http/protobuf
67+
# Configure endpoint.
68+
#
69+
# Environment variable: OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
70+
endpoint: http://localhost:4318
71+
# Configure certificate.
72+
#
73+
# Environment variable: OTEL_EXPORTER_OTLP_CERTIFICATE, OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE
74+
certificate: /app/cert.pem
75+
# Configure mTLS private client key.
76+
#
77+
# Environment variable: OTEL_EXPORTER_OTLP_CLIENT_KEY, OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY
78+
client_key: /app/cert.pem
79+
# Configure mTLS client certificate.
80+
#
81+
# Environment variable: OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE, OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE
82+
client_certificate: /app/cert.pem
83+
# Configure headers.
84+
#
85+
# Environment variable: OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_TRACES_HEADERS
86+
headers:
87+
api-key: !!str 1234
88+
# Configure compression.
89+
#
90+
# Environment variable: OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_TRACES_COMPRESSION
91+
compression: gzip
92+
# Configure max time (in milliseconds) to wait for each export.
93+
#
94+
# Environment variable: OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_TRACES_TIMEOUT
95+
timeout: 10000
96+
# Configure a batch span processor.
97+
- batch:
98+
# Configure exporter.
99+
#
100+
# Environment variable: OTEL_TRACES_EXPORTER
101+
exporter:
102+
# Configure exporter to be zipkin.
103+
zipkin:
104+
# Configure endpoint.
105+
#
106+
# Environment variable: OTEL_EXPORTER_ZIPKIN_ENDPOINT
107+
endpoint: http://localhost:9411/api/v2/spans
108+
# Configure max time (in milliseconds) to wait for each export.
109+
#
110+
# Environment variable: OTEL_EXPORTER_ZIPKIN_TIMEOUT
111+
timeout: 10000
112+
# Configure a simple span processor.
113+
- simple:
114+
# Configure exporter.
115+
exporter:
116+
# Configure exporter to be console.
117+
console: {}
118+
# Configure span limits. See also attribute_limits.
119+
limits:
120+
# Configure max span attribute value size. Overrides attribute_limits.attribute_value_length_limit.
121+
#
122+
# Environment variable: OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT
123+
attribute_value_length_limit: 4096
124+
# Configure max span attribute count. Overrides attribute_limits.attribute_count_limit.
125+
#
126+
# Environment variable: OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT
127+
attribute_count_limit: 128
128+
# Configure max span event count.
129+
#
130+
# Environment variable: OTEL_SPAN_EVENT_COUNT_LIMIT
131+
event_count_limit: 128
132+
# Configure max span link count.
133+
#
134+
# Environment variable: OTEL_SPAN_LINK_COUNT_LIMIT
135+
link_count_limit: 128
136+
# Configure max attributes per span event.
137+
#
138+
# Environment variable: OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT
139+
event_attribute_count_limit: 128
140+
# Configure max attributes per span link.
141+
#
142+
# Environment variable: OTEL_LINK_ATTRIBUTE_COUNT_LIMIT
143+
link_attribute_count_limit: 128
144+
# Configure the sampler.
145+
sampler:
146+
# Configure sampler to be parent_based. Known values include: always_off, always_on, jaeger_remote, parent_based, trace_id_ratio_based.
147+
#
148+
# Environment variable: OTEL_TRACES_SAMPLER=parentbased_*
149+
sometimes_mondays_on:
150+
probability: 0.8
151+
152+
# Configure resource for all signals.
153+
resource:
154+
# Configure resource attributes.
155+
#
156+
# Environment variable: OTEL_RESOURCE_ATTRIBUTES
157+
attributes:
158+
# Configure `service.name` resource attribute
159+
#
160+
# Environment variable: OTEL_SERVICE_NAME
161+
service.name: !!str "unknown_service"
162+
# Configure the resource schema URL.
163+
schema_url: https://opentelemetry.io/schemas/1.16.0

_file_configuration/tests/test_file_configuration.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,35 @@ def test_dry_run():
188188
file_configuration, processed_schema, "tracer_provider", dry_run=True
189189
)
190190
)
191+
192+
193+
def test_plugin():
194+
195+
file_configuration = load_file_configuration(
196+
data_path.joinpath("file_configuration").
197+
joinpath("file_configuration_2.yaml")
198+
)
199+
200+
schema_path = data_path.joinpath("schema").joinpath(
201+
"opentelemetry_file_configuration.json"
202+
)
203+
204+
resolved_schema = resolve_schema(schema_path)
205+
206+
try:
207+
validate_file_configuration(resolved_schema, file_configuration)
208+
except Exception as error:
209+
fail(f"Unexpected exception raised: {error}")
210+
211+
assert (
212+
resolved_schema
213+
["properties"]
214+
["tracer_provider"]
215+
["properties"]
216+
["sampler"]
217+
["properties"]
218+
["sometimes_mondays_on"]
219+
["properties"]
220+
["probability"]
221+
["type"]
222+
) == "number"

0 commit comments

Comments
 (0)