Skip to content

Commit 9a5812f

Browse files
authored
fix:push上报模式支持自动注销 (#210)
1 parent 78d3930 commit 9a5812f

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

plugin/metrics/prometheus/reporter.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ import (
4141

4242
const (
4343
// PluginName is the name of the plugin.
44-
PluginName = "prometheus"
45-
_metricsPull = "pull"
46-
_metricsPush = "push"
47-
_defaultJobName = "polaris-client"
44+
PluginName = "prometheus"
45+
_metricsPull = "pull"
46+
_metricsPush = "push"
47+
_defaultJobName = "polaris-client"
48+
_defaultJobInstance = "instance"
4849
)
4950

5051
var _ statreporter.StatReporter = (*PrometheusReporter)(nil)
@@ -232,6 +233,9 @@ func (s *PrometheusReporter) Destroy() error {
232233
if s.cancel != nil {
233234
s.cancel()
234235
}
236+
if s.action != nil {
237+
s.action.Close()
238+
}
235239
return nil
236240
}
237241

@@ -251,6 +255,7 @@ type ReportAction interface {
251255
Init(initCtx *plugin.InitContext, reporter *PrometheusReporter)
252256
Run(ctx context.Context)
253257
Info() model.StatInfo
258+
Close()
254259
}
255260

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

285+
func (pa *PullAction) Close() {
286+
}
287+
280288
func (pa *PullAction) doAggregation(ctx context.Context) {
281289
ticker := time.NewTicker(30 * time.Second)
282290

@@ -351,6 +359,7 @@ type PushAction struct {
351359
initCtx *plugin.InitContext
352360
reporter *PrometheusReporter
353361
cfg *Config
362+
pusher *push.Pusher
354363
}
355364

356365
func (pa *PushAction) Init(initCtx *plugin.InitContext, reporter *PrometheusReporter) {
@@ -359,6 +368,15 @@ func (pa *PushAction) Init(initCtx *plugin.InitContext, reporter *PrometheusRepo
359368
return
360369
}
361370
pa.cfg = cfgValue.(*Config)
371+
pa.pusher = push.
372+
New(pa.cfg.Address, _defaultJobName).
373+
Grouping(_defaultJobInstance, pa.initCtx.SDKContextID)
374+
}
375+
376+
func (pa *PushAction) Close() {
377+
if pa.pusher != nil {
378+
pa.pusher.Delete()
379+
}
362380
}
363381

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

383-
if err := push.
384-
New(pa.cfg.Address, _defaultJobName).
401+
if err := pa.pusher.
385402
Gatherer(pa.reporter.registry).
386403
Push(); err != nil {
387404
log.GetBaseLogger().Errorf("push metrics to pushgateway fail: %s", err.Error())

0 commit comments

Comments
 (0)