diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py index 211655d93a2..db4ce72d266 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py @@ -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, ) @@ -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: @@ -152,7 +138,7 @@ def _translate_data( ) ) - self._collector_log_kwargs = {} + self._collector_kwargs = {} self._translate_name(log_data) self._translate_time(log_data) @@ -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( diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py index 248b970b6f3..ec30cdac1c3 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py @@ -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) @@ -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 diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/trace_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/trace_exporter/__init__.py index 5f18de1ac01..075b961250b 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/trace_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/trace_exporter/__init__.py @@ -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: @@ -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: @@ -214,9 +196,7 @@ 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 @@ -224,7 +204,7 @@ def _translate_status(self, sdk_span: ReadableSpan) -> 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, @@ -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) @@ -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(