-
Notifications
You must be signed in to change notification settings - Fork 181
/
logs.go
124 lines (104 loc) · 3.91 KB
/
logs.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package logs
import (
"github.com/aws/amazon-cloudwatch-agent/translator"
"github.com/aws/amazon-cloudwatch-agent/translator/jsonconfig/mergeJsonRule"
"github.com/aws/amazon-cloudwatch-agent/translator/jsonconfig/mergeJsonUtil"
parent "github.com/aws/amazon-cloudwatch-agent/translator/translate"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/util"
)
type Rule translator.Rule
var ChildRule = map[string]Rule{}
const (
SectionKey = "logs"
Output_Cloudwatch_Logs = "cloudwatchlogs"
)
func GetCurPath() string {
curPath := parent.GetCurPath() + SectionKey + "/"
return curPath
}
func RegisterRule(fieldname string, r Rule) {
ChildRule[fieldname] = r
}
type Logs struct {
FileStateFolder string
MetadataInfo map[string]string
}
var GlobalLogConfig = Logs{}
func (l *Logs) ApplyRule(input interface{}) (returnKey string, returnVal interface{}) {
im := input.(map[string]interface{})
result := map[string]interface{}{}
inputs := map[string]interface{}{}
processors := map[string]interface{}{}
cloudwatchConfig := map[string]interface{}{}
GlobalLogConfig.MetadataInfo = util.GetMetadataInfo(util.Ec2MetadataInfoProvider)
//Check if this plugin exist in the input instance
//If not, not process
if _, ok := im[SectionKey]; !ok {
returnKey = ""
returnVal = ""
translator.AddInfoMessages("", "No log configuration found.")
} else {
//If yes, process it
for _, rule := range ChildRule {
key, val := rule.ApplyRule(im[SectionKey])
//If key == "", then no instance of this class in input
if key != "" {
if key == "metrics_collected" {
if metricsResult, ok := val.(map[string]map[string]interface{}); ok {
if metricsInputs, ok := metricsResult["inputs"]; ok {
for k, v := range metricsInputs {
inputs[k] = v
}
}
if metricsProcessors, ok := metricsResult["processors"]; ok {
for k, v := range metricsProcessors {
processors[k] = v
}
}
}
} else if key == "inputs" {
// inputs here are coming from logs_collected
inputs = translator.MergeTwoUniqueMaps(inputs, val.(map[string]interface{}))
} else if key == Output_Cloudwatch_Logs {
cloudwatchConfig = translator.MergeTwoUniqueMaps(cloudwatchConfig, val.(map[string]interface{}))
}
}
}
cloudwatchInfo := map[string]interface{}{}
cloudwatchInfo["cloudwatchlogs"] = []interface{}{cloudwatchConfig}
result["outputs"] = cloudwatchInfo
if len(inputs) > 0 {
result["inputs"] = inputs
}
if len(processors) > 0 {
result["processors"] = processors
}
// TODO the processor logic should be refined to be opt-in instead of opt-out, since the processor is more specific inside container
// Calling SetMetricPath to set the "section tag" to all inputs plugins, and specify "section tag" handling in all processors/output plugin.
translator.SetMetricPath(result, SectionKey)
if _, ok := inputs["k8sapiserver"]; ok {
// Calling SetMetricPathForOneInput is to set an override for a single input plugin, all the metrics generated by the single input plugin
// will only go through a specified list of processor plugins.
translator.SetMetricPathForOneInput(result, SectionKey, "k8sapiserver", []string{"k8sdecorator"})
}
if _, ok = inputs["socket_listener"]; ok {
translator.SetMetricPathForOneInput(result, SectionKey, "socket_listener", []string{})
}
returnKey = SectionKey
returnVal = result
}
return
}
var MergeRuleMap = map[string]mergeJsonRule.MergeRule{}
func (l *Logs) Merge(source map[string]interface{}, result map[string]interface{}) {
mergeJsonUtil.MergeMap(source, result, SectionKey, MergeRuleMap, GetCurPath())
}
func init() {
l := new(Logs)
parent.RegisterLinuxRule(SectionKey, l)
parent.RegisterDarwinRule(SectionKey, l)
parent.RegisterWindowsRule(SectionKey, l)
mergeJsonUtil.MergeRuleMap[SectionKey] = l
}