meter.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if cert != nil {
  112. switch cert.CertType {
  113. case ssh.UserCert:
  114. return []string{"user"}
  115. case ssh.HostCert:
  116. return []string{"host"}
  117. }
  118. }
  119. return []string{"unknown"}
  120. }
  121. func incrProvisionerCounter(cv *prometheus.CounterVec, p provisioner.Interface, err error, extraValues ...string) {
  122. var name string
  123. if p != nil {
  124. name = p.GetName()
  125. }
  126. values := append([]string{
  127. name, strconv.FormatBool(err == nil),
  128. }, extraValues...)
  129. cv.WithLabelValues(values...).Inc()
  130. }
  131. // KMSSigned implements [authority.Meter] for [Meter].
  132. func (m *Meter) KMSSigned(err error) {
  133. if err == nil {
  134. m.kms.signed.Inc()
  135. } else {
  136. m.kms.errors.Inc()
  137. }
  138. }
  139. // provisionerInstruments wraps the counters exported by provisioners.
  140. type provisionerInstruments struct {
  141. rekeyed *prometheus.CounterVec
  142. renewed *prometheus.CounterVec
  143. signed *prometheus.CounterVec
  144. webhookAuthorized *prometheus.CounterVec
  145. webhookEnriched *prometheus.CounterVec
  146. }
  147. func newProvisionerInstruments(subsystem string, signLabels, webhookLabels []string) *provisionerInstruments {
  148. return &provisionerInstruments{
  149. rekeyed: newCounterVec(subsystem, "rekeyed_total", "Number of certificates rekeyed", signLabels...),
  150. renewed: newCounterVec(subsystem, "renewed_total", "Number of certificates renewed", signLabels...),
  151. signed: newCounterVec(subsystem, "signed_total", "Number of certificates signed", signLabels...),
  152. webhookAuthorized: newCounterVec(subsystem, "webhook_authorized_total", "Number of authorizing webhooks called", webhookLabels...),
  153. webhookEnriched: newCounterVec(subsystem, "webhook_enriched_total", "Number of enriching webhooks called", webhookLabels...),
  154. }
  155. }
  156. type kms struct {
  157. signed prometheus.Counter
  158. errors prometheus.Counter
  159. }
  160. func newCounterVec(subsystem, name, help string, labels ...string) *prometheus.CounterVec {
  161. opts := opts(subsystem, name, help)
  162. return prometheus.NewCounterVec(prometheus.CounterOpts(opts), labels)
  163. }
  164. func opts(subsystem, name, help string) prometheus.Opts {
  165. return prometheus.Opts{
  166. Namespace: "step_ca",
  167. Subsystem: subsystem,
  168. Name: name,
  169. Help: help,
  170. }
  171. }