-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (138 loc) · 5.79 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
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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/hpcloud/tail"
"github.com/rabobank/nginx2sfx/conf"
"github.com/rabobank/nginx2sfx/model"
"io"
"log"
"net/http"
"os"
"time"
)
type mapKey struct {
statusCode string
uri string
method string
serverProtocol string
metricName string
}
var (
lastProcessed int64
metricBuffer = map[mapKey]float64{}
tlsConfig = tls.Config{InsecureSkipVerify: conf.SkipSslValidation}
client = http.Client{Transport: &http.Transport{TLSClientConfig: &tlsConfig}, Timeout: time.Duration(conf.SfxTimeout) * time.Second}
)
func main() {
fmt.Printf("nginx2sfx, CommitHash %s, Version %s\n", conf.COMMIT, conf.VERSION)
// check envvars (and exit if incomplete)
conf.EnvironmentComplete()
// check for existence of the file:
for {
if _, err := os.Stat(conf.InputFile); os.IsNotExist(err) {
fmt.Printf("waiting for file %s to appear...\n", conf.InputFile)
time.Sleep(5 * time.Second)
} else {
break
}
}
if fileChannel, err := tail.TailFile(conf.InputFile, tail.Config{Follow: true, Logger: tail.DiscardingLogger}); err != nil {
panic(err)
} else {
lineCount := 0 // used to keep track when to send a batch of metrics to Sfx
totalLineCount := 0 // used to keep track when to truncate the file
for line := range fileChannel.Lines {
lineCount++
totalLineCount++
updateMetrics(line.Text)
if lineCount >= conf.BatchSize || time.Now().Unix()-lastProcessed >= conf.BatchInterval {
send2sfx()
lineCount = 0
lastProcessed = time.Now().Unix()
metricBuffer = make(map[mapKey]float64) // clear the buffer, so all metrics start with 0 again
// truncate the file if it gets too big
if totalLineCount > 5000 {
totalLineCount = 0
if err = os.Truncate(conf.InputFile, 0); err != nil {
log.Printf("Failed to truncate file %s: %s", conf.InputFile, err)
}
}
}
}
}
}
func updateMetrics(logLine string) {
nginxLog := model.LogLine{}
if err := json.Unmarshal([]byte(logLine), &nginxLog); err != nil {
fmt.Printf("failed to parse logline: %s : %s\n", logLine, err)
} else {
if !conf.UriAsDimension {
nginxLog.Uri = "" // if we don't want to use the uri as a dimension, we set it to an empty string
}
// The statuscode, uri, method and serverProtocol can vary, so they are in the key of the map. When sending the metrics to Sfx we also add all the "static" dimensions like app_name, space_name...
if element, found := metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_count"}]; found {
metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_count"}] = element + 1
} else {
metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_count"}] = 1
}
if element, found := metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_totalTime"}]; found {
metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_totalTime"}] = element + nginxLog.RequestTime
} else {
metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_totalTime"}] = nginxLog.RequestTime
}
if element, found := metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_totalBytes"}]; found {
metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_totalBytes"}] = element + nginxLog.BodyBytesSent
} else {
metricBuffer[mapKey{nginxLog.Status, nginxLog.Uri, nginxLog.Method, nginxLog.ServerProtocol, "nginx_http_requests_totalBytes"}] = nginxLog.BodyBytesSent
}
}
}
func send2sfx() {
counterMetrics := model.CounterMetrics{}
var dimensions model.Dimensions
for ix, metric := range metricBuffer {
if conf.UriAsDimension {
dimensions = model.Dimensions{Uri: ix.uri, Method: ix.method, ServerProtocol: ix.serverProtocol, StatusCode: ix.statusCode, Cfenv: conf.CfEnv, CfInstanceIndex: conf.CfInstanceIndex, CfAppName: conf.VcapApp.Name, CfAppId: conf.VcapApp.ApplicationID, CfSpaceName: conf.VcapApp.SpaceName, CfOrgName: conf.VcapApp.OrganizationName}
} else {
dimensions = model.Dimensions{Method: ix.method, ServerProtocol: ix.serverProtocol, StatusCode: ix.statusCode, Cfenv: conf.CfEnv, CfInstanceIndex: conf.CfInstanceIndex, CfAppName: conf.VcapApp.Name, CfAppId: conf.VcapApp.ApplicationID, CfSpaceName: conf.VcapApp.SpaceName, CfOrgName: conf.VcapApp.OrganizationName}
}
sfxMetric := model.CounterMetric{
Metric: ix.metricName,
Value: metric,
Dimensions: dimensions,
}
counterMetrics.Counter = append(counterMetrics.Counter, sfxMetric)
}
postBodyBytes, err := json.Marshal(counterMetrics)
if err != nil {
fmt.Println(err)
return
}
if conf.Debug {
fmt.Printf("sending to SignalFx: %s\n", string(postBodyBytes))
}
req, _ := http.NewRequest("POST", conf.SfxUrl, bytes.NewBuffer(postBodyBytes))
req.Header.Add("X-SF-Token", conf.SfxToken)
req.Header.Add("Content-Type", "application/json")
var resp *http.Response
resp, err = client.Do(req)
if err == nil && resp != nil && resp.StatusCode == http.StatusOK {
// do nothing, all went well
} else {
if resp != nil {
body, _ := io.ReadAll(resp.Body)
errString := fmt.Sprintf("failed to send log, response from POST to %s: %s\n%s", conf.SfxUrl, resp.Status, body)
fmt.Println(errString)
err = errors.New(errString)
} else {
errString := fmt.Sprintf("failed to send log, response from POST to %s: %v", conf.SfxUrl, err)
fmt.Println(errString)
err = errors.New(errString)
}
}
lastProcessed = time.Now().Unix()
}