Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMM-12712 Shards collector. #762

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
6850049
PMM-12712 Shard ID and count.
JiriCtvrtka Dec 11, 2023
6eab6f0
PMM-12712 Name and count.
JiriCtvrtka Dec 11, 2023
650bdce
PMM-12712 Sharded collector.
JiriCtvrtka Dec 13, 2023
0bf0882
PMM-12712 Remove missed print.
JiriCtvrtka Dec 13, 2023
e926775
PMM-12712 Revert test changes.
JiriCtvrtka Dec 13, 2023
507245f
PMM-12712 Revert old not related changes.
JiriCtvrtka Dec 13, 2023
fcabf4b
PMM-12712 Another changes.
JiriCtvrtka Dec 14, 2023
91140c1
PMM-12712 Missed print.
JiriCtvrtka Dec 14, 2023
971594b
PMM-12712 Align naming with task description.
JiriCtvrtka Dec 14, 2023
11c7e0a
PMM-12712 Add mongos test client.
JiriCtvrtka Dec 14, 2023
4f4f5c3
PMM-12712 Lint.
JiriCtvrtka Dec 14, 2023
45c9e39
PMM-12712 Lint.
JiriCtvrtka Dec 14, 2023
f5ed91d
PMM-12712 Typo.
JiriCtvrtka Dec 14, 2023
f93cc38
PMM-12712 Lint.
JiriCtvrtka Dec 14, 2023
88451ba
PMM-12712 Test higher sleep.
JiriCtvrtka Dec 19, 2023
1cad77b
Revert "PMM-12712 Test higher sleep."
JiriCtvrtka Dec 19, 2023
c4d20eb
PMM-12712 Test of pipeline.
JiriCtvrtka Dec 19, 2023
78f775b
PMM-12712 Change.
JiriCtvrtka Dec 19, 2023
1727c19
PMM-12712 Test.
JiriCtvrtka Dec 19, 2023
3b97b99
PMM-12712 Test.
JiriCtvrtka Dec 19, 2023
c827122
PMM-12712 Test.
JiriCtvrtka Dec 19, 2023
c7dd46e
PMM-12712 Correct aggregation to get chunks info.
JiriCtvrtka Dec 20, 2023
33ef736
PMM-12712 Another progress.
JiriCtvrtka Dec 21, 2023
82d4c43
PMM-12702 Small refactor.
JiriCtvrtka Dec 21, 2023
3da2738
PMM-12712 Test.
JiriCtvrtka Dec 21, 2023
564efec
PMM-12712 Fix, tests.
JiriCtvrtka Dec 22, 2023
6863eb8
PMM-12712 Temp.
JiriCtvrtka Dec 22, 2023
5f33085
PMM-12712 Improve sharded test.
JiriCtvrtka Dec 22, 2023
7cfc273
PMM-12712 Change in workflow.
JiriCtvrtka Dec 22, 2023
21ddafb
PMM-12712 Remove print.
JiriCtvrtka Dec 22, 2023
09057de
PMM-12712 Init script changes.
JiriCtvrtka Dec 22, 2023
8a51df8
PMM-12712 Remove print.
JiriCtvrtka Dec 22, 2023
abfe3f5
PMM-12712 Bigger sleep.
JiriCtvrtka Dec 22, 2023
17cfccd
PMM-12712 Refactor.
JiriCtvrtka Jan 2, 2024
5949050
PMM-12712 Small refactor.
JiriCtvrtka Jan 2, 2024
e64dd93
PMM-12712 Another refactor.
JiriCtvrtka Jan 2, 2024
c4d6a07
PMM-12712 Fix after refactor.
JiriCtvrtka Jan 2, 2024
73fe6b7
PMM-12712 Alias.
JiriCtvrtka Jan 2, 2024
74745d6
PMM-12712 Alias for shell in older versions.
JiriCtvrtka Jan 2, 2024
c71ba2e
PMM-12712 Static test shard.
JiriCtvrtka Jan 2, 2024
bb18c39
PMM-12712 Fix another tests, mongo 4 shell script.
JiriCtvrtka Jan 2, 2024
9bab36e
PMM-12712 Big sleep test.
JiriCtvrtka Jan 2, 2024
20c1a98
PMM-12712 Michael's fix.
JiriCtvrtka Jan 5, 2024
ab69356
PMM-12712 Sleep.
JiriCtvrtka Jan 5, 2024
b526bf7
PMM-12712 Env.
JiriCtvrtka Jan 5, 2024
c96f816
Merge branch 'PMM-12510-correctly-recognize-arbiter-nodes' into PMM-1…
JiriCtvrtka Jan 8, 2024
55383a5
PMM-12712 Test.
JiriCtvrtka Jan 8, 2024
a6b1fec
PMM-12712 Fix test.
JiriCtvrtka Jan 8, 2024
a1cecd3
PMM-12712 Bigger sleep after changes.
JiriCtvrtka Jan 8, 2024
a80d80e
PMM-12712 Remove duplicate lines.
JiriCtvrtka Jan 8, 2024
aa72186
PMM-12712 Renaming from sharded to shards.
JiriCtvrtka Jan 8, 2024
39eed79
PMM-12712 Better script to detect proper mongo client.
JiriCtvrtka Jan 8, 2024
6517964
PMM-12712 Init script refactor.
JiriCtvrtka Jan 8, 2024
69caa3e
PMM-12712 Fix.
JiriCtvrtka Jan 8, 2024
26b2061
PMM-12712 Small refactor.
JiriCtvrtka Jan 8, 2024
424bc9d
Update main.go
JiriCtvrtka Jan 8, 2024
35b90c9
Update exporter/shards_collector.go
JiriCtvrtka Jan 8, 2024
4856289
PMM-12712 Remove mongo 6 for now, skip test for shards.
JiriCtvrtka Jan 9, 2024
16019a0
PMM-12712 Revert sleep length.
JiriCtvrtka Jan 9, 2024
1793719
PMM-12712 Mongo client based on vendor.
JiriCtvrtka Jan 9, 2024
a86f807
Merge branch 'PMM-12510-correctly-recognize-arbiter-nodes' into PMM-1…
JiriCtvrtka Jan 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Opts struct {
EnableIndexStats bool
EnableCollStats bool
EnableProfile bool
EnableSharded bool

EnableOverrideDescendingIndex bool

Expand Down Expand Up @@ -163,6 +164,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
e.opts.EnableIndexStats = true
e.opts.EnableCurrentopMetrics = true
e.opts.EnableProfile = true
e.opts.EnableSharded = true
}

// arbiter only have isMaster privileges
Expand All @@ -175,6 +177,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
e.opts.EnableIndexStats = false
e.opts.EnableCurrentopMetrics = false
e.opts.EnableProfile = false
e.opts.EnableSharded = false
}

// If we manually set the collection names we want or auto discovery is set.
Expand Down Expand Up @@ -230,6 +233,11 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
registry.MustRegister(rsgsc)
}

if e.opts.EnableSharded && requestOpts.EnableSharded {
sc := newShardedCollector(ctx, client, e.opts.Logger, e.opts.CompatibleMode)
registry.MustRegister(sc)
}

return registry
}

Expand Down Expand Up @@ -304,6 +312,8 @@ func (e *Exporter) Handler() http.Handler {
requestOpts.EnableCollStats = true
case "profile":
requestOpts.EnableProfile = true
case "sharded":
requestOpts.EnableSharded = true
}
}

Expand Down
105 changes: 105 additions & 0 deletions exporter/sharded_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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 shardedCollector struct {
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
ctx context.Context
base *baseCollector
compatible bool
}

// newShardedCollector creates collector collecting metrics about chunks for sharded Mongo.
func newShardedCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatibleMode bool) *shardedCollector {
return &shardedCollector{
ctx: ctx,
base: newBaseCollector(client, logger),
compatible: compatibleMode,
}
}

func (d *shardedCollector) Describe(ch chan<- *prometheus.Desc) {
d.base.Describe(d.ctx, ch, d.collect)
}

func (d *shardedCollector) Collect(ch chan<- prometheus.Metric) {
d.base.Collect(ch)
}

func (d *shardedCollector) collect(ch chan<- prometheus.Metric) {
defer measureCollectTime(ch, "mongodb", "sharded")()

client := d.base.client
logger := d.base.logger

aggregation := bson.D{
{Key: "$group", Value: bson.M{"_id": "$shard", "count": bson.M{"$sum": 1}, "ns": bson.M{"$first": "$ns"}}},
}
cur, err := client.Database("config").Collection("chunks").Aggregate(context.Background(), mongo.Pipeline{aggregation})
if err != nil {
logger.Errorf("cannot get $sharded cursor for collection config.chunks: %s", err)
}

var chunks []bson.M
err = cur.All(context.Background(), &chunks)
if err != nil {
logger.Errorf("cannot get $sharded for collection config.chunks: %s", err)
}

logger.Debug("$sharded metrics for config.chunks")
debugResult(logger, chunks)

for _, c := range chunks {
var ok bool
var id, namespace string
if id, ok = c["_id"].(string); !ok {
logger.Warning("$sharded chunk with wrong ID found")
continue
}
if namespace, ok = c["ns"].(string); !ok {
logger.Warning("not valid namespace in $sharded chunk")
continue
}

split := strings.Split(namespace, ".")
database := split[0]
collection := ""
if len(split) >= 2 { //nolint:gomnd
collection = strings.Join(split[1:], ".")
}

prefix := "sharded collection chunks"
labels := make(map[string]string)
labels["database"] = database
labels["collection"] = collection
labels["shard"] = id

for _, metric := range makeMetrics(prefix, c, labels, d.compatible) {
ch <- metric
}
}
}

var _ prometheus.Collector = (*shardedCollector)(nil)
49 changes: 49 additions & 0 deletions exporter/sharded_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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"

"github.com/percona/mongodb_exporter/internal/tu"
)

//nolint:paralleltest
func TestShardedCollector(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

client := tu.DefaultTestClientMongoS(ctx, t)
c := newShardedCollector(ctx, client, logrus.New(), false)

expected := strings.NewReader(`
# HELP mongodb_sharded_collection_chunks_count sharded collection chunks.
# TYPE mongodb_sharded_collection_chunks_count counter
mongodb_sharded_collection_chunks_count{collection="system.sessions",database="config",shard="rs1"} 512
mongodb_sharded_collection_chunks_count{collection="system.sessions",database="config",shard="rs2"} 512` + "\n")
filter := []string{
"mongodb_sharded_collection_chunks_count",
}
err := testutil.CollectAndCompare(c, expected, filter...)
assert.NoError(t, err)
}
9 changes: 9 additions & 0 deletions internal/tu/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
return TestClient(ctx, port, t)
}

// DefaultTestClientMongoS returns the mongos MongoDB connection used for tests. It is a direct
// connection to the mongos server.
func DefaultTestClientMongoS(ctx context.Context, t *testing.T) *mongo.Client {

Check failure on line 76 in internal/tu/testutils.go

View workflow job for this annotation

GitHub Actions / Lint Check

test helper function should start from t.Helper() (thelper)
port, err := PortForContainer("mongos")
require.NoError(t, err)

return TestClient(ctx, port, t)
}

// GetImageNameForDefault returns image name and version of running
// default test mongo container.
func GetImageNameForDefault() (string, string, error) {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
EnableIndexStats bool `name:"collector.indexstats" help:"Enable collecting metrics from $indexStats"`
EnableCollStats bool `name:"collector.collstats" help:"Enable collecting metrics from $collStats"`
EnableProfile bool `name:"collector.profile" help:"Enable collecting metrics from profile"`
EnableSharded bool `help:"Enable collecting metrics from sharded sharded Mongo about chunks" name:"collector.sharded"`

Check failure on line 60 in main.go

View workflow job for this annotation

GitHub Actions / Lint Check

Duplicate words (sharded) found (dupword)
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved

EnableOverrideDescendingIndex bool `name:"metrics.overridedescendingindex" help:"Enable descending index name override to replace -1 with _DESC"`

Expand Down Expand Up @@ -152,6 +153,7 @@
EnableIndexStats: opts.EnableIndexStats,
EnableCollStats: opts.EnableCollStats,
EnableProfile: opts.EnableProfile,
EnableSharded: opts.EnableSharded,

EnableOverrideDescendingIndex: opts.EnableOverrideDescendingIndex,

Expand Down
Loading