metrics.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package metrics provides functionality for collecting and managing metrics in the external-secrets system.
  14. package metrics
  15. import (
  16. "github.com/prometheus/client_golang/prometheus"
  17. "sigs.k8s.io/controller-runtime/pkg/metrics"
  18. "github.com/external-secrets/external-secrets/runtime/constants"
  19. )
  20. const (
  21. // ExternalSecretSubsystem is the subsystem name used for external secret metrics.
  22. ExternalSecretSubsystem = "externalsecret"
  23. providerAPICalls = "provider_api_calls_count"
  24. )
  25. var (
  26. syncCallsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
  27. Subsystem: ExternalSecretSubsystem,
  28. Name: providerAPICalls,
  29. Help: "Number of API calls towards the secret provider",
  30. }, []string{"provider", "call", "status"})
  31. )
  32. // ObserveAPICall records metrics for an API call to a provider.
  33. func ObserveAPICall(provider, call string, err error) {
  34. syncCallsTotal.WithLabelValues(provider, call, deriveStatus(err)).Inc()
  35. }
  36. func deriveStatus(err error) string {
  37. if err != nil {
  38. return constants.StatusError
  39. }
  40. return constants.StatusSuccess
  41. }
  42. func init() {
  43. metrics.Registry.MustRegister(syncCallsTotal)
  44. }