-
Notifications
You must be signed in to change notification settings - Fork 236
/
http.go
38 lines (31 loc) · 1.14 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package metrics
import (
"net/http"
"time"
"github.com/Azure/azure-container-networking/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
httpPort = ":10091"
nodeMetricsPath = "/node-metrics"
clusterMetricsPath = "/cluster-metrics"
)
var started = false
// StartHTTP starts a HTTP server in a Go routine with endpoint on port 8000. Metrics are exposed on the endpoint /metrics.
// By being exposed, the metrics can be scraped by a Prometheus Server or Container Insights.
// The function will pause for delayAmountAfterStart seconds after starting the HTTP server for the first time.
func StartHTTP(delayAmountAfterStart int) {
if started {
return
}
started = true
http.Handle(nodeMetricsPath, getHandler(true))
http.Handle(clusterMetricsPath, getHandler(false))
log.Logf("Starting Prometheus HTTP Server")
go http.ListenAndServe(httpPort, nil)
time.Sleep(time.Second * time.Duration(delayAmountAfterStart))
}
// getHandler returns the HTTP handler for the metrics endpoint
func getHandler(isNodeLevel bool) http.Handler {
return promhttp.HandlerFor(getRegistry(isNodeLevel), promhttp.HandlerOpts{})
}