http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright © The ESO Authors
  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 server provides gRPC server infrastructure for v2 providers.
  14. package server
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "time"
  20. "github.com/prometheus/client_golang/prometheus"
  21. "github.com/prometheus/client_golang/prometheus/promhttp"
  22. ctrl "sigs.k8s.io/controller-runtime"
  23. )
  24. const (
  25. // DefaultMetricsPort is the default port for the HTTP metrics server.
  26. DefaultMetricsPort = 8081
  27. // DefaultMetricsPath is the default path for the metrics endpoint.
  28. DefaultMetricsPath = "/metrics"
  29. )
  30. var metricsLog = ctrl.Log.WithName("metrics-server")
  31. // MetricsServer serves Prometheus metrics via HTTP.
  32. type MetricsServer struct {
  33. server *http.Server
  34. registry *prometheus.Registry
  35. }
  36. // NewMetricsServer creates a new HTTP metrics server.
  37. func NewMetricsServer(port int, registry *prometheus.Registry) *MetricsServer {
  38. if registry == nil {
  39. registry = prometheus.NewRegistry()
  40. }
  41. mux := http.NewServeMux()
  42. mux.Handle(DefaultMetricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{
  43. ErrorHandling: promhttp.ContinueOnError,
  44. }))
  45. // Add health check endpoint
  46. mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
  47. w.WriteHeader(http.StatusOK)
  48. if _, err := w.Write([]byte("ok")); err != nil {
  49. metricsLog.Error(err, "failed to write health response")
  50. }
  51. })
  52. server := &http.Server{
  53. Addr: fmt.Sprintf(":%d", port),
  54. Handler: mux,
  55. ReadHeaderTimeout: 10 * time.Second,
  56. ReadTimeout: 30 * time.Second,
  57. WriteTimeout: 30 * time.Second,
  58. IdleTimeout: 90 * time.Second,
  59. }
  60. return &MetricsServer{
  61. server: server,
  62. registry: registry,
  63. }
  64. }
  65. // Start starts the HTTP metrics server.
  66. func (m *MetricsServer) Start(ctx context.Context) error {
  67. metricsLog.Info("Starting metrics server", "addr", m.server.Addr, "path", DefaultMetricsPath)
  68. // Start server in goroutine
  69. errChan := make(chan error, 1)
  70. go func() {
  71. if err := m.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  72. errChan <- fmt.Errorf("metrics server error: %w", err)
  73. }
  74. }()
  75. // Wait for context cancellation or server error
  76. select {
  77. case <-ctx.Done():
  78. metricsLog.Info("Shutting down metrics server")
  79. shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  80. defer cancel()
  81. if err := m.server.Shutdown(shutdownCtx); err != nil {
  82. return fmt.Errorf("metrics server shutdown error: %w", err)
  83. }
  84. return nil
  85. case err := <-errChan:
  86. return err
  87. }
  88. }
  89. // GetRegistry returns the Prometheus registry.
  90. func (m *MetricsServer) GetRegistry() *prometheus.Registry {
  91. return m.registry
  92. }