-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #744 from kongfei605/mongo_back
mongodb back
- Loading branch information
Showing
20 changed files
with
3,626 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// mongodb_exporter | ||
// Copyright (C) 2022 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 exporter | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/sirupsen/logrus" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type baseCollector struct { | ||
client *mongo.Client | ||
logger *logrus.Logger | ||
|
||
lock sync.Mutex | ||
metricsCache []prometheus.Metric | ||
} | ||
|
||
// newBaseCollector creates a skeletal collector, which is used to create other collectors. | ||
func newBaseCollector(client *mongo.Client, logger *logrus.Logger) *baseCollector { | ||
return &baseCollector{ | ||
client: client, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (d *baseCollector) Describe(ctx context.Context, ch chan<- *prometheus.Desc, collect func(mCh chan<- prometheus.Metric)) { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
} | ||
|
||
d.lock.Lock() | ||
defer d.lock.Unlock() | ||
|
||
d.metricsCache = make([]prometheus.Metric, 0, defaultCacheSize) | ||
|
||
// This is a copy/paste of prometheus.DescribeByCollect(d, ch) with the aggreated functionality | ||
// to populate the metrics cache. Since on each scrape Prometheus will call Describe and inmediatelly | ||
// after it will call Collect, it is safe to populate the cache here. | ||
metrics := make(chan prometheus.Metric) | ||
go func() { | ||
collect(metrics) | ||
close(metrics) | ||
}() | ||
|
||
for m := range metrics { | ||
d.metricsCache = append(d.metricsCache, m) // populate the cache | ||
ch <- m.Desc() | ||
} | ||
} | ||
|
||
func (d *baseCollector) Collect(ch chan<- prometheus.Metric, collect func(mCh chan<- prometheus.Metric)) { | ||
d.lock.Lock() | ||
defer d.lock.Unlock() | ||
|
||
if len(d.metricsCache) > 0 { | ||
for _, metric := range d.metricsCache { | ||
ch <- metric | ||
} | ||
|
||
return | ||
} | ||
|
||
collect(ch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// mongodb_exporter | ||
// Copyright (C) 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 exporter | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/sirupsen/logrus" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type collstatsCollector struct { | ||
ctx context.Context | ||
base *baseCollector | ||
|
||
compatibleMode bool | ||
discoveringMode bool | ||
topologyInfo labelsGetter | ||
|
||
collections []string | ||
} | ||
|
||
// newCollectionStatsCollector creates a collector for statistics about collections. | ||
func newCollectionStatsCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible, discovery bool, topology labelsGetter, collections []string) *collstatsCollector { | ||
return &collstatsCollector{ | ||
ctx: ctx, | ||
base: newBaseCollector(client, logger), | ||
|
||
compatibleMode: compatible, | ||
discoveringMode: discovery, | ||
topologyInfo: topology, | ||
|
||
collections: collections, | ||
} | ||
} | ||
|
||
func (d *collstatsCollector) Describe(ch chan<- *prometheus.Desc) { | ||
d.base.Describe(d.ctx, ch, d.collect) | ||
} | ||
|
||
func (d *collstatsCollector) Collect(ch chan<- prometheus.Metric) { | ||
d.base.Collect(ch, d.collect) | ||
} | ||
|
||
func (d *collstatsCollector) collect(ch chan<- prometheus.Metric) { | ||
|
||
collections := d.collections | ||
|
||
client := d.base.client | ||
logger := d.base.logger | ||
|
||
if d.discoveringMode { | ||
namespaces, err := listAllCollections(d.ctx, client, d.collections, systemDBs) | ||
if err != nil { | ||
logger.Errorf("cannot auto discover databases and collections: %s", err.Error()) | ||
|
||
return | ||
} | ||
|
||
collections = fromMapToSlice(namespaces) | ||
} | ||
|
||
for _, dbCollection := range collections { | ||
parts := strings.Split(dbCollection, ".") | ||
if len(parts) < 2 { //nolint:gomnd | ||
continue | ||
} | ||
|
||
database := parts[0] | ||
collection := strings.Join(parts[1:], ".") // support collections having a . | ||
|
||
aggregation := bson.D{ | ||
{ | ||
Key: "$collStats", Value: bson.M{ | ||
// TODO: PMM-9568 : Add support to handle histogram metrics | ||
"latencyStats": bson.M{"histograms": false}, | ||
"storageStats": bson.M{"scale": 1}, | ||
}, | ||
}, | ||
} | ||
project := bson.D{ | ||
{ | ||
Key: "$project", Value: bson.M{ | ||
"storageStats.wiredTiger": 0, | ||
"storageStats.indexDetails": 0, | ||
}, | ||
}, | ||
} | ||
|
||
cursor, err := client.Database(database).Collection(collection).Aggregate(d.ctx, mongo.Pipeline{aggregation, project}) | ||
if err != nil { | ||
logger.Errorf("cannot get $collstats cursor for collection %s.%s: %s", database, collection, err) | ||
|
||
continue | ||
} | ||
|
||
var stats []bson.M | ||
if err = cursor.All(d.ctx, &stats); err != nil { | ||
logger.Errorf("cannot get $collstats for collection %s.%s: %s", database, collection, err) | ||
|
||
continue | ||
} | ||
|
||
logger.Debugf("$collStats metrics for %s.%s", database, collection) | ||
debugResult(logger, stats) | ||
|
||
prefix := "collstats" | ||
labels := d.topologyInfo.baseLabels() | ||
labels["database"] = database | ||
labels["collection"] = collection | ||
|
||
for _, metrics := range stats { | ||
for _, metric := range makeMetrics(prefix, metrics, labels, d.compatibleMode) { | ||
ch <- metric | ||
} | ||
} | ||
} | ||
} | ||
|
||
func fromMapToSlice(databases map[string][]string) []string { | ||
var collections []string | ||
for db, cols := range databases { | ||
for _, value := range cols { | ||
collections = append(collections, db+"."+value) | ||
} | ||
} | ||
|
||
return collections | ||
} | ||
|
||
var _ prometheus.Collector = (*collstatsCollector)(nil) |
Oops, something went wrong.