Skip to content

Commit

Permalink
Merge pull request #29 from jelmersnoeck/add-metrics-server
Browse files Browse the repository at this point in the history
Add Prometheus Metrics server.
  • Loading branch information
jelmersnoeck authored Sep 15, 2018
2 parents 10ec760 + 0dda91b commit e0a41f6
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Log when something goes wrong with GarbageCollection.
- Use a queue based system to sync items.
- Added `/_healthz` endpoint and set up probes.
- Added `/metrics` endpoint for Prometheus.

### Fixed

Expand All @@ -20,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Set up lower resource requests/limits for the Deployment.
- MonitorTemplate is now namespace scoped instead of cluster scoped.
- The operator now uses cache informers to reconcile state.

## v0.1.1 - 2018-09-02

Expand Down
127 changes: 127 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ required = [
[[constraint]]
branch = "master"
name = "github.com/DreamItGetIT/statuscake"

[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.8.0"
25 changes: 0 additions & 25 deletions internal/httpsvc/health.go

This file was deleted.

35 changes: 35 additions & 0 deletions internal/httpsvc/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package httpsvc

import (
"fmt"
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// Metrics represents a Metrics server which can be used to expose a prometheus
// metrics.
type Metrics struct {
Server
*prometheus.Registry
}

// Start starts the Health Server.
func (s *Metrics) Start(stopCh <-chan struct{}) error {
registerMetrics(&s.ServeMux, s.Registry)
registerHealthCheck(&s.ServeMux)

return s.Server.Start(stopCh)
}

func registerMetrics(mux *http.ServeMux, reg *prometheus.Registry) {
mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
}

func registerHealthCheck(mux *http.ServeMux) {
mux.HandleFunc("/_healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
}
31 changes: 21 additions & 10 deletions internal/ingressmonitor/cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package cmd

import (
"log"
"os"
"time"

"github.com/jelmersnoeck/ingress-monitor/internal/httpsvc"
"github.com/jelmersnoeck/ingress-monitor/internal/ingressmonitor"
"github.com/jelmersnoeck/ingress-monitor/internal/metrics"
"github.com/jelmersnoeck/ingress-monitor/internal/provider"
"github.com/jelmersnoeck/ingress-monitor/internal/provider/logger"
"github.com/jelmersnoeck/ingress-monitor/internal/provider/statuscake"
"github.com/jelmersnoeck/ingress-monitor/internal/signals"
"github.com/jelmersnoeck/ingress-monitor/pkg/client/generated/clientset/versioned"

"github.com/prometheus/client_golang/prometheus"

"github.com/spf13/cobra"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -24,8 +28,8 @@ var operatorFlags struct {
KubeConfig string
ResyncPeriod string

HTTPAddr string
HTTPPort int
MetricsAddr string
MetricsPort int
}

// operatorCmd represents the operator command
Expand Down Expand Up @@ -63,18 +67,25 @@ func runOperator(cmd *cobra.Command, args []string) {
statuscake.Register(fact)
logger.Register(fact)

// register the health server
healthSrv := &httpsvc.Health{
// create new prometheus registry
registry := prometheus.NewRegistry()
registry.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
registry.MustRegister(prometheus.NewGoCollector())

// new metrics collector
mtrc := metrics.New(registry)
metricssvc := httpsvc.Metrics{
Server: httpsvc.Server{
Addr: operatorFlags.HTTPAddr,
Port: operatorFlags.HTTPPort,
Addr: operatorFlags.MetricsAddr,
Port: operatorFlags.MetricsPort,
},
Registry: registry,
}
go healthSrv.Start(stopCh)
go metricssvc.Start(stopCh)

op, err := ingressmonitor.NewOperator(
kubeClient, imClient, operatorFlags.Namespace,
resync, fact,
resync, fact, mtrc,
)
if err != nil {
log.Fatalf("Error building IngressMonitor Operator: %s", err)
Expand All @@ -93,6 +104,6 @@ func init() {
operatorCmd.PersistentFlags().StringVar(&operatorFlags.KubeConfig, "kubeconfig", "", "Kubeconfig which should be used to talk to the API.")
operatorCmd.PersistentFlags().StringVar(&operatorFlags.ResyncPeriod, "resync-period", "30s", "Resyncing period to ensure all monitors are up to date.")

operatorCmd.PersistentFlags().StringVar(&operatorFlags.HTTPAddr, "http-addr", "0.0.0.0", "address the health server will bind to")
operatorCmd.PersistentFlags().IntVar(&operatorFlags.HTTPPort, "http-port", 9090, "port on which the health server is available")
operatorCmd.PersistentFlags().StringVar(&operatorFlags.MetricsAddr, "metrics-addr", "0.0.0.0", "address the metrics server will bind to")
operatorCmd.PersistentFlags().IntVar(&operatorFlags.MetricsPort, "metrics-port", 9090, "port on which the metrics server is available")
}
Loading

0 comments on commit e0a41f6

Please sign in to comment.