mirror of
https://github.com/eliasstepanik/caddy-ingess.git
synced 2026-01-11 04:28:28 +00:00
export prometheus metrics on :9090
This commit is contained in:
parent
abe4a47df6
commit
b595f86fda
@ -3,9 +3,12 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/ingress/internal/controller"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/sirupsen/logrus"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
@ -48,36 +51,40 @@ func main() {
|
||||
// start ingress controller
|
||||
c := controller.NewCaddyController(kubeClient, restClient, cfg)
|
||||
|
||||
// create http server to expose controller health metrics
|
||||
healthPort := 9090
|
||||
go startMetricsServer(healthPort)
|
||||
reg := prometheus.NewRegistry()
|
||||
|
||||
logrus.Info("Starting the caddy ingress controller")
|
||||
reg.MustRegister(prometheus.NewGoCollector())
|
||||
reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{
|
||||
PidFn: func() (int, error) { return os.Getpid(), nil },
|
||||
ReportErrors: true,
|
||||
}))
|
||||
|
||||
// create http server to expose controller health metrics
|
||||
go startMetricsServer(reg)
|
||||
|
||||
// start the ingress controller
|
||||
stopCh := make(chan struct{}, 1)
|
||||
defer close(stopCh)
|
||||
|
||||
logrus.Info("Starting the caddy ingress controller")
|
||||
go c.Run(stopCh)
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
type healthChecker struct{}
|
||||
|
||||
func (h *healthChecker) Name() string {
|
||||
return "caddy-ingress-controller"
|
||||
}
|
||||
|
||||
func (h *healthChecker) Check(_ *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func startMetricsServer(port int) {
|
||||
func startMetricsServer(reg *prometheus.Registry) {
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle(
|
||||
"/metrics",
|
||||
promhttp.InstrumentMetricHandler(
|
||||
reg,
|
||||
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),
|
||||
),
|
||||
)
|
||||
|
||||
logrus.Info("Exporting metrics on :9090")
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf(":%v", port),
|
||||
Addr: fmt.Sprintf(":%v", 9090),
|
||||
Handler: mux,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
|
||||
1
go.mod
1
go.mod
@ -9,6 +9,7 @@ require (
|
||||
github.com/mholt/certmagic v0.5.1
|
||||
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/prometheus/client_golang v0.9.2
|
||||
github.com/sirupsen/logrus v1.2.0
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
|
||||
google.golang.org/grpc v1.20.1 // indirect
|
||||
|
||||
@ -72,7 +72,7 @@ func NewCaddyController(kubeClient *kubernetes.Clientset, restClient rest.Interf
|
||||
controller.podInfo = podInfo
|
||||
|
||||
// load caddy config from file if mounted with config map
|
||||
var caddyCfgMap caddy.Config
|
||||
var caddyCfgMap *caddy.Config
|
||||
cfgPath := "/etc/caddy/config.json"
|
||||
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
|
||||
controller.usingConfigMap = true
|
||||
@ -104,7 +104,7 @@ func NewCaddyController(kubeClient *kubernetes.Clientset, restClient rest.Interf
|
||||
controller.informer = informer
|
||||
|
||||
// setup store to keep track of resources
|
||||
controller.resourceStore = store.NewStore(controller.kubeClient, podInfo.Namespace, cfg, &caddyCfgMap)
|
||||
controller.resourceStore = store.NewStore(controller.kubeClient, podInfo.Namespace, cfg, caddyCfgMap)
|
||||
|
||||
// attempt to do initial sync of status addresses with ingresses
|
||||
controller.dispatchSync()
|
||||
|
||||
@ -63,6 +63,8 @@ spec:
|
||||
containerPort: 80
|
||||
- name: https
|
||||
containerPort: 443
|
||||
- name: metrics
|
||||
containerPort: 9090
|
||||
env:
|
||||
- name: POD_NAME
|
||||
valueFrom:
|
||||
|
||||
14
kubernetes/generated/metricsservice.yaml
Normal file
14
kubernetes/generated/metricsservice.yaml
Normal file
@ -0,0 +1,14 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: metrics
|
||||
namespace: caddy-system
|
||||
labels:
|
||||
app: caddy-ingress-controller
|
||||
spec:
|
||||
ports:
|
||||
- name: metrics
|
||||
port: 80
|
||||
protocol: TCP
|
||||
targetPort: metrics
|
||||
type: "ClusterIP"
|
||||
@ -15,4 +15,6 @@ deploy:
|
||||
- kubernetes/generated/clusterrolebinding.yaml
|
||||
- kubernetes/generated/deployment.yaml
|
||||
- kubernetes/generated/serviceaccount.yaml
|
||||
- kubernetes/generated/metricsservice.yaml
|
||||
# - kubernetes/generated/configmap.yaml
|
||||
# - kubernetes/generated/loadbalancer.yaml
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user