Skip to content
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
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ endif
bash ./build.env
go install $(EXTRA_BUILD_FLAGS) $(VT_GO_PARALLEL) -ldflags "$(shell tools/build_version_flags.sh)" ./go/...

debug:
ifndef NOBANNER
echo $$(date): Building source tree
endif
bash ./build.env
go install $(EXTRA_BUILD_FLAGS) $(VT_GO_PARALLEL) -ldflags "$(shell tools/build_version_flags.sh)" -gcflags -'N -l' ./go/...

# install copies the files needed to run Vitess into the given directory tree.
# Usage: make install PREFIX=/path/to/install/root
install: build
Expand Down
82 changes: 59 additions & 23 deletions go/stats/prometheusbackend/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/log"
)

type metricFuncCollector struct {
Expand All @@ -40,6 +41,7 @@ func newMetricFuncCollector(v stats.Variable, name string, vt prometheus.ValueTy
nil),
vt: vt}

// Will panic if it fails
prometheus.MustRegister(collector)
}

Expand All @@ -50,7 +52,12 @@ func (mc *metricFuncCollector) Describe(ch chan<- *prometheus.Desc) {

// Collect implements Collector.
func (mc *metricFuncCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(mc.desc, mc.vt, float64(mc.f()))
metric, err := prometheus.NewConstMetric(mc.desc, mc.vt, float64(mc.f()))
if err != nil {
log.Errorf("Error adding metric: %s", mc.desc)
} else {
ch <- metric
}
}

// countersWithSingleLabelCollector collects stats.CountersWithSingleLabel.
Expand Down Expand Up @@ -81,11 +88,12 @@ func (c *countersWithSingleLabelCollector) Describe(ch chan<- *prometheus.Desc)
// Collect implements Collector.
func (c *countersWithSingleLabelCollector) Collect(ch chan<- prometheus.Metric) {
for tag, val := range c.counters.Counts() {
ch <- prometheus.MustNewConstMetric(
c.desc,
c.vt,
float64(val),
tag)
metric, err := prometheus.NewConstMetric(c.desc, c.vt, float64(val), tag)
if err != nil {
log.Errorf("Error adding metric: %s", c.desc)
} else {
ch <- metric
}
}
}

Expand Down Expand Up @@ -117,11 +125,12 @@ func (g *gaugesWithSingleLabelCollector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements Collector.
func (g *gaugesWithSingleLabelCollector) Collect(ch chan<- prometheus.Metric) {
for tag, val := range g.gauges.Counts() {
ch <- prometheus.MustNewConstMetric(
g.desc,
g.vt,
float64(val),
tag)
metric, err := prometheus.NewConstMetric(g.desc, g.vt, float64(val), tag)
if err != nil {
log.Errorf("Error adding metric: %s", g.desc)
} else {
ch <- metric
}
}
}

Expand Down Expand Up @@ -153,7 +162,12 @@ func (c *metricWithMultiLabelsCollector) Collect(ch chan<- prometheus.Metric) {
for lvs, val := range c.cml.Counts() {
labelValues := strings.Split(lvs, ".")
value := float64(val)
ch <- prometheus.MustNewConstMetric(c.desc, prometheus.CounterValue, value, labelValues...)
metric, err := prometheus.NewConstMetric(c.desc, prometheus.CounterValue, value, labelValues...)
if err != nil {
log.Errorf("Error adding metric: %s", c.desc)
} else {
ch <- metric
}
}
}

Expand Down Expand Up @@ -185,7 +199,12 @@ func (c *gaugesWithMultiLabelsCollector) Collect(ch chan<- prometheus.Metric) {
for lvs, val := range c.gml.Counts() {
labelValues := strings.Split(lvs, ".")
value := float64(val)
ch <- prometheus.MustNewConstMetric(c.desc, prometheus.GaugeValue, value, labelValues...)
metric, err := prometheus.NewConstMetric(c.desc, prometheus.GaugeValue, value, labelValues...)
if err != nil {
log.Errorf("Error adding metric: %s", c.desc)
} else {
ch <- metric
}
}
}

Expand Down Expand Up @@ -219,7 +238,12 @@ func (c *metricsFuncWithMultiLabelsCollector) Collect(ch chan<- prometheus.Metri
for lvs, val := range c.cfml.Counts() {
labelValues := strings.Split(lvs, ".")
value := float64(val)
ch <- prometheus.MustNewConstMetric(c.desc, c.vt, value, labelValues...)
metric, err := prometheus.NewConstMetric(c.desc, c.vt, value, labelValues...)
if err != nil {
log.Errorf("Error adding metric: %s", c.desc)
} else {
ch <- metric
}
}
}

Expand Down Expand Up @@ -256,12 +280,16 @@ func (c *timingsCollector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements Collector.
func (c *timingsCollector) Collect(ch chan<- prometheus.Metric) {
for cat, his := range c.t.Histograms() {
ch <- prometheus.MustNewConstHistogram(
c.desc,
metric, err := prometheus.NewConstHistogram(c.desc,
uint64(his.Count()),
float64(his.Total())/1000000000,
makeCumulativeBuckets(c.cutoffs, his.Buckets()),
cat)
makeCumulativeBuckets(c.cutoffs,
his.Buckets()), cat)
if err != nil {
log.Errorf("Error adding metric: %s", c.desc)
} else {
ch <- metric
}
}
}

Expand Down Expand Up @@ -310,12 +338,17 @@ func (c *multiTimingsCollector) Describe(ch chan<- *prometheus.Desc) {
func (c *multiTimingsCollector) Collect(ch chan<- prometheus.Metric) {
for cat, his := range c.mt.Timings.Histograms() {
labelValues := strings.Split(cat, ".")
ch <- prometheus.MustNewConstHistogram(
metric, err := prometheus.NewConstHistogram(
c.desc,
uint64(his.Count()),
float64(his.Total())/1000000000,
makeCumulativeBuckets(c.cutoffs, his.Buckets()),
labelValues...)
if err != nil {
log.Errorf("Error adding metric: %s", c.desc)
} else {
ch <- metric
}
}
}

Expand Down Expand Up @@ -351,10 +384,13 @@ func (c *histogramCollector) Describe(ch chan<- *prometheus.Desc) {

// Collect implements Collector.
func (c *histogramCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstHistogram(
c.desc,
metric, err := prometheus.NewConstHistogram(c.desc,
uint64(c.h.Count()),
float64(c.h.Total()),
makeCumulativeBuckets(c.cutoffs, c.h.Buckets()),
)
makeCumulativeBuckets(c.cutoffs, c.h.Buckets()))
if err != nil {
log.Errorf("Error adding metric: %s", c.desc)
} else {
ch <- metric
}
}
38 changes: 38 additions & 0 deletions go/stats/prometheusbackend/collectors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2019 The Vitess Authors.

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 prometheusbackend

import (
"testing"

"vitess.io/vitess/go/stats"
)

func getStats() map[string]int64 {
stats := make(map[string]int64)
stats["table1.plan"] = 1
stats["table2.plan"] = 2
// Errors are expected to be logged from collectors.go when
// adding this value (issue #5599). Test will succeed, as intended.
stats["table3.dot.plan"] = 3
return stats
}

func TestCollector(t *testing.T) {
c := stats.NewCountersFuncWithMultiLabels("Name", "description", []string{"Table", "Plan"}, getStats)
c.Counts()
}
4 changes: 3 additions & 1 deletion go/vt/vttablet/tabletserver/query_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -494,7 +495,8 @@ type QueryStats struct {

// AddStats adds the given stats for the planName.tableName
func (qe *QueryEngine) AddStats(planName, tableName string, queryCount int64, duration, mysqlTime time.Duration, rowCount, errorCount int64) {
key := tableName + "." + planName
// table names can contain "." characters, replace them!
key := strings.Replace(tableName, ".", "_", -1) + "." + planName

qe.queryStatsMu.RLock()
stats, ok := qe.queryStats[key]
Expand Down
11 changes: 6 additions & 5 deletions go/vt/vttablet/tabletserver/schema/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/json"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -514,7 +515,7 @@ func (se *Engine) getTableRows() map[string]int64 {
defer se.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range se.tables {
tstats[k] = v.TableRows.Get()
tstats[strings.Replace(k, ".", "_", -1)] = v.TableRows.Get()
}
return tstats
}
Expand All @@ -524,7 +525,7 @@ func (se *Engine) getDataLength() map[string]int64 {
defer se.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range se.tables {
tstats[k] = v.DataLength.Get()
tstats[strings.Replace(k, ".", "_", -1)] = v.DataLength.Get()
}
return tstats
}
Expand All @@ -534,7 +535,7 @@ func (se *Engine) getIndexLength() map[string]int64 {
defer se.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range se.tables {
tstats[k] = v.IndexLength.Get()
tstats[strings.Replace(k, ".", "_", -1)] = v.IndexLength.Get()
}
return tstats
}
Expand All @@ -544,7 +545,7 @@ func (se *Engine) getDataFree() map[string]int64 {
defer se.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range se.tables {
tstats[k] = v.DataFree.Get()
tstats[strings.Replace(k, ".", "_", -1)] = v.DataFree.Get()
}
return tstats
}
Expand All @@ -554,7 +555,7 @@ func (se *Engine) getMaxDataLength() map[string]int64 {
defer se.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range se.tables {
tstats[k] = v.MaxDataLength.Get()
tstats[strings.Replace(k, ".", "_", -1)] = v.MaxDataLength.Get()
}
return tstats
}
Expand Down