meter.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Package metrix implements stats-related functionality.
  2. package metrix
  3. import (
  4. "crypto/x509"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "golang.org/x/crypto/ssh"
  10. "github.com/prometheus/client_golang/prometheus"
  11. "github.com/prometheus/client_golang/prometheus/promhttp"
  12. )
  13. // New initializes and returns a new [Meter].
  14. func New() (m *Meter) {
  15. initializedAt := time.Now()
  16. defaultLabels := []string{"provisioner", "success"}
  17. sshSignLabels := []string{"provisioner", "success", "type"}
  18. m = &Meter{
  19. uptime: prometheus.NewGaugeFunc(
  20. prometheus.GaugeOpts(opts(
  21. "",
  22. "uptime_seconds",
  23. "Number of seconds since service start",
  24. )),
  25. func() float64 {
  26. return float64(time.Since(initializedAt) / time.Second)
  27. },
  28. ),
  29. ssh: newProvisionerInstruments("ssh", sshSignLabels, defaultLabels),
  30. x509: newProvisionerInstruments("x509", defaultLabels, defaultLabels),
  31. kms: &kms{
  32. signed: prometheus.NewCounter(prometheus.CounterOpts(opts("kms", "signed", "Number of KMS-backed signatures"))),
  33. errors: prometheus.NewCounter(prometheus.CounterOpts(opts("kms", "errors", "Number of KMS-related errors"))),
  34. },
  35. }
  36. reg := prometheus.NewRegistry()
  37. reg.MustRegister(
  38. m.uptime,
  39. m.ssh.rekeyed,
  40. m.ssh.renewed,
  41. m.ssh.signed,
  42. m.ssh.webhookAuthorized,
  43. m.ssh.webhookEnriched,
  44. m.x509.rekeyed,
  45. m.x509.renewed,
  46. m.x509.signed,
  47. m.x509.webhookAuthorized,
  48. m.x509.webhookEnriched,
  49. m.kms.signed,
  50. m.kms.errors,
  51. )
  52. h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{
  53. Registry: reg,
  54. Timeout: 5 * time.Second,
  55. MaxRequestsInFlight: 10,
  56. })
  57. mux := http.NewServeMux()
  58. mux.Handle("/metrics", h)
  59. m.Handler = mux
  60. return
  61. }
  62. // Meter wraps the functionality of a Prometheus-compatible HTTP handler.
  63. type Meter struct {
  64. http.Handler
  65. uptime prometheus.GaugeFunc
  66. ssh *provisionerInstruments
  67. x509 *provisionerInstruments
  68. kms *kms
  69. }
  70. // SSHRekeyed implements [authority.Meter] for [Meter].
  71. func (m *Meter) SSHRekeyed(cert *ssh.Certificate, p provisioner.Interface, err error) {
  72. incrProvisionerCounter(m.ssh.rekeyed, p, err, sshCertValues(cert)...)
  73. }
  74. // SSHRenewed implements [authority.Meter] for [Meter].
  75. func (m *Meter) SSHRenewed(cert *ssh.Certificate, p provisioner.Interface, err error) {
  76. incrProvisionerCounter(m.ssh.renewed, p, err, sshCertValues(cert)...)
  77. }
  78. // SSHSigned implements [authority.Meter] for [Meter].
  79. func (m *Meter) SSHSigned(cert *ssh.Certificate, p provisioner.Interface, err error) {
  80. incrProvisionerCounter(m.ssh.signed, p, err, sshCertValues(cert)...)
  81. }
  82. // SSHWebhookAuthorized implements [authority.Meter] for [Meter].
  83. func (m *Meter) SSHWebhookAuthorized(p provisioner.Interface, err error) {
  84. incrProvisionerCounter(m.ssh.webhookAuthorized, p, err)
  85. }
  86. // SSHWebhookEnriched implements [authority.Meter] for [Meter].
  87. func (m *Meter) SSHWebhookEnriched(p provisioner.Interface, err error) {
  88. incrProvisionerCounter(m.ssh.webhookEnriched, p, err)
  89. }
  90. // X509Rekeyed implements [authority.Meter] for [Meter].
  91. func (m *Meter) X509Rekeyed(_ []*x509.Certificate, p provisioner.Interface, err error) {
  92. incrProvisionerCounter(m.x509.rekeyed, p, err)
  93. }
  94. // X509Renewed implements [authority.Meter] for [Meter].
  95. func (m *Meter) X509Renewed(_ []*x509.Certificate, p provisioner.Interface, err error) {
  96. incrProvisionerCounter(m.x509.renewed, p, err)
  97. }
  98. // X509Signed implements [authority.Meter] for [Meter].
  99. func (m *Meter) X509Signed(_ []*x509.Certificate, p provisioner.Interface, err error) {
  100. incrProvisionerCounter(m.x509.signed, p, err)
  101. }
  102. // X509WebhookAuthorized implements [authority.Meter] for [Meter].
  103. func (m *Meter) X509WebhookAuthorized(p provisioner.Interface, err error) {
  104. incrProvisionerCounter(m.x509.webhookAuthorized, p, err)
  105. }
  106. // X509WebhookEnriched implements [authority.Meter] for [Meter].
  107. func (m *Meter) X509WebhookEnriched(p provisioner.Interface, err error) {
  108. incrProvisionerCounter(m.x509.webhookEnriched, p, err)
  109. }
  110. func sshCertValues(cert *ssh.Certificate) []string {
  111. switch cert.CertType {
  112. case ssh.UserCert:
  113. return []string{"user"}
  114. case ssh.HostCert:
  115. return []string{"host"}
  116. default:
  117. return []string{"unknown"}
  118. }
  119. }
  120. func incrProvisionerCounter(cv *prometheus.CounterVec, p provisioner.Interface, err error, extraValues ...string) {
  121. var name string
  122. if p != nil {
  123. name = p.GetName()
  124. }
  125. values := append([]string{
  126. name, strconv.FormatBool(err == nil),
  127. }, extraValues...)
  128. cv.WithLabelValues(values...).Inc()
  129. }
  130. // KMSSigned implements [authority.Meter] for [Meter].
  131. func (m *Meter) KMSSigned(err error) {
  132. if err == nil {
  133. m.kms.signed.Inc()
  134. } else {
  135. m.kms.errors.Inc()
  136. }
  137. }
  138. // provisionerInstruments wraps the counters exported by provisioners.
  139. type provisionerInstruments struct {
  140. rekeyed *prometheus.CounterVec
  141. renewed *prometheus.CounterVec
  142. signed *prometheus.CounterVec
  143. webhookAuthorized *prometheus.CounterVec
  144. webhookEnriched *prometheus.CounterVec
  145. }
  146. func newProvisionerInstruments(subsystem string, signLabels, webhookLabels []string) *provisionerInstruments {
  147. return &provisionerInstruments{
  148. rekeyed: newCounterVec(subsystem, "rekeyed_total", "Number of certificates rekeyed", signLabels...),
  149. renewed: newCounterVec(subsystem, "renewed_total", "Number of certificates renewed", signLabels...),
  150. signed: newCounterVec(subsystem, "signed_total", "Number of certificates signed", signLabels...),
  151. webhookAuthorized: newCounterVec(subsystem, "webhook_authorized_total", "Number of authorizing webhooks called", webhookLabels...),
  152. webhookEnriched: newCounterVec(subsystem, "webhook_enriched_total", "Number of enriching webhooks called", webhookLabels...),
  153. }
  154. }
  155. type kms struct {
  156. signed prometheus.Counter
  157. errors prometheus.Counter
  158. }
  159. func newCounterVec(subsystem, name, help string, labels ...string) *prometheus.CounterVec {
  160. opts := opts(subsystem, name, help)
  161. return prometheus.NewCounterVec(prometheus.CounterOpts(opts), labels)
  162. }
  163. func opts(subsystem, name, help string) prometheus.Opts {
  164. return prometheus.Opts{
  165. Namespace: "step_ca",
  166. Subsystem: subsystem,
  167. Name: name,
  168. Help: help,
  169. }
  170. }