Created
February 25, 2024 18:04
-
-
Save devalexandre/bc10c52800f757b77f87a247a9390892 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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