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

Add param for indent size to LogRecord.to_json() #2870

Merged
merged 5 commits into from
Aug 20, 2022

Conversation

dougramirez
Copy link
Contributor

@dougramirez dougramirez commented Aug 10, 2022

Description

This change will allow callers to the LogRecord.to_json() func to specify an indent. Previously the func used 4 as the indent. The consequences of that hard coding meant that log records were being emitted on > 1 line.

This PR will allow callers to send None to the func, which allows the LogRecord to be emitted on a single line. This has the benefit of allowing downstream consumers to read the log as single line and index its keys for queries.

The example below shows how this can be leveraged by sending the indent level as a param to the LogRecord.to_json() func as part of a formatter to the exporter.

This addresses an issue previously documented here: Single log line

logs.py

import time
from os import linesep

from opentelemetry.sdk._logs import (
    LogEmitterProvider,
    LogRecord,
    get_log_emitter_provider,
    set_log_emitter_provider,
)
from opentelemetry.sdk._logs.export import ConsoleLogExporter, SimpleLogProcessor
from opentelemetry.sdk.resources import Resource


def configure_log_emitter_provider(indent):
    provider = LogEmitterProvider(resource=Resource.create())
    set_log_emitter_provider(provider)
    formatter = lambda record: LogRecord.to_json(record, indent=indent) + linesep
    exporter = ConsoleLogExporter(formatter=formatter)
    provider.add_log_processor(SimpleLogProcessor(exporter))


if __name__ == "__main__":
    configure_log_emitter_provider(indent=None)
    log_emitter = get_log_emitter_provider().get_log_emitter(
        "logs.py",
        "0.0.1",
    )
    log_record = LogRecord(
        timestamp=time.time_ns(),
        body="I am a log line.  The one and only.",
    )
    log_emitter.emit(log_record)
{"body": "I am a log line.  The one and only.", "severity_number": "None", "severity_text": null, "attributes": null, "timestamp": "2022-08-10T13:26:04.048569Z", "trace_id": "", "span_id": "", "trace_flags": null, "resource": ""}

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Ran opentelemetry-api test suite
  • Ran opentelemetry-sdk test suite

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated

@dougramirez dougramirez requested a review from a team August 10, 2022 13:29
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 10, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@srikanthccv
Copy link
Member

I just want to call out couple of things based on your usage. The Console{Trace/Metric/Log}Exporters primarily intended for the debugging. And Simple{Span/Log}Processors are blocking and are not recommended for production usage. This console exporter is not part of the sdk specification so it might be dropped in future. If you want to have log/trace co-relation you might just want to use https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/logging/logging.html

@dougramirez
Copy link
Contributor Author

I just want to call out couple of things based on your usage. The Console{Trace/Metric/Log}Exporters primarily intended for the debugging. And Simple{Span/Log}Processors are blocking and are not recommended for production usage. This console exporter is not part of the sdk specification so it might be dropped in future. If you want to have log/trace co-relation you might just want to use https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/logging/logging.html

Thank you for the call out! If the console exporter gets dropped, is it safe to assume that LogRecord and the to_json() func will exist in the future as the Logs Data Model is Stable?

@srikanthccv
Copy link
Member

srikanthccv commented Aug 11, 2022

Maybe, but we do not guarantee anything about logs SDK.

@ocelotl
Copy link
Contributor

ocelotl commented Aug 17, 2022

@dougramirez please fix the conflicts in this PR

@srikanthccv srikanthccv merged commit e6c8020 into open-telemetry:main Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants