Skip to content

Conversation

@mhamza15
Copy link
Collaborator

@mhamza15 mhamza15 commented Jan 6, 2026

Description

Adds opt-in structured JSON logging using zerolog. Vitess continues to use glog by default.

New flags:

  • --structured-logging - Enable structured logging
  • --structured-logging-level - Set log level (default: info)
  • --structured-logging-pretty - Human-readable output
  • --structured-logging-file - Log to file instead of stdout

Internal changes: New structured logging API in go/vt/log/zlog.go with S-prefixed functions (e.g., SInfo(), SExit()). Both old and new APIs route to the correct backend based on flags.

Example JSON output:

$ vttablet --structured-logging

{"level":"info","caller":"/Users/mhamza/dev/vitess/go/vt/servenv/servenv_unix.go:57","time":"2026-01-06T09:22:36-05:00","message":"Version: 24.0.0-SNAPSHOT (Git revision  branch '') built on  by @ using go1.25.5 darwin/arm64"}
{"level":"fatal","caller":"/Users/mhamza/dev/vitess/go/vt/topo/server.go:257","time":"2026-01-06T09:22:36-05:00","message":"topo-global-server-address must be configured"}

Example pretty output:

$ vttablet --structured-logging --structured-logging-pretty

2026-01-06T09:23:13-05:00 INF go/vt/servenv/servenv_unix.go:57 > Version: 24.0.0-SNAPSHOT (Git revision  branch '') built on  by @ using go1.25.5 darwin/arm64
2026-01-06T09:23:13-05:00 FTL go/vt/topo/server.go:257 > topo-global-server-address must be configured

Roadmap:

  • v25: Structured logging becomes default, glog deprecated
  • v26: glog removed

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • Tests were added or are not required
  • Documentation was added or is not required

Adds opt-in structured JSON logging with [zerolog](https://github.com/rs/zerolog). By default, Vitess will continue to use unstructured logging through `glog`.

The approach taken here is two-fold: changing the external API (CLI
flags used by users), and adding a new internal logging API (log
functions called around the codebase).

For the external API, the changes are simple. To opt-in to structured logging, use these new flags:

- `--structured-logging`: Enables structured logging.
- `--structured-logging-level`: Minimum log level: trace, debug, info, warn, error, fatal, panic, disabled (default: info)
- `--structured-logging-pretty`: Enable pretty, human-readable output (default: false)
- `--structured-logging-file`: Log to a file instead of stdout

The existing `--log-rotate-max-size` flag controls the max size of the log file before rotation. Otherwise, the new structured logging flags are mutually exclusive with the old `glog` flags.

For the internal API, we have to be able to support a few scenarios in the
transition period:

1. The default and current setup: no structured logging enabled, and all
   logging around the codebase uses the existing global unstructured
   functions in `go/vt/log`.
2. Structured logging is enabled, but all the logging continues to use
   the existing global unstructured functions in `go/vt/log`.
3. Structured logging is not enabled, but some log sites around the
   codebase start migrating to the new structured API. These need to be
   logged with `glog`, even though they are meant to be structured.
4. Structured logging is enabled, and not all log sites around the
   codebase have been migrated to the new structured API.

To support all these permutations, two things were done:

1. The existing global unstructured functions check if structured
   logging is enabled. If it isn't, log with `glog` as normal. If it is,
   log through zerolog, passing the entire message to zerolog's `Msg`.

   This helps with all cases above. A log using the old API will log to `glog` if structured logging isn't enabled, or to zerolog if it is. With structured logging disabled, normal `glog` output:

   ```console
   $ go run ./go/cmd/vttablet/

   F0106 10:00:26.433396   86344 server.go:257] topo-global-server-address must be configured
   ```

   With structured logging enabled, message is logged in the `message` key in the structured output:

   ```console
   $ go run ./go/cmd/vttablet/

   {"level":"info","caller":"/Users/mhamza/dev/vitess/go/vt/servenv/servenv_unix.go:57","time":"2026-01-06T09:22:36-05:00","message":"Version: 24.0.0-SNAPSHOT (Git revision  branch '') built on  by @ using go1.25.5 darwin/arm64"}
   {"level":"fatal","caller":"/Users/mhamza/dev/vitess/go/vt/topo/server.go:257","time":"2026-01-06T09:22:36-05:00","message":"topo-global-server-address must be configured"}
   ```

2. To support the cases where structured logging isn't enabled, but some
   log sites have started using the new API, I've added a custom zerolog
   writer called `glogWriter`. Instead of writing to `stdout` or a file
   as zerolog would normally, it instead takes zerolog's output buffer
   and logs it using `glog`. Example:

   ```diff
   -		log.Exitf("topo-global-server-address must be configured")
   +		log.SExit().Msg("topo-global-server-address must be configured")
   ```

   ```console
   $ go run ./go/cmd/vttablet

   F0106 10:07:36.390862   92158 server.go:259] {"level":"fatal","caller":"/Users/mhamza/dev/vitess/go/vt/topo/server.go:259","time":"2026-01-06T10:07:36-05:00","message":"topo-global-server-address must be configured"}
   ```

   As it is being logged through `glog`, all existing `glog` flags will
   continue to work, just that the normal unstructured message is now
   zerolog's JSON output.

This supports any permutation of structured logging enabled/disabled +
log site using old/new API.

Note that I did not touch `glog`'s `V()` API. It can be easily mimicked
in zerolog using a wrapper type, but since there are only a few places
in the codebase that actually use it, it might be simpler just to go and
change them all to use normal logging. Still mulling this over, will
follow up with another PR.

Signed-off-by: Mohamed Hamza <[email protected]>

---

Example JSON output:

```console
$ vttablet --structured-logging

{"level":"info","caller":"/Users/mhamza/dev/vitess/go/vt/servenv/servenv_unix.go:57","time":"2026-01-06T09:22:36-05:00","message":"Version: 24.0.0-SNAPSHOT (Git revision  branch '') built on  by @ using go1.25.5 darwin/arm64"}
{"level":"fatal","caller":"/Users/mhamza/dev/vitess/go/vt/topo/server.go:257","time":"2026-01-06T09:22:36-05:00","message":"topo-global-server-address must be configured"}
```

Example pretty output:

```console
$ vttablet --structured-logging --structured-logging-pretty

2026-01-06T09:23:13-05:00 INF go/vt/servenv/servenv_unix.go:57 > Version: 24.0.0-SNAPSHOT (Git revision  branch '') built on  by @ using go1.25.5 darwin/arm64
2026-01-06T09:23:13-05:00 FTL go/vt/topo/server.go:257 > topo-global-server-address must be configured
```

In v25, structured logging will become the default and `glog` and its flags will be deprecated. In v26, `glog` will be removed.

Note: I initially marked the old unstructured functions in `log` as
deprecated. This made `staticcheck` complain about any usage of them.
While this is nice so that we can be proactive in changing them to the
structured API, it means that committing is blocked for each changed
file unless the _entire_ package is migrated away from the deprecated
functions. This is really aggressive, so I've instead left a note to
mention to move to the structured API in each function's doc. When the
time comes, we can switch the entire codebase to the new API in one fell
swoop.

Signed-off-by: Mohamed Hamza <[email protected]>
@mhamza15 mhamza15 self-assigned this Jan 6, 2026
@mhamza15 mhamza15 added Type: Enhancement Logical improvement (somewhere between a bug and feature) Benchmark me Add label to PR to run benchmarks Component: Observability Pull requests that touch tracing/metrics/monitoring labels Jan 6, 2026
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Jan 6, 2026

Hello! 👋

This Pull Request is now handled by arewefastyet. The current HEAD and future commits will be benchmarked.

You can find the performance comparison on the arewefastyet website.

@github-actions github-actions bot added this to the v24.0.0 milestone Jan 6, 2026
@vitess-bot vitess-bot bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 6, 2026
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Jan 6, 2026

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
@mhamza15 mhamza15 removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 6, 2026
@mhamza15 mhamza15 marked this pull request as ready for review January 6, 2026 15:44
@promptless
Copy link
Contributor

promptless bot commented Jan 6, 2026

📝 Documentation updates detected!

New suggestion: Document structured logging feature

@codecov
Copy link

codecov bot commented Jan 6, 2026

Codecov Report

❌ Patch coverage is 30.36649% with 133 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.87%. Comparing base (7a3acd5) to head (8e5ac13).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/log/zlog.go 22.38% 52 Missing ⚠️
go/vt/log/glog.go 35.06% 50 Missing ⚠️
go/vt/log/log.go 33.33% 26 Missing ⚠️
go/vt/servenv/servenv.go 37.50% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #19100      +/-   ##
==========================================
- Coverage   69.90%   69.87%   -0.04%     
==========================================
  Files        1612     1614       +2     
  Lines      215817   215960     +143     
==========================================
+ Hits       150865   150897      +32     
- Misses      64952    65063     +111     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Mohamed Hamza <[email protected]>
Signed-off-by: Mohamed Hamza <[email protected]>
@mhamza15
Copy link
Collaborator Author

mhamza15 commented Jan 8, 2026

Moving back to draft to think this through a bit more

@mhamza15 mhamza15 marked this pull request as draft January 8, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Benchmark me Add label to PR to run benchmarks Component: Observability Pull requests that touch tracing/metrics/monitoring Type: Enhancement Logical improvement (somewhere between a bug and feature)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant