prome_init.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build prometheus || !core
  15. // +build prometheus !core
  16. package server
  17. import (
  18. "context"
  19. "fmt"
  20. "net/http"
  21. "time"
  22. "github.com/gorilla/mux"
  23. "github.com/prometheus/client_golang/prometheus/promhttp"
  24. "github.com/lf-edge/ekuiper/internal/conf"
  25. )
  26. func init() {
  27. p := &promeComp{}
  28. servers["prometheus"] = p
  29. components["prometheus"] = p
  30. }
  31. type promeComp struct {
  32. s *http.Server
  33. }
  34. func (p *promeComp) register() {
  35. // Do nothing
  36. }
  37. func (p *promeComp) rest(r *mux.Router) {
  38. portPrometheus := conf.Config.Basic.PrometheusPort
  39. portRest := conf.Config.Basic.RestPort
  40. if portPrometheus == portRest {
  41. r.Handle("/metrics", promhttp.Handler())
  42. msg := fmt.Sprintf("Register prometheus metrics to http://localhost:%d/metrics", portPrometheus)
  43. logger.Infof(msg)
  44. fmt.Println(msg)
  45. }
  46. }
  47. func (p *promeComp) serve() {
  48. if conf.Config.Basic.Prometheus {
  49. // Start prometheus service
  50. portPrometheus := conf.Config.Basic.PrometheusPort
  51. if portPrometheus <= 0 {
  52. logger.Fatal("Miss configuration prometheusPort")
  53. }
  54. portRest := conf.Config.Basic.RestPort
  55. if portPrometheus != portRest {
  56. mux := http.NewServeMux()
  57. mux.Handle("/metrics", promhttp.Handler())
  58. srvPrometheus := &http.Server{
  59. Addr: fmt.Sprintf("0.0.0.0:%d", portPrometheus),
  60. WriteTimeout: time.Second * 15,
  61. ReadTimeout: time.Second * 15,
  62. IdleTimeout: time.Second * 60,
  63. Handler: mux,
  64. }
  65. go func() {
  66. if err := srvPrometheus.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  67. logger.Fatal("Listen prometheus error: ", err)
  68. }
  69. }()
  70. p.s = srvPrometheus
  71. msg := fmt.Sprintf("Serving prometheus metrics on port http://localhost:%d/metrics", portPrometheus)
  72. logger.Infof(msg)
  73. fmt.Println(msg)
  74. }
  75. }
  76. }
  77. func (p *promeComp) close() {
  78. if p.s != nil {
  79. if err := p.s.Shutdown(context.TODO()); err != nil {
  80. logger.Errorf("prometheus server shutdown error: %v", err)
  81. }
  82. logger.Info("prometheus server successfully shutdown.")
  83. }
  84. }