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

[WIP] user-facing Logs API that includes events #4213

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,44 @@
ResourceLogs,
ScopeLogs,
)
from opentelemetry.sdk._logs import LogData
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord


def encode_logs(batch: Sequence[LogData]) -> ExportLogsServiceRequest:
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,78 +77,70 @@ def test_dropped_attributes_count(self):
)

@staticmethod
def _get_sdk_log_data() -> List[LogData]:
log1 = LogData(
log_record=SDKLogRecord(
timestamp=1644650195189786880,
observed_timestamp=1644650195189786881,
trace_id=89564621134313219400156819398935297684,
span_id=1312458408527513268,
trace_flags=TraceFlags(0x01),
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Do not go gentle into that good night. Rage, rage against the dying of the light",
resource=SDKResource(
{"first_resource": "value"},
"resource_schema_url",
),
attributes={"a": 1, "b": "c"},
def _get_sdk_log_data() -> List[SDKLogRecord]:
log1 = SDKLogRecord(
timestamp=1644650195189786880,
observed_timestamp=1644650195189786881,
trace_id=89564621134313219400156819398935297684,
span_id=1312458408527513268,
trace_flags=TraceFlags(0x01),
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Do not go gentle into that good night. Rage, rage against the dying of the light",
resource=SDKResource(
{"first_resource": "value"},
"resource_schema_url",
),
attributes={"a": 1, "b": "c"},
instrumentation_scope=InstrumentationScope(
"first_name", "first_version"
),
)

log2 = LogData(
log_record=SDKLogRecord(
timestamp=1644650249738562048,
observed_timestamp=1644650249738562049,
trace_id=0,
span_id=0,
trace_flags=TraceFlags.DEFAULT,
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Cooper, this is no time for caution!",
resource=SDKResource({"second_resource": "CASE"}),
attributes={},
),
log2 = SDKLogRecord(
timestamp=1644650249738562048,
observed_timestamp=1644650249738562049,
trace_id=0,
span_id=0,
trace_flags=TraceFlags.DEFAULT,
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Cooper, this is no time for caution!",
resource=SDKResource({"second_resource": "CASE"}),
attributes={},
instrumentation_scope=InstrumentationScope(
"second_name", "second_version"
),
)

log3 = LogData(
log_record=SDKLogRecord(
timestamp=1644650427658989056,
observed_timestamp=1644650427658989057,
trace_id=271615924622795969659406376515024083555,
span_id=4242561578944770265,
trace_flags=TraceFlags(0x01),
severity_text="DEBUG",
severity_number=SeverityNumber.DEBUG,
body="To our galaxy",
resource=SDKResource({"second_resource": "CASE"}),
attributes={"a": 1, "b": "c"},
),
log3 = SDKLogRecord(
timestamp=1644650427658989056,
observed_timestamp=1644650427658989057,
trace_id=271615924622795969659406376515024083555,
span_id=4242561578944770265,
trace_flags=TraceFlags(0x01),
severity_text="DEBUG",
severity_number=SeverityNumber.DEBUG,
body="To our galaxy",
resource=SDKResource({"second_resource": "CASE"}),
attributes={"a": 1, "b": "c"},
instrumentation_scope=None,
)

log4 = LogData(
log_record=SDKLogRecord(
timestamp=1644650584292683008,
observed_timestamp=1644650584292683009,
trace_id=212592107417388365804938480559624925555,
span_id=6077757853989569223,
trace_flags=TraceFlags(0x01),
severity_text="INFO",
severity_number=SeverityNumber.INFO,
body="Love is the one thing that transcends time and space",
resource=SDKResource(
{"first_resource": "value"},
"resource_schema_url",
),
attributes={"filename": "model.py", "func_name": "run_method"},
log4 = SDKLogRecord(
timestamp=1644650584292683008,
observed_timestamp=1644650584292683009,
trace_id=212592107417388365804938480559624925555,
span_id=6077757853989569223,
trace_flags=TraceFlags(0x01),
severity_text="INFO",
severity_number=SeverityNumber.INFO,
body="Love is the one thing that transcends time and space",
resource=SDKResource(
{"first_resource": "value"},
"resource_schema_url",
),
attributes={"filename": "model.py", "func_name": "run_method"},
instrumentation_scope=InstrumentationScope(
"another_name", "another_version"
),
Expand Down Expand Up @@ -293,37 +285,33 @@ def get_test_logs(
return sdk_logs, pb2_service_request

@staticmethod
def _get_test_logs_dropped_attributes() -> List[LogData]:
log1 = LogData(
log_record=SDKLogRecord(
timestamp=1644650195189786880,
trace_id=89564621134313219400156819398935297684,
span_id=1312458408527513268,
trace_flags=TraceFlags(0x01),
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Do not go gentle into that good night. Rage, rage against the dying of the light",
resource=SDKResource({"first_resource": "value"}),
attributes={"a": 1, "b": "c", "user_id": "B121092"},
limits=LogLimits(max_attributes=1),
),
def _get_test_logs_dropped_attributes() -> List[SDKLogRecord]:
log1 = SDKLogRecord(
timestamp=1644650195189786880,
trace_id=89564621134313219400156819398935297684,
span_id=1312458408527513268,
trace_flags=TraceFlags(0x01),
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Do not go gentle into that good night. Rage, rage against the dying of the light",
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"
),
)

log2 = LogData(
log_record=SDKLogRecord(
timestamp=1644650249738562048,
trace_id=0,
span_id=0,
trace_flags=TraceFlags.DEFAULT,
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Cooper, this is no time for caution!",
resource=SDKResource({"second_resource": "CASE"}),
attributes={},
),
log2 = SDKLogRecord(
timestamp=1644650249738562048,
trace_id=0,
span_id=0,
trace_flags=TraceFlags.DEFAULT,
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Cooper, this is no time for caution!",
resource=SDKResource({"second_resource": "CASE"}),
attributes={},
instrumentation_scope=InstrumentationScope(
"second_name", "second_version"
),
Expand Down
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
Loading