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

Log correct caller information when SpanLogger is used #547

Merged
merged 5 commits into from
Jul 29, 2024

Conversation

charleskorn
Copy link
Contributor

@charleskorn charleskorn commented Jul 17, 2024

What this PR does:

This PR ensures that correct caller information is logged when SpanLogger is used.

Previously, messages logged with SpanLogger would have caller=spanlogger.go:xxx.

Unfortunately we can't retrieve the existing Caller instance configured in the logger and replace it with another with an increased stack depth, as there's no way to retrieve existing key/value pairs associated with a logger. So instead I've created a spanLoggerAwareCaller that determines the correct stack depth when it is first used.

For similar reasons, this also makes it difficult to only add caller information when the logger is configured to provide caller information, and so SpanLoggers will now unconditionally add caller information to all messages logged. Given all of the applications I checked (Mimir, Loki and Tempo) configure their loggers to include caller information, this doesn't seem to be a big issue, but it did require some changes to some tests that assert on log messages emitted through a SpanLogger.

Which issue(s) this PR fixes:

(none)

Checklist

  • Tests updated
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@charleskorn charleskorn force-pushed the charleskorn/spanlogger-caller branch from 54ea07b to bc9704f Compare July 17, 2024 01:04
@charleskorn charleskorn marked this pull request as ready for review July 17, 2024 01:15
@bboreham
Copy link
Contributor

Is there a benchmark for this code?
Particularly interested whether we pay the stack-trace price for logs that are filtered out, eg debug logs.

Copy link
Collaborator

@aknuds1 aknuds1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, excepting @bboreham's question, and please also see my comment about possibly using filepath.Base.

spanlogger/spanlogger_test.go Outdated Show resolved Hide resolved
@aknuds1 aknuds1 requested a review from bboreham July 22, 2024 15:32
@charleskorn charleskorn force-pushed the charleskorn/spanlogger-caller branch from c68ae29 to 273af4f Compare July 23, 2024 11:53
@charleskorn
Copy link
Contributor Author

Is there a benchmark for this code? Particularly interested whether we pay the stack-trace price for logs that are filtered out, eg debug logs.

I've added one in 273af4f. (The existing BenchmarkSpanLogger benchmark didn't exercise the right behaviour, as the values are never evaluated in that benchmark.)

Results:

goos: darwin
goarch: arm64
pkg: github.com/grafana/dskit/spanlogger
                                                               │  main.txt   │              after.txt              │
                                                               │   sec/op    │   sec/op     vs base                │
SpanLoggerWithRealLogger/info_and_above_allowed/log-10           1.193µ ± 1%   1.995µ ± 3%   +67.18% (p=0.002 n=6)
SpanLoggerWithRealLogger/info_and_above_allowed/level.debug-10   165.8n ± 1%   597.8n ± 4%  +260.66% (p=0.002 n=6)
SpanLoggerWithRealLogger/info_and_above_allowed/debuglog-10      3.837n ± 2%   3.901n ± 3%         ~ (p=0.223 n=6)
SpanLoggerWithRealLogger/all_levels_allowed/log-10               1.227µ ± 4%   1.999µ ± 2%   +62.88% (p=0.002 n=6)
SpanLoggerWithRealLogger/all_levels_allowed/level.debug-10       1.238µ ± 2%   2.054µ ± 1%   +66.02% (p=0.002 n=6)
SpanLoggerWithRealLogger/all_levels_allowed/debuglog-10          1.407µ ± 1%   2.341µ ± 3%   +66.41% (p=0.002 n=6)
geomean                                                          342.7n        595.7n        +73.83%

                                                               │   main.txt    │               after.txt               │
                                                               │     B/op      │     B/op      vs base                 │
SpanLoggerWithRealLogger/info_and_above_allowed/log-10           696.0 ± 15%     1040.0 ± 22%  +49.43% (p=0.002 n=6)
SpanLoggerWithRealLogger/info_and_above_allowed/level.debug-10   400.0 ±  0%      704.0 ±  0%  +76.00% (p=0.002 n=6)
SpanLoggerWithRealLogger/info_and_above_allowed/debuglog-10      0.000 ±  0%      0.000 ±  0%        ~ (p=1.000 n=6) ¹
SpanLoggerWithRealLogger/all_levels_allowed/log-10               696.0 ± 34%     1040.0 ± 22%  +49.43% (p=0.002 n=6)
SpanLoggerWithRealLogger/all_levels_allowed/level.debug-10       968.0 ±  0%     1304.0 ±  0%  +34.71% (p=0.002 n=6)
SpanLoggerWithRealLogger/all_levels_allowed/debuglog-10          904.0 ±  0%     1312.0 ±  0%  +45.13% (p=0.002 n=6)
geomean                                                                      ²                 +40.47%               ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                               │   main.txt   │               after.txt               │
                                                               │  allocs/op   │  allocs/op   vs base                  │
SpanLoggerWithRealLogger/info_and_above_allowed/log-10           10.00 ± 0%      15.00 ± 0%   +50.00% (p=0.002 n=6)
SpanLoggerWithRealLogger/info_and_above_allowed/level.debug-10   5.000 ± 0%     10.000 ± 0%  +100.00% (p=0.002 n=6)
SpanLoggerWithRealLogger/info_and_above_allowed/debuglog-10      0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=6) ¹
SpanLoggerWithRealLogger/all_levels_allowed/log-10               10.00 ± 0%      15.00 ± 0%   +50.00% (p=0.002 n=6)
SpanLoggerWithRealLogger/all_levels_allowed/level.debug-10       13.00 ± 0%      18.00 ± 0%   +38.46% (p=0.002 n=6)
SpanLoggerWithRealLogger/all_levels_allowed/debuglog-10          12.00 ± 0%      17.00 ± 0%   +41.67% (p=0.002 n=6)
geomean                                                                     ²                 +43.76%               ²
¹ all samples are equal
² summaries must be >0 to compute geomean

The extra cost comes because go-kit/log doesn't recognise that we've overridden the caller field, and so evaluates both the the caller added by SpanLogger, and the original caller added by the application. I'm not sure there's much we can do to avoid this without adding deduplication to go-kit/log that would make the non-SpanLogger logging path slower.

@charleskorn
Copy link
Contributor Author

@bboreham are you satisfied with the benchmarks above? If you have any ideas on how to make this less costly, I'd love to hear them.

@charleskorn
Copy link
Contributor Author

Given the approval, I'm going to merge this, but if there is any late feedback, I'd be happy to hear it.

@charleskorn charleskorn merged commit 5eca05b into main Jul 29, 2024
6 checks passed
@charleskorn charleskorn deleted the charleskorn/spanlogger-caller branch July 29, 2024 00:27
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.

3 participants