From b6f232b52175995d9b4dc2f36e7b25602543db9a Mon Sep 17 00:00:00 2001 From: "yongzhi.yang" Date: Fri, 28 Jun 2019 15:00:42 +0800 Subject: [PATCH] Probe and promthues server. --- go.mod | 2 +- main.go | 25 +++++++++++++++++++++ pkg/monitor/probes.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100755 pkg/monitor/probes.go diff --git a/go.mod b/go.mod index 952d2a7..be973bb 100644 --- a/go.mod +++ b/go.mod @@ -57,7 +57,7 @@ require ( github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.8.1 - github.com/prometheus/client_golang v0.9.2 // indirect + github.com/prometheus/client_golang v0.9.2 github.com/russross/blackfriday v1.5.1 // indirect github.com/sirupsen/logrus v1.3.0 // indirect github.com/soheilhy/cmux v0.1.4 // indirect diff --git a/main.go b/main.go index a2a1508..a842961 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,9 @@ import ( "flag" "github.com/jasonlvhit/gocron" "github.com/yangyongzhi/sym-operator/pkg/helm" + "github.com/yangyongzhi/sym-operator/pkg/monitor" "k8s.io/client-go/rest" + "net/http" "os" "time" @@ -88,11 +90,34 @@ func main() { kubeInformerFactory.Start(stopCh) symInformerFactory.Start(stopCh) + // Start a cron scheduler go func() { <-gocron.Start() }() + //Start a monitor for symphony operator + //monitorErrCh := make(chan error) + go func() { + mux := monitor.NewProbesMux() + + // Register gRPC server to prometheus to initialized matrix + //goprom.Register(rootServer) + monitor.AddPrometheusHandler(mux) + + if err := http.ListenAndServe(monitor.MonitorAddr, mux); err != nil { + //monitorErrCh <- err + klog.Fatalf("Error start monitor server: %s", err.Error()) + } + }() + //select { + //case err := <-srvErrCh: + // logger.Fatalf("Server died: %s", err) + //case err := <-monitorErrCh: + // klog.Fatalf("Monitor server died: %s", err.Error()) + //} + if err = controller.Run(2, stopCh); err != nil { klog.Fatalf("Error running controller: %s", err.Error()) } + } func init() { diff --git a/pkg/monitor/probes.go b/pkg/monitor/probes.go new file mode 100755 index 0000000..3ab2c3c --- /dev/null +++ b/pkg/monitor/probes.go @@ -0,0 +1,51 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package monitor + +import ( + "github.com/prometheus/client_golang/prometheus/promhttp" + "net/http" +) + +const MonitorAddr = ":44100" + +const ( + isReadyMessage = "I am health." + isAliveMessage = "I am alive." +) + +func readinessProbe(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte(isReadyMessage)) +} + +func livenessProbe(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte(isAliveMessage)) +} + +func NewProbesMux() *http.ServeMux { + mux := http.NewServeMux() + mux.HandleFunc("/readiness", readinessProbe) + mux.HandleFunc("/liveness", livenessProbe) + return mux +} + +func AddPrometheusHandler(mux *http.ServeMux) { + // Register HTTP handler for the global Prometheus registry. + mux.Handle("/metrics", promhttp.Handler()) +}