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:push上报模式支持自动注销 #210

Merged
merged 8 commits into from
Jul 17, 2024
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 @@

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 @@
if s.cancel != nil {
s.cancel()
}
if s.action != nil {
s.action.Close()
}
return nil
}

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

type PullAction struct {
Expand All @@ -277,6 +282,9 @@
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 @@
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 @@
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.15.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 @@
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
Loading