Skip to content

Commit

Permalink
web-monitor: support dynamic metric (baidu#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuqing6767 authored Jun 12, 2021
1 parent 55daa98 commit 228ae3b
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 13 deletions.
107 changes: 94 additions & 13 deletions web-monitor/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@
Usage:
import "github.com/baidu/go-lib/web-monitor/metrics"
// define counter struct type
type ServerState {
ReqServed *Counter // field type must be *Counter or *Gauge or *State
ConServed *Counter
ConActive *Gauge
}
// create metrics
var m Metrics
// define counter struct type
type ServerState {
ReqServed *Counter // field type must be *Counter or *Gauge or *State
ConServed *Counter
ConActive *Gauge
}
// create metrics
var m Metrics
var s ServerState
m.Init(&s, "PROXY", 20)
// counter operations
s.ConActive.Inc(2)
// counter operations
s.ConActive.Inc(2)
s.ConServed.Inc(1)
s.ReqServed.Inc(1)
s.ConActive.Dec(1)
// get absoulute data for all metrics
m.Counter("CounterName").Inc(1)
m.Gauge("GaugeName").Inc(1)
m.State("StateName").Set("StateValue")
// get absoulute data for all metrics
stateData := m.GetAll()
// get diff data for all counters(gauge don't have diff data)
// get diff data for all counters(gauge don't have diff data)
stateDiff := m.GetDiff()
*/
package metrics
Expand Down Expand Up @@ -151,6 +155,83 @@ func validateMetrics(metrics interface{}) error {
return nil
}

// NewEmptyMetrics initializes empty Metrics
func NewEmptyMetrics(prefix string, intervalS int) *Metrics {
m := &Metrics{}
m.Init(&struct{}{}, prefix, intervalS)

return m
}

// Counter get or create a Counter by name
func (m *Metrics) Counter(name string) *Counter {
key := m.convert(name)

m.lock.RLock()
if val, ok := m.counterMap[key]; ok {
m.lock.RUnlock()
return val
}
m.lock.RUnlock()

m.lock.Lock()
defer m.lock.Unlock()

if val, ok := m.counterMap[key]; ok {
return val
}

val := new(Counter)
m.counterMap[key] = val
return val
}

// Gauge get or create a Gauge by name
func (m *Metrics) Gauge(name string) *Gauge {
key := m.convert(name)

m.lock.RLock()
if val, ok := m.gaugeMap[key]; ok {
m.lock.RUnlock()
return val
}
m.lock.RUnlock()

m.lock.Lock()
defer m.lock.Unlock()

if val, ok := m.gaugeMap[key]; ok {
return val
}

val := new(Gauge)
m.gaugeMap[key] = val
return val
}

// State get or create a State by name
func (m *Metrics) State(name string) *State {
key := m.convert(name)

m.lock.RLock()
if val, ok := m.stateMap[key]; ok {
m.lock.RUnlock()
return val
}
m.lock.RUnlock()

m.lock.Lock()
defer m.lock.Unlock()

if val, ok := m.stateMap[key]; ok {
return val
}

val := new(State)
m.stateMap[key] = val
return val
}

// GetAll gets absoulute values for all counters
func (m *Metrics) GetAll() *MetricsData {
d := NewMetricsData(m.metricPrefix, KindTotal)
Expand Down
55 changes: 55 additions & 0 deletions web-monitor/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package metrics

import (
"fmt"
"reflect"
"testing"
"time"
)

import (
Expand Down Expand Up @@ -246,3 +248,56 @@ func BenchmarkStateGet(b *testing.B) {
s.GetAll()
}
}

func TestNewMetrics(t *testing.T) {
m := NewEmptyMetrics("test", 1)

tick200Ms := time.NewTicker(time.Millisecond * 200)
tick2S := time.NewTicker(time.Second * 2)

for {
select {
case <-tick200Ms.C:
// simulate concurrent
go func() {
for i := 0; i <= 100; i++ {
m.Counter(fmt.Sprintf("COUNT_%d", i%5)).Inc(1)
m.Gauge(fmt.Sprintf("GAUGE_%d", i%5)).Inc(1)
m.State(fmt.Sprintf("State_%d", i%5)).Set("State")
}

for i := 100; i > 0; i-- {
m.Counter(fmt.Sprintf("COUNT_%d", i%5)).Inc(1)
m.Gauge(fmt.Sprintf("GAUGE_%d", i%5)).Inc(1)
m.State(fmt.Sprintf("State_%d", i%5)).Set("State")
}
}()
case <-tick2S.C:
tick2S.Stop()
tick200Ms.Stop()

counters := m.GetAll().CounterData
if size := len(counters); size != 5 {
t.Errorf("CountData len want 5, got %v", size)
}
for k, v := range counters {
if v < 1 {
t.Errorf("CountData[%v] want > 0, got %v", k, v)
}
}
return
}
}

}

func TestMetrics_Counter(t *testing.T) {
m := NewEmptyMetrics("test", 1)
m.Counter("1").Inc(1)
if len(m.counterMap) != 1 {
t.Errorf("want 1, got: %v", len(m.counterMap))
}
if v := m.counterMap["1"].Get(); v != 1 {
t.Errorf("want 1, got: %v", v)
}
}

0 comments on commit 228ae3b

Please sign in to comment.