forked from newrelic/newrelic-pcf-nozzle-tile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logmessage.go
263 lines (238 loc) · 8.17 KB
/
logmessage.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package logmessage
import (
"context"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
"github.com/newrelic/newrelic-pcf-nozzle-tile/app"
"github.com/newrelic/newrelic-pcf-nozzle-tile/cfclient/cfapps"
"github.com/newrelic/newrelic-pcf-nozzle-tile/config"
"github.com/newrelic/newrelic-pcf-nozzle-tile/newrelic/accumulators"
"github.com/newrelic/newrelic-pcf-nozzle-tile/newrelic/attributes"
"github.com/newrelic/newrelic-pcf-nozzle-tile/newrelic/entities"
"github.com/newrelic/newrelic-pcf-nozzle-tile/newrelic/metrics"
"github.com/newrelic/newrelic-pcf-nozzle-tile/newrelic/nrpcf"
)
// Nrevents extends event.Accumulator for
// Firehose LogMessage Envelope Event Types
type Nrevents struct {
accumulators.Accumulator
CFAppManager *cfapps.CFAppManager
filtersEnabled bool
logsEnabled bool
sourceIncFilter []string
sourceExcFilter []string
messageIncFilter []string
messageExcFilter []string
}
// New satisfies event.Accumulator
func (n Nrevents) New() accumulators.Interface {
i := Nrevents{
Accumulator: accumulators.NewAccumulator(
"*loggregator_v2.Envelope_Log",
),
CFAppManager: cfapps.GetInstance(),
}
i.sourceExcFilter = i.Config().GetFilter("LOGMESSAGE_SOURCE_EXCLUDE")
i.sourceIncFilter = i.Config().GetFilter("LOGMESSAGE_SOURCE_INCLUDE")
i.messageExcFilter = i.Config().GetFilter("LOGMESSAGE_MESSAGE_EXCLUDE")
i.messageIncFilter = i.Config().GetFilter("LOGMESSAGE_MESSAGE_INCLUDE")
i.filtersEnabled = i.AreFiltersEnabled()
i.logsEnabled = i.Config().GetBool("LOGS_LOGMESSAGE")
return i
}
// Update satisfies event.Accumulator
// func (n Nrevents) Update(e *events.Envelope) {
func (n Nrevents) Update(e *loggregator_v2.Envelope) {
// Check filters first. Other work can be avoided if filters aren't matched
if n.filtersEnabled {
// Include filters will be checked first, then exclude filters
// Stop processing this envelope if the include filters are not matched
if n.IsIncluded(string(e.GetLog().Payload), e.Tags["source_type"]) == false {
return
}
// Stop processing this envelope if the exclude filters are matched
if n.IsExcluded(string(e.GetLog().Payload), e.Tags["source_type"]) == true {
return
}
}
entity := n.GetEntity(e, nrpcf.GetPCFAttributes(e))
logEntry := attributes.NewAttributes()
// Append application instance attributes to the log entry.
logEntry.AppendAll(n.CFAppManager.GetAppInstanceAttributes(e.GetSourceId(), n.ConvertSourceInstance(e.GetInstanceId())))
// msgContent := e.GetLogMessage().GetMessage()
msgContent := e.GetLog().Payload
// Check to see if NR Logs is enabled for this accumulator
if n.logsEnabled {
// Add log message attributes
logEntry.SetAttribute("message", string(msgContent))
// epoch timestamp from envelope converted to ms
logEntry.SetAttribute("timestamp", (e.GetTimestamp() / (int64(time.Millisecond) / int64(time.Nanosecond))))
logEntry.SetAttribute("app.id", e.GetSourceId())
logEntry.SetAttribute("source.type", n.GetTag(e, "source_type"))
logEntry.SetAttribute("source.instance", e.GetInstanceId())
logEntry.SetAttribute("message.type", n.getLogMessageType(e.GetLog()))
logEntry.SetAttribute("agent.subscription", n.Config().GetString("FIREHOSE_ID"))
logEntry.AppendAll(entity.Attributes())
// Will need to determine what type of insert client is needed based on config.
// Some of the attributes above may not be needed for log messages.
client := nrpcf.GetLogClientForApp(entity)
client.EnqueueLogEntry(context.Background(), logEntry.Marshal())
return
}
// Mesages over 4K in length will be rejected by the Event API. Trim the message before sending.
if len(msgContent) > 4096 {
msgContent = msgContent[0:4095]
logEntry.SetAttribute("log.message.truncated", true)
}
// Add log message attributes
logEntry.SetAttribute("log.message", string(msgContent))
// epoch timestamp from envelope converted to ms
ts := (e.GetTimestamp() / (int64(time.Millisecond) / int64(time.Nanosecond)))
logEntry.SetAttribute("timestamp", ts)
// Continuing to report as log.timestamp as well for backwards compatibility
logEntry.SetAttribute("log.timestamp", ts)
logEntry.SetAttribute("log.app.id", e.GetSourceId())
logEntry.SetAttribute("log.source.type", n.GetTag(e, "source_type"))
logEntry.SetAttribute("log.source.instance", e.GetInstanceId())
logEntry.SetAttribute("log.message.type", n.getLogMessageType(e.GetLog()))
logEntry.SetAttribute(
"eventType",
n.Config().GetString(config.NewRelicEventTypeLogMessage),
)
logEntry.SetAttribute("agent.subscription", n.Config().GetString("FIREHOSE_ID"))
logEntry.AppendAll(entity.Attributes())
// Will need to determine what type of insert client is needed based on config.
// Some of the attributes above may not be needed for log messages.
client := nrpcf.GetInsertClientForApp(entity)
client.EnqueueEvent(context.Background(), logEntry.Marshal())
}
// HarvestMetrics - stub for LogMessages, which are all events...
func (n Nrevents) HarvestMetrics(
entity *entities.Entity,
metric *metrics.Metric,
) {
}
// AreFiltersEnabled populate struct to eliminate potentially unnecessary filter calls
func (n Nrevents) AreFiltersEnabled() bool {
if n.sourceExcFilter == nil && n.sourceIncFilter == nil && n.messageExcFilter == nil && n.messageIncFilter == nil {
app.Get().Log.Debug("Log message filters disabled")
return false
}
app.Get().Log.Debug("Log message filters enabled")
return true
}
// IsIncluded ...
func (n Nrevents) IsIncluded(
logMessage string,
logSource string,
) bool {
matchFound := false
srcMatchFound := n.IsIncludedLogSource(logSource)
msgMatchFound := n.IsIncludedLogMessage(logMessage)
// If both a source and message include filter are set, both must be matched to include log message.
if srcMatchFound && msgMatchFound {
matchFound = true
}
return matchFound
}
// IsExcluded ...
func (n Nrevents) IsExcluded(
logMessage string,
logSource string,
) bool {
// First exclude filter is log source - if matched, exclude this log entry.
if n.IsExcludedLogSource(logSource) {
return true
}
// Second exclude filter is a message exclude - if matched, exclude this log entry.
if n.IsExcludedLogMessage(logMessage) {
return true
}
return false
}
// IsExcludedLogSource determines if envelopes with this log source should be dropped.
func (n Nrevents) IsExcludedLogSource(
logSource string,
) bool {
if n.sourceExcFilter != nil {
for _, filter := range n.sourceExcFilter {
if strings.Compare(strings.TrimSpace(filter), logSource) == 0 {
return true
}
}
}
return false
}
// IsExcludedLogMessage determines if envelopes with this log message should be dropped.
func (n Nrevents) IsExcludedLogMessage(
logMessage string,
) bool {
if n.messageExcFilter != nil {
for _, filter := range n.messageExcFilter {
if strings.Contains(logMessage, strings.TrimSpace(filter)) {
return true
}
}
}
return false
}
// IsIncludedLogSource determines if envelopes with this log source should be included.
func (n Nrevents) IsIncludedLogSource(
logSource string,
) bool {
if n.sourceIncFilter != nil {
for _, filter := range n.sourceIncFilter {
if strings.Compare(strings.TrimSpace(filter), logSource) == 0 {
return true
}
}
} else {
return true
}
return false
}
// IsIncludedLogMessage determines if envelopes with this log message should be included.
func (n Nrevents) IsIncludedLogMessage(
logMessage string,
) bool {
if n.messageIncFilter != nil {
for _, filter := range n.messageIncFilter {
if strings.Contains(logMessage, strings.TrimSpace(filter)) {
return true
}
}
} else {
return true
}
return false
}
// ConvertSourceInstance from a string to int32
func (n Nrevents) ConvertSourceInstance(
i string,
) int32 {
if num, err := strconv.ParseInt(i, 10, 32); err == nil {
return int32(num)
}
return 0
}
// getLogMessageType returns the message type (OUT or ERR)
func (n Nrevents) getLogMessageType(log *loggregator_v2.Log) string {
if log.Type == loggregator_v2.Log_OUT {
return "OUT"
}
return "ERR"
}
// GetTag ...
func (n Nrevents) GetTag(
e *loggregator_v2.Envelope,
ta string,
) string {
if tv, ok := e.Tags[ta]; ok {
return tv
}
return ""
}