Skip to content

Encounter Invalid type <class 'NoneType'> of value None issue. #4392

@DennySORA

Description

@DennySORA

Describe your environment

OS: AWS Ubuntu
telemetry.auto.version: 0.50b0
telemetry.sdk.language: python
telemetry.sdk.name: opentelemetry
telemetry.sdk.version: 1.29.0

What happened?

When I was developing a backend using Python FastAPI, an internal error occurred, throwing an exception. Normally, I should be able to find the error logs in Loki, but for some unknown reason, an OpenTelemetry error was triggered instead, making it impossible for me to trace the application's errors.

My exception should have contained data, but OpenTelemetry displayed it as None, which is very strange. However, even in such cases, it should still be able to display the information correctly. It seems I need to ensure that the body can function properly even if it is None.

Steps to Reproduce

  try:
      1/0
  except Exception as e
      logging.error({"error": None})

Actual Result

{
  "body": "Exception while exporting logs.",
  "severity": "ERROR",
  "attributes": {
    "code.filepath": "/otel-auto-instrumentation-python/opentelemetry/sdk/_logs/_internal/export/__init__.py",
    "code.function": "_export_batch",
    "code.lineno": 313,
    "exception.message": "Invalid type <class 'NoneType'> of value None",
    "exception.stacktrace": "Traceback (most recent call last):\n  File \"/otel-auto-instrumentation-python/opentelemetry/sdk/_logs/_internal/export/__init__.py\", line 311, in _export_batch\n    self._exporter.export(self._log_records[:idx])  # type: ignore\n    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/otel-auto-instrumentation-python/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py\", line 138, in export\n    serialized_data = encode_logs(batch).SerializeToString()\n                      ^^^^^^^^^^^^^^^^^^\n  File \"/otel-auto-instrumentation-python/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py\", line 38, in encode_logs\n    return ExportLogsServiceRequest(resource_logs=_encode_resource_logs(batch))\n                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/otel-auto-instrumentation-python/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py\", line 62, in _encode_resource_logs\n    pb2_log = _encode_log(sdk_log)\n              ^^^^^^^^^^^^^^^^^^^^\n  File \"/otel-auto-instrumentation-python/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py\", line 48, in _encode_log\n    body=_encode_value(log_data.log_record.body),\n         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/otel-auto-instrumentation-python/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py\", line 85, in _encode_value\n    values=[_encode_key_value(str(k), v) for k, v in value.items()]\n            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/otel-auto-instrumentation-python/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py\", line 92, in _encode_key_value\n    return PB2KeyValue(key=key, value=_encode_value(value))\n                                      ^^^^^^^^^^^^^^^^^^^^\n  File \"/otel-auto-instrumentation-python/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py\", line 88, in _encode_value\n    raise Exception(f\"Invalid type {type(value)} of value {value}\")\nException: Invalid type <class 'NoneType'> of value None\n",
    "exception.type": "Exception",
    "otelServiceName": "aladdin-internal-pii-ai-proxy",
    "otelSpanID": "0",
    "otelTraceID": "0",
    "otelTraceSampled": false
  },
  "resources": {
    "telemetry.auto.version": "0.50b0",
    "telemetry.sdk.language": "python",
    "telemetry.sdk.name": "opentelemetry",
    "telemetry.sdk.version": "1.25.0"
  },
  "instrumentation_scope": {
    "name": "opentelemetry.sdk._logs._internal"
  }
}

Additional context

No response

Would you like to implement a fix?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions