@@ -20,6 +20,8 @@ import (
2020 "fmt"
2121 "io/ioutil"
2222 "net/http"
23+ "net/http/pprof"
24+ "os"
2325 "time"
2426
2527 "github.com/aws/aws-sdk-go/service/cloudwatch"
@@ -37,32 +39,38 @@ import (
3739var (
3840 metricsCmd = & cobra.Command {
3941 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.` ,
4245 }
4346
4447 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.` ,
4852 Run : func (cmd * cobra.Command , args []string ) {
4953 getCmd (cmd , args )
5054 },
5155 }
5256
5357 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.` ,
5762 Run : func (cmd * cobra.Command , args []string ) {
5863 displayPromDescCmd (cmd , args )
5964 },
6065 }
6166
6267 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.` ,
6674 Run : func (cmd * cobra.Command , args []string ) {
6775 collectCmd (cmd , args )
6876 },
@@ -76,7 +84,7 @@ func init() {
7684 metricsCmd .AddCommand (metricsCollectCmd )
7785
7886 // 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" )
8088 if err := viper .BindPFlag ("application.metricStatPeriod" , metricsGetCmd .PersistentFlags ().Lookup ("metricStatPeriod" )); err != nil {
8189 log .Error (err )
8290 }
@@ -89,18 +97,17 @@ func init() {
8997 metricsGetCmd .Flags ().StringP ("outFormat" , "" , "yaml" , "Output format for results, possible values: [yaml|json]" )
9098 metricsGetCmd .Flags ().StringP ("outFile" , "" , "" , "Output file where to store the results." )
9199
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" )
94102}
95103
96104func getCmd (cmd * cobra.Command , args []string ) {
97105
98106 loadFromMetricsFiles (& conf )
99107 validateMetricsQueries (& conf )
100108
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 ())
104111
105112 startTime , endTime , period := metrics .GetTimeStamps (time .Now (), conf .Application .MetricStatPeriod , conf .Application .MetricTimeWindow )
106113 log .Debugf ("Start Time: %s" , startTime .Format (time .RFC3339 ))
@@ -110,6 +117,8 @@ func getCmd(cmd *cobra.Command, args []string) {
110117 m := metrics .New (& conf )
111118 mdi := m .GetMetricDataInput (startTime , endTime , period , "" )
112119
120+ log .Debugf ("Metrics queries: %s" , mdi .String ())
121+
113122 sess := awshelper .NewSession (& conf .AWS )
114123 svc := cloudwatch .New (sess )
115124 mdo , err := svc .GetMetricData (mdi )
@@ -151,9 +160,8 @@ func displayPromDescCmd(cmd *cobra.Command, args []string) {
151160 loadFromMetricsFiles (& conf )
152161 validateMetricsQueries (& conf )
153162
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 ())
157165
158166 m := metrics .New (& conf )
159167
@@ -167,9 +175,8 @@ func collectCmd(cmd *cobra.Command, args []string) {
167175 loadFromMetricsFiles (& conf )
168176 validateMetricsQueries (& conf )
169177
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 ())
173180
174181 m := metrics .New (& conf )
175182 sess := awshelper .NewSession (& conf .AWS )
@@ -178,11 +185,53 @@ func collectCmd(cmd *cobra.Command, args []string) {
178185 c := collector .New (& conf , m , cwc )
179186
180187 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+ })
182229
183230 a , _ := cmd .Flags ().GetString ("address" )
184231 p , _ := cmd .Flags ().GetString ("port" )
185232 soc := fmt .Sprintf ("%s:%v" , a , p )
233+
186234 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 ))
188237}
0 commit comments