VictoriaMetrics / metrics

Lightweight alternative to github.com/prometheus/client_golang
MIT License
543 stars 63 forks source link

add push hook #51

Closed ClemChou000 closed 11 months ago

ClemChou000 commented 1 year ago

add push hook in order to execute some extra actions like reset registry or update metrics synchronously

hagen1778 commented 11 months ago

Hey @KoyomiKun!

Have you tried using InitPushExt function and add hook to the writeMetrics func(w io.Writer) function?

valyala commented 11 months ago

Closing this pull request, since the extra actions can be performed inside writeMetrics callback, which is passed to InitPushExt and InitPushExtWithOptions, as @hagen1778 pointed out above. For example:

customMetricsSet := metrics.NewSet()
// add the needed metrics to customMetricsSet ...
// ...

writeMetrics := func(w io.Writer) {
  pushHook()  // call pushHook before sending the customMetricsSet metrics to pushURL
  customMetricsSet.WritePrometheus(w)
}
if err := metrics.InitPushExt(pushURL, pushInterval, extraLabels, writeMetrics); err != nil {
  panic(err)
}

If you still feel that the pushHook is needed as a separate callback, then try adding it to the PushOptions in a new pull request.