@@ -29,15 +29,34 @@ import (
29
29
"github.com/aws/amazon-vpc-cni-k8s/cmd/cni-metrics-helper/metrics"
30
30
"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
31
31
"github.com/aws/amazon-vpc-cni-k8s/pkg/publisher"
32
+ "github.com/aws/amazon-vpc-cni-k8s/utils/prometheusmetrics"
32
33
)
33
34
34
35
const (
35
36
appName = "cni-metrics-helper"
37
+
38
+ // metricsPort is the port for prometheus metrics
39
+ metricsPort = 61681
40
+
41
+ // Environment variable to enable the metrics endpoint on 61681
42
+ envEnablePrometheusMetrics = "USE_PROMETHEUS"
43
+ )
44
+
45
+ var (
46
+ prometheusRegistered = false
36
47
)
37
48
38
49
type options struct {
39
- submitCW bool
40
- help bool
50
+ submitCW bool
51
+ help bool
52
+ submitPrometheus bool
53
+ }
54
+
55
+ func prometheusRegister () {
56
+ if ! prometheusRegistered {
57
+ prometheusmetrics .PrometheusRegister ()
58
+ prometheusRegistered = true
59
+ }
41
60
}
42
61
43
62
func main () {
@@ -52,6 +71,7 @@ func main() {
52
71
flags := pflag .NewFlagSet ("" , pflag .ExitOnError )
53
72
flags .AddGoFlagSet (flag .CommandLine )
54
73
flags .BoolVar (& options .submitCW , "cloudwatch" , true , "a bool" )
74
+ flags .BoolVar (& options .submitPrometheus , "prometheus metrics" , false , "a bool" )
55
75
56
76
flags .Usage = func () {
57
77
_ , _ = fmt .Fprintf (os .Stderr , "Usage of %s:\n " , os .Args [0 ])
@@ -85,6 +105,17 @@ func main() {
85
105
}
86
106
}
87
107
108
+ prometheusENV , found := os .LookupEnv (envEnablePrometheusMetrics )
109
+ if found {
110
+ if strings .Compare (prometheusENV , "yes" ) == 0 || strings .Compare (prometheusENV , "true" ) == 0 {
111
+ options .submitPrometheus = true
112
+ prometheusRegister ()
113
+ }
114
+ if strings .Compare (prometheusENV , "no" ) == 0 || strings .Compare (prometheusENV , "false" ) == 0 {
115
+ options .submitPrometheus = false
116
+ }
117
+ }
118
+
88
119
metricUpdateIntervalEnv , found := os .LookupEnv ("METRIC_UPDATE_INTERVAL" )
89
120
if ! found {
90
121
metricUpdateIntervalEnv = "30"
@@ -103,7 +134,7 @@ func main() {
103
134
// should be name/identifier for the cluster if specified
104
135
clusterID , _ := os .LookupEnv ("AWS_CLUSTER_ID" )
105
136
106
- log .Infof ("Starting CNIMetricsHelper. Sending metrics to CloudWatch: %v, LogLevel %s, metricUpdateInterval %d" , options .submitCW , logConfig .LogLevel , metricUpdateInterval )
137
+ log .Infof ("Starting CNIMetricsHelper. Sending metrics to CloudWatch: %v, Prometheus: %v, LogLevel %s, metricUpdateInterval %d" , options .submitCW , options . submitPrometheus , logConfig .LogLevel , metricUpdateInterval )
107
138
108
139
clientSet , err := k8sapi .GetKubeClientSet ()
109
140
if err != nil {
@@ -129,8 +160,13 @@ func main() {
129
160
defer cw .Stop ()
130
161
}
131
162
163
+ if options .submitPrometheus {
164
+ // Start prometheus server
165
+ go prometheusmetrics .ServeMetrics (metricsPort )
166
+ }
167
+
132
168
podWatcher := metrics .NewDefaultPodWatcher (k8sClient , log )
133
- var cniMetric = metrics .CNIMetricsNew (clientSet , cw , options .submitCW , log , podWatcher )
169
+ var cniMetric = metrics .CNIMetricsNew (clientSet , cw , options .submitCW , options . submitPrometheus , log , podWatcher )
134
170
135
171
// metric loop
136
172
for range time .Tick (time .Duration (metricUpdateInterval ) * time .Second ) {
0 commit comments