[PR #760] [MERGED] feat: Support OTel and JSON for logs (via log/slog) #629

Closed
opened 2025-10-07 00:19:41 +03:00 by OVERLORD · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/pocket-id/pocket-id/pull/760
Author: @ItalyPaleAle
Created: 7/20/2025
Status: Merged
Merged: 7/27/2025
Merged by: @kmendell

Base: mainHead: log-slog-1


📝 Commits (10+)

📊 Changes

20 files changed (+432 additions, -204 deletions)

View changed files

📝 Dockerfile (+2 -2)
📝 backend/go.mod (+11 -7)
📝 backend/go.sum (+18 -10)
📝 backend/internal/bootstrap/application_images_bootstrap.go (+7 -5)
📝 backend/internal/bootstrap/bootstrap.go (+10 -6)
📝 backend/internal/bootstrap/db_bootstrap.go (+22 -20)
backend/internal/bootstrap/observability_boostrap.go (+210 -0)
backend/internal/bootstrap/otel_boostrap.go (+0 -107)
📝 backend/internal/bootstrap/router_bootstrap.go (+23 -17)
📝 backend/internal/bootstrap/services_bootstrap.go (+5 -1)
📝 backend/internal/common/env_config.go (+1 -0)
📝 backend/internal/common/version.go (+3 -0)
📝 backend/internal/controller/oidc_controller.go (+2 -2)
📝 backend/internal/service/app_config_service.go (+0 -1)
📝 backend/internal/service/audit_log_service.go (+8 -5)
📝 backend/internal/service/geolite_service.go (+7 -7)
📝 backend/internal/service/jwt_service.go (+3 -4)
📝 backend/internal/service/ldap_service.go (+9 -8)
📝 backend/internal/service/user_service.go (+6 -2)
backend/internal/utils/slogfanout.go (+85 -0)

📄 Description

This PR adds support for sending logs to a collector using OpenTelemetry (OTel). Additionally, it allows printing log lines as JSON.

OTel for logs

We added support for OTel for traces and metrics with #495. The missing piece for observability was logs, which did not support OTel yet.

  • This PR adds the ability to send logs to an OTel collector, configured using the typical env vars for OpenTelemetry (such as OTEL_LOGS_EXPORTER=otlp and OTEL_EXPORTER_OTLP_ENDPOINT).
  • In order to support OTel, this PR adds a log exporter to the log/slog package, and configures slog as the default logger.
  • By making slog the default log handler, all log calls that use the "old" log. Print functions are automatically converted to slog calls (and sent to an OTel collector, if configured)
  • Both gorm and gin required using wrappers to make them print logs that work well with slog and can be sent to OTel

Note: one of the most useful features of slog and OTel is the ability to have logs automatically correlated to traces. When we use log.Print, the correlation isn't sent to OTel, so logs aren't correlated.
In order for logs to be correlated, we will need to convert all log calls to use slog and receive the context (at least in methods that have a trace, such as those that are invoked by a HTTP request).
I have shown an example here:
https://github.com/pocket-id/pocket-id/pull/760/files#diff-21ae94b87213fba831ae50c5ff9a5c85c28396c549269cfd1e33a0561c158298R260
I have not converted other log calls, so those will need to be done in subsequent PRs.

Log as JSON

This was an easy add since we were implementing slog support.

You can now set LOG_JSON=true and logs are printed out as JSON! JSON-formatted logs are often preferred when logs are sent to tools that parse them.

JSON logging is disabled by default.

When the Pocket ID binary runs in a terminal (such as the user manually running pocket-id in the shell), logs are colored; otherwise, such as when running in a container or piping logs to a file, colors are disabled.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/pocket-id/pocket-id/pull/760 **Author:** [@ItalyPaleAle](https://github.com/ItalyPaleAle) **Created:** 7/20/2025 **Status:** ✅ Merged **Merged:** 7/27/2025 **Merged by:** [@kmendell](https://github.com/kmendell) **Base:** `main` ← **Head:** `log-slog-1` --- ### 📝 Commits (10+) - [`775a60e`](https://github.com/pocket-id/pocket-id/commit/775a60ea05fc6123d275dfae95723abe649957ac) Initial work on OTel support for logs with slog - [`2fb9f42`](https://github.com/pocket-id/pocket-id/commit/2fb9f428a1421488a4234a12c2bb2f9d1566099e) Converted Gin to slog - [`3488a5f`](https://github.com/pocket-id/pocket-id/commit/3488a5f47db2bbf871bdba157392846047fc07a8) Example of contextual logs - [`b8fe400`](https://github.com/pocket-id/pocket-id/commit/b8fe400ec6ad24200b822f44b97f0d9e109dc8b4) Contextual logs when sending emails - [`a692ee3`](https://github.com/pocket-id/pocket-id/commit/a692ee3a04badcf5108c0f87da221666e0564ed6) Merge branch 'main' of https://github.com/pocket-id/pocket-id into log-slog-1 - [`d27eeab`](https://github.com/pocket-id/pocket-id/commit/d27eeabb7d0de788ddf51c8f5de334d1bd95d52b) Some more logs updated - [`5e394c5`](https://github.com/pocket-id/pocket-id/commit/5e394c57949c72b5f5c09c414879c371bf2d8d4d) Merge branch 'main' into log-slog-1 - [`ef7e9fb`](https://github.com/pocket-id/pocket-id/commit/ef7e9fb79fe466794196ec4a94f80ebfb79863ce) Merge branch 'main' into log-slog-1 - [`fb5f0d4`](https://github.com/pocket-id/pocket-id/commit/fb5f0d4fb3d3941f349c6e90cc82c8f1f84f05dd) Merge branch 'main' into log-slog-1 - [`d91bfd0`](https://github.com/pocket-id/pocket-id/commit/d91bfd03fa71c62d4d91110a6ecbb429f92f265d) Merge branch 'main' into log-slog-1 ### 📊 Changes **20 files changed** (+432 additions, -204 deletions) <details> <summary>View changed files</summary> 📝 `Dockerfile` (+2 -2) 📝 `backend/go.mod` (+11 -7) 📝 `backend/go.sum` (+18 -10) 📝 `backend/internal/bootstrap/application_images_bootstrap.go` (+7 -5) 📝 `backend/internal/bootstrap/bootstrap.go` (+10 -6) 📝 `backend/internal/bootstrap/db_bootstrap.go` (+22 -20) ➕ `backend/internal/bootstrap/observability_boostrap.go` (+210 -0) ➖ `backend/internal/bootstrap/otel_boostrap.go` (+0 -107) 📝 `backend/internal/bootstrap/router_bootstrap.go` (+23 -17) 📝 `backend/internal/bootstrap/services_bootstrap.go` (+5 -1) 📝 `backend/internal/common/env_config.go` (+1 -0) 📝 `backend/internal/common/version.go` (+3 -0) 📝 `backend/internal/controller/oidc_controller.go` (+2 -2) 📝 `backend/internal/service/app_config_service.go` (+0 -1) 📝 `backend/internal/service/audit_log_service.go` (+8 -5) 📝 `backend/internal/service/geolite_service.go` (+7 -7) 📝 `backend/internal/service/jwt_service.go` (+3 -4) 📝 `backend/internal/service/ldap_service.go` (+9 -8) 📝 `backend/internal/service/user_service.go` (+6 -2) ➕ `backend/internal/utils/slogfanout.go` (+85 -0) </details> ### 📄 Description This PR adds support for sending logs to a collector using OpenTelemetry (OTel). Additionally, it allows printing log lines as JSON. ## OTel for logs We added support for OTel for traces and metrics with #495. The missing piece for observability was logs, which did not support OTel yet. - This PR adds the ability to send logs to an OTel collector, configured using the typical env vars for OpenTelemetry (such as `OTEL_LOGS_EXPORTER=otlp` and `OTEL_EXPORTER_OTLP_ENDPOINT`). - In order to support OTel, this PR adds a log exporter to the `log/slog` package, and configures slog as the default logger. - By making slog the default log handler, all log calls that use the "old" `log. Print` functions are **automatically converted to slog calls** (and sent to an OTel collector, if configured) - Both gorm and gin required using wrappers to make them print logs that work well with slog and can be sent to OTel > **Note:** one of the most useful features of slog and OTel is the ability to have logs automatically correlated to traces. When we use `log.Print`, the correlation isn't sent to OTel, so logs aren't correlated. > In order for logs to be correlated, we will need to convert all log calls to use slog and receive the context (at least in methods that have a trace, such as those that are invoked by a HTTP request). > I have shown an example here: > https://github.com/pocket-id/pocket-id/pull/760/files#diff-21ae94b87213fba831ae50c5ff9a5c85c28396c549269cfd1e33a0561c158298R260 > I have **not** converted other log calls, so those will need to be done in subsequent PRs. ## Log as JSON This was an easy add since we were implementing slog support. You can now set `LOG_JSON=true` and logs are printed out as JSON! JSON-formatted logs are often preferred when logs are sent to tools that parse them. JSON logging is disabled by default. > When the Pocket ID binary runs in a terminal (such as the user manually running `pocket-id` in the shell), logs are colored; otherwise, such as when running in a container or piping logs to a file, colors are disabled. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
OVERLORD added the pull-request label 2025-10-07 00:19:41 +03:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pocket-id#629