Skip to content

Commit

Permalink
fix:push上报模式支持自动注销 (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Jul 17, 2024
1 parent 78d3930 commit 9a5812f
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions plugin/metrics/prometheus/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ import (

const (
// PluginName is the name of the plugin.
PluginName = "prometheus"
_metricsPull = "pull"
_metricsPush = "push"
_defaultJobName = "polaris-client"
PluginName = "prometheus"
_metricsPull = "pull"
_metricsPush = "push"
_defaultJobName = "polaris-client"
_defaultJobInstance = "instance"
)

var _ statreporter.StatReporter = (*PrometheusReporter)(nil)
Expand Down Expand Up @@ -232,6 +233,9 @@ func (s *PrometheusReporter) Destroy() error {
if s.cancel != nil {
s.cancel()
}
if s.action != nil {
s.action.Close()
}
return nil
}

Expand All @@ -251,6 +255,7 @@ type ReportAction interface {
Init(initCtx *plugin.InitContext, reporter *PrometheusReporter)
Run(ctx context.Context)
Info() model.StatInfo
Close()
}

type PullAction struct {
Expand All @@ -277,6 +282,9 @@ func (pa *PullAction) Init(initCtx *plugin.InitContext, reporter *PrometheusRepo
pa.bindPort = int32(pa.cfg.port)
}

func (pa *PullAction) Close() {
}

func (pa *PullAction) doAggregation(ctx context.Context) {
ticker := time.NewTicker(30 * time.Second)

Expand Down Expand Up @@ -351,6 +359,7 @@ type PushAction struct {
initCtx *plugin.InitContext
reporter *PrometheusReporter
cfg *Config
pusher *push.Pusher
}

func (pa *PushAction) Init(initCtx *plugin.InitContext, reporter *PrometheusReporter) {
Expand All @@ -359,6 +368,15 @@ func (pa *PushAction) Init(initCtx *plugin.InitContext, reporter *PrometheusRepo
return
}
pa.cfg = cfgValue.(*Config)
pa.pusher = push.
New(pa.cfg.Address, _defaultJobName).
Grouping(_defaultJobInstance, pa.initCtx.SDKContextID)
}

func (pa *PushAction) Close() {
if pa.pusher != nil {
pa.pusher.Delete()

Check failure on line 378 in plugin/metrics/prometheus/reporter.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16.x)

Error return value of `pa.pusher.Delete` is not checked (errcheck)
}
}

func (pa *PushAction) Run(ctx context.Context) {
Expand All @@ -380,8 +398,7 @@ func (pa *PushAction) Run(ctx context.Context) {
statcommon.PutDataFromContainerInOrder(pa.reporter.metricVecCaches, pa.reporter.rateLimitCollector,
pa.reporter.rateLimitCollector.GetCurrentRevision())

if err := push.
New(pa.cfg.Address, _defaultJobName).
if err := pa.pusher.
Gatherer(pa.reporter.registry).
Push(); err != nil {
log.GetBaseLogger().Errorf("push metrics to pushgateway fail: %s", err.Error())
Expand Down

0 comments on commit 9a5812f

Please sign in to comment.