Skip to content

Commit a3e0414

Browse files
author
Oluwole Fadeyi
authored
Merge pull request #297 from jetstack/add-prometheus
Add prometheus server
2 parents 360d91f + 3daf621 commit a3e0414

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

cmd/agent.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,11 @@ func init() {
124124
false,
125125
"Enables the pprof profiling server on the agent (port: 6060).",
126126
)
127+
agentCmd.PersistentFlags().BoolVarP(
128+
&agent.Prometheus,
129+
"enable-metrics",
130+
"",
131+
false,
132+
"Enables Prometheus metrics server on the agent (port: 8081).",
133+
)
127134
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/maxatome/go-testdeep v1.11.0
2525
github.com/pkg/errors v0.9.1
2626
github.com/pmylund/go-cache v2.1.0+incompatible
27+
github.com/prometheus/client_golang v1.9.0
2728
github.com/sirupsen/logrus v1.8.1
2829
github.com/spf13/cobra v1.4.0
2930
github.com/spf13/pflag v1.0.5

pkg/agent/run.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/jetstack/preflight/pkg/datagatherer"
2424
dgerror "github.com/jetstack/preflight/pkg/datagatherer/error"
2525
"github.com/jetstack/preflight/pkg/version"
26+
"github.com/prometheus/client_golang/prometheus/promhttp"
2627
"github.com/spf13/cobra"
2728
)
2829

@@ -56,6 +57,9 @@ var APIToken string
5657
// Profiling flag enabled pprof endpoints to run on the agent
5758
var Profiling bool
5859

60+
// Prometheus flag enabled Prometheus metrics endpoint to run on the agent
61+
var Prometheus bool
62+
5963
// schema version of the data sent by the agent.
6064
// The new default version is v2.
6165
// In v2 the agent posts data readings using api.gathereredResources
@@ -79,6 +83,17 @@ func Run(cmd *cobra.Command, args []string) {
7983
}
8084
}()
8185
}
86+
if Prometheus {
87+
log.Printf("Prometheus was enabled.\nRunning prometheus server on port :8081")
88+
go func() {
89+
metricsServer := http.NewServeMux()
90+
metricsServer.Handle("/metrics", promhttp.Handler())
91+
err := http.ListenAndServe(":8081", metricsServer)
92+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
93+
log.Fatalf("failed to run prometheus server: %s", err)
94+
}
95+
}()
96+
}
8297

8398
dataGatherers := map[string]datagatherer.DataGatherer{}
8499
var wg sync.WaitGroup

0 commit comments

Comments
 (0)