-
Notifications
You must be signed in to change notification settings - Fork 134
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
Collect process (beats) stdout and stderr #455
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
78f353d
Vagrantfile: add dev box
AndersonQ 8f246aa
so far so good
AndersonQ cf4b6dc
tidy up and tests
AndersonQ aa93af8
wip
AndersonQ 62bb6e8
remove debug
AndersonQ 11d8f48
remove unrelated changes
AndersonQ 58d6240
changelog
AndersonQ 6a166d3
Vagrantfile
AndersonQ 7396cb4
pr changes
AndersonQ 2780131
Revert "Vagrantfile"
AndersonQ 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package process | ||
|
||
import ( | ||
"github.com/elastic/elastic-agent/pkg/core/logger" | ||
) | ||
|
||
type logStd int | ||
|
||
const ( | ||
agentConsoleName = "agent.console.name" | ||
agentConsoleType = "agent.console.type" | ||
|
||
logStdOut logStd = iota | ||
logStdErr | ||
) | ||
|
||
func (l logStd) String() string { | ||
switch l { | ||
case logStdOut: | ||
return "stdout" | ||
case logStdErr: | ||
return "stderr" | ||
} | ||
|
||
return "unknown" | ||
} | ||
|
||
type loggerWriter struct { | ||
format string | ||
logf func(format string, args ...interface{}) | ||
} | ||
|
||
func newLoggerWriter(appName string, std logStd, log *logger.Logger) loggerWriter { | ||
log = log.With( | ||
agentConsoleName, appName, | ||
agentConsoleType, std.String()) | ||
|
||
logf := log.Infof | ||
if std == logStdErr { | ||
logf = log.Errorf | ||
} | ||
|
||
return loggerWriter{ | ||
format: appName + " " + std.String() + ": %q", | ||
logf: logf, | ||
} | ||
} | ||
|
||
func (l loggerWriter) Write(p []byte) (n int, err error) { | ||
l.logf(l.format, string(p)) | ||
return len(p), nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package process | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/elastic/elastic-agent/pkg/core/logger" | ||
) | ||
|
||
func Test_loggerWriter(t *testing.T) { | ||
tc := []struct { | ||
name string | ||
args struct { | ||
appName string | ||
logTo logStd | ||
} | ||
logMsg string | ||
logLevel zapcore.Level | ||
}{ | ||
{name: "capture stdout", | ||
args: struct { | ||
appName string | ||
logTo logStd | ||
}{ | ||
appName: "somebeats", | ||
logTo: logStdOut, | ||
}, | ||
logMsg: "stdout log", | ||
logLevel: zapcore.InfoLevel, | ||
}, | ||
{name: "capture stderr", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe alight the |
||
args: struct { | ||
appName string | ||
logTo logStd | ||
}{ | ||
appName: "somebeats", | ||
logTo: logStdErr, | ||
}, | ||
logMsg: "stderr log", | ||
logLevel: zapcore.ErrorLevel, | ||
}, | ||
} | ||
|
||
for _, tt := range tc { | ||
logg, obs := logger.NewTesting("test-loggerWriter") | ||
logg = logg.With("previous-field", "previous-value") | ||
|
||
l := newLoggerWriter(tt.args.appName, tt.args.logTo, logg) | ||
_, _ = l.Write([]byte(tt.logMsg)) | ||
|
||
logs := obs.All() | ||
require.Equal(t, 1, len(logs)) | ||
|
||
log := logs[0] | ||
assert.Equal(t, log.Level, tt.logLevel) | ||
assert.Contains(t, log.Message, tt.logMsg) | ||
assert.Equal(t, log.ContextMap()[agentConsoleName], tt.args.appName) | ||
assert.Equal(t, log.ContextMap()[agentConsoleType], tt.args.logTo.String()) | ||
assert.Equal(t, log.ContextMap()["previous-field"], "previous-value") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package logger | ||
|
||
import ( | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"go.uber.org/zap/zaptest/observer" | ||
) | ||
|
||
// NewTesting creates a testing logger that buffers the logs in memory and | ||
// logs in debug level. Check observer.ObservedLogs for more details. | ||
func NewTesting(name string) (*Logger, *observer.ObservedLogs) { | ||
core, obs := observer.New(zapcore.DebugLevel) | ||
|
||
logger := logp.NewLogger( | ||
name, | ||
zap.WrapCore(func(in zapcore.Core) zapcore.Core { | ||
return zapcore.NewTee(in, core) | ||
})) | ||
return logger, obs | ||
} |
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.
so, there is no way that the proc or the proc.Process can be nil here?
seeing that the nil check was here before and now is removed.
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.
That seems indeed odd to me, I would keep that and separate investigation. See elastic/beats#26126
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.
my bad, I was removing some debug logs and removed more than I should