Skip to content

Commit 01f0ef9

Browse files
authored
Merge pull request #89 from percona/develop
Version 0.4.0
2 parents adabd4f + 507afc7 commit 01f0ef9

File tree

766 files changed

+116970
-13656
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

766 files changed

+116970
-13656
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ sudo: required
33
language: go
44

55
go:
6-
- 1.8.x
76
- 1.9.x
87
- master
98

@@ -13,7 +12,9 @@ matrix:
1312

1413
env:
1514
- MONGODB_IMAGE=mongo:3.4
15+
- MONGODB_IMAGE=mongo:3.6
1616
- MONGODB_IMAGE=percona/percona-server-mongodb:3.4
17+
- MONGODB_IMAGE=perconalab/percona-server-mongodb:3.6
1718

1819
services:
1920
- docker

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## v0.4.0 (2017-01-17)
4+
5+
* New flags `-collect.database` and `-collect.collection` can be used to enable collection of database and collection
6+
metrics. They are disabled by default.
7+
* MongoDB connections are now kept between the scrapes. New flag `-mongodb.max-connections` (with the default value `1`)
8+
controls the maximum number of established connections.
9+
* Add standard metrics:
10+
* `mongodb_scrape_errors_total`
11+
* `mongodb_up`
12+
* Some queries now contain [cursor comments](https://www.percona.com/blog/2017/06/21/tracing-mongodb-queries-to-code-with-cursor-comments/)
13+
with source code locations.
14+
* Go vendoring switched to [dep](https://github.com/golang/dep).
15+
316
## v0.3.1 (2017-09-08)
417

518
* Better logging for scrape errors.

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ export MONGODB_URL='mongodb://localhost:27017'
1616

1717
## Vendoring
1818

19-
We use [Glide](https://glide.sh) to vendor dependencies. Please use released version of Glide (i.e. do not `go get`
20-
from `master` branch). Also please use `--strip-vendor` flag.
19+
We use [dep](https://github.com/golang/dep) to vendor dependencies.
20+
Please use released version of dep (i.e. do not `go get` from `master` branch).

Gopkg.lock

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ db.getSiblingDB("admin").createUser({
6464
export MONGODB_URL=mongodb://mongodb_exporter:s3cr3tpassw0rd@localhost:27017
6565
```
6666

67+
If you use [x.509 Certificates to Authenticate Clients](https://docs.mongodb.com/manual/tutorial/configure-x509-client-authentication/), pass in username and `authMechanism` via [connection options](https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options) to the MongoDB uri. Eg:
68+
69+
```
70+
mongodb://CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry@localhost:27017/?authMechanism=MONGODB-X509
71+
```
72+
6773
## Note about how this works
6874

6975
Point the process to any mongo port and it will detect if it is a mongos, replicaset member, or stand alone mongod and return the appropriate metrics for that type of node. This was done to preent the need to an exporter per type of process.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.2
1+
0.4.0
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package collector_mongod
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"github.com/prometheus/common/log"
6+
"gopkg.in/mgo.v2"
7+
"gopkg.in/mgo.v2/bson"
8+
)
9+
10+
var (
11+
collectionSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
12+
Namespace: Namespace,
13+
Subsystem: "db_coll",
14+
Name: "size",
15+
Help: "The total size in memory of all records in a collection",
16+
}, []string{"db", "coll"})
17+
collectionObjectCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
18+
Namespace: Namespace,
19+
Subsystem: "db_coll",
20+
Name: "count",
21+
Help: "The number of objects or documents in this collection",
22+
}, []string{"db", "coll"})
23+
collectionAvgObjSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
24+
Namespace: Namespace,
25+
Subsystem: "db_coll",
26+
Name: "avgobjsize",
27+
Help: "The average size of an object in the collection (plus any padding)",
28+
}, []string{"db", "coll"})
29+
collectionStorageSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
30+
Namespace: Namespace,
31+
Subsystem: "db_coll",
32+
Name: "storage_size",
33+
Help: "The total amount of storage allocated to this collection for document storage",
34+
}, []string{"db", "coll"})
35+
collectionIndexes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
36+
Namespace: Namespace,
37+
Subsystem: "db_coll",
38+
Name: "indexes",
39+
Help: "The number of indexes on the collection",
40+
}, []string{"db", "coll"})
41+
collectionIndexesSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
42+
Namespace: Namespace,
43+
Subsystem: "db_coll",
44+
Name: "indexes_size",
45+
Help: "The total size of all indexes",
46+
}, []string{"db", "coll"})
47+
)
48+
49+
// CollectionStatList contains stats from all collections
50+
type CollectionStatList struct {
51+
Members []CollectionStatus
52+
}
53+
54+
// CollectionStatus represents stats about a collection in database (mongod and raw from mongos)
55+
type CollectionStatus struct {
56+
Database string
57+
Name string
58+
Size int `bson:"size,omitempty"`
59+
Count int `bson:"count,omitempty"`
60+
AvgObjSize int `bson:"avgObjSize,omitempty"`
61+
StorageSize int `bson:"storageSize,omitempty"`
62+
Indexes int `bson:"indexSizes,omitempty"`
63+
IndexesSize int `bson:"totalIndexSize,omitempty"`
64+
}
65+
66+
// Export exports database stats to prometheus
67+
func (collStatList *CollectionStatList) Export(ch chan<- prometheus.Metric) {
68+
for _, member := range collStatList.Members {
69+
ls := prometheus.Labels{
70+
"db": member.Database,
71+
"coll": member.Name,
72+
}
73+
collectionSize.With(ls).Set(float64(member.Size))
74+
collectionObjectCount.With(ls).Set(float64(member.Count))
75+
collectionAvgObjSize.With(ls).Set(float64(member.AvgObjSize))
76+
collectionStorageSize.With(ls).Set(float64(member.StorageSize))
77+
collectionIndexes.With(ls).Set(float64(member.Indexes))
78+
collectionIndexesSize.With(ls).Set(float64(member.IndexesSize))
79+
}
80+
collectionSize.Collect(ch)
81+
collectionObjectCount.Collect(ch)
82+
collectionAvgObjSize.Collect(ch)
83+
collectionStorageSize.Collect(ch)
84+
collectionIndexes.Collect(ch)
85+
collectionIndexesSize.Collect(ch)
86+
}
87+
88+
// Describe describes database stats for prometheus
89+
func (collStatList *CollectionStatList) Describe(ch chan<- *prometheus.Desc) {
90+
collectionSize.Describe(ch)
91+
collectionObjectCount.Describe(ch)
92+
collectionAvgObjSize.Describe(ch)
93+
collectionStorageSize.Describe(ch)
94+
collectionIndexes.Describe(ch)
95+
collectionIndexesSize.Describe(ch)
96+
}
97+
98+
// GetDatabaseStatus returns stats for a given database
99+
func GetCollectionStatList(session *mgo.Session) *CollectionStatList {
100+
collectionStatList := &CollectionStatList{}
101+
database_names, err := session.DatabaseNames()
102+
if err != nil {
103+
log.Error("Failed to get database names")
104+
return nil
105+
}
106+
for _, db := range database_names {
107+
collection_names, err := session.DB(db).CollectionNames()
108+
if err != nil {
109+
log.Error("Failed to get collection names for db=" + db)
110+
return nil
111+
}
112+
for _, collection_name := range collection_names {
113+
collStatus := CollectionStatus{}
114+
err := session.DB(db).Run(bson.D{{"collStats", collection_name}, {"scale", 1}}, &collStatus)
115+
collStatus.Database = db
116+
collStatus.Name = collection_name
117+
if err != nil {
118+
log.Error("Failed to get collection status.")
119+
return nil
120+
}
121+
collectionStatList.Members = append(collectionStatList.Members, collStatus)
122+
}
123+
}
124+
125+
return collectionStatList
126+
}

0 commit comments

Comments
 (0)