/
main.go
92 lines (79 loc) · 2.5 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
86
87
88
89
90
91
92
//go:generate goversioninfo
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/newrelic/infra-integrations-sdk/integration"
"github.com/newrelic/infra-integrations-sdk/log"
"github.com/newrelic/nri-postgresql/src/args"
"github.com/newrelic/nri-postgresql/src/collection"
"github.com/newrelic/nri-postgresql/src/connection"
"github.com/newrelic/nri-postgresql/src/inventory"
"github.com/newrelic/nri-postgresql/src/metrics"
)
const (
integrationName = "com.newrelic.postgresql"
)
var (
integrationVersion = "0.0.0"
gitCommit = ""
buildDate = ""
)
func main() {
var args args.ArgumentList
// Create Integration
pgIntegration, err := integration.New(integrationName, integrationVersion, integration.Args(&args))
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
if args.ShowVersion {
fmt.Printf(
"New Relic %s integration Version: %s, Platform: %s, GoVersion: %s, GitCommit: %s, BuildDate: %s\n",
strings.Title(strings.Replace(integrationName, "com.newrelic.", "", 1)),
integrationVersion,
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
runtime.Version(),
gitCommit,
buildDate)
os.Exit(0)
}
// Setup logging with verbose
log.SetupLogging(args.Verbose)
// Validate arguments
if err := args.Validate(); err != nil {
log.Error("Configuration error for args %v: %s", args, err.Error())
os.Exit(1)
}
connectionInfo := connection.DefaultConnectionInfo(&args)
collectionList, err := collection.BuildCollectionList(args, connectionInfo)
if err != nil {
log.Error("Error creating list of entities to collect: %s", err)
os.Exit(1)
}
instance, err := pgIntegration.Entity(fmt.Sprintf("%s:%s", args.Hostname, args.Port), "pg-instance")
if err != nil {
log.Error("Error creating instance entity: %s", err.Error())
os.Exit(1)
}
if args.HasMetrics() {
metrics.PopulateMetrics(connectionInfo, collectionList, instance, pgIntegration, args.Pgbouncer, args.CollectDbLockMetrics, args.CollectBloatMetrics, args.CustomMetricsQuery)
if args.CustomMetricsConfig != "" {
metrics.PopulateCustomMetricsFromFile(connectionInfo, args.CustomMetricsConfig, pgIntegration)
}
}
if args.HasInventory() {
con, err := connectionInfo.NewConnection(connectionInfo.DatabaseName())
if err != nil {
log.Error("Inventory collection failed: error creating connection to PostgreSQL: %s", err.Error())
} else {
defer con.Close()
inventory.PopulateInventory(instance, con)
}
}
if err = pgIntegration.Publish(); err != nil {
log.Error(err.Error())
}
}