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

Fix app name in metric is unknown_go_service (#453) #457

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (
"net"
"net/http"

"github.com/alibaba/sentinel-golang/core/circuitbreaker"
"github.com/alibaba/sentinel-golang/core/config"
"github.com/alibaba/sentinel-golang/core/flow"
"github.com/alibaba/sentinel-golang/core/log/metric"
"github.com/alibaba/sentinel-golang/core/stat"
"github.com/alibaba/sentinel-golang/core/system_metric"
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
"github.com/alibaba/sentinel-golang/util"
Expand Down Expand Up @@ -87,6 +90,11 @@ func initCoreComponents() error {
}
}

flow.Init()
stat.Init()
system_metric.Init()
circuitbreaker.Init()

systemStatInterval := config.SystemStatCollectIntervalMs()
loadStatInterval := systemStatInterval
cpuStatInterval := systemStatInterval
Expand Down
22 changes: 14 additions & 8 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,19 +50,24 @@ const (
)

var (
stateChangedCounter = metric_exporter.NewCounter(
"circuit_breaker_state_changed_total",
"Circuit breaker total state change count",
[]string{"resource", "from_state", "to_state"})
once sync.Once
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to use sync.Once

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll fix it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to consider putting it inside a separate instance of sentinel instead of a global instance?


stateChangedCounter metric_exporter.Counter
)

func init() {
metric_exporter.Register(stateChangedCounter)
func Init() {
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 {
var state State
state = Closed
state := Closed

return &state
}
Expand Down
6 changes: 6 additions & 0 deletions core/circuitbreaker/circuit_breaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package circuitbreaker

import (
"errors"
"os"
"sync/atomic"
"testing"

Expand All @@ -27,6 +28,11 @@ import (
"github.com/stretchr/testify/mock"
)

func TestMain(m *testing.M) {
Init()
os.Exit(m.Run())
}

type CircuitBreakerMock struct {
mock.Mock
}
Expand Down
7 changes: 7 additions & 0 deletions core/flow/rule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package flow

import (
"os"
"reflect"
"testing"

Expand All @@ -27,6 +28,12 @@ func clearData() {
tcMap = make(TrafficControllerMap)
currentRules = make(map[string][]*Rule, 0)
}

func TestMain(m *testing.M) {
Init()
os.Exit(m.Run())
}

func TestSetAndRemoveTrafficShapingGenerator(t *testing.T) {
tsc := &TrafficShapingController{}

Expand Down
20 changes: 14 additions & 6 deletions core/flow/traffic_shaping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
package flow

import (
"sync"

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

var (
resourceFlowThresholdGauge = metric_exporter.NewGauge(
"resource_flow_threshold",
"Resource flow threshold",
[]string{"resource"})
once sync.Once

resourceFlowThresholdGauge metric_exporter.Gauge
)

func init() {
metric_exporter.Register(resourceFlowThresholdGauge)
func Init() {
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
19 changes: 13 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,14 +31,19 @@ const (
var (
DefaultSlot = &Slot{}

handledCounter = metric_exporter.NewCounter(
"handled_total",
"Total handled count",
[]string{"resource", "result", "block_type"})
once sync.Once
handledCounter metric_exporter.Counter
)

func init() {
metric_exporter.Register(handledCounter)
func Init() {
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
30 changes: 20 additions & 10 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 All @@ -50,14 +51,8 @@ var (

ssStopChan = make(chan struct{})

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

func init() {
Expand All @@ -73,9 +68,24 @@ func init() {
currentProcessOnce.Do(func() {
currentProcess.Store(p)
})
}

metric_exporter.Register(cpuRatioGauge)
metric_exporter.Register(processMemoryGauge)
func Init() {
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
6 changes: 6 additions & 0 deletions core/system_metric/sys_metric_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package system_metric

import (
"os"
"sync"
"testing"
"time"
Expand All @@ -23,6 +24,11 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
Init()
os.Exit(m.Run())
}

func TestCurrentLoad(t *testing.T) {
defer currentLoad.Store(NotRetrievedLoadValue)

Expand Down
4 changes: 1 addition & 3 deletions exporter/metric/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var (
exporter Exporter

host string
app string
pid string
namespace string
)
Expand All @@ -39,7 +38,6 @@ func init() {
if host == "" {
host = "unknown"
}
app = config.AppName()
pid = strconv.Itoa(os.Getpid())
namespace = "sentinel_go"
}
Expand Down Expand Up @@ -111,8 +109,8 @@ func (e *prometheusExporter) HTTPHandler() http.Handler {
func newConstLabels() map[string]string {
return map[string]string{
"host": host,
"app": app,
"pid": pid,
"app": config.AppName(),
}
}

Expand Down