-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (71 loc) · 1.88 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/kununulabs/newrelic-mysql-reporter/mysql"
"github.com/kununulabs/newrelic-mysql-reporter/yaml"
"github.com/newrelic/newrelic-telemetry-sdk-go/telemetry"
)
// GetURL returns the insights api url based on account id and region
func GetURL(region, account string) string {
return strings.ToLower(fmt.Sprintf(
"https://insights-collector.%s01.nr-data.net/v1/accounts/%s/events",
region,
account,
))
}
func main() {
config, err := yaml.New(os.Args[1])
if err != nil {
panic(err)
}
if len(config.Metrics) < 1 {
panic("No metrics provided. Aborting.")
}
mysqlConnection, err := mysql.GetConnection(
os.Getenv("DATABASE_URL"),
os.Getenv("DATABASE_USERNAME"),
os.Getenv("DATABASE_PASSWORD"),
)
if err != nil {
panic(err)
}
defer mysqlConnection.Close()
harvester, err := telemetry.NewHarvester(
telemetry.ConfigAPIKey(os.Getenv("NEW_RELIC_INSIGHTS_INSERT_KEY")),
telemetry.ConfigBasicAuditLogger(os.Stdout),
telemetry.ConfigBasicDebugLogger(os.Stdout),
telemetry.ConfigBasicErrorLogger(os.Stdout),
telemetry.ConfigEventsURLOverride(GetURL(
os.Getenv("NEW_RELIC_REGION"),
os.Getenv("NEW_RELIC_ACCOUNT_ID"),
)),
telemetry.ConfigHarvestPeriod(0),
)
if err != nil {
panic(err)
}
for _, metric := range config.Metrics {
result := 0
if err := mysqlConnection.QueryRow(metric.Query).Scan(&result); err != nil {
log.Printf("%s: %s\n", metric.Name, err.Error())
continue
}
log.Printf("%s: %d\n", metric.Name, result)
attributes := make(map[string]interface{}, len(config.Attributes)+1)
for key, val := range config.Attributes {
attributes[key] = val
}
attributes["value"] = result
harvester.RecordEvent(telemetry.Event{
EventType: metric.Name,
Timestamp: time.Now(),
Attributes: attributes,
})
}
harvester.HarvestNow(context.Background())
}