Skip to content

Commit

Permalink
Probe and promthues server.
Browse files Browse the repository at this point in the history
  • Loading branch information
yongzhi.yang committed Jun 28, 2019
1 parent 52d7b0f commit b6f232b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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() {
Expand Down
51 changes: 51 additions & 0 deletions pkg/monitor/probes.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit b6f232b

Please sign in to comment.