forked from newrelic/nri-flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.go
151 lines (136 loc) · 4.36 KB
/
integration.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2019 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package integration
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/newrelic/nri-flex/internal/config"
"github.com/newrelic/nri-flex/internal/discovery"
"github.com/newrelic/nri-flex/internal/load"
"github.com/newrelic/nri-flex/internal/outputs"
"github.com/sirupsen/logrus"
)
// RunFlex runs flex
// if mode is "" run in default mode
func RunFlex(mode string) {
verboseLogging := os.Getenv("VERBOSE")
if load.Args.Verbose || verboseLogging == "true" || verboseLogging == "1" {
load.Logrus.SetLevel(logrus.TraceLevel)
}
load.Logrus.WithFields(logrus.Fields{
"version": load.IntegrationVersion,
"GOOS": runtime.GOOS,
"GOARCH": runtime.GOARCH,
}).Info(load.IntegrationName)
// store config ymls
configs := []load.Config{}
switch mode {
case "lambda":
addConfigsFromPath("/var/task/pkg/flexConfigs/", &configs)
if config.SyncGitConfigs("/tmp/") {
addConfigsFromPath("/tmp/", &configs)
}
default:
// running as default
config.SyncGitConfigs("")
if load.Args.ConfigFile != "" {
addSingleConfigFile(load.Args.ConfigFile, &configs)
} else {
addConfigsFromPath(load.Args.ConfigDir, &configs)
}
if load.Args.ContainerDiscovery || load.Args.Fargate {
discovery.Run(&configs)
}
}
if load.ContainerID == "" && mode != "test" && mode != "lambda" && runtime.GOOS != "darwin" {
discovery.Processes()
}
load.Logrus.Info(fmt.Sprintf("flex: config files loaded %d", len(configs)))
config.RunFiles(&configs)
outputs.StatusSample()
if load.Args.InsightsURL != "" && load.Args.InsightsAPIKey != "" {
outputs.SendToInsights()
} else if load.Args.MetricAPIUrl != "" && (load.Args.InsightsAPIKey != "" || load.Args.MetricAPIKey != "") && len(load.MetricsStore.Data) > 0 {
outputs.SendToMetricAPI()
} else if len(load.MetricsStore.Data) > 0 && (load.Args.MetricAPIUrl == "" || (load.Args.InsightsAPIKey == "" || load.Args.MetricAPIKey == "")) {
load.Logrus.Debug("flex: metric_api is being used, but metric url and/or key has not been set", len(configs))
}
}
func addSingleConfigFile(configFile string, configs *[]load.Config) {
file, err := os.Stat(configFile)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"err": err,
"file": configFile,
}).Fatal("config: failed to read")
}
path := strings.Replace(filepath.FromSlash(configFile), file.Name(), "", -1)
files := []os.FileInfo{file}
config.LoadFiles(configs, files, path)
}
func addConfigsFromPath(path string, configs *[]load.Config) {
configPath := filepath.FromSlash(path)
files, err := ioutil.ReadDir(configPath)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"err": err,
"dir": path,
}).Fatal("config: failed to read")
}
config.LoadFiles(configs, files, configPath)
}
// SetDefaults set flex defaults
func SetDefaults() {
load.Logrus.Out = os.Stderr
load.FlexStatusCounter.M = make(map[string]int)
load.FlexStatusCounter.M["EventCount"] = 0
load.FlexStatusCounter.M["EventDropCount"] = 0
load.FlexStatusCounter.M["ConfigsProcessed"] = 0
}
// SetEnvs set environment variable argument overrides
func SetEnvs() {
load.AWSExecutionEnv = os.Getenv("AWS_EXECUTION_ENV")
gitService := os.Getenv("GIT_SERVICE")
if gitService != "" {
load.Args.GitService = gitService
}
gitRepo := os.Getenv("GIT_REPO")
if gitRepo != "" {
load.Args.GitRepo = gitRepo
load.Args.GitToken = os.Getenv("GIT_TOKEN")
load.Args.GitUser = os.Getenv("GIT_USER")
}
insightsAPIKey := os.Getenv("INSIGHTS_API_KEY")
if insightsAPIKey != "" {
load.Args.InsightsAPIKey = insightsAPIKey
load.Args.InsightsURL = os.Getenv("INSIGHTS_URL")
}
if os.Getenv("KUBERNETES_SERVICE_HOST") != "" {
load.IsKubernetes = true
}
eventLimit, err := strconv.Atoi(os.Getenv("EVENT_LIMIT"))
if err == nil && eventLimit > 0 {
load.Args.EventLimit = eventLimit
}
configSync, err := strconv.ParseBool(os.Getenv("PROCESS_CONFIGS_SYNC"))
if err == nil && configSync {
load.Args.ProcessConfigsSync = configSync
}
fargate, err := strconv.ParseBool(os.Getenv("FARGATE"))
if err == nil && fargate {
load.Args.Fargate = fargate
}
cd, err := strconv.ParseBool(os.Getenv("CONTAINER_DISCOVERY"))
if err == nil && cd {
load.Args.ContainerDiscovery = cd
}
load.Args.MetricAPIUrl = os.Getenv("METRIC_API_URL")
load.Args.MetricAPIKey = os.Getenv("METRIC_API_KEY")
}