Skip to content

Commit

Permalink
mysql plugin support global queries
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Dec 19, 2022
1 parent 18a4b35 commit 191646e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
9 changes: 9 additions & 0 deletions conf/input.mysql/mysql.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# # collect interval
# interval = 15

# [[queries]]
# mesurement = "users"
# metric_fields = [ "total" ]
# label_fields = [ "service" ]
# timeout = "3s"
# request = '''
# select 'n9e' as service, count(*) as total from n9e_v5.users
# '''

[[instances]]
# address = "127.0.0.1:3306"
# username = "root"
Expand Down
5 changes: 5 additions & 0 deletions inputs/mysql/custom_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func (ins *Instance) gatherCustomQueries(slist *types.SampleList, db *sql.DB, gl
wg.Add(1)
go ins.gatherOneQuery(slist, db, globalTags, wg, ins.Queries[i])
}

for i := 0; i < len(ins.GlobalQueries); i++ {
wg.Add(1)
go ins.gatherOneQuery(slist, db, globalTags, wg, ins.GlobalQueries[i])
}
}

func (ins *Instance) gatherOneQuery(slist *types.SampleList, db *sql.DB, globalTags map[string]string, wg *sync.WaitGroup, query QueryConfig) {
Expand Down
7 changes: 5 additions & 2 deletions inputs/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type Instance struct {
Parameters string `toml:"parameters"`
TimeoutSeconds int64 `toml:"timeout_seconds"`

Queries []QueryConfig `toml:"queries"`
Queries []QueryConfig `toml:"queries"`
GlobalQueries []QueryConfig `toml:"-"`

ExtraStatusMetrics bool `toml:"extra_status_metrics"`
ExtraInnodbMetrics bool `toml:"extra_innodb_metrics"`
Expand Down Expand Up @@ -149,7 +150,8 @@ func (ins *Instance) InitValidMetrics() {

type MySQL struct {
config.PluginConfig
Instances []*Instance `toml:"instances"`
Instances []*Instance `toml:"instances"`
Queries []QueryConfig `toml:"queries"`
}

func init() {
Expand All @@ -161,6 +163,7 @@ func init() {
func (m *MySQL) GetInstances() []inputs.Instance {
ret := make([]inputs.Instance, len(m.Instances))
for i := 0; i < len(m.Instances); i++ {
m.Instances[i].GlobalQueries = m.Queries
ret[i] = m.Instances[i]
}
return ret
Expand Down
14 changes: 9 additions & 5 deletions inputs/oracle/oracle_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type Instance struct {
DisableConnectionPool bool `toml:"disable_connection_pool"`
MaxOpenConnections int `toml:"max_open_connections"`
Metrics []MetricConfig `toml:"metrics"`

client *sqlx.DB
GlobalMetrics []MetricConfig `toml:"-"`
client *sqlx.DB
}

type MetricConfig struct {
Expand Down Expand Up @@ -68,9 +68,7 @@ func (o *Oracle) Drop() {
func (o *Oracle) GetInstances() []inputs.Instance {
ret := make([]inputs.Instance, len(o.Instances))
for i := 0; i < len(o.Instances); i++ {
if len(o.Instances[i].Metrics) < len(o.Metrics) {
o.Instances[i].Metrics = append(o.Instances[i].Metrics, o.Metrics...)
}
o.Instances[i].GlobalMetrics = o.Metrics
ret[i] = o.Instances[i]
}
return ret
Expand Down Expand Up @@ -127,6 +125,12 @@ func (ins *Instance) Gather(slist *types.SampleList) {
go ins.scrapeMetric(waitMetrics, slist, m, tags)
}

for i := 0; i < len(ins.GlobalMetrics); i++ {
m := ins.GlobalMetrics[i]
waitMetrics.Add(1)
go ins.scrapeMetric(waitMetrics, slist, m, tags)
}

waitMetrics.Wait()
}

Expand Down

0 comments on commit 191646e

Please sign in to comment.