Skip to content

Commit 7e0ef9d

Browse files
authored
Merge pull request #3 from simplifi/logging_improvements
Improve logging output, add debug
2 parents 77c34a2 + a730d68 commit 7e0ef9d

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

cmd/anemometer/cli/start.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
var (
1212
configPath string
13+
debug bool
1314
)
1415

1516
var startCmd = &cobra.Command{
@@ -27,6 +28,12 @@ func init() {
2728
"c",
2829
"/etc/anemometer.yml",
2930
"the full path to the yaml config file, default: /etc/anemometer.yml")
31+
startCmd.Flags().BoolVarP(
32+
&debug,
33+
"debug",
34+
"d",
35+
false,
36+
"enable debugging output in the logs, default: false")
3037
rootCmd.AddCommand(startCmd)
3138
}
3239

@@ -47,7 +54,7 @@ func start() {
4754
if err != nil {
4855
log.Panicf("ERROR: Failed to start monitor '%v': %v", mtConfig.Name, err)
4956
}
50-
go mt.Start()
57+
go mt.Start(debug)
5158
}
5259
// Block until something kills the process
5360
<-exit

pkg/anemometer/monitor/monitor.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ func createStatsdClient(address string, tags []string) (*statsd.Client, error) {
7878
}
7979

8080
// Start the Monitor
81-
func (m *Monitor) Start() {
81+
func (m *Monitor) Start(debug bool) {
8282
for {
83-
log.Printf("INFO: Sleeping for %d seconds", m.sleepDuration)
83+
log.Printf("INFO: [%s] Sleeping for %d seconds", m.name, m.sleepDuration)
8484
time.Sleep(time.Duration(m.sleepDuration) * time.Second)
8585

8686
// Execute our query
8787
rows, err := m.databaseConn.Query(m.sql)
8888
if err != nil {
89-
log.Printf("ERROR: %v", err)
89+
log.Printf("ERROR: [%s] %v", m.name, err)
9090
sendErrorMetric(m.statsdClient, m.name)
9191
continue
9292
}
@@ -98,15 +98,15 @@ func (m *Monitor) Start() {
9898
// Convert our result row into a map
9999
rowMap, err := rowsToMap(cols, rows)
100100
if err != nil {
101-
log.Printf("ERROR: %v", err)
101+
log.Printf("ERROR: [%s] %v", m.name, err)
102102
sendErrorMetric(m.statsdClient, m.name)
103103
continue
104104
}
105105

106106
// Grab the metric column from the results and convert it
107107
metric, err := getMetric(rowMap)
108108
if err != nil {
109-
log.Printf("ERROR: %v", err)
109+
log.Printf("ERROR: [%s] %v", m.name, err)
110110
sendErrorMetric(m.statsdClient, m.name)
111111
continue
112112
}
@@ -115,8 +115,17 @@ func (m *Monitor) Start() {
115115
tags := getTags(rowMap)
116116

117117
// Push the metric to Datadog
118+
if debug {
119+
log.Printf(
120+
"DEBUG: [%s] Publishing metric - Name: %s, Value: %f, Tags: %v",
121+
m.name,
122+
m.metric,
123+
metric,
124+
tags,
125+
)
126+
}
118127
if err = m.statsdClient.Gauge(m.metric, metric, tags, 1); err != nil {
119-
log.Printf("ERROR: %v", err)
128+
log.Printf("ERROR: [%s] %v", m.name, err)
120129
sendErrorMetric(m.statsdClient, m.name)
121130
}
122131
}

0 commit comments

Comments
 (0)