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

Add shard server stats to the mongoDB input plugin #3808

Merged
merged 1 commit into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions plugins/inputs/mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ and create a single measurement containing values e.g.
* resident_megabytes
* updates_per_sec
* vsize_megabytes
* total_in_use
* total_available
* total_created
* total_refreshing
* ttl_deletes_per_sec
* ttl_passes_per_sec
* repl_lag
Expand Down
8 changes: 8 additions & 0 deletions plugins/inputs/mongodb/mongodb_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ var DefaultClusterStats = map[string]string{
"jumbo_chunks": "JumboChunksCount",
}

var DefaultShardStats = map[string]string{
"total_in_use": "TotalInUse",
"total_available": "TotalAvailable",
"total_created": "TotalCreated",
"total_refreshing": "TotalRefreshing",
}

var MmapStats = map[string]string{
"mapped_megabytes": "Mapped",
"non-mapped_megabytes": "NonMapped",
Expand Down Expand Up @@ -127,6 +134,7 @@ func (d *MongodbData) AddDefaultStats() {
d.addStat(statLine, DefaultReplStats)
}
d.addStat(statLine, DefaultClusterStats)
d.addStat(statLine, DefaultShardStats)
if d.StatLine.StorageEngine == "mmapv1" {
d.addStat(statLine, MmapStats)
} else if d.StatLine.StorageEngine == "wiredTiger" {
Expand Down
25 changes: 25 additions & 0 deletions plugins/inputs/mongodb/mongodb_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ func TestAddWiredTigerStats(t *testing.T) {
}
}

func TestAddShardStats(t *testing.T) {
d := NewMongodbData(
&StatLine{
TotalInUse: 0,
TotalAvailable: 0,
TotalCreated: 0,
TotalRefreshing: 0,
},
tags,
)

var acc testutil.Accumulator

d.AddDefaultStats()
d.flush(&acc)

for key, _ := range DefaultShardStats {
assert.True(t, acc.HasInt64Field("mongodb", key))
}
}

func TestStateTag(t *testing.T) {
d := NewMongodbData(
&StatLine{
Expand Down Expand Up @@ -147,6 +168,10 @@ func TestStateTag(t *testing.T) {
"ttl_deletes_per_sec": int64(0),
"ttl_passes_per_sec": int64(0),
"jumbo_chunks": int64(0),
"total_in_use": int64(0),
"total_available": int64(0),
"total_created": int64(0),
"total_refreshing": int64(0),
}
acc.AssertContainsTaggedFields(t, "mongodb", fields, stateTags)
}
13 changes: 12 additions & 1 deletion plugins/inputs/mongodb/mongodb_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,18 @@ func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool) error
JumboChunksCount: int64(jumbo_chunks),
}

result_db_stats := &DbStats{}
resultShards := &ShardStats{}
err = s.Session.DB("admin").Run(bson.D{
{
Name: "shardConnPoolStats",
Value: 1,
},
}, &resultShards)
if err != nil {
log.Println("E! Error getting database shard stats (" + err.Error() + ")")
}

result_db_stats := &DbStats{}
if gatherDbStats == true {
names := []string{}
names, err = s.Session.DatabaseNames()
Expand Down Expand Up @@ -88,6 +98,7 @@ func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool) error
ReplSetStatus: result_repl,
ClusterStatus: result_cluster,
DbStats: result_db_stats,
ShardStats: resultShards,
}

defer func() {
Expand Down
19 changes: 19 additions & 0 deletions plugins/inputs/mongodb/mongostat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type MongoStatus struct {
ReplSetStatus *ReplSetStatus
ClusterStatus *ClusterStatus
DbStats *DbStats
ShardStats *ShardStats
}

type ServerStatus struct {
Expand Down Expand Up @@ -116,6 +117,14 @@ type WiredTiger struct {
Cache CacheStats `bson:"cache"`
}

// ShardStats stores information from shardConnPoolStats.
type ShardStats struct {
TotalInUse int64 `bson:"totalInUse"`
TotalAvailable int64 `bson:"totalAvailable"`
TotalCreated int64 `bson:"totalCreated"`
TotalRefreshing int64 `bson:"totalRefreshing"`
}

type ConcurrentTransactions struct {
Write ConcurrentTransStats `bson:"write"`
Read ConcurrentTransStats `bson:"read"`
Expand Down Expand Up @@ -450,6 +459,9 @@ type StatLine struct {

// DB stats field
DbStatsLines []DbStatLine

// Shard stats
TotalInUse, TotalAvailable, TotalCreated, TotalRefreshing int64
}

type DbStatLine struct {
Expand Down Expand Up @@ -783,5 +795,12 @@ func NewStatLine(oldMongo, newMongo MongoStatus, key string, all bool, sampleSec
returnVal.DbStatsLines = append(returnVal.DbStatsLines, *dbStatLine)
}

// Set shard stats
newShardStats := *newMongo.ShardStats
returnVal.TotalInUse = newShardStats.TotalInUse
returnVal.TotalAvailable = newShardStats.TotalAvailable
returnVal.TotalCreated = newShardStats.TotalCreated
returnVal.TotalRefreshing = newShardStats.TotalRefreshing

return returnVal
}