Skip to content

Commit

Permalink
[otlp-exporter] refactor _translate_attributes (#2355)
Browse files Browse the repository at this point in the history
* [otlp-exporter] refactor _translate_attributes

The method to translate attributes is common across signals, rafactoring the code to make it so.

* fix lint
  • Loading branch information
Alex Boten authored Jan 7, 2022
1 parent d8bebdd commit f5d8720
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from grpc import ChannelCredentials, Compression
from opentelemetry.exporter.otlp.proto.grpc.exporter import (
OTLPExporterMixin,
_translate_key_values,
get_resource_data,
_translate_value,
)
Expand Down Expand Up @@ -65,49 +64,36 @@ def __init__(
)

def _translate_name(self, log_data: LogData) -> None:
self._collector_log_kwargs["name"] = log_data.log_record.name
self._collector_kwargs["name"] = log_data.log_record.name

def _translate_time(self, log_data: LogData) -> None:
self._collector_log_kwargs[
self._collector_kwargs[
"time_unix_nano"
] = log_data.log_record.timestamp

def _translate_span_id(self, log_data: LogData) -> None:
self._collector_log_kwargs[
self._collector_kwargs[
"span_id"
] = log_data.log_record.span_id.to_bytes(8, "big")

def _translate_trace_id(self, log_data: LogData) -> None:
self._collector_log_kwargs[
self._collector_kwargs[
"trace_id"
] = log_data.log_record.trace_id.to_bytes(16, "big")

def _translate_trace_flags(self, log_data: LogData) -> None:
self._collector_log_kwargs["flags"] = int(
log_data.log_record.trace_flags
)
self._collector_kwargs["flags"] = int(log_data.log_record.trace_flags)

def _translate_body(self, log_data: LogData):
self._collector_log_kwargs["body"] = _translate_value(
self._collector_kwargs["body"] = _translate_value(
log_data.log_record.body
)

def _translate_severity_text(self, log_data: LogData):
self._collector_log_kwargs[
self._collector_kwargs[
"severity_text"
] = log_data.log_record.severity_text

def _translate_attributes(self, log_data: LogData) -> None:
if log_data.log_record.attributes:
self._collector_log_kwargs["attributes"] = []
for key, value in log_data.log_record.attributes.items():
try:
self._collector_log_kwargs["attributes"].append(
_translate_key_values(key, value)
)
except Exception: # pylint: disable=broad-except
pass

def _translate_data(
self, data: Sequence[LogData]
) -> ExportLogsServiceRequest:
Expand Down Expand Up @@ -152,7 +138,7 @@ def _translate_data(
)
)

self._collector_log_kwargs = {}
self._collector_kwargs = {}

self._translate_name(log_data)
self._translate_time(log_data)
Expand All @@ -161,14 +147,14 @@ def _translate_data(
self._translate_trace_flags(log_data)
self._translate_body(log_data)
self._translate_severity_text(log_data)
self._translate_attributes(log_data)
self._translate_attributes(log_data.log_record.attributes)

self._collector_log_kwargs[
self._collector_kwargs[
"severity_number"
] = log_data.log_record.severity_number.value

instrumentation_library_logs.logs.append(
PB2LogRecord(**self._collector_log_kwargs)
PB2LogRecord(**self._collector_kwargs)
)

return ExportLogsServiceRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def __init__(
self._timeout = timeout or int(
environ.get(OTEL_EXPORTER_OTLP_TIMEOUT, 10)
)
self._collector_span_kwargs = None
self._collector_kwargs = None

compression = (
environ_to_compression(OTEL_EXPORTER_OTLP_COMPRESSION)
Expand All @@ -263,6 +263,20 @@ def _translate_data(
) -> ExportServiceRequestT:
pass

def _translate_attributes(self, attributes) -> None:
if attributes:

self._collector_kwargs["attributes"] = []

for key, value in attributes.items():

try:
self._collector_kwargs["attributes"].append(
_translate_key_values(key, value)
)
except Exception as error: # pylint: disable=broad-except
logger.exception(error)

def _export(self, data: TypingSequence[SDKDataT]) -> ExportResultT:

max_value = 64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,58 +117,42 @@ def __init__(
)

def _translate_name(self, sdk_span: ReadableSpan) -> None:
self._collector_span_kwargs["name"] = sdk_span.name
self._collector_kwargs["name"] = sdk_span.name

def _translate_start_time(self, sdk_span: ReadableSpan) -> None:
self._collector_span_kwargs[
"start_time_unix_nano"
] = sdk_span.start_time
self._collector_kwargs["start_time_unix_nano"] = sdk_span.start_time

def _translate_end_time(self, sdk_span: ReadableSpan) -> None:
self._collector_span_kwargs["end_time_unix_nano"] = sdk_span.end_time
self._collector_kwargs["end_time_unix_nano"] = sdk_span.end_time

def _translate_span_id(self, sdk_span: ReadableSpan) -> None:
self._collector_span_kwargs[
"span_id"
] = sdk_span.context.span_id.to_bytes(8, "big")
self._collector_kwargs["span_id"] = sdk_span.context.span_id.to_bytes(
8, "big"
)

def _translate_trace_id(self, sdk_span: ReadableSpan) -> None:
self._collector_span_kwargs[
self._collector_kwargs[
"trace_id"
] = sdk_span.context.trace_id.to_bytes(16, "big")

def _translate_parent(self, sdk_span: ReadableSpan) -> None:
if sdk_span.parent is not None:
self._collector_span_kwargs[
self._collector_kwargs[
"parent_span_id"
] = sdk_span.parent.span_id.to_bytes(8, "big")

def _translate_context_trace_state(self, sdk_span: ReadableSpan) -> None:
if sdk_span.context.trace_state is not None:
self._collector_span_kwargs["trace_state"] = ",".join(
self._collector_kwargs["trace_state"] = ",".join(
[
f"{key}={value}"
for key, value in (sdk_span.context.trace_state.items())
]
)

def _translate_attributes(self, sdk_span: ReadableSpan) -> None:
if sdk_span.attributes:

self._collector_span_kwargs["attributes"] = []

for key, value in sdk_span.attributes.items():

try:
self._collector_span_kwargs["attributes"].append(
_translate_key_values(key, value)
)
except Exception as error: # pylint: disable=broad-except
logger.exception(error)

def _translate_events(self, sdk_span: ReadableSpan) -> None:
if sdk_span.events:
self._collector_span_kwargs["events"] = []
self._collector_kwargs["events"] = []

for sdk_span_event in sdk_span.events:

Expand All @@ -187,13 +171,11 @@ def _translate_events(self, sdk_span: ReadableSpan) -> None:
except Exception as error:
logger.exception(error)

self._collector_span_kwargs["events"].append(
collector_span_event
)
self._collector_kwargs["events"].append(collector_span_event)

def _translate_links(self, sdk_span: ReadableSpan) -> None:
if sdk_span.links:
self._collector_span_kwargs["links"] = []
self._collector_kwargs["links"] = []

for sdk_span_link in sdk_span.links:

Expand All @@ -214,17 +196,15 @@ def _translate_links(self, sdk_span: ReadableSpan) -> None:
except Exception as error:
logger.exception(error)

self._collector_span_kwargs["links"].append(
collector_span_link
)
self._collector_kwargs["links"].append(collector_span_link)

def _translate_status(self, sdk_span: ReadableSpan) -> None:
# pylint: disable=no-member
if sdk_span.status is not None:
deprecated_code = Status.DEPRECATED_STATUS_CODE_OK
if sdk_span.status.status_code == StatusCode.ERROR:
deprecated_code = Status.DEPRECATED_STATUS_CODE_UNKNOWN_ERROR
self._collector_span_kwargs["status"] = Status(
self._collector_kwargs["status"] = Status(
deprecated_code=deprecated_code,
code=sdk_span.status.status_code.value,
message=sdk_span.status.description,
Expand Down Expand Up @@ -274,7 +254,7 @@ def _translate_data(
sdk_span.instrumentation_info
)
)
self._collector_span_kwargs = {}
self._collector_kwargs = {}

self._translate_name(sdk_span)
self._translate_start_time(sdk_span)
Expand All @@ -283,30 +263,30 @@ def _translate_data(
self._translate_trace_id(sdk_span)
self._translate_parent(sdk_span)
self._translate_context_trace_state(sdk_span)
self._translate_attributes(sdk_span)
self._translate_attributes(sdk_span.attributes)
self._translate_events(sdk_span)
self._translate_links(sdk_span)
self._translate_status(sdk_span)
if sdk_span.dropped_attributes:
self._collector_span_kwargs[
self._collector_kwargs[
"dropped_attributes_count"
] = sdk_span.dropped_attributes
if sdk_span.dropped_events:
self._collector_span_kwargs[
self._collector_kwargs[
"dropped_events_count"
] = sdk_span.dropped_events
if sdk_span.dropped_links:
self._collector_span_kwargs[
self._collector_kwargs[
"dropped_links_count"
] = sdk_span.dropped_links

self._collector_span_kwargs["kind"] = getattr(
self._collector_kwargs["kind"] = getattr(
CollectorSpan.SpanKind,
f"SPAN_KIND_{sdk_span.kind.name}",
)

instrumentation_library_spans.spans.append(
CollectorSpan(**self._collector_span_kwargs)
CollectorSpan(**self._collector_kwargs)
)

return ExportTraceServiceRequest(
Expand Down

0 comments on commit f5d8720

Please sign in to comment.