-
Notifications
You must be signed in to change notification settings - Fork 21
PMM-5680 store logs #342
base: main
Are you sure you want to change the base?
PMM-5680 store logs #342
Conversation
storelogs/main.go
Outdated
dt := time.Now() | ||
log = dt.Format("01-02-2006 15:04:05") + " " + l.entry.Level.String() + ": " + log | ||
for _, v := range l.entry.Data { | ||
log = log + fmt.Sprintf(" %v", v) |
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.
1 solution make slice of strings and then join
2 solution by any buffer
in log = log + fmt.Sprintf(" %v", v)
too many garbage https://gosamples.dev/concatenate-strings/
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.
changed to buffer
agentlocal/agent_local.go
Outdated
@@ -63,6 +63,11 @@ type Server struct { | |||
reloadCloseOnce sync.Once | |||
} | |||
|
|||
func (s *Server) mustEmbedUnimplementedAgentLocalServer() { |
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.
it's wrong, possible solution remove this requirement by some protoc --arg or by embeding [agentlocalpb|agentpb].UnimplementedAgentLocalServer
storelogs/main.go
Outdated
|
||
type LogsStore struct { | ||
log *ring.Ring | ||
Entry *logrus.Entry |
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.
entry *logrus.Entry
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.
I need use it here newProcessLogger(sl.Entry, keepLogLines, redactWords),
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.
mustEmbed need delete
# Conflicts: # go.mod # go.sum
agentlocal/agent_local.go
Outdated
reload chan struct{} | ||
reloadCloseOnce sync.Once | ||
|
||
agentlocalpb.UnimplementedAgentLocalServer | ||
} | ||
|
||
//// AgentLogs contains information about Agent logs. | ||
//type AgentLogs struct { |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
commentFormatting: put a space between //
and comment text (gocritic)
commands/run.go
Outdated
@@ -40,6 +41,7 @@ func Run() { | |||
// handle termination signals | |||
signals := make(chan os.Signal, 1) | |||
signal.Notify(signals, unix.SIGTERM, unix.SIGINT) | |||
ringLog := storelogs.New(500) |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
mnd: Magic number: 500, in detected (gomnd)
agents/supervisor/supervisor.go
Outdated
var res map[string][]string | ||
|
||
for id, agent := range s.agentProcesses { | ||
res[fmt.Sprintf("%s %s", id, agent.requestedState.Type.String())] = agent.logs.GetLogs() |
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.
let's put type to the beginning and let's remove /agent_id/
from id
.
commands/run.go
Outdated
"github.com/percona/pmm-agent/versioner" | ||
) | ||
|
||
const COUNT_SERVER_LOGS = 500 |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
don't use ALL_CAPS in Go names; use CamelCase (golint)
agents/supervisor/supervisor.go
Outdated
@@ -325,8 +349,9 @@ func filter(existing, new map[string]agentpb.AgentParams) (toStart, toRestart, t | |||
|
|||
//nolint:golint | |||
const ( | |||
type_TEST_SLEEP inventorypb.AgentType = 998 // process | |||
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | |||
type_TEST_SLEEP inventorypb.AgentType = 998 // process |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
var-naming: don't use underscores in Go names; const type_TEST_SLEEP should be typeTESTSLEEP (revive)
agents/supervisor/supervisor.go
Outdated
type_TEST_SLEEP inventorypb.AgentType = 998 // process | ||
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | ||
type_TEST_SLEEP inventorypb.AgentType = 998 // process | ||
type_TEST_NOOP inventorypb.AgentType = 999 // built-in |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
var-naming: don't use underscores in Go names; const type_TEST_NOOP should be typeTESTNOOP (revive)
agents/supervisor/supervisor.go
Outdated
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | ||
type_TEST_SLEEP inventorypb.AgentType = 998 // process | ||
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | ||
COUNT_AGENT_LOGS = 10 |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
var-naming: don't use ALL_CAPS in Go names; use CamelCase (revive)
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.
let's use camelCase.
In the cases above snake_case is used because they are mocks for enum and it's better to move them to supervisor_test.go
agents/supervisor/supervisor.go
Outdated
res := make(map[string][]string) | ||
|
||
for id, agent := range s.agentProcesses { | ||
newId := strings.ReplaceAll(id, "/agent_id/", "") |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
var newId
should be newID
(golint)
agents/supervisor/supervisor.go
Outdated
} | ||
|
||
for id, agent := range s.builtinAgents { | ||
newId := strings.ReplaceAll(id, "/agent_id/", "") |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
var newId
should be newID
(golint)
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.
looks good to me, just a few minor comments
agentlocal/agent_local.go
Outdated
reload chan struct{} | ||
reloadCloseOnce sync.Once | ||
|
||
agentlocalpb.UnimplementedAgentLocalServer | ||
} | ||
|
||
// NewServer creates new server. | ||
// | ||
//` |
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.
let's revert
agentlocal/agent_local.go
Outdated
@@ -289,6 +298,42 @@ func (s *Server) runJSONServer(ctx context.Context, grpcAddress string) { | |||
mux.Handle("/debug/", http.DefaultServeMux) | |||
mux.Handle("/debug", debugPageHandler) | |||
mux.Handle("/", proxyMux) | |||
mux.HandleFunc("/logs.zip", func(w http.ResponseWriter, r *http.Request) { |
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.
can we add a test for logs.zip?
agents/supervisor/supervisor.go
Outdated
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | ||
type_TEST_SLEEP inventorypb.AgentType = 998 // process | ||
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | ||
COUNT_AGENT_LOGS = 10 |
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.
let's increase the value
agents/supervisor/supervisor.go
Outdated
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | ||
type_TEST_SLEEP inventorypb.AgentType = 998 // process | ||
type_TEST_NOOP inventorypb.AgentType = 999 // built-in | ||
COUNT_AGENT_LOGS = 10 |
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.
let's use camelCase.
In the cases above snake_case is used because they are mocks for enum and it's better to move them to supervisor_test.go
# Conflicts: # agents/supervisor/supervisor.go
// check interfaces | ||
var ( | ||
_ agentlocalpb.AgentLocalServer = (*Server)(nil) | ||
) | ||
|
||
func (s *Server) Zip(w http.ResponseWriter, r *http.Request) { |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
exported method Server.Zip
should have comment or be unexported (golint)
@@ -108,3 +116,91 @@ func TestServerStatus(t *testing.T) { | |||
assert.Equal(t, expected, actual) | |||
}) | |||
} | |||
|
|||
func TestGetZipFile(t *testing.T) { |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
Function TestGetZipFile missing the call to method parallel (paralleltest)
@@ -108,3 +116,91 @@ func TestServerStatus(t *testing.T) { | |||
assert.Equal(t, expected, actual) | |||
}) | |||
} | |||
|
|||
func TestGetZipFile(t *testing.T) { | |||
setup := func(t *testing.T) ([]*agentlocalpb.AgentInfo, *mockSupervisor, *mockClient, *config.Config) { |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
test helper function should start from t.Helper() (thelper)
for _, serverLog := range s.ringLogs.GetLogs() { | ||
_, err := b.WriteString(serverLog) | ||
if err != nil { | ||
return nil, err |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
error returned from external package is unwrapped: sig: func (*bytes.Buffer).WriteString(s string) (n int, err error) (wrapcheck)
for _, l := range logs { | ||
_, err := b.WriteString(l + "\n") | ||
if err != nil { | ||
return nil, err |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
error returned from external package is unwrapped: sig: func (*bytes.Buffer).WriteString(s string) (n int, err error) (wrapcheck)
} | ||
err := writer.Close() | ||
if err != nil { | ||
return nil, err |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
error returned from external package is unwrapped: sig: func (*archive/zip.Writer).Close() error (wrapcheck)
b, err := ioutil.ReadAll(rec.Body) | ||
require.NoError(t, err) | ||
|
||
expectedFile, err := generateTestZip(s) |
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.
You are generating zip file that you will later compare with, but what if it fails? The output will be not readable.
I think it would be better to unzip received file, and verify content.
Here is example of equality check failing:
The failure message is impossible to understand, because it is a diff between two binary files
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.
please see my comment
agentlocal/agent_local.go
Outdated
// | ||
|
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.
let's revert it.
agentlocal/agent_local.go
Outdated
log.Fatal(err) | ||
} | ||
} | ||
addData(writer, "server.txt", b.Bytes()) |
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.
I think it's better to rename it to pmm-agent.txt
or http-server
if it contains only http server logs.
addData(writer, "server.txt", b.Bytes()) | |
addData(writer, "http-server.txt", b.Bytes()) |
storelogs/storelogs.go
Outdated
func (l *LogsStore) Write(b []byte) (n int, err error) { | ||
l.m.Lock() | ||
l.log.Value = string(b) | ||
l.m.Unlock() |
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.
let's unlock it one line below.
PMM-5680
Build: SUBMODULES-2425