Skip to content

Commit

Permalink
Add single instance initialization to Init()
Browse files Browse the repository at this point in the history
Initialize global metric variables
  • Loading branch information
kklinan committed Feb 12, 2022
1 parent 0f34e31 commit 849c4da
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
17 changes: 11 additions & 6 deletions core/circuitbreaker/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package circuitbreaker

import (
"reflect"
"sync"
"sync/atomic"

"github.com/alibaba/sentinel-golang/core/base"
Expand Down Expand Up @@ -49,16 +50,20 @@ const (
)

var (
once sync.Once

stateChangedCounter metric_exporter.Counter
)

func Init() {
stateChangedCounter = metric_exporter.NewCounter(
"circuit_breaker_state_changed_total",
"Circuit breaker total state change count",
[]string{"resource", "from_state", "to_state"},
)
metric_exporter.Register(stateChangedCounter)
once.Do(func() {
stateChangedCounter = metric_exporter.NewCounter(
"circuit_breaker_state_changed_total",
"Circuit breaker total state change count",
[]string{"resource", "from_state", "to_state"},
)
metric_exporter.Register(stateChangedCounter)
})
}

func newState() *State {
Expand Down
18 changes: 12 additions & 6 deletions core/flow/traffic_shaping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
package flow

import (
"sync"

"github.com/alibaba/sentinel-golang/core/base"
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
)

var (
once sync.Once

resourceFlowThresholdGauge metric_exporter.Gauge
)

func Init() {
resourceFlowThresholdGauge = metric_exporter.NewGauge(
"resource_flow_threshold",
"Resource flow threshold",
[]string{"resource"},
)
metric_exporter.Register(resourceFlowThresholdGauge)
once.Do(func() {
resourceFlowThresholdGauge = metric_exporter.NewGauge(
"resource_flow_threshold",
"Resource flow threshold",
[]string{"resource"},
)
metric_exporter.Register(resourceFlowThresholdGauge)
})
}

// TrafficShapingCalculator calculates the actual traffic shaping threshold
Expand Down
17 changes: 11 additions & 6 deletions core/stat/stat_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package stat

import (
"sync"

"github.com/alibaba/sentinel-golang/core/base"
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
"github.com/alibaba/sentinel-golang/util"
Expand All @@ -29,16 +31,19 @@ const (
var (
DefaultSlot = &Slot{}

once sync.Once
handledCounter metric_exporter.Counter
)

func Init() {
handledCounter = metric_exporter.NewCounter(
"handled_total",
"Total handled count",
[]string{"resource", "result", "block_type"},
)
metric_exporter.Register(handledCounter)
once.Do(func() {
handledCounter = metric_exporter.NewCounter(
"handled_total",
"Total handled count",
[]string{"resource", "result", "block_type"},
)
metric_exporter.Register(handledCounter)
})
}

type Slot struct {
Expand Down
29 changes: 16 additions & 13 deletions core/system_metric/sys_metric_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
currentCpuUsage atomic.Value
currentMemoryUsage atomic.Value

once sync.Once
loadStatCollectorOnce sync.Once
memoryStatCollectorOnce sync.Once
cpuStatCollectorOnce sync.Once
Expand Down Expand Up @@ -70,19 +71,21 @@ func init() {
}

func Init() {
cpuRatioGauge = metric_exporter.NewGauge(
"cpu_ratio",
"Process cpu ratio",
[]string{},
)
metric_exporter.Register(cpuRatioGauge)

processMemoryGauge = metric_exporter.NewGauge(
"process_memory_bytes",
"Process memory in bytes",
[]string{},
)
metric_exporter.Register(processMemoryGauge)
once.Do(func() {
cpuRatioGauge = metric_exporter.NewGauge(
"cpu_ratio",
"Process cpu ratio",
[]string{},
)
metric_exporter.Register(cpuRatioGauge)

processMemoryGauge = metric_exporter.NewGauge(
"process_memory_bytes",
"Process memory in bytes",
[]string{},
)
metric_exporter.Register(processMemoryGauge)
})
}

// getMemoryStat returns the current machine's memory statistic
Expand Down

0 comments on commit 849c4da

Please sign in to comment.