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 label distribution metrics #1402

Merged
merged 6 commits into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ var (
Help: "Status of the cluster.",
}, []string{"type", "namespace"})

placementStatusGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pd",
Subsystem: "cluster",
Name: "placement_status",
Help: "Status of the cluster placement.",
}, []string{"type", "name", "namespace"})

healthStatusGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pd",
Expand Down Expand Up @@ -164,4 +172,5 @@ func init() {
prometheus.MustRegister(metadataGauge)
prometheus.MustRegister(etcdStateGauge)
prometheus.MustRegister(patrolCheckRegionsHistogram)
prometheus.MustRegister(placementStatusGauge)
}
28 changes: 24 additions & 4 deletions server/store_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
package server

import (
"fmt"
"strconv"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/namespace"
)

const (
unknow = "unknow"
nolouch marked this conversation as resolved.
Show resolved Hide resolved
labelType = "label"
)

type storeStatistics struct {
opt *scheduleOption
namespace string
Expand All @@ -35,16 +41,26 @@ type storeStatistics struct {
StorageCapacity uint64
RegionCount int
LeaderCount int
LabelCounter map[string]int
}

func newStoreStatistics(opt *scheduleOption, namespace string) *storeStatistics {
return &storeStatistics{
opt: opt,
namespace: namespace,
opt: opt,
namespace: namespace,
LabelCounter: make(map[string]int),
}
}

func (s *storeStatistics) Observe(store *core.StoreInfo) {
for _, k := range s.opt.GetLocationLabels() {
v := store.GetLabelValue(k)
if v == "" {
v = unknow
}
key := fmt.Sprintf("%s:%s", k, v)
s.LabelCounter[key]++
}
id := strconv.FormatUint(store.GetId(), 10)
// Store state.
switch store.GetState() {
Expand Down Expand Up @@ -100,8 +116,12 @@ func (s *storeStatistics) Collect() {
metrics["storage_size"] = float64(s.StorageSize)
metrics["storage_capacity"] = float64(s.StorageCapacity)

for label, value := range metrics {
clusterStatusGauge.WithLabelValues(label, s.namespace).Set(value)
for typ, value := range metrics {
clusterStatusGauge.WithLabelValues(typ, s.namespace).Set(value)
}

for name, value := range s.LabelCounter {
placementStatusGauge.WithLabelValues(labelType, name, s.namespace).Set(float64(value))
}
}

Expand Down
73 changes: 73 additions & 0 deletions server/store_statistics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2018 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"time"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/namespace"
)

var _ = Suite(&testStoreStatisticsSuite{})

type testStoreStatisticsSuite struct{}

func (t *testStoreStatisticsSuite) TestStoreStatistics(c *C) {
_, opt := newTestScheduleConfig()
rep := opt.GetReplication().load()
rep.LocationLabels = []string{"zone", "host"}

metaStores := []*metapb.Store{
{Id: 1, Address: "mock://tikv-1", Labels: []*metapb.StoreLabel{{Key: "zone", Value: "z1"}, {Key: "host", Value: "h1"}}},
{Id: 2, Address: "mock://tikv-2", Labels: []*metapb.StoreLabel{{Key: "zone", Value: "z1"}, {Key: "host", Value: "h2"}}},
{Id: 3, Address: "mock://tikv-3", Labels: []*metapb.StoreLabel{{Key: "zone", Value: "z2"}, {Key: "host", Value: "h1"}}},
{Id: 4, Address: "mock://tikv-4", Labels: []*metapb.StoreLabel{{Key: "zone", Value: "z2"}, {Key: "host", Value: "h2"}}},
{Id: 5, Address: "mock://tikv-5", Labels: []*metapb.StoreLabel{{Key: "zone", Value: "z3"}, {Key: "host", Value: "h1"}}},
{Id: 6, Address: "mock://tikv-6", Labels: []*metapb.StoreLabel{{Key: "zone", Value: "z3"}, {Key: "host", Value: "h2"}}},
{Id: 7, Address: "mock://tikv-7", Labels: []*metapb.StoreLabel{{Key: "host", Value: "h1"}}},
{Id: 8, Address: "mock://tikv-8", Labels: []*metapb.StoreLabel{{Key: "host", Value: "h2"}}},
}
var stores []*core.StoreInfo
for _, m := range metaStores {
s := core.NewStoreInfo(m)
s.LastHeartbeatTS = time.Now()
stores = append(stores, s)
}

stores[3].State = metapb.StoreState_Offline
stores[4].LastHeartbeatTS = stores[4].LastHeartbeatTS.Add(-time.Hour)
storeStats := newStoreStatisticsMap(opt, namespace.DefaultClassifier)
for _, store := range stores {
storeStats.Observe(store)
}
stats := storeStats.stats["global"]

c.Assert(stats.Up, Equals, 6)
c.Assert(stats.Down, Equals, 1)
c.Assert(stats.Offline, Equals, 1)
c.Assert(stats.RegionCount, Equals, 0)
c.Assert(stats.Unhealth, Equals, 0)
c.Assert(stats.Disconnect, Equals, 0)
c.Assert(stats.Tombstone, Equals, 0)
c.Assert(stats.LowSpace, Equals, 8)
c.Assert(stats.LabelCounter["zone:z1"], Equals, 2)
c.Assert(stats.LabelCounter["zone:z2"], Equals, 2)
c.Assert(stats.LabelCounter["zone:z3"], Equals, 2)
c.Assert(stats.LabelCounter["host:h1"], Equals, 4)
c.Assert(stats.LabelCounter["host:h2"], Equals, 4)
c.Assert(stats.LabelCounter["zone:unknow"], Equals, 2)
}