opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

WIP: add a package for structlog handler

Open Samyak2 opened this issue 1 year ago • 2 comments
trafficstars

Description

Closes open-telemetry/opentelemetry-python#2993

A few notes about this handler:

  • A similar set up has been used in prod with Signoz
  • Some things may be specific to Signoz
  • Uses some things from private modules in both structlog and opentelemetry-sdk

TODO

  • [x] Add a new package with the handler/processor
  • [ ] Add tests
  • [ ] Publish this package to PyPI

Type of change

Please delete options that are not relevant.

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [x] This change requires a documentation update

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

  • [ ] Test A

Does This PR Require a Core Repo Change?

  • [ ] Yes. - Link to PR:
  • [ ] No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • [ ] Followed the style guidelines of this project
  • [ ] Changelogs have been updated
  • [ ] Unit tests have been added
  • [ ] Documentation has been updated

Samyak2 avatar Jan 28 '24 15:01 Samyak2

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: Samyak2 / name: Samyak Sarnayak (8969c0015f04c5a22de0fda4d2b5db04c290f182, 9ca4694cbd909ce7eed0efa8e6d0aac5c2c35688)

If you're like me and you're trying to figure out how to get this working, here's what I have that seems to be working with honeycomb.io:

import socket

from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
    OTLPLogExporter,
)

...

    structlog.configure(
        cache_logger_on_first_use=True,
        wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
        processors=[
            structlog.contextvars.merge_contextvars,
            structlog.processors.add_log_level,
            # structlog.processors.format_exc_info,
            # structlog.processors.TimeStamper(fmt="iso", utc=True),
            OpenTelemetryExporter(
                os.environ.get("OTEL_SERVICE_NAME") or "",
                socket.gethostname(),
                OTLPLogExporter(),
            ),
            structlog.processors.JSONRenderer(serializer=orjson.dumps),
        ],
        logger_factory=structlog.BytesLoggerFactory(),
    )

That logic needs cleanup but hopefully this helps someone.

JamesHutchison avatar Apr 01 '24 21:04 JamesHutchison