Skip to content

Instantly share code, notes, and snippets.

@devalexandre
Created February 25, 2024 18:04
Show Gist options
  • Save devalexandre/bc10c52800f757b77f87a247a9390892 to your computer and use it in GitHub Desktop.
Save devalexandre/bc10c52800f757b77f87a247a9390892 to your computer and use it in GitHub Desktop.
func PutCounter(name, helper string) {
mu.Lock()
defer mu.Unlock()
// Tenta criar um novo coletor.
counterOpts := prometheus.CounterOpts{
Name: name,
Help: helper,
}
counter := prometheus.NewCounter(counterOpts)
// Tenta registrar o coletor no Registry personalizado.
// Se o registro falhar devido a um coletor duplicado, ignora o erro.
err := customRegistry.Register(counter)
if err != nil {
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
// Trata outros erros que não sejam de registro duplicado.
panic(err)
}
// Se o coletor já estiver registrado, obtém o coletor existente.
existingCollector, _ := customRegistry.Get(name)
counter, _ = existingCollector.(prometheus.Counter)
}
// Incrementa o contador.
counter.Inc()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment