Skip to content

Commit e16f7dc

Browse files
committed
encode instrumentation scope schema url and attributes to otlp proto messages. update tests to cover the schema_url and attributes.
1 parent 35337ce commit e16f7dc

File tree

7 files changed

+347
-17
lines changed

7 files changed

+347
-17
lines changed

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def _encode_instrumentation_scope(
5959
return PB2InstrumentationScope(
6060
name=instrumentation_scope.name,
6161
version=instrumentation_scope.version,
62+
attributes=_encode_attributes(instrumentation_scope.attributes),
6263
)
6364

6465

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def _encode_resource_logs(batch: Sequence[LogData]) -> List[ResourceLogs]:
8282
ScopeLogs(
8383
scope=(_encode_instrumentation_scope(sdk_instrumentation)),
8484
log_records=pb2_logs,
85+
schema_url=sdk_instrumentation.schema_url
86+
if sdk_instrumentation
87+
else None,
8588
)
8689
)
8790
pb2_resource_logs.append(

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
from opentelemetry.exporter.otlp.proto.common._internal import (
1919
_encode_attributes,
20+
_encode_instrumentation_scope,
2021
_encode_span_id,
2122
_encode_trace_id,
2223
)
2324
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import (
2425
ExportMetricsServiceRequest,
2526
)
26-
from opentelemetry.proto.common.v1.common_pb2 import InstrumentationScope
2727
from opentelemetry.proto.metrics.v1 import metrics_pb2 as pb2
2828
from opentelemetry.proto.resource.v1.resource_pb2 import (
2929
Resource as PB2Resource,
@@ -219,10 +219,8 @@ def _encode_resource_metrics(resource_metrics, resource_metrics_dict):
219219
# there is no need to check for existing instrumentation scopes
220220
# here.
221221
pb2_scope_metrics = pb2.ScopeMetrics(
222-
scope=InstrumentationScope(
223-
name=instrumentation_scope.name,
224-
version=instrumentation_scope.version,
225-
)
222+
scope=_encode_instrumentation_scope(instrumentation_scope),
223+
schema_url=instrumentation_scope.schema_url,
226224
)
227225

228226
scope_metrics_dict[instrumentation_scope] = pb2_scope_metrics

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ def _encode_resource_spans(
8989
PB2ScopeSpans(
9090
scope=(_encode_instrumentation_scope(sdk_instrumentation)),
9191
spans=pb2_spans,
92+
schema_url=sdk_instrumentation.schema_url
93+
if sdk_instrumentation
94+
else None,
9295
)
9396
)
9497
pb2_resource_spans.append(

exporter/opentelemetry-exporter-otlp-proto-common/tests/test_log_encoder.py

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,54 @@ def _get_sdk_log_data() -> List[LogData]:
154154
),
155155
)
156156

157-
return [log1, log2, log3, log4]
157+
log5 = LogData(
158+
log_record=SDKLogRecord(
159+
timestamp=1644650584292683022,
160+
observed_timestamp=1644650584292683022,
161+
trace_id=212592107417388365804938480559624925522,
162+
span_id=6077757853989569222,
163+
trace_flags=TraceFlags(0x01),
164+
severity_text="ERROR",
165+
severity_number=SeverityNumber.ERROR,
166+
body="This instrumentation scope has a schema url",
167+
resource=SDKResource(
168+
{"first_resource": "value"},
169+
"resource_schema_url",
170+
),
171+
attributes={"filename": "model.py", "func_name": "run_method"},
172+
),
173+
instrumentation_scope=InstrumentationScope(
174+
"scope_with_url",
175+
"scope_with_url_version",
176+
"instrumentation_schema_url",
177+
),
178+
)
179+
180+
log6 = LogData(
181+
log_record=SDKLogRecord(
182+
timestamp=1644650584292683033,
183+
observed_timestamp=1644650584292683033,
184+
trace_id=212592107417388365804938480559624925533,
185+
span_id=6077757853989569233,
186+
trace_flags=TraceFlags(0x01),
187+
severity_text="FATAL",
188+
severity_number=SeverityNumber.FATAL,
189+
body="This instrumentation scope has a schema url and attributes",
190+
resource=SDKResource(
191+
{"first_resource": "value"},
192+
"resource_schema_url",
193+
),
194+
attributes={"filename": "model.py", "func_name": "run_method"},
195+
),
196+
instrumentation_scope=InstrumentationScope(
197+
"scope_with_attributes",
198+
"scope_with_attributes_version",
199+
"instrumentation_schema_url",
200+
{"one": 1, "two": "2"},
201+
),
202+
)
203+
204+
return [log1, log2, log3, log4, log5, log6]
158205

159206
def get_test_logs(
160207
self,
@@ -229,6 +276,71 @@ def get_test_logs(
229276
)
230277
],
231278
),
279+
PB2ScopeLogs(
280+
scope=PB2InstrumentationScope(
281+
name="scope_with_url",
282+
version="scope_with_url_version",
283+
),
284+
schema_url="instrumentation_schema_url",
285+
log_records=[
286+
PB2LogRecord(
287+
time_unix_nano=1644650584292683022,
288+
observed_time_unix_nano=1644650584292683022,
289+
trace_id=_encode_trace_id(
290+
212592107417388365804938480559624925522
291+
),
292+
span_id=_encode_span_id(
293+
6077757853989569222
294+
),
295+
flags=int(TraceFlags(0x01)),
296+
severity_text="ERROR",
297+
severity_number=SeverityNumber.ERROR.value,
298+
body=_encode_value(
299+
"This instrumentation scope has a schema url"
300+
),
301+
attributes=_encode_attributes(
302+
{
303+
"filename": "model.py",
304+
"func_name": "run_method",
305+
}
306+
),
307+
)
308+
],
309+
),
310+
PB2ScopeLogs(
311+
scope=PB2InstrumentationScope(
312+
name="scope_with_attributes",
313+
version="scope_with_attributes_version",
314+
attributes=_encode_attributes(
315+
{"one": 1, "two": "2"}
316+
),
317+
),
318+
schema_url="instrumentation_schema_url",
319+
log_records=[
320+
PB2LogRecord(
321+
time_unix_nano=1644650584292683033,
322+
observed_time_unix_nano=1644650584292683033,
323+
trace_id=_encode_trace_id(
324+
212592107417388365804938480559624925533
325+
),
326+
span_id=_encode_span_id(
327+
6077757853989569233
328+
),
329+
flags=int(TraceFlags(0x01)),
330+
severity_text="FATAL",
331+
severity_number=SeverityNumber.FATAL.value,
332+
body=_encode_value(
333+
"This instrumentation scope has a schema url and attributes"
334+
),
335+
attributes=_encode_attributes(
336+
{
337+
"filename": "model.py",
338+
"func_name": "run_method",
339+
}
340+
),
341+
)
342+
],
343+
),
232344
],
233345
schema_url="resource_schema_url",
234346
),

0 commit comments

Comments
 (0)