Skip to content

Commit 94f392c

Browse files
shapirojosephcopybara-github
authored andcommitted
Receive common discovery information via channel in MySQL workload agent service
PiperOrigin-RevId: 698427381
1 parent a65651b commit 94f392c

File tree

5 files changed

+357
-27
lines changed

5 files changed

+357
-27
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/sijms/go-ora v1.3.2
1818
github.com/spf13/cobra v1.8.1
1919
github.com/spf13/pflag v1.0.5
20-
go.uber.org/zap v1.24.0
20+
go.uber.org/zap v1.27.0
2121
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
2222
google.golang.org/genproto v0.0.0-20240205150955-31a09d347014
2323
google.golang.org/genproto/googleapis/api v0.0.0-20240205150955-31a09d347014
@@ -59,8 +59,7 @@ require (
5959
go.opentelemetry.io/otel v1.24.0 // indirect
6060
go.opentelemetry.io/otel/metric v1.24.0 // indirect
6161
go.opentelemetry.io/otel/trace v1.24.0 // indirect
62-
go.uber.org/atomic v1.7.0 // indirect
63-
go.uber.org/multierr v1.6.0 // indirect
62+
go.uber.org/multierr v1.10.0 // indirect
6463
golang.org/x/crypto v0.21.0 // indirect
6564
golang.org/x/net v0.23.0 // indirect
6665
golang.org/x/oauth2 v0.17.0 // indirect

go.sum

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
1919
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2020
github.com/GoogleCloudPlatform/sapagent v1.5.2-0.20241015171556-8aee46464472 h1:Ge9C/Yhj5zG64k+L3VwqidqwLfhznsQYkmZ6w6Gzhlo=
2121
github.com/GoogleCloudPlatform/sapagent v1.5.2-0.20241015171556-8aee46464472/go.mod h1:V8DxNa3SvogrCoGLhAAHqemw421pzQOoAW5O5MlI5Us=
22-
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
23-
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
2422
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
2523
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
2624
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -117,7 +115,6 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
117115
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
118116
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
119117
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
120-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
121118
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
122119
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
123120
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
@@ -144,14 +141,12 @@ go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZ
144141
go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E=
145142
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
146143
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
147-
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
148-
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
149-
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
150-
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
151-
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
152-
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
153-
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
154-
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
144+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
145+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
146+
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
147+
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
148+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
149+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
155150
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
156151
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
157152
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

internal/daemon/daemon.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ func (d *Daemon) startdaemonHandler(ctx context.Context, cancel context.CancelFu
159159
recoverableStart.StartRoutine(ctx)
160160
}
161161

162-
d.runCommonDiscovery(ctx)
163-
164162
// Log a RUNNING usage metric once a day.
165163
go usagemetrics.LogRunningDaily()
166164
d.waitForShutdown(shutdownch, cancel)
@@ -187,9 +185,3 @@ func (d *Daemon) waitForShutdown(ch <-chan os.Signal, cancel context.CancelFunc)
187185
time.Sleep(3 * time.Second)
188186
log.Logger.Info("Shutting down...")
189187
}
190-
191-
// runCommonDiscovery runs common discovery on a timer and updates the configuration based on the results.
192-
func (d *Daemon) runCommonDiscovery(ctx context.Context) {
193-
// TODO: Implement the recurring common discovery.
194-
log.Logger.Info("Running common discovery - NOT YET IMPLEMENTED")
195-
}

internal/daemon/mysql/mysql.go

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"time"
2323

24+
"go.uber.org/zap/zapcore"
2425
"github.com/GoogleCloudPlatform/sapagent/shared/log"
2526
"github.com/GoogleCloudPlatform/sapagent/shared/recovery"
2627
"github.com/GoogleCloudPlatform/workloadagent/internal/commondiscovery"
@@ -32,9 +33,11 @@ import (
3233

3334
// Service implements the interfaces for MySQL workload agent service.
3435
type Service struct {
35-
Config *cpb.Configuration
36-
CloudProps *cpb.CloudProperties
37-
CommonCh chan commondiscovery.Result
36+
Config *cpb.Configuration
37+
CloudProps *cpb.CloudProperties
38+
CommonCh chan commondiscovery.Result
39+
processes commondiscovery.Result
40+
mySQLProcesses []commondiscovery.ProcessWrapper
3841
// ... MySQL-specific attributes ...
3942
}
4043

@@ -48,8 +51,16 @@ type runMetricCollectionArgs struct {
4851

4952
// Start initiates the MySQL workload agent service
5053
func (s *Service) Start(ctx context.Context, a any) {
51-
if !s.Config.GetMysqlConfiguration().GetEnabled() {
52-
log.CtxLogger(ctx).Info("MySQL workload agent service is disabled")
54+
if s.Config.GetMysqlConfiguration() == nil || s.Config.GetMysqlConfiguration().Enabled == nil {
55+
// If MySQL workload agent service is not explicitly enabled in the configuration,
56+
// then check if the workload is present on the host.
57+
log.CtxLogger(ctx).Info("MySQL workload agent service is not explicitly enabled in the configuration")
58+
if !s.checkCommonDiscovery(ctx) {
59+
return
60+
}
61+
} else if !s.Config.GetMysqlConfiguration().GetEnabled() {
62+
// If MySQL workload agent service is explicitly disabled in the configuration, then return.
63+
log.CtxLogger(ctx).Info("MySQL workload agent service is disabled in the configuration")
5364
return
5465
}
5566

@@ -78,6 +89,10 @@ func (s *Service) Start(ctx context.Context, a any) {
7889
case <-ctx.Done():
7990
log.CtxLogger(ctx).Info("MySQL workload agent service cancellation requested")
8091
return
92+
case s.processes = <-s.CommonCh:
93+
log.CtxLogger(ctx).Debugw("MySQL workload agent service received common discovery result", "result", s.processes)
94+
s.identifyMySQLProcesses(ctx)
95+
return
8196
}
8297
}
8398

@@ -126,6 +141,52 @@ func runMetricCollection(ctx context.Context, a any) {
126141
}
127142
}
128143

144+
// checkCommonDiscovery checks for common discovery results.
145+
// Returns true if MySQL workload is present on the host.
146+
// Otherwise, returns false to indicate that the context is done.
147+
func (s *Service) checkCommonDiscovery(ctx context.Context) bool {
148+
for {
149+
select {
150+
case <-ctx.Done():
151+
log.CtxLogger(ctx).Info("MySQL workload agent service cancellation requested")
152+
return false
153+
case s.processes = <-s.CommonCh:
154+
log.CtxLogger(ctx).Debugw("MySQL workload agent service received common discovery result", "NumProcesses", len(s.processes.Processes))
155+
s.identifyMySQLProcesses(ctx)
156+
if s.isWorkloadPresent() {
157+
log.CtxLogger(ctx).Info("MySQL workload is present, starting discovery and metric collection")
158+
return true
159+
}
160+
log.CtxLogger(ctx).Debug("MySQL workload is not present")
161+
}
162+
}
163+
}
164+
165+
func (s *Service) identifyMySQLProcesses(ctx context.Context) {
166+
for _, process := range s.processes.Processes {
167+
name, err := process.Name()
168+
if err == nil && name == "mysqld" {
169+
s.mySQLProcesses = append(s.mySQLProcesses, process)
170+
}
171+
}
172+
s.logMySQLProcesses(ctx, zapcore.DebugLevel)
173+
}
174+
175+
func (s *Service) isWorkloadPresent() bool {
176+
return len(s.mySQLProcesses) > 0
177+
}
178+
179+
func (s *Service) logMySQLProcesses(ctx context.Context, loglevel zapcore.Level) {
180+
log.CtxLogger(ctx).Logf(loglevel, "Number of MySQL processes found: %v", len(s.mySQLProcesses))
181+
for _, process := range s.mySQLProcesses {
182+
name, _ := process.Name()
183+
username, _ := process.Username()
184+
cmdline, _ := process.CmdlineSlice()
185+
env, _ := process.Environ()
186+
log.CtxLogger(ctx).Logw(loglevel, "MySQL process", "name", name, "username", username, "cmdline", cmdline, "env", env, "pid", process.Pid())
187+
}
188+
}
189+
129190
// String returns the name of the MySQL service.
130191
func (s *Service) String() string {
131192
return "MySQL Service"

0 commit comments

Comments
 (0)