@@ -20,6 +20,8 @@ import (
20
20
"fmt"
21
21
"io/ioutil"
22
22
"net/http"
23
+ "net/http/pprof"
24
+ "os"
23
25
"time"
24
26
25
27
"github.com/aws/aws-sdk-go/service/cloudwatch"
@@ -37,32 +39,38 @@ import (
37
39
var (
38
40
metricsCmd = & cobra.Command {
39
41
Use : "metrics [commands]" ,
40
- Short : "Useful to debug your metrics" ,
41
- Long : `metrics commands` ,
42
+ Short : "Useful to debug your metrics defined into the queries files." ,
43
+ Long : `The set of commands defined here are usefully to check and test the metrics
44
+ defined into your metrics queries files.` ,
42
45
}
43
46
44
47
metricsGetCmd = & cobra.Command {
45
- Use : "get [options] [args]" ,
46
- Short : "get metrics" ,
47
- Long : `Get metrics from AWS CloudWatch using the metrics queries defined in the [yaml|json] files` ,
48
+ Use : "get" ,
49
+ Short : "Execute a call to AWS CloudWatch API to get metrics defined into the metrics queries files." ,
50
+ Long : `Using this command you can execute a call to AWS CloudWatch API to get metrics
51
+ defined into the metrics queries files.` ,
48
52
Run : func (cmd * cobra.Command , args []string ) {
49
53
getCmd (cmd , args )
50
54
},
51
55
}
52
56
53
57
metricsDisplayPromDescCmd = & cobra.Command {
54
- Use : "display [options] [args]" ,
55
- Short : "display promDesc" ,
56
- Long : `Display prometheus metrics definitions` ,
58
+ Use : "display" ,
59
+ Short : "Display prometheus metrics description build from metrics queries files." ,
60
+ Long : `Using this command you can display prometheus metrics description build from metrics queries files
61
+ into prometheus format, will be the exactly information you will see as a metric name, help string and dimensions.` ,
57
62
Run : func (cmd * cobra.Command , args []string ) {
58
63
displayPromDescCmd (cmd , args )
59
64
},
60
65
}
61
66
62
67
metricsCollectCmd = & cobra.Command {
63
- Use : "collect [options] [args]" ,
64
- Short : "collect metrics" ,
65
- Long : `Collect metrics from AWS CloudWatch using prometheus collector` ,
68
+ Use : "collect" ,
69
+ Short : "Start a basic web server with the collector working and every request is sent to AWS CloudWatch API to collect metrics." ,
70
+ Long : `Using this command you can start a basic web server with the collector working and every request is sent
71
+ to AWS CloudWatch API to collect metrics, this work as the command "server start", but you don't
72
+ need to use it as a replacement of the last command, this will be used only to test the result
73
+ before moving to production state.` ,
66
74
Run : func (cmd * cobra.Command , args []string ) {
67
75
collectCmd (cmd , args )
68
76
},
@@ -76,7 +84,7 @@ func init() {
76
84
metricsCmd .AddCommand (metricsCollectCmd )
77
85
78
86
// Behavior parameters
79
- metricsGetCmd .PersistentFlags ().StringVar (& conf .Application .MetricStatPeriod , "metricStatPeriod" , "5m" , "The AWS Cloudwatch metrics query stats period" )
87
+ metricsGetCmd .PersistentFlags ().StringVar (& conf .Application .MetricStatPeriod , "metricStatPeriod" , "5m" , "The AWS CloudWatch metrics query stats period" )
80
88
if err := viper .BindPFlag ("application.metricStatPeriod" , metricsGetCmd .PersistentFlags ().Lookup ("metricStatPeriod" )); err != nil {
81
89
log .Error (err )
82
90
}
@@ -89,18 +97,17 @@ func init() {
89
97
metricsGetCmd .Flags ().StringP ("outFormat" , "" , "yaml" , "Output format for results, possible values: [yaml|json]" )
90
98
metricsGetCmd .Flags ().StringP ("outFile" , "" , "" , "Output file where to store the results." )
91
99
92
- metricsCollectCmd .Flags ().StringP ("address" , "" , "127.0.0.1" , "Test server address, empty means all addresses" )
93
- metricsCollectCmd .Flags ().StringP ("port" , "" , "8080" , "Test server port" )
100
+ metricsCollectCmd .Flags ().StringP ("address" , "" , "127.0.0.1" , "Server address, empty means all addresses" )
101
+ metricsCollectCmd .Flags ().StringP ("port" , "" , "8080" , "Server port" )
94
102
}
95
103
96
104
func getCmd (cmd * cobra.Command , args []string ) {
97
105
98
106
loadFromMetricsFiles (& conf )
99
107
validateMetricsQueries (& conf )
100
108
101
- if conf .Server .Debug {
102
- log .Debug (conf .ToJSON ())
103
- }
109
+ log .Debugf ("Available configuration: %s" , conf .ToJSON ())
110
+ log .Debugf ("Available Env Vars: %s" , os .Environ ())
104
111
105
112
startTime , endTime , period := metrics .GetTimeStamps (time .Now (), conf .Application .MetricStatPeriod , conf .Application .MetricTimeWindow )
106
113
log .Debugf ("Start Time: %s" , startTime .Format (time .RFC3339 ))
@@ -110,6 +117,8 @@ func getCmd(cmd *cobra.Command, args []string) {
110
117
m := metrics .New (& conf )
111
118
mdi := m .GetMetricDataInput (startTime , endTime , period , "" )
112
119
120
+ log .Debugf ("Metrics queries: %s" , mdi .String ())
121
+
113
122
sess := awshelper .NewSession (& conf .AWS )
114
123
svc := cloudwatch .New (sess )
115
124
mdo , err := svc .GetMetricData (mdi )
@@ -151,9 +160,8 @@ func displayPromDescCmd(cmd *cobra.Command, args []string) {
151
160
loadFromMetricsFiles (& conf )
152
161
validateMetricsQueries (& conf )
153
162
154
- if conf .Server .Debug {
155
- log .Debug (conf .ToJSON ())
156
- }
163
+ log .Debugf ("Available configuration: %s" , conf .ToJSON ())
164
+ log .Debugf ("Available Env Vars: %s" , os .Environ ())
157
165
158
166
m := metrics .New (& conf )
159
167
@@ -167,9 +175,8 @@ func collectCmd(cmd *cobra.Command, args []string) {
167
175
loadFromMetricsFiles (& conf )
168
176
validateMetricsQueries (& conf )
169
177
170
- if conf .Server .Debug {
171
- log .Debug (conf .ToJSON ())
172
- }
178
+ log .Debugf ("Available configuration: %s" , conf .ToJSON ())
179
+ log .Debugf ("Available Env Vars: %s" , os .Environ ())
173
180
174
181
m := metrics .New (& conf )
175
182
sess := awshelper .NewSession (& conf .AWS )
@@ -178,11 +185,53 @@ func collectCmd(cmd *cobra.Command, args []string) {
178
185
c := collector .New (& conf , m , cwc )
179
186
180
187
prometheus .MustRegister (c )
181
- http .Handle ("/metrics" , promhttp .Handler ())
188
+ mux := http .NewServeMux ()
189
+
190
+ // metrics path
191
+ mux .Handle ("/metrics" , promhttp .Handler ())
192
+
193
+ // Debug & Profiling
194
+ mux .HandleFunc ("/debug/pprof/" , pprof .Index )
195
+ mux .HandleFunc ("/debug/pprof/heap" , pprof .Index )
196
+ mux .HandleFunc ("/debug/pprof/mutex" , pprof .Index )
197
+ mux .HandleFunc ("/debug/pprof/goroutine" , pprof .Index )
198
+ mux .HandleFunc ("/debug/pprof/threadcreate" , pprof .Index )
199
+ mux .HandleFunc ("/debug/pprof/block" , pprof .Index )
200
+ mux .HandleFunc ("/debug/pprof/cmdline" , pprof .Cmdline )
201
+ mux .HandleFunc ("/debug/pprof/profile" , pprof .Profile )
202
+ mux .HandleFunc ("/debug/pprof/symbol" , pprof .Symbol )
203
+ mux .HandleFunc ("/debug/pprof/trace" , pprof .Trace )
204
+
205
+ // default root path
206
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
207
+ _ , err := w .Write ([]byte (`<html>
208
+ <head><title>` + appName + ` Exporter</title></head>
209
+ <body>
210
+ <h1>` + appName + ` Exporter</h1>
211
+ <p><a href='/metrics'>Metrics</a></p>
212
+ <h2>Debug and profile</h2>
213
+ <p><a href='/debug/pprof/'>/debug/pprof</a></p>
214
+ <p><a href='/debug/pprof/heap'>/debug/pprof/heap</a></p>
215
+ <p><a href='/debug/pprof/mutex'>/debug/pprof/mutex</a></p>
216
+ <p><a href='/debug/pprof/goroutine'>/debug/pprof/goroutine</a></p>
217
+ <p><a href='/debug/pprof/threadcreate'>/debug/pprof/threadcreate</a></p>
218
+ <p><a href='/debug/pprof/block'>/debug/pprof/block</a></p>
219
+ <p><a href='/debug/pprof/cmdline'>/debug/pprof/cmdline</a></p>
220
+ <p><a href='/debug/pprof/profile'>/debug/pprof/profile</a></p>
221
+ <p><a href='/debug/pprof/symbol'>/debug/pprof/symbol</a></p>
222
+ <p><a href='/debug/pprof/trace'>/debug/pprof/trace</a></p>
223
+ </body>
224
+ </html>` ))
225
+ if err != nil {
226
+ log .Error (err )
227
+ }
228
+ })
182
229
183
230
a , _ := cmd .Flags ().GetString ("address" )
184
231
p , _ := cmd .Flags ().GetString ("port" )
185
232
soc := fmt .Sprintf ("%s:%v" , a , p )
233
+
186
234
log .Infof ("Starting test server on %s" , soc )
187
- log .Fatal (http .ListenAndServe (soc , nil ))
235
+ log .Warn ("Don't use this server as your default server used for prometheus exporter, instead use 'server start' command." )
236
+ log .Fatal (http .ListenAndServe (soc , mux ))
188
237
}
0 commit comments