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 support for OpenTelemetry logging #199

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Used:
```bash
# for main project
python3 -m pip install flask flask-login numpy pymongo oauthlib coverage black ldap3 toml
# if you want to OTLP log exporter
python3 -m pip opentelemetry-sdk opentelemetry-exporter-otlp
# for docs
python3 -m pip install sphinx myst_parser sphinx_rtd_theme sphinxcontrib.httpdomain
```
Expand Down
38 changes: 32 additions & 6 deletions clockwork_web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
register_config("flask.testing", False, validator=boolean)
register_config("flask.login_disabled", False, validator=boolean)

register_config("sentry.dns", "", validator=string)
register_config("sentry.dsn", "", validator=string)
register_config("sentry.dns", "", validator=string) # deprecated because typo
register_config("sentry.traces_sample_rate", 1.0, validator=anything)

LOGGING_LEVEL_MAPPING = dict(
Expand Down Expand Up @@ -64,6 +65,7 @@
)

register_config("logging.journald", False, validator=boolean)
register_config("logging.otel", "", validator=string)

logger = logging.getLogger()

Expand Down Expand Up @@ -126,8 +128,32 @@ def format(self, record):
logging.info("Logging to journald")


sentry_dns = get_config("sentry.dns")
if sentry_dns:
if get_config("logging.otel") != "":
from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.resources import Resource

logger_provider = LoggerProvider(
resource=Resource.create(
{
"service.name": "clockwork",
"service.instance.id": os.uname().nodename,
}
),
)
set_logger_provider(logger_provider)

otlp_exporter = OTLPLogExporter(endpoint=get_config("logging.otel"), insecure=True)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(otlp_exporter))
handler = LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider)
logger.addHandler(handler)

sentry_dsn = get_config("sentry.dsn")
if not sentry_dsn:
sentry_dsn = get_config("sentry.dns") # Old typo
if sentry_dsn:
# Not sure about how sentry works, but it probably does
# some behind-the-scenes things before the flask components
# are loaded. It's not clear to me if we really need to ensure
Expand All @@ -139,7 +165,7 @@ def format(self, record):
from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(
dsn=sentry_dns,
dsn=sentry_dsn,
integrations=[
FlaskIntegration(),
],
Expand All @@ -148,10 +174,10 @@ def format(self, record):
# We recommend having a lower value in production.
traces_sample_rate=get_config("sentry.traces_sample_rate"),
)
logging.info("Loaded sentry logging at %s.", sentry_dns)
logging.info("Loaded sentry logging at %s.", sentry_dsn)
else:
logging.info(
"Not loading sentry because the sentry.dns config is empty or is missing."
"Not loading sentry because the sentry.dsn config is empty or is missing."
)


Expand Down
Loading