-
Notifications
You must be signed in to change notification settings - Fork 148
PMM-12548 Slowlog as new MongoDB query source. #3776
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
Draft
JiriCtvrtka
wants to merge
35
commits into
v3
Choose a base branch
from
PMM-12548-new-mongo-query-source
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10,131
−2,086
Draft
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
3f3c960
PMM-12548 Admin part.
JiriCtvrtka eda4fec
PMM-12548 New mongo agents structure.
JiriCtvrtka 2d29da3
PMM-12548 Changes for agent.
JiriCtvrtka 1aba2ea
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 1abd56f
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 8bc1ca3
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 755b360
PMM-12548 Compose.
JiriCtvrtka 3513beb
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 03308e5
PMM-12548 Change SSL to TLS since its deprecated.
JiriCtvrtka 6e707e9
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 6bceb49
PMM-12548 Slowlog prefixes for testing, tls instead ssl.
JiriCtvrtka 7f765ec
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 47f721b
PMM-12548 Reader test.
JiriCtvrtka 1a0f27d
PMM-12548 Percona Mongo, formatting.
JiriCtvrtka 298fdbb
PMM-12548 Add aggregator.
JiriCtvrtka 2d87252
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 170b9e2
PMM-12548 Renaming to mongolog.
JiriCtvrtka 898abbd
PMM-12548 Managed part, changes in agent.
JiriCtvrtka d6be814
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 7d2d105
PMM-12548 Small refactor.
JiriCtvrtka b804de8
PMM-12548 Move file reader.
JiriCtvrtka 4b40566
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 0c17f11
PMM-12548 Fix.
JiriCtvrtka cf7cb81
PMM-12548 Empty line.
JiriCtvrtka be53fbf
PMM-12548 Percona Toolkit with json tags.
JiriCtvrtka 49a18ae
PMM-12548 Sum.
JiriCtvrtka 7427063
PMM-12548 Refactor, logic changes.
JiriCtvrtka e572dbf
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka eca8c3c
PMM-12548 Missed add/change agents in API.
JiriCtvrtka 6d6469e
PMM-12548 Lint.
JiriCtvrtka ff27a35
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 83f14f0
PMM-12548 Fix Mongolog admin response.
JiriCtvrtka 17890a0
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka ba33053
PMM-12548 Lint.
JiriCtvrtka 5e80b7a
Merge branch 'mainv3' into PMM-12548-new-mongo-query-source
JiriCtvrtka 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
112 changes: 112 additions & 0 deletions
112
admin/commands/inventory/add_agent_qan_mongodb_mongolog_agent.go
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (C) 2023 Percona LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package inventory | ||
|
||
import ( | ||
"github.com/percona/pmm/admin/commands" | ||
"github.com/percona/pmm/admin/pkg/flags" | ||
"github.com/percona/pmm/api/inventory/v1/json/client" | ||
agents "github.com/percona/pmm/api/inventory/v1/json/client/agents_service" | ||
) | ||
|
||
var addAgentQANMongoDBMongologAgentResultT = commands.ParseTemplate(` | ||
QAN MongoDB Mongolog agent added. | ||
Agent ID : {{ .Agent.AgentID }} | ||
PMM-Agent ID : {{ .Agent.PMMAgentID }} | ||
Service ID : {{ .Agent.ServiceID }} | ||
Username : {{ .Agent.Username }} | ||
TLS enabled : {{ .Agent.TLS }} | ||
Skip TLS verification : {{ .Agent.TLSSkipVerify }} | ||
|
||
Status : {{ .Agent.Status }} | ||
Disabled : {{ .Agent.Disabled }} | ||
Custom labels : {{ .Agent.CustomLabels }} | ||
`) | ||
|
||
type addAgentQANMongoDBMongologAgentResult struct { | ||
Agent *agents.AddAgentOKBodyQANMongodbMongologAgent `json:"qan_mongodb_mongolog_agent"` | ||
} | ||
|
||
func (res *addAgentQANMongoDBMongologAgentResult) Result() {} | ||
|
||
func (res *addAgentQANMongoDBMongologAgentResult) String() string { | ||
return commands.RenderTemplate(addAgentQANMongoDBMongologAgentResultT, res) | ||
} | ||
|
||
// AddAgentQANMongoDBMongologAgentCommand is used by Kong for CLI flags and commands. | ||
// | ||
//nolint:lll | ||
type AddAgentQANMongoDBMongologAgentCommand struct { | ||
PMMAgentID string `arg:"" help:"The pmm-agent identifier which runs this instance"` | ||
ServiceID string `arg:"" help:"Service identifier"` | ||
Username string `arg:"" optional:"" help:"MongoDB username for scraping metrics"` | ||
Password string `help:"MongoDB password for scraping metrics"` | ||
CustomLabels map[string]string `mapsep:"," help:"Custom user-assigned labels"` | ||
SkipConnectionCheck bool `help:"Skip connection check"` | ||
MaxQueryLength int32 `placeholder:"NUMBER" help:"Limit query length in QAN (default: server-defined; -1: no limit)"` | ||
DisableQueryExamples bool `name:"disable-queryexamples" help:"Disable collection of query examples"` | ||
TLS bool `help:"Use TLS to connect to the database"` | ||
TLSSkipVerify bool `help:"Skip TLS certificates validation"` | ||
TLSCertificateKeyFile string `help:"Path to TLS certificate PEM file"` | ||
TLSCertificateKeyFilePassword string `help:"Password for certificate"` | ||
TLSCaFile string `help:"Path to certificate authority file"` | ||
AuthenticationMechanism string `help:"Authentication mechanism. Default is empty. Use MONGODB-X509 for ssl certificates"` | ||
|
||
flags.LogLevelFatalFlags | ||
} | ||
|
||
// RunCmd executes the AddAgentQANMongoDBMongologAgentCommand and returns the result. | ||
func (cmd *AddAgentQANMongoDBMongologAgentCommand) RunCmd() (commands.Result, error) { | ||
customLabels := commands.ParseCustomLabels(cmd.CustomLabels) | ||
|
||
tlsCertificateKey, err := commands.ReadFile(cmd.TLSCertificateKeyFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tlsCa, err := commands.ReadFile(cmd.TLSCaFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
params := &agents.AddAgentParams{ | ||
Body: agents.AddAgentBody{ | ||
QANMongodbMongologAgent: &agents.AddAgentParamsBodyQANMongodbMongologAgent{ | ||
PMMAgentID: cmd.PMMAgentID, | ||
ServiceID: cmd.ServiceID, | ||
Username: cmd.Username, | ||
Password: cmd.Password, | ||
CustomLabels: customLabels, | ||
SkipConnectionCheck: cmd.SkipConnectionCheck, | ||
MaxQueryLength: cmd.MaxQueryLength, | ||
TLS: cmd.TLS, | ||
TLSSkipVerify: cmd.TLSSkipVerify, | ||
TLSCertificateKey: tlsCertificateKey, | ||
TLSCertificateKeyFilePassword: cmd.TLSCertificateKeyFilePassword, | ||
TLSCa: tlsCa, | ||
AuthenticationMechanism: cmd.AuthenticationMechanism, | ||
LogLevel: cmd.LogLevelFatalFlags.LogLevel.EnumValue(), | ||
}, | ||
}, | ||
Context: commands.Ctx, | ||
} | ||
|
||
resp, err := client.Default.AgentsService.AddAgent(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &addAgentQANMongoDBMongologAgentResult{ | ||
Agent: resp.Payload.QANMongodbMongologAgent, | ||
}, nil | ||
} |
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
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
153 changes: 153 additions & 0 deletions
153
agent/agents/mongodb/mongolog/internal/collector/collector.go
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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright (C) 2023 Percona LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package collector implements collecting mongo logs from file. | ||
package collector | ||
|
||
import ( | ||
"context" | ||
"runtime/pprof" | ||
"sync" | ||
"time" | ||
|
||
"github.com/percona/percona-toolkit/src/go/mongolib/proto" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// New creates new Collector. | ||
func New(logsPath string, logger *logrus.Entry) *Collector { | ||
return &Collector{ | ||
logsPath: logsPath, | ||
logger: logger.WithField("log", logsPath), | ||
} | ||
} | ||
|
||
// Collector is used by Mongolog agent. | ||
type Collector struct { | ||
// dependencies | ||
logsPath string | ||
logger *logrus.Entry | ||
|
||
// provides | ||
docsChan chan proto.SystemProfile | ||
|
||
// state | ||
m sync.Mutex // Lock() to protect internal consistency of the service | ||
running bool // Is this service running? | ||
doneChan chan struct{} // close(doneChan) to notify goroutines that they should shutdown | ||
wg *sync.WaitGroup // Wait() for goroutines to stop after being notified they should shutdown | ||
} | ||
|
||
// Start starts but doesn't wait until it exits | ||
func (c *Collector) Start(ctx context.Context) (<-chan proto.SystemProfile, error) { | ||
c.m.Lock() | ||
defer c.m.Unlock() | ||
if c.running { | ||
return nil, nil | ||
} | ||
|
||
// create new channels over which we will communicate to... | ||
// ... outside world by sending collected docs | ||
c.docsChan = make(chan proto.SystemProfile, 100) | ||
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. 🚫 [golangci] reported by reviewdog 🐶 |
||
// ... inside goroutine to close it | ||
c.doneChan = make(chan struct{}) | ||
|
||
// start a goroutine and Add() it to WaitGroup | ||
// so we could later Wait() for it to finish | ||
c.wg = &sync.WaitGroup{} | ||
c.wg.Add(1) | ||
|
||
// create ready sync.Cond so we could know when goroutine actually started getting data from db | ||
ready := sync.NewCond(&sync.Mutex{}) | ||
ready.L.Lock() | ||
defer ready.L.Unlock() | ||
|
||
labels := pprof.Labels("component", "mongodb.aggregator") | ||
go pprof.Do(ctx, labels, func(ctx context.Context) { | ||
start( | ||
ctx, | ||
c.wg, | ||
c.logsPath, | ||
c.docsChan, | ||
c.doneChan, | ||
ready, | ||
c.logger) | ||
}) | ||
|
||
// wait until we actually fetch data from db | ||
ready.Wait() | ||
|
||
c.running = true | ||
|
||
return c.docsChan, nil | ||
} | ||
|
||
// Stop stops running | ||
func (c *Collector) Stop() { | ||
c.m.Lock() | ||
defer c.m.Unlock() | ||
|
||
if !c.running { | ||
return | ||
} | ||
|
||
c.running = false | ||
close(c.doneChan) // notify goroutine to close | ||
c.wg.Wait() // wait for goroutines to exit | ||
close(c.docsChan) // we can now safely close channels goroutines write to as goroutine is stopped | ||
} | ||
|
||
func start(ctx context.Context, wg *sync.WaitGroup, logsPath string, | ||
docsChan chan<- proto.SystemProfile, doneChan <-chan struct{}, ready *sync.Cond, logger *logrus.Entry, | ||
) { | ||
// signal WaitGroup when goroutine finished | ||
defer wg.Done() | ||
|
||
fr, err := NewReader(ctx, docsChan, doneChan, logsPath, logger) | ||
if err != nil { | ||
logger.Error(err) | ||
return | ||
} | ||
go func() { | ||
fr.ReadFile() | ||
logger.Debugln("reading routine quit") | ||
}() | ||
|
||
firstTry := true | ||
|
||
for { | ||
select { | ||
// check if we should shutdown | ||
case <-ctx.Done(): | ||
return | ||
case <-doneChan: | ||
return | ||
// wait some time before reconnecting | ||
case <-time.After(1 * time.Second): | ||
} | ||
|
||
// After first failure in connection we signal that we are ready anyway | ||
// this way service starts, and will automatically connect when db is available. | ||
if firstTry { | ||
signalReady(ready) | ||
firstTry = false | ||
} | ||
} | ||
} | ||
|
||
func signalReady(ready *sync.Cond) { | ||
ready.L.Lock() | ||
defer ready.L.Unlock() | ||
ready.Broadcast() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
🚫 [golangci] reported by reviewdog 🐶
directive
//nolint:lll
is unused for linter "lll" (nolintlint)