forked from percona/mongodb_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_exporter.go
138 lines (120 loc) · 6.56 KB
/
mongodb_exporter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2017 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 main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/percona/exporter_shared"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
pmmVersion "github.com/percona/pmm/version"
"github.com/percona/mongodb_exporter/collector"
"github.com/percona/mongodb_exporter/shared"
)
const (
program = "mongodb_exporter"
versionDataFormat = "20060102-15:04:05"
)
var (
listenAddressF = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9216").String()
metricsPathF = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
collectDatabaseF = kingpin.Flag("collect.database", "Enable collection of Database metrics").Bool()
collectCollectionF = kingpin.Flag("collect.collection", "Enable collection of Collection metrics").Bool()
collectTopF = kingpin.Flag("collect.topmetrics", "Enable collection of table top metrics").Bool()
collectIndexUsageF = kingpin.Flag("collect.indexusage", "Enable collection of per index usage stats").Bool()
mongodbCollectConnPoolStatsF = kingpin.Flag("collect.connpoolstats", "Collect MongoDB connpoolstats").Bool()
suppressCollectShardingStatusF = kingpin.Flag("suppress.collectshardingstatus", "Suppress the collection of Sharding Status").Default("false").Bool()
collectDatabaseProfilerF = kingpin.Flag("collect.databaseprofiler", "Enable collection of database system.profiler metrics").Bool()
collectDatabaseCurrentOpsF = kingpin.Flag("collect.currentop", "Enable collection of $currentOp metrics").Bool()
databaseProfilerLookbackF = kingpin.Flag("databaseprofiler.lookback", "Size of the system.profile scan window, in seconds").Default("30").Int64()
databaseProfilerMillisThresholdF = kingpin.Flag("databaseprofiler.millisthreshold", "Min query duration, in ms, for slow queries to count").Default("0").Int64()
databaseProfilerDocsExaminedThresholdF = kingpin.Flag("databaseprofiler.docsexaminedthreshold", "Min query duration, for slow queries to count").Default("1000").Int64()
databaseCurrentOpsMillisThreshold = kingpin.Flag("currentop.millisthreshold", "Min query duration, in ms, for current ops to count").Default("1000").Int64()
uriF = kingpin.Flag("mongodb.uri", "MongoDB URI, format").
PlaceHolder("[mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]").
Default("mongodb://localhost:27017").
Envar("MONGODB_URI").
String()
testF = kingpin.Flag("test", "Check MongoDB connection, print buildInfo() information and exit.").Bool()
)
func main() {
initVersionInfo()
log.AddFlags(kingpin.CommandLine)
kingpin.Parse()
if *testF {
buildInfo, err := shared.TestConnection(
shared.MongoSessionOpts{
URI: *uriF,
},
)
if err != nil {
log.Errorf("Can't connect to MongoDB: %s", err)
os.Exit(1)
}
fmt.Println(string(buildInfo))
os.Exit(0)
}
// TODO: Maybe we should move version.Info() and version.BuildContext() to https://github.com/percona/exporter_shared
// See: https://jira.percona.com/browse/PMM-3250 and https://github.com/percona/mongodb_exporter/pull/132#discussion_r262227248
log.Infoln("Starting", program, version.Info())
log.Infoln("Build context", version.BuildContext())
programCollector := version.NewCollector(program)
mongodbCollector := collector.NewMongodbCollector(&collector.MongodbCollectorOpts{
URI: *uriF,
CollectDatabaseMetrics: *collectDatabaseF,
CollectCollectionMetrics: *collectCollectionF,
CollectTopMetrics: *collectTopF,
CollectIndexUsageStats: *collectIndexUsageF,
CollectConnPoolStats: *mongodbCollectConnPoolStatsF,
SuppressCollectShardingStatus: *suppressCollectShardingStatusF,
CollectDatabaseCurrentOps: *collectDatabaseCurrentOpsF,
CollectDatabaseProfiler: *collectDatabaseProfilerF,
DatabaseProfilerLookback: *databaseProfilerLookbackF,
DatabaseProfilerMillisThreshold: *databaseProfilerMillisThresholdF,
DatabaseProfilerDocsExaminedThreshold: *databaseProfilerDocsExaminedThresholdF,
DatabaseCurrentOpsMillisThreshold: *databaseCurrentOpsMillisThreshold,
})
prometheus.MustRegister(programCollector, mongodbCollector)
promHandler := promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))
exporter_shared.RunServer("MongoDB", *listenAddressF, *metricsPathF, promHandler)
}
// initVersionInfo sets version info
// If binary was build for PMM with environment variable PMM_RELEASE_VERSION
// `--version` will be displayed in PMM format. Also `PMM Version` will be connected
// to application version and will be printed in all logs.
// TODO: Refactor after moving version.Info() and version.BuildContext() to https://github.com/percona/exporter_shared
// See: https://jira.percona.com/browse/PMM-3250 and https://github.com/percona/mongodb_exporter/pull/132#discussion_r262227248
func initVersionInfo() {
version.Version = pmmVersion.Version
version.Revision = pmmVersion.FullCommit
version.Branch = pmmVersion.Branch
if buildDate, err := strconv.ParseInt(pmmVersion.Timestamp, 10, 64); err != nil {
version.BuildDate = time.Unix(0, 0).Format(versionDataFormat)
} else {
version.BuildDate = time.Unix(buildDate, 0).Format(versionDataFormat)
}
if pmmVersion.PMMVersion != "" {
version.Version += "-pmm-" + pmmVersion.PMMVersion
kingpin.Version(pmmVersion.FullInfo())
} else {
kingpin.Version(version.Print(program))
}
kingpin.HelpFlag.Short('h')
kingpin.CommandLine.Help = fmt.Sprintf("%s exports various MongoDB metrics in Prometheus format.\n", pmmVersion.ShortInfo())
}