-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
124 lines (103 loc) · 2.76 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package collector
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/go-logr/logr"
_ "github.com/go-sql-driver/mysql" // MySQL driver
_ "github.com/lib/pq" // PostgreSQL driver
"github.com/mackerelio-labs/mackerel-sql-metric-collector/exporter"
"github.com/mackerelio-labs/mackerel-sql-metric-collector/query"
_ "github.com/mattn/go-sqlite3" // SQLite3 driver
_ "github.com/speee/go-athena" // AWS Athena driver
"golang.org/x/sync/errgroup"
_ "gorm.io/driver/bigquery/driver" // BigQuery driver
)
// Collector represents ...
type Collector struct {
config *Config
exporter exporter.Exporter
logger logr.Logger
}
// NewCollector is ...
func NewCollector(conf *Config, exporter exporter.Exporter, logger logr.Logger) (*Collector, error) {
return &Collector{
config: conf,
exporter: exporter,
logger: logger,
}, nil
}
// Run collect and post metrics.
func (c *Collector) Run(queries []query.Query) error {
return c.RunWithContext(context.Background(), queries)
}
// RunWithContext collect and post metrics with context.Context.
func (c *Collector) RunWithContext(ctx context.Context, queries []query.Query) error {
db, err := openDataSource(c.config.DSN)
if err != nil {
return err
}
defer db.Close()
queue := make(chan struct{}, c.config.MaxConcurrency-1)
eg := &errgroup.Group{} // Create *errgroup.Group as we want to run all queries.
for _, q := range queries {
queue <- struct{}{}
q := q
eg.Go(func() error {
defer func() {
<-queue
}()
metrics, err := q.ExecuteWithContext(ctx, db, c.logger)
if err != nil {
return err
}
return c.exporter.ExportWithContext(ctx, c.detectService(q), metrics)
})
}
if err := eg.Wait(); err != nil {
return err
}
return nil
}
func (c *Collector) detectService(q query.Query) string {
s := q.GetService()
if s == "" {
return c.config.DefaultService
}
return s
}
func openDataSource(dsn string) (*sql.DB, error) {
driverName, dataSourceName, err := parseDSN(dsn)
if err != nil {
return nil, err
}
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}
const (
bigQueryDriverName = "bigquery"
bigQueryDSNPrefix = "bigquery://"
)
func parseDSN(dsn string) (string, string, error) {
var driverName, dataSourceName string
// see. https://github.com/go-sql-driver/mysql#dsn-data-source-name
parts := strings.SplitN(dsn, "://", 2)
if len(parts) != 2 {
return driverName, dataSourceName, fmt.Errorf("invalid dsn: %s", dsn)
}
driverName = parts[0]
dataSourceName = parts[1]
// for gorm.io/driver/bigquery/driver
if driverName == bigQueryDriverName {
dataSourceName = bigQueryDSNPrefix + dataSourceName
}
return driverName, dataSourceName, nil
}