Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTLP exporter: encode instrumentation scope schema url and attributes to otlp proto #4359

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix OTLP encoders missing instrumentation scope schema url and attributes
([#4359](https://github.com/open-telemetry/opentelemetry-python/pull/4359))
- Add `attributes` field in `metrics.get_meter` wrapper function
([#4364](https://github.com/open-telemetry/opentelemetry-python/pull/4364))
- Add Python 3.13 support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def _encode_instrumentation_scope(
return PB2InstrumentationScope(
name=instrumentation_scope.name,
version=instrumentation_scope.version,
attributes=_encode_attributes(instrumentation_scope.attributes),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def _encode_resource_logs(batch: Sequence[LogData]) -> List[ResourceLogs]:
ScopeLogs(
scope=(_encode_instrumentation_scope(sdk_instrumentation)),
log_records=pb2_logs,
schema_url=sdk_instrumentation.schema_url
if sdk_instrumentation
else None,
)
)
pb2_resource_logs.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

from opentelemetry.exporter.otlp.proto.common._internal import (
_encode_attributes,
_encode_instrumentation_scope,
_encode_span_id,
_encode_trace_id,
)
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import (
ExportMetricsServiceRequest,
)
from opentelemetry.proto.common.v1.common_pb2 import InstrumentationScope
from opentelemetry.proto.metrics.v1 import metrics_pb2 as pb2
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as PB2Resource,
Expand Down Expand Up @@ -219,10 +219,8 @@ def _encode_resource_metrics(resource_metrics, resource_metrics_dict):
# there is no need to check for existing instrumentation scopes
# here.
pb2_scope_metrics = pb2.ScopeMetrics(
scope=InstrumentationScope(
name=instrumentation_scope.name,
version=instrumentation_scope.version,
)
scope=_encode_instrumentation_scope(instrumentation_scope),
schema_url=instrumentation_scope.schema_url,
)

scope_metrics_dict[instrumentation_scope] = pb2_scope_metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def _encode_resource_spans(
PB2ScopeSpans(
scope=(_encode_instrumentation_scope(sdk_instrumentation)),
spans=pb2_spans,
schema_url=sdk_instrumentation.schema_url
if sdk_instrumentation
else None,
)
)
pb2_resource_spans.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,54 @@ def _get_sdk_log_data() -> List[LogData]:
),
)

return [log1, log2, log3, log4]
log5 = LogData(
log_record=SDKLogRecord(
timestamp=1644650584292683022,
observed_timestamp=1644650584292683022,
trace_id=212592107417388365804938480559624925522,
span_id=6077757853989569222,
trace_flags=TraceFlags(0x01),
severity_text="ERROR",
severity_number=SeverityNumber.ERROR,
body="This instrumentation scope has a schema url",
resource=SDKResource(
{"first_resource": "value"},
"resource_schema_url",
),
attributes={"filename": "model.py", "func_name": "run_method"},
),
instrumentation_scope=InstrumentationScope(
"scope_with_url",
"scope_with_url_version",
"instrumentation_schema_url",
),
)

log6 = LogData(
log_record=SDKLogRecord(
timestamp=1644650584292683033,
observed_timestamp=1644650584292683033,
trace_id=212592107417388365804938480559624925533,
span_id=6077757853989569233,
trace_flags=TraceFlags(0x01),
severity_text="FATAL",
severity_number=SeverityNumber.FATAL,
body="This instrumentation scope has a schema url and attributes",
resource=SDKResource(
{"first_resource": "value"},
"resource_schema_url",
),
attributes={"filename": "model.py", "func_name": "run_method"},
),
instrumentation_scope=InstrumentationScope(
"scope_with_attributes",
"scope_with_attributes_version",
"instrumentation_schema_url",
{"one": 1, "two": "2"},
),
)

return [log1, log2, log3, log4, log5, log6]

def get_test_logs(
self,
Expand Down Expand Up @@ -229,6 +276,71 @@ def get_test_logs(
)
],
),
PB2ScopeLogs(
scope=PB2InstrumentationScope(
name="scope_with_url",
version="scope_with_url_version",
),
schema_url="instrumentation_schema_url",
log_records=[
PB2LogRecord(
time_unix_nano=1644650584292683022,
observed_time_unix_nano=1644650584292683022,
trace_id=_encode_trace_id(
212592107417388365804938480559624925522
),
span_id=_encode_span_id(
6077757853989569222
),
flags=int(TraceFlags(0x01)),
severity_text="ERROR",
severity_number=SeverityNumber.ERROR.value,
body=_encode_value(
"This instrumentation scope has a schema url"
),
attributes=_encode_attributes(
{
"filename": "model.py",
"func_name": "run_method",
}
),
)
],
),
PB2ScopeLogs(
scope=PB2InstrumentationScope(
name="scope_with_attributes",
version="scope_with_attributes_version",
attributes=_encode_attributes(
{"one": 1, "two": "2"}
),
),
schema_url="instrumentation_schema_url",
log_records=[
PB2LogRecord(
time_unix_nano=1644650584292683033,
observed_time_unix_nano=1644650584292683033,
trace_id=_encode_trace_id(
212592107417388365804938480559624925533
),
span_id=_encode_span_id(
6077757853989569233
),
flags=int(TraceFlags(0x01)),
severity_text="FATAL",
severity_number=SeverityNumber.FATAL.value,
body=_encode_value(
"This instrumentation scope has a schema url and attributes"
),
attributes=_encode_attributes(
{
"filename": "model.py",
"func_name": "run_method",
}
),
)
],
),
],
schema_url="resource_schema_url",
),
Expand Down
Loading