Skip to content

Commit

Permalink
remove LogData
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Dec 7, 2024
1 parent 87c2b08 commit f22d25d
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,43 @@
ResourceLogs,
ScopeLogs,
)
from opentelemetry.sdk._logs import LogData


def encode_logs(batch: Sequence[LogData]) -> ExportLogsServiceRequest:
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord
def encode_logs(batch: Sequence[SDKLogRecord]) -> ExportLogsServiceRequest:
return ExportLogsServiceRequest(resource_logs=_encode_resource_logs(batch))


def _encode_log(log_data: LogData) -> PB2LogRecord:
def _encode_log(log_record: SDKLogRecord) -> PB2LogRecord:
span_id = (
None
if log_data.log_record.span_id == 0
else _encode_span_id(log_data.log_record.span_id)
if log_record.span_id == 0
else _encode_span_id(log_record.span_id)
)
trace_id = (
None
if log_data.log_record.trace_id == 0
else _encode_trace_id(log_data.log_record.trace_id)
if log_record.trace_id == 0
else _encode_trace_id(log_record.trace_id)
)
body = log_data.log_record.body
body = log_record.body
return PB2LogRecord(
time_unix_nano=log_data.log_record.timestamp,
observed_time_unix_nano=log_data.log_record.observed_timestamp,
time_unix_nano=log_record.timestamp,
observed_time_unix_nano=log_record.observed_timestamp,
span_id=span_id,
trace_id=trace_id,
flags=int(log_data.log_record.trace_flags),
flags=int(log_record.trace_flags),
body=_encode_value(body) if body is not None else None,
severity_text=log_data.log_record.severity_text,
attributes=_encode_attributes(log_data.log_record.attributes),
dropped_attributes_count=log_data.log_record.dropped_attributes,
severity_number=log_data.log_record.severity_number.value,
severity_text=log_record.severity_text,
attributes=_encode_attributes(log_record.attributes),
dropped_attributes_count=log_record.dropped_attributes,
severity_number=log_record.severity_number.value,
)


def _encode_resource_logs(batch: Sequence[LogData]) -> List[ResourceLogs]:
def _encode_resource_logs(batch: Sequence[SDKLogRecord]) -> List[ResourceLogs]:
sdk_resource_logs = defaultdict(lambda: defaultdict(list))

for sdk_log in batch:
sdk_resource = sdk_log.log_record.resource
sdk_resource = sdk_log.resource
sdk_instrumentation = sdk_log.instrumentation_scope or None
pb2_log = _encode_log(sdk_log)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as PB2Resource,
)
from opentelemetry.sdk._logs import LogData, LogLimits
from opentelemetry.sdk._logs import LogLimits
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
Expand All @@ -54,7 +54,7 @@ def test_encode(self):
def test_encode_no_body(self):
sdk_logs, expected_encoding = self.get_test_logs()
for log in sdk_logs:
log.log_record.body = None
log.body = None

for resource_log in expected_encoding.resource_logs:
for scope_log in resource_log.scope_logs:
Expand All @@ -66,7 +66,7 @@ def test_encode_no_body(self):
def test_dropped_attributes_count(self):
sdk_logs = self._get_test_logs_dropped_attributes()
encoded_logs = encode_logs(sdk_logs)
self.assertTrue(hasattr(sdk_logs[0].log_record, "dropped_attributes"))
self.assertTrue(hasattr(sdk_logs[0], "dropped_attributes"))
self.assertEqual(
# pylint:disable=no-member
encoded_logs.resource_logs[0]
Expand All @@ -77,9 +77,8 @@ def test_dropped_attributes_count(self):
)

@staticmethod
def _get_sdk_log_data() -> List[LogData]:
log1 = LogData(
log_record=SDKLogRecord(
def _get_sdk_log_data() -> List[SDKLogRecord]:
log1 = SDKLogRecord(
timestamp=1644650195189786880,
observed_timestamp=1644650195189786881,
trace_id=89564621134313219400156819398935297684,
Expand All @@ -93,14 +92,12 @@ def _get_sdk_log_data() -> List[LogData]:
"resource_schema_url",
),
attributes={"a": 1, "b": "c"},
),
instrumentation_scope=InstrumentationScope(
"first_name", "first_version"
),
instrumentation_scope=InstrumentationScope(
"first_name", "first_version"
),
)

log2 = LogData(
log_record=SDKLogRecord(
log2 = SDKLogRecord(
timestamp=1644650249738562048,
observed_timestamp=1644650249738562049,
trace_id=0,
Expand All @@ -111,14 +108,12 @@ def _get_sdk_log_data() -> List[LogData]:
body="Cooper, this is no time for caution!",
resource=SDKResource({"second_resource": "CASE"}),
attributes={},
),
instrumentation_scope=InstrumentationScope(
"second_name", "second_version"
),
instrumentation_scope=InstrumentationScope(
"second_name", "second_version"
),
)

log3 = LogData(
log_record=SDKLogRecord(
log3 = SDKLogRecord(
timestamp=1644650427658989056,
observed_timestamp=1644650427658989057,
trace_id=271615924622795969659406376515024083555,
Expand All @@ -129,12 +124,10 @@ def _get_sdk_log_data() -> List[LogData]:
body="To our galaxy",
resource=SDKResource({"second_resource": "CASE"}),
attributes={"a": 1, "b": "c"},
),
instrumentation_scope=None,
)
instrumentation_scope=None,
)

log4 = LogData(
log_record=SDKLogRecord(
log4 = SDKLogRecord(
timestamp=1644650584292683008,
observed_timestamp=1644650584292683009,
trace_id=212592107417388365804938480559624925555,
Expand All @@ -148,10 +141,9 @@ def _get_sdk_log_data() -> List[LogData]:
"resource_schema_url",
),
attributes={"filename": "model.py", "func_name": "run_method"},
),
instrumentation_scope=InstrumentationScope(
"another_name", "another_version"
),
instrumentation_scope=InstrumentationScope(
"another_name", "another_version"
),
)

return [log1, log2, log3, log4]
Expand Down Expand Up @@ -293,9 +285,8 @@ def get_test_logs(
return sdk_logs, pb2_service_request

@staticmethod
def _get_test_logs_dropped_attributes() -> List[LogData]:
log1 = LogData(
log_record=SDKLogRecord(
def _get_test_logs_dropped_attributes() -> List[SDKLogRecord]:
log1 = SDKLogRecord(
timestamp=1644650195189786880,
trace_id=89564621134313219400156819398935297684,
span_id=1312458408527513268,
Expand All @@ -306,14 +297,12 @@ def _get_test_logs_dropped_attributes() -> List[LogData]:
resource=SDKResource({"first_resource": "value"}),
attributes={"a": 1, "b": "c", "user_id": "B121092"},
limits=LogLimits(max_attributes=1),
),
instrumentation_scope=InstrumentationScope(
"first_name", "first_version"
),
)
instrumentation_scope=InstrumentationScope(
"first_name", "first_version"
),
)

log2 = LogData(
log_record=SDKLogRecord(
log2 = SDKLogRecord(
timestamp=1644650249738562048,
trace_id=0,
span_id=0,
Expand All @@ -323,10 +312,9 @@ def _get_test_logs_dropped_attributes() -> List[LogData]:
body="Cooper, this is no time for caution!",
resource=SDKResource({"second_resource": "CASE"}),
attributes={},
),
instrumentation_scope=InstrumentationScope(
"second_name", "second_version"
),
)
instrumentation_scope=InstrumentationScope(
"second_name", "second_version"
),
)

return [log1, log2]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from opentelemetry.proto.collector.logs.v1.logs_service_pb2_grpc import (
LogsServiceStub,
)
from opentelemetry.sdk._logs import LogData
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord
from opentelemetry.sdk._logs.export import LogExporter, LogExportResult
from opentelemetry.sdk.environment_variables import (
Expand Down Expand Up @@ -103,11 +102,11 @@ def __init__(
)

def _translate_data(
self, data: Sequence[LogData]
self, data: Sequence[SDKLogRecord]
) -> ExportLogsServiceRequest:
return encode_logs(data)

def export(self, batch: Sequence[LogData]) -> LogExportResult:
def export(self, batch: Sequence[SDKLogRecord]) -> LogExportResult:
return self._export(batch)

def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
Expand Down
Loading

0 comments on commit f22d25d

Please sign in to comment.