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

Make log_level centrally configurable #859

Merged
merged 8 commits into from Dec 10, 2020

Conversation

axw
Copy link
Member

@axw axw commented Dec 10, 2020

$ELASTIC_APM_LOG_LEVEL now defines the local value, and this can be overridden via central config. If central config is updated and then removed, we'll revert back to the local value like with other config.

If a custom logger is defined, or no log file is specified, the log_level attribute is ignored regardless of whether central config is used. If a custom logger is defined we will log a warning to it about the central config change being ineffective.

Closes #829

Expose the concrete LevelLogger type, so we can add a
means of updating the level via central config.
Also expose the default level as a constant,
and the level constants.
$ELASTIC_APM_LOG_LEVEL now defines the local
value, and this can be overridden via central
config. If central config is updated and then
removed, we'll revert back to the local value
like with other config.

If a custom logger is defined, the log_level
attribute is ignored regardless of whether
central config is used.
@axw axw requested a review from a team December 10, 2020 06:35
@apmmachine
Copy link
Collaborator

apmmachine commented Dec 10, 2020

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Build Cause: Pull request #859 updated

  • Start Time: 2020-12-10T07:42:35.488+0000

  • Duration: 23 min 28 sec

Test stats 🧪

Test Results
Failed 0
Passed 7660
Skipped 188
Total 7848

@codecov-io
Copy link

Codecov Report

Merging #859 (4e2e909) into master (251ea72) will decrease coverage by 0.10%.
The diff coverage is 63.04%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #859      +/-   ##
==========================================
- Coverage   83.82%   83.72%   -0.11%     
==========================================
  Files         142      142              
  Lines        7888     7913      +25     
==========================================
+ Hits         6612     6625      +13     
- Misses        868      879      +11     
- Partials      408      409       +1     
Impacted Files Coverage Δ
internal/apmcloudutil/provider.go 80.00% <ø> (ø)
utils.go 76.41% <0.00%> (ø)
config.go 71.17% <38.46%> (-2.04%) ⬇️
internal/apmlog/logger.go 80.82% <70.37%> (-1.00%) ⬇️
tracer.go 88.75% <100.00%> (+0.08%) ⬆️
transport/http.go 83.33% <0.00%> (-1.37%) ⬇️
profiling.go 71.92% <0.00%> (+3.50%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 251ea72...4e2e909. Read the comment docs.

return "warn"
case errorLevel:
case ErrorLevel:
Copy link
Contributor

Choose a reason for hiding this comment

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

CriticalLevel is missing

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch, thanks!

@@ -426,6 +426,14 @@ func newTracer(opts TracerOptions) *Tracer {
t.setLocalInstrumentationConfig(envSanitizeFieldNames, func(cfg *instrumentationConfigValues) {
cfg.sanitizedFieldNames = opts.sanitizedFieldNames
})
if apmlog.DefaultLogger != nil {
defaultLogLevel := apmlog.DefaultLogger.Level()
Copy link
Contributor

Choose a reason for hiding this comment

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

Hasn't this already potentially changed from the local ENV to the remote config? I don't see how this falls back to the original value in case a remote config value was provided but isn't any longer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not 100% sure I follow your question.

  • apmlog.DefaultLogger.Level() is called when constructing the Tracer, so there's no remote config yet received. We're just capturing the initial (default/environment variable) value.
  • the function literal passed into t.setLocalInstrumentationConfig below will be invoked immediately (having no effect, since it's just setting the level to the same value), and also when remote config is set and later removed. The latter bit is taken care of by

    apm-agent-go/config.go

    Lines 407 to 417 in 879c34a

    for k := range old {
    if _, ok := attrs[k]; ok {
    continue
    }
    updates = append(updates, func(cfg *instrumentationConfig) {
    if f, ok := cfg.local[envName(k)]; ok {
    f(&cfg.instrumentationConfigValues)
    }
    })
    debugf("central config update: reverted %s to local config", k)
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

I missed defaultLogLevel := apmlog.DefaultLogger.Level()
in https://github.com/elastic/apm-agent-go/pull/859/files#diff-4e84387cbc4fa01c5f9e572c02575e6c8536608159891072d2fbfa69af83a86fR430 - all good then, thanks.

@@ -97,9 +100,31 @@ func TestTracerCentralConfigUpdate(t *testing.T) {
assert.Len(t, payloads.Transactions[0].Context.Request.Cookies, 1)
return payloads.Transactions[0].Context.Request.Cookies[0].Value == "[REDACTED]"
})
t.Run("log_level", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I might be missing something - but are there any tests for actually applying the remote log level and also falling back to the local config if a remote config is no longer provided?

Copy link
Member Author

Choose a reason for hiding this comment

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

testTracerCentralConfigUpdate (which this test calls) does that in

apm-agent-go/config_test.go

Lines 142 to 165 in 879c34a

tracer.SetLogger(apmtest.NewTestLogger(t))
assert.False(t, isRemote(tracer))
timeout := time.After(10 * time.Second)
// We each response payload twice, which causes us to block until
// the first one is fully consumed.
for i := 0; i < 2; i++ {
select {
case responses <- response{etag: "foo", body: serverResponse}:
case <-timeout:
t.Fatal("timed out waiting for config update")
}
}
assert.True(t, isRemote(tracer))
for i := 0; i < 2; i++ {
select {
case responses <- response{etag: "bar", body: "{}"}:
case <-timeout:
t.Fatal("timed out waiting for config update")
}
}
assert.False(t, isRemote(tracer))
.

Each of these sub-tests defines a config key, value to set remotely, and a predicate that indicates whether the remote config has been applied. The code block I pointed to first checks that with the initial tracer config the predicate returns false; then sets the remote config and checks the predicate returns true; then clears out all remote config and again checks that the predicate returns false.

@axw axw merged commit 491755f into elastic:master Dec 10, 2020
@axw axw deleted the log-level-central-config branch December 10, 2020 10:56
case "info":
return infoLevel, nil
return InfoLevel, nil
case "warn":
Copy link
Member

Choose a reason for hiding this comment

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

(This is my first time looking at the go agent, and really reviewing much Go code at all, so feel free to ignore if I'm missing something obvious.)

The spec uses "warning" rather than "warn". Is this function parsing level strings from central config?

Copy link
Member Author

Choose a reason for hiding this comment

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

@trentm Thanks, nice pick up! You're quite right, I missed that. I'll open another PR to fix it.

axw added a commit to elastic/kibana that referenced this pull request Dec 14, 2020
elastic/apm-agent-go#859 adds
central config support for 'log_level' to the Go agent,
so we can now enable it in the UI too.

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
axw added a commit to elastic/kibana that referenced this pull request Dec 14, 2020
elastic/apm-agent-go#859 adds
central config support for 'log_level' to the Go agent,
so we can now enable it in the UI too.

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[META 321] Add log_level spec
5 participants