Skip to content

Commit 23dc265

Browse files
author
Xuewei Zhang
committed
Add Prometheus exporter.
1 parent a071760 commit 23dc265

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

cmd/node_problem_detector.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"k8s.io/node-problem-detector/cmd/options"
2626
"k8s.io/node-problem-detector/pkg/exporters/k8sexporter"
27+
"k8s.io/node-problem-detector/pkg/exporters/prometheusexporter"
2728
"k8s.io/node-problem-detector/pkg/problemdaemon"
2829
"k8s.io/node-problem-detector/pkg/problemdetector"
2930
"k8s.io/node-problem-detector/pkg/types"
@@ -57,6 +58,10 @@ func main() {
5758
exporters = append(exporters, ke)
5859
glog.Info("K8s exporter started.")
5960
}
61+
if pe := prometheusexporter.NewExporterOrDie(npdo); pe != nil {
62+
exporters = append(exporters, pe)
63+
glog.Info("Prometheus exporter started.")
64+
}
6065
if len(exporters) == 0 {
6166
glog.Fatalf("No exporter is successfully setup")
6267
}

cmd/options/options.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ type NodeProblemDetectorOptions struct {
5252
// ApiServerOverride is the custom URI used to connect to Kubernetes ApiServer.
5353
ApiServerOverride string
5454

55+
// prometheusExporter options
56+
// PrometheusServerPort is the port to bind the Prometheus scrape endpoint. Use 0 to disable.
57+
PrometheusServerPort int
58+
// PrometheusServerAddress is the address to bind the Prometheus scrape endpoint.
59+
PrometheusServerAddress string
60+
5561
// problem daemon options
5662

5763
// SystemLogMonitorConfigPaths specifies the list of paths to system log monitor configuration
@@ -96,6 +102,11 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
96102
fs.StringVar(&npdo.ServerAddress, "address",
97103
"127.0.0.1", "The address to bind the node problem detector server.")
98104

105+
fs.IntVar(&npdo.PrometheusServerPort, "prometheus-port",
106+
20257, "The port to bind the Prometheus scrape endpoint. Prometheus exporter is enabled by default at port 20257. Use 0 to disable.")
107+
fs.StringVar(&npdo.PrometheusServerAddress, "prometheus-address",
108+
"127.0.0.1", "The address to bind the Prometheus scrape endpoint.")
109+
99110
for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() {
100111
npdo.MonitorConfigPaths[problemDaemonName] = &[]string{}
101112
fs.StringSliceVar(
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package prometheusexporter
18+
19+
import (
20+
"net"
21+
"net/http"
22+
"strconv"
23+
24+
"contrib.go.opencensus.io/exporter/prometheus"
25+
"github.com/golang/glog"
26+
"go.opencensus.io/stats/view"
27+
28+
"k8s.io/node-problem-detector/cmd/options"
29+
"k8s.io/node-problem-detector/pkg/types"
30+
)
31+
32+
type prometheusExporter struct{}
33+
34+
// NewExporterOrDie creates an exporter to export metrics to Prometheus, panics if error occurs.
35+
func NewExporterOrDie(npdo *options.NodeProblemDetectorOptions) types.Exporter {
36+
if npdo.PrometheusServerPort <= 0 {
37+
return nil
38+
}
39+
40+
addr := net.JoinHostPort(npdo.PrometheusServerAddress, strconv.Itoa(npdo.PrometheusServerPort))
41+
pe, err := prometheus.NewExporter(prometheus.Options{})
42+
if err != nil {
43+
glog.Fatalf("Failed to create Prometheus exporter: %v", err)
44+
}
45+
go func() {
46+
mux := http.NewServeMux()
47+
mux.Handle("/metrics", pe)
48+
if err := http.ListenAndServe(addr, mux); err != nil {
49+
glog.Fatalf("Failed to start Prometheus scrape endpoint: %v", err)
50+
}
51+
}()
52+
view.RegisterExporter(pe)
53+
return &prometheusExporter{}
54+
}
55+
56+
// ExportProblems does nothing.
57+
// Prometheus exporter only exports metrics.
58+
func (pe *prometheusExporter) ExportProblems(status *types.Status) {
59+
return
60+
}

0 commit comments

Comments
 (0)