-
Notifications
You must be signed in to change notification settings - Fork 748
Description
Is your feature request related to a problem?
I would like to propose adding a native OTLPJsonConsoleLogExporter
to the OpenTelemetry Python SDK, which would export logs in OTLP/JSON directly to stdout in the OTLP/JSON file encoding.
Similarly to #3515, this would cover for organisational and/or reliability needs.
Motivation comes from the respective Java Exporter. Additional information about how this is used can be found at https://opentelemetry.io/blog/2024/collecting-otel-compliant-java-logs-from-files/.
(Also related to open-telemetry/opentelemetry-specification#3817).
Describe the solution you'd like
An OTLPJsonConsoleLogExporter
exporter that could be used like this:
provider = LoggerProvider(resource=Resource.create({"service.name": "my-service"}))
exporter = OTLPJsonConsoleLogExporter()
provider.add_log_record_processor(BatchLogRecordProcessor(exporter))
To produce output like this:
{"resource_logs": [{"resource": {"attributes": [{"key": "telemetry.sdk.language", "value": {"string_value": "python"}}, {"key": "telemetry.sdk.name", "value": {"string_value": "opentelemetry"}}, {"key": "telemetry.sdk.version", "value": {"string_value": "1.34.1"}}, {"key": "service.name", "value": {"string_value": "my-python-app"}}], "dropped_attributes_count": 0}, "scope_logs": [{"scope": {"name": "my-otlpjson-logger", "version": "", "attributes": [], "dropped_attributes_count": 0}, "log_records": [{"time_unix_nano": "1751016599680267008", "severity_number": "SEVERITY_NUMBER_INFO", "severity_text": "INFO", "body": {"string_value": "strict OTLP/JSON log message"}, "attributes": [{"key": "environment", "value": {"string_value": "dev"}}, {"key": "code.file.path", "value": {"string_value": "/Users/foo/Documents/some/python/otlpjson/logging.py"}}, {"key": "code.function.name", "value": {"string_value": "<module>"}}, {"key": "code.line.number", "value": {"int_value": "67"}}], "observed_time_unix_nano": "1751016599680286000", "dropped_attributes_count": 0, "flags": 0, "trace_id": "", "span_id": ""}], "schema_url": ""}], "schema_url": ""}]}
The above output can be parsed back into OTLP by the following Opentelemetry Collector components:
A very simple working prototype can be found at https://gist.github.com/ChrsMark/dc36896f0e5755e7bbaab7c49b5aa5a8.