Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.
Closed
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2017 Google Inc.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
28 changes: 28 additions & 0 deletions go/cmd/vtgate/plugin_prombackend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2017 Google 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,
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 main

// This plugin imports Prometheus to allow for instrumentation
// with the Prometheus client library

import (
"vitess.io/vitess/go/stats/prombackend"
)

func init() {
prombackend.Init("vtgate")
}
9 changes: 9 additions & 0 deletions go/cmd/vttablet/plugin_prombackend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"vitess.io/vitess/go/stats/prombackend"
)

func init() {
prombackend.Init("vttablet")
}
8 changes: 4 additions & 4 deletions go/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const (

var (
// Metrics
timings = stats.NewTimings("MysqlServerTimings")
connCount = stats.NewInt("MysqlServerConnCount")
connAccept = stats.NewInt("MysqlServerConnAccepted")
connSlow = stats.NewInt("MysqlServerConnSlow")
timings = stats.NewTimings("MysqlServerTimings", "MySQL server timings")
connCount = stats.NewInt("MysqlServerConnCount", "Connection count for MySQL servers")
connAccept = stats.NewInt("MysqlServerConnAccepted", "Connections accepted by MySQL server")
connSlow = stats.NewInt("MysqlServerConnSlow", "Slow MySQL server connections")
)

// A Handler is an interface used by Listener to send queries.
Expand Down
4 changes: 2 additions & 2 deletions go/proc/counting_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type countingConnection struct {
func Published(l net.Listener, countTag, acceptTag string) net.Listener {
return &CountingListener{
Listener: l,
ConnCount: stats.NewInt(countTag),
ConnAccept: stats.NewInt(acceptTag),
ConnCount: stats.NewInt(countTag, "Connection count inside net.Listener"),
ConnAccept: stats.NewInt(acceptTag, "Connections accepted inside net.Listener"),
}
}

Expand Down
136 changes: 118 additions & 18 deletions go/stats/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,28 @@ type Counters struct {
// modification to the actual number (int64) should be done with atomic funcs.
mu sync.RWMutex
counts map[string]*int64
help string
}

// NewCounters create a new Counters instance. If name is set, the variable
// gets published. The functional also accepts an optional list of tags that
// gets published. The function also accepts an optional list of tags that
// pre-creates them initialized to 0.
func NewCounters(name string, tags ...string) *Counters {
c := &Counters{counts: make(map[string]*int64)}
func NewCounters(name string, help string, tags ...string) *Counters {
c := &Counters{
counts: make(map[string]*int64),
help: help,
}

for _, tag := range tags {
c.counts[tag] = new(int64)
}
if name != "" {
publish(name, c)
}
publishPullCounters(c, name)
return c
}

// String is used by expvar.
func (c *Counters) String() string {
b := bytes.NewBuffer(make([]byte, 0, 4096))

Expand Down Expand Up @@ -97,19 +102,19 @@ func (c *Counters) Add(name string, value int64) {
atomic.AddInt64(a, value)
}

// Set sets the value of a named counter.
func (c *Counters) Set(name string, value int64) {
a := c.getValueAddr(name)
atomic.StoreInt64(a, value)
}

// Reset resets all counter values
func (c *Counters) Reset() {
c.mu.Lock()
defer c.mu.Unlock()
c.counts = make(map[string]*int64)
}

// ResetCounter resets a specific counter value to 0
func (c *Counters) ResetCounter(name string) {
a := c.getValueAddr(name)
atomic.StoreInt64(a, int64(0))
}

// Counts returns a copy of the Counters' map.
func (c *Counters) Counts() map[string]int64 {
c.mu.RLock()
Expand All @@ -122,6 +127,39 @@ func (c *Counters) Counts() map[string]int64 {
return counts
}

// Help returns the help string.
func (c *Counters) Help() string {
return c.help
}

// Gauges is similar to Counters, except its values can go up and down.
type Gauges struct {
Counters
}

// NewGauges creates a new Gauge and publishes it if the name is set.
func NewGauges(name string, help string, tags ...string) *Gauges {
g := &Gauges{Counters: Counters{
counts: make(map[string]*int64),
help: help,
}}

for _, tag := range tags {
g.Counters.counts[tag] = new(int64)
}
if name != "" {
publish(name, g)
publishPullGauges(g, name)
}
return g
}

// Set sets the value of a named counter.
func (g *Gauges) Set(name string, value int64) {
a := g.Counters.getValueAddr(name)
atomic.StoreInt64(a, value)
}

// CountersFunc converts a function that returns
// a map of int64 as an expvar.
type CountersFunc func() map[string]int64
Expand Down Expand Up @@ -162,14 +200,18 @@ type MultiCounters struct {

// NewMultiCounters creates a new MultiCounters instance, and publishes it
// if name is set.
func NewMultiCounters(name string, labels []string) *MultiCounters {
func NewMultiCounters(name string, help string, labels []string) *MultiCounters {
t := &MultiCounters{
Counters: Counters{counts: make(map[string]*int64)},
labels: labels,
Counters: Counters{
counts: make(map[string]*int64),
help: help},
labels: labels,
}
if name != "" {
publish(name, t)
}

publishPullMultiCounters(t, name)
return t
}

Expand All @@ -187,13 +229,61 @@ func (mc *MultiCounters) Add(names []string, value int64) {
mc.Counters.Add(mapKey(names), value)
}

// Set sets the value of a named counter. len(names) must be equal to
// len(Labels)
func (mc *MultiCounters) Set(names []string, value int64) {
// ResetCounter resets the value of a named counter back to 0. len(names)
// must be equal to len(Labels)
func (mc *MultiCounters) ResetCounter(names []string) {
if len(names) != len(mc.labels) {
panic("MultiCounters: wrong number of values in Set")
}
mc.Counters.Set(mapKey(names), value)

mc.Counters.ResetCounter(mapKey(names))
}

// MultiGauges is a MultiCounters implementation where the values can go up and down
type MultiGauges struct {
Gauges
labels []string
}

// NewMultiGauges creates a new MultiGauges instance, and publishes it
// if name is set.
func NewMultiGauges(name string, help string, labels []string) *MultiGauges {
t := &MultiGauges{
Gauges: Gauges{Counters{
counts: make(map[string]*int64),
help: help,
}},
labels: labels,
}
if name != "" {
publish(name, t)
}

publishPullMultiGauges(t, name)
return t
}

// Labels returns the list of labels.
func (mg *MultiGauges) Labels() []string {
return mg.labels
}

// Add adds a value to a named counter. len(names) must be equal to
// len(Labels)
func (mg *MultiGauges) Add(names []string, value int64) {
if len(names) != len(mg.labels) {
panic("MultiGauges: wrong number of values in Add")
}
mg.Gauges.Counters.Add(mapKey(names), value)
}

// Set sets the value of a named counter. len(names) must be equal to
// len(Labels)
func (mg *MultiGauges) Set(names []string, value int64) {
if len(names) != len(mg.labels) {
panic("MultiGauges: wrong number of values in Set")
}
mg.Gauges.Set(mapKey(names), value)
}

// MultiCountersFunc is a multidimensional CountersFunc implementation
Expand All @@ -205,23 +295,33 @@ func (mc *MultiCounters) Set(names []string, value int64) {
type MultiCountersFunc struct {
CountersFunc
labels []string
help string
}

// Labels returns the list of labels.
func (mcf *MultiCountersFunc) Labels() []string {
return mcf.labels
}

// Help returns the help string
func (mcf *MultiCountersFunc) Help() string {
return mcf.help
}

// NewMultiCountersFunc creates a new MultiCountersFunc mapping to the provided
// function.
func NewMultiCountersFunc(name string, labels []string, f CountersFunc) *MultiCountersFunc {
func NewMultiCountersFunc(name string, labels []string, help string, f CountersFunc) *MultiCountersFunc {
t := &MultiCountersFunc{
CountersFunc: f,
labels: labels,
help: help,
}
if name != "" {
publish(name, t)
}

publishPullMultiCountersFunc(t, name)

return t
}

Expand Down
18 changes: 9 additions & 9 deletions go/stats/counters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func TestCounters(t *testing.T) {
clear()
c := NewCounters("counter1")
c := NewCounters("counter1", "help")
c.Add("c1", 1)
c.Add("c2", 1)
c.Add("c2", 1)
Expand Down Expand Up @@ -56,14 +56,14 @@ func TestCounters(t *testing.T) {

func TestCountersTags(t *testing.T) {
clear()
c := NewCounters("counterTag1")
c := NewCounters("counterTag1", "help")
want := map[string]int64{}
got := c.Counts()
if !reflect.DeepEqual(got, want) {
t.Errorf("want %v, got %v", want, got)
}

c = NewCounters("counterTag2", "tag1", "tag2")
c = NewCounters("counterTag2", "help", "tag1", "tag2")
want = map[string]int64{"tag1": 0, "tag2": 0}
got = c.Counts()
if !reflect.DeepEqual(got, want) {
Expand All @@ -73,7 +73,7 @@ func TestCountersTags(t *testing.T) {

func TestMultiCounters(t *testing.T) {
clear()
c := NewMultiCounters("mapCounter1", []string{"aaa", "bbb"})
c := NewMultiCounters("mapCounter1", "help", []string{"aaa", "bbb"})
c.Add([]string{"c1a", "c1b"}, 1)
c.Add([]string{"c2a", "c2b"}, 1)
c.Add([]string{"c2a", "c2b"}, 1)
Expand All @@ -89,7 +89,7 @@ func TestMultiCounters(t *testing.T) {
if counts["c2a.c2b"] != 2 {
t.Errorf("want 2, got %d", counts["c2a.c2b"])
}
f := NewMultiCountersFunc("", []string{"aaa", "bbb"}, func() map[string]int64 {
f := NewMultiCountersFunc("", []string{"aaa", "bbb"}, "help", func() map[string]int64 {
return map[string]int64{
"c1a.c1b": 1,
"c2a.c2b": 2,
Expand All @@ -102,7 +102,7 @@ func TestMultiCounters(t *testing.T) {

func TestMultiCountersDot(t *testing.T) {
clear()
c := NewMultiCounters("mapCounter2", []string{"aaa", "bbb"})
c := NewMultiCounters("mapCounter2", "help", []string{"aaa", "bbb"})
c.Add([]string{"c1.a", "c1b"}, 1)
c.Add([]string{"c2a", "c2.b"}, 1)
c.Add([]string{"c2a", "c2.b"}, 1)
Expand All @@ -129,7 +129,7 @@ func TestCountersHook(t *testing.T) {
gotv = v.(*Counters)
})

v := NewCounters("counter2")
v := NewCounters("counter2", "help")
if gotname != "counter2" {
t.Errorf("want counter2, got %s", gotname)
}
Expand All @@ -138,7 +138,7 @@ func TestCountersHook(t *testing.T) {
}
}

var benchCounter = NewCounters("bench")
var benchCounter = NewCounters("bench", "help")

func BenchmarkCounters(b *testing.B) {
clear()
Expand All @@ -152,7 +152,7 @@ func BenchmarkCounters(b *testing.B) {
})
}

var benchMultiCounter = NewMultiCounters("benchMulti", []string{"call", "keyspace", "dbtype"})
var benchMultiCounter = NewMultiCounters("benchMulti", "help", []string{"call", "keyspace", "dbtype"})

func BenchmarkMultiCounters(b *testing.B) {
clear()
Expand Down
Loading