-
Notifications
You must be signed in to change notification settings - Fork 527
Disable in-mem telemetry collection by default #3492
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
Merged
azdagron
merged 4 commits into
spiffe:main
from
azdagron:disable-in-mem-telemetry-by-default
Oct 12, 2022
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,162 +1,116 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/armon/go-metrics" | ||
"github.com/sirupsen/logrus" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/spiffe/spire/test/util" | ||
"github.com/spiffe/spire/test/spiretest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewInmemRunner(t *testing.T) { | ||
config := testInmemConfig() | ||
_, err := newInmemRunner(config) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestDefaultEnabledNewInmemRunner(t *testing.T) { | ||
t.Run("enabled when block undeclared", func(t *testing.T) { | ||
runner, err := newInmemRunner(testInmemConfig()) | ||
assert.Nil(t, err) | ||
assert.True(t, runner.isConfigured()) | ||
}) | ||
|
||
t.Run("enabled flag undeclared", func(t *testing.T) { | ||
config := testInmemConfig() | ||
config.FileConfig = FileConfig{ | ||
InMem: &InMem{}, | ||
} | ||
runner, err := newInmemRunner(config) | ||
assert.Nil(t, err) | ||
assert.True(t, runner.isConfigured()) | ||
}) | ||
|
||
t.Run("enabled flag declared", func(t *testing.T) { | ||
enabledFlag := true | ||
|
||
config := testInmemConfig() | ||
config.FileConfig = FileConfig{ | ||
InMem: &InMem{ | ||
Enabled: &enabledFlag, | ||
func TestInMem(t *testing.T) { | ||
enabled := true | ||
disabled := false | ||
|
||
for _, tt := range []struct { | ||
test string | ||
inMemConfig *InMem | ||
removeLoggerWriter bool | ||
expectErr string | ||
expectEnabled bool | ||
expectLogs []spiretest.LogEntry | ||
}{ | ||
{ | ||
test: "disabled when InMem block undeclared", | ||
inMemConfig: nil, | ||
expectEnabled: false, | ||
}, | ||
{ | ||
test: "enabled when InMem block declared but deprecated enabled flag unset", | ||
inMemConfig: &InMem{}, | ||
expectEnabled: true, | ||
}, | ||
{ | ||
test: "enabled when InMem block declared and deprecated enabled flag set to true", | ||
inMemConfig: &InMem{DeprecatedEnabled: &enabled}, | ||
expectEnabled: true, | ||
expectLogs: []spiretest.LogEntry{ | ||
{ | ||
Level: logrus.WarnLevel, | ||
Message: "The enabled flag is deprecated in the InMem configuration and will be removed in a future release; omit the InMem block to disable in-memory telemetry", | ||
}, | ||
}, | ||
} | ||
runner, err := newInmemRunner(config) | ||
assert.Nil(t, err) | ||
assert.True(t, runner.isConfigured()) | ||
}) | ||
} | ||
|
||
func TestDisabledNewInmemRunner(t *testing.T) { | ||
enabledFlag := false | ||
|
||
config := &MetricsConfig{ | ||
ServiceName: "foo", | ||
FileConfig: FileConfig{ | ||
InMem: &InMem{ | ||
Enabled: &enabledFlag, | ||
}, | ||
{ | ||
test: "disabled when InMem block declared and deprecated enabled flag set to false", | ||
inMemConfig: &InMem{DeprecatedEnabled: &disabled}, | ||
expectEnabled: false, | ||
expectLogs: []spiretest.LogEntry{ | ||
{ | ||
Level: logrus.WarnLevel, | ||
Message: "The enabled flag is deprecated in the InMem configuration and will be removed in a future release; omit the InMem block to disable in-memory telemetry", | ||
}, | ||
}, | ||
}, | ||
} | ||
runner, err := newInmemRunner(config) | ||
assert.Nil(t, err) | ||
assert.False(t, runner.isConfigured()) | ||
} | ||
|
||
func TestWarnOnFutureDisable(t *testing.T) { | ||
// It is not possible to send signals to process on windows except for kill | ||
if runtime.GOOS == "windows" { | ||
t.Skip() | ||
} | ||
logger, hook := test.NewNullLogger() | ||
|
||
// Get a real logrus.Entry | ||
logger.SetLevel(logrus.DebugLevel) | ||
c := &MetricsConfig{ | ||
Logger: logger, | ||
ServiceName: "foo", | ||
} | ||
|
||
ir, err := newInmemRunner(c) | ||
require.Nil(t, err) | ||
require.Equal(t, 1, len(ir.sinks())) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
errCh := make(chan error, 1) | ||
go func() { | ||
errCh <- ir.run(ctx) | ||
}() | ||
|
||
// Send signal, wait for signal handling + logging | ||
util.RunWithTimeout(t, time.Minute, func() { | ||
for { | ||
p, err := os.FindProcess(os.Getpid()) | ||
require.NoError(t, err) | ||
require.NoError(t, p.Signal(metrics.DefaultSignal)) | ||
|
||
require.NoError(t, ctx.Err()) | ||
{ | ||
test: "disabled when unexpected logger passed", | ||
inMemConfig: &InMem{}, | ||
removeLoggerWriter: true, | ||
expectEnabled: false, | ||
expectLogs: []spiretest.LogEntry{ | ||
{ | ||
Level: logrus.WarnLevel, | ||
Message: "Unknown logging subsystem; disabling telemetry signaling", | ||
}, | ||
}, | ||
}, | ||
} { | ||
t.Run(tt.test, func(t *testing.T) { | ||
var logger logrus.FieldLogger | ||
var hook *test.Hook | ||
logger, hook = test.NewNullLogger() | ||
if tt.removeLoggerWriter { | ||
logger = noWriterLogger(logger) | ||
} | ||
|
||
if entry := hook.LastEntry(); entry != nil { | ||
assert.Equal(t, "The in-memory telemetry sink will be disabled by default in a future release."+ | ||
" If you wish to continue using it, please enable it in the telemetry configuration", entry.Message) | ||
runner, err := newInmemRunner(&MetricsConfig{ | ||
Logger: logger, | ||
ServiceName: "foo", | ||
FileConfig: FileConfig{InMem: tt.inMemConfig}, | ||
}) | ||
if tt.expectErr != "" { | ||
require.EqualError(t, err, tt.expectErr) | ||
assert.Nil(t, runner) | ||
return | ||
} | ||
} | ||
}) | ||
|
||
cancel() | ||
require.NoError(t, <-errCh) | ||
} | ||
|
||
func TestInmemSinks(t *testing.T) { | ||
ir, err := newInmemRunner(testUnknownInmemConfig()) | ||
require.Nil(t, err) | ||
assert.Equal(t, 0, len(ir.sinks())) | ||
|
||
ir, err = newInmemRunner(testInmemConfig()) | ||
require.Nil(t, err) | ||
assert.Equal(t, 1, len(ir.sinks())) | ||
} | ||
|
||
func TestInmemIsConfigured(t *testing.T) { | ||
ir, err := newInmemRunner(testInmemConfig()) | ||
require.Nil(t, err) | ||
assert.True(t, ir.isConfigured()) | ||
require.NoError(t, err) | ||
if tt.expectEnabled { | ||
assert.True(t, runner.isConfigured()) | ||
assert.Len(t, runner.sinks(), 1) | ||
} else { | ||
assert.False(t, runner.isConfigured()) | ||
assert.Len(t, runner.sinks(), 0) | ||
} | ||
|
||
ir, err = newInmemRunner(testUnknownInmemConfig()) | ||
require.Nil(t, err) | ||
assert.False(t, ir.isConfigured()) | ||
spiretest.AssertLogs(t, hook.AllEntries(), tt.expectLogs) | ||
}) | ||
} | ||
} | ||
|
||
func testInmemConfig() *MetricsConfig { | ||
l, _ := test.NewNullLogger() | ||
logger, _ := test.NewNullLogger() | ||
return &MetricsConfig{ | ||
Logger: l, | ||
Logger: logger, | ||
ServiceName: "foo", | ||
FileConfig: FileConfig{InMem: &InMem{}}, | ||
} | ||
} | ||
|
||
func testUnknownInmemConfig() *MetricsConfig { | ||
l, _ := test.NewNullLogger() | ||
|
||
// unknownLogger only provides logrus.FieldLogger interface and does not give | ||
// access to the underlying writer via the Writer() method. | ||
unknownLogger := struct { | ||
logrus.FieldLogger | ||
}{ | ||
FieldLogger: l, | ||
} | ||
|
||
return &MetricsConfig{ | ||
Logger: unknownLogger, | ||
ServiceName: "foo", | ||
} | ||
func noWriterLogger(logger logrus.FieldLogger) logrus.FieldLogger { | ||
// Hide the type of the underlying logger to hide the io.Writer | ||
// implementation | ||
return struct{ logrus.FieldLogger }{FieldLogger: logger} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may we keep following the table format that was introduced recently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yep. Thanks for pointing that out. Fixed.