-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collector for working with data from the system profile (#710)
* Collector for working with data from the system profile * add description * fix: tab instead of spaces --------- Co-authored-by: Alex Tymchuk <[email protected]>
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/sirupsen/logrus" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type profileCollector struct { | ||
ctx context.Context | ||
base *baseCollector | ||
compatibleMode bool | ||
topologyInfo labelsGetter | ||
profiletimets int | ||
} | ||
|
||
// newProfileCollector creates a collector for being processed queries. | ||
func newProfileCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, | ||
compatible bool, topology labelsGetter, profileTimeTS int, | ||
) *profileCollector { | ||
return &profileCollector{ | ||
ctx: ctx, | ||
base: newBaseCollector(client, logger), | ||
compatibleMode: compatible, | ||
topologyInfo: topology, | ||
profiletimets: profileTimeTS, | ||
} | ||
} | ||
|
||
func (d *profileCollector) Describe(ch chan<- *prometheus.Desc) { | ||
d.base.Describe(d.ctx, ch, d.collect) | ||
} | ||
|
||
func (d *profileCollector) Collect(ch chan<- prometheus.Metric) { | ||
d.base.Collect(ch) | ||
} | ||
|
||
func (d *profileCollector) collect(ch chan<- prometheus.Metric) { | ||
defer measureCollectTime(ch, "mongodb", "profile")() | ||
|
||
logger := d.base.logger | ||
client := d.base.client | ||
timeScrape := d.profiletimets | ||
|
||
databases, err := databases(d.ctx, client, nil, nil) | ||
if err != nil { | ||
errors.Wrap(err, "cannot get the database names list") | ||
return | ||
} | ||
|
||
// Now time + '--collector.profile-time-ts' | ||
ts := primitive.NewDateTimeFromTime(time.Now().Add(-time.Duration(time.Second * time.Duration(timeScrape)))) | ||
|
||
labels := d.topologyInfo.baseLabels() | ||
|
||
// Get all slow queries from all databases | ||
cmd := bson.M{"ts": bson.M{"$gte": ts}} | ||
for _, db := range databases { | ||
res, err := client.Database(db).Collection("system.profile").CountDocuments(d.ctx, cmd) | ||
if err != nil { | ||
errors.Wrapf(err, "cannot read system.profile") | ||
break | ||
} | ||
labels["database"] = db | ||
|
||
m := primitive.M{"count": res} | ||
|
||
logger.Debug("profile response from MongoDB:") | ||
debugResult(logger, primitive.M{db: m}) | ||
|
||
for _, metric := range makeMetrics("profile_slow_query", m, labels, d.compatibleMode) { | ||
ch <- metric | ||
} | ||
} | ||
} |
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,69 @@ | ||
// 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" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"go.mongodb.org/mongo-driver/bson" | ||
|
||
"github.com/percona/mongodb_exporter/internal/tu" | ||
) | ||
|
||
func TestProfileCollector(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
|
||
client := tu.DefaultTestClient(ctx, t) | ||
|
||
database := client.Database("testdb") | ||
database.Drop(ctx) //nolint | ||
|
||
defer func() { | ||
err := database.Drop(ctx) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
// Enable database profiler https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler/ | ||
cmd := bson.M{"profile": 2} | ||
_ = database.RunCommand(ctx, cmd) | ||
|
||
ti := labelsGetterMock{} | ||
|
||
c := newProfileCollector(ctx, client, logrus.New(), false, ti, 30) | ||
|
||
expected := strings.NewReader(` | ||
# HELP mongodb_profile_slow_query_count profile_slow_query. | ||
# TYPE mongodb_profile_slow_query_count counter | ||
mongodb_profile_slow_query_count{database="admin"} 0 | ||
mongodb_profile_slow_query_count{database="config"} 0 | ||
mongodb_profile_slow_query_count{database="local"} 0 | ||
mongodb_profile_slow_query_count{database="testdb"} 0` + | ||
"\n") | ||
|
||
filter := []string{ | ||
"mongodb_profile_slow_query_count", | ||
} | ||
|
||
err := testutil.CollectAndCompare(c, expected, filter...) | ||
assert.NoError(t, err) | ||
} |
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