Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

PMM-5680 store logs #342

Open
wants to merge 70 commits into
base: main
Choose a base branch
from
Open

PMM-5680 store logs #342

wants to merge 70 commits into from

Conversation

qwest812
Copy link
Contributor

@qwest812 qwest812 commented Mar 23, 2022

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)
Copy link
Contributor

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/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to buffer

@@ -63,6 +63,11 @@ type Server struct {
reloadCloseOnce sync.Once
}

func (s *Server) mustEmbedUnimplementedAgentLocalServer() {
Copy link
Contributor

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


type LogsStore struct {
log *ring.Ring
Entry *logrus.Entry
Copy link
Contributor

Choose a reason for hiding this comment

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

entry *logrus.Entry

Copy link
Contributor Author

@qwest812 qwest812 Apr 12, 2022

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),

Copy link
Contributor

@YaroslavPodorvanov YaroslavPodorvanov left a comment

Choose a reason for hiding this comment

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

mustEmbed need delete

reload chan struct{}
reloadCloseOnce sync.Once

agentlocalpb.UnimplementedAgentLocalServer
}

//// AgentLogs contains information about Agent logs.
//type AgentLogs struct {
Copy link
Contributor

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)
Copy link
Contributor

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)

var res map[string][]string

for id, agent := range s.agentProcesses {
res[fmt.Sprintf("%s %s", id, agent.requestedState.Type.String())] = agent.logs.GetLogs()
Copy link
Member

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
Copy link
Contributor

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)

@@ -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
Copy link
Contributor

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)

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
Copy link
Contributor

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)

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
Copy link
Contributor

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)

Copy link
Member

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

res := make(map[string][]string)

for id, agent := range s.agentProcesses {
newId := strings.ReplaceAll(id, "/agent_id/", "")
Copy link
Contributor

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)

}

for id, agent := range s.builtinAgents {
newId := strings.ReplaceAll(id, "/agent_id/", "")
Copy link
Contributor

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)

@qwest812 qwest812 requested a review from BupycHuk May 11, 2022 08:54
Copy link
Member

@BupycHuk BupycHuk left a 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

reload chan struct{}
reloadCloseOnce sync.Once

agentlocalpb.UnimplementedAgentLocalServer
}

// NewServer creates new server.
//
//`
Copy link
Member

Choose a reason for hiding this comment

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

let's revert

@@ -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) {
Copy link
Member

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?

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
Copy link
Member

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

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
Copy link
Member

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

// check interfaces
var (
_ agentlocalpb.AgentLocalServer = (*Server)(nil)
)

func (s *Server) Zip(w http.ResponseWriter, r *http.Request) {
Copy link
Contributor

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) {
Copy link
Contributor

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) {
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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:
image

The failure message is impossible to understand, because it is a diff between two binary files

Copy link
Contributor

@ritbl ritbl left a 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

@qwest812 qwest812 requested a review from BupycHuk May 30, 2022 19:10
Comment on lines 71 to 76
//

Copy link
Member

Choose a reason for hiding this comment

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

let's revert it.

log.Fatal(err)
}
}
addData(writer, "server.txt", b.Bytes())
Copy link
Member

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.

Suggested change
addData(writer, "server.txt", b.Bytes())
addData(writer, "http-server.txt", b.Bytes())

func (l *LogsStore) Write(b []byte) (n int, err error) {
l.m.Lock()
l.log.Value = string(b)
l.m.Unlock()
Copy link
Member

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.

@qwest812 qwest812 requested a review from ritbl June 1, 2022 12:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants