forked from fluent/fluentd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.rb
166 lines (145 loc) · 4.89 KB
/
agent.rb
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
#
# Fluentd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'fluent/configurable'
require 'fluent/plugin'
require 'fluent/output'
require 'fluent/match'
module Fluent
#
# Agent is a resource unit who manages emittable plugins
#
# Next step: `fluentd/root_agent.rb`
# Next step: `fluentd/label.rb`
#
class Agent
include Configurable
def initialize(log:)
super()
@context = nil
@outputs = []
@filters = []
@lifecycle_control_list = nil
# lifecycle_control_list is the list of plugins in this agent, and ordered
# from plugins which DOES emit, then DOESN'T emit
# (input -> output w/ router -> filter -> output w/o router)
# for start: use this order DESC
# (because plugins which appears later in configurations will receive events from plugins which appears ealier)
# for stop/before_shutdown/shutdown/after_shutdown/close/terminate: use this order ASC
@lifecycle_cache = nil
@log = log
@event_router = EventRouter.new(NoMatchMatch.new(log), self)
@error_collector = nil
end
attr_reader :log
attr_reader :outputs
attr_reader :filters
attr_reader :context
attr_reader :event_router
attr_reader :error_collector
def configure(conf)
super
# initialize <match> and <filter> elements
conf.elements('filter', 'match').each { |e|
next if e.for_another_worker?
pattern = e.arg.empty? ? '**' : e.arg
type = e['@type']
raise ConfigError, "Missing '@type' parameter on <#{e.name}> directive" unless type
if e.name == 'filter'
add_filter(type, pattern, e)
else
add_match(type, pattern, e)
end
}
end
def lifecycle_control_list
return @lifecycle_control_list if @lifecycle_control_list
lifecycle_control_list = {
input: [],
output_with_router: [],
filter: [],
output: [],
}
if self.respond_to?(:inputs)
inputs.each do |i|
lifecycle_control_list[:input] << i
end
end
outputs.each do |o|
if o.has_router?
lifecycle_control_list[:output_with_router] << o
else
lifecycle_control_list[:output] << o
end
end
filters.each do |f|
lifecycle_control_list[:filter] << f
end
@lifecycle_control_list = lifecycle_control_list
end
def lifecycle(desc: false)
kind_list = if desc
[:output, :filter, :output_with_router]
else
[:output_with_router, :filter, :output]
end
kind_list.each do |kind|
list = if desc
lifecycle_control_list[kind].reverse
else
lifecycle_control_list[kind]
end
display_kind = (kind == :output_with_router ? :output : kind)
list.each do |instance|
yield instance, display_kind
end
end
end
def add_match(type, pattern, conf)
log_type = conf.for_this_worker? ? :default : :worker0
log.info log_type, "adding match#{@context.nil? ? '' : " in #{@context}"}", pattern: pattern, type: type
output = Plugin.new_output(type)
output.context_router = @event_router
output.configure(conf)
@outputs << output
if output.respond_to?(:outputs) && output.respond_to?(:multi_output?) && output.multi_output?
# TODO: ruby 2.3 or later: replace `output.respond_to?(:multi_output?) && output.multi_output?` with output&.multi_output?
outputs = if output.respond_to?(:static_outputs)
output.static_outputs
else
output.outputs
end
@outputs.push(*outputs)
end
@event_router.add_rule(pattern, output)
output
end
def add_filter(type, pattern, conf)
log_type = conf.for_this_worker? ? :default : :worker0
log.info log_type, "adding filter#{@context.nil? ? '' : " in #{@context}"}", pattern: pattern, type: type
filter = Plugin.new_filter(type)
filter.context_router = @event_router
filter.configure(conf)
@filters << filter
@event_router.add_rule(pattern, filter)
filter
end
# For handling invalid record
def emit_error_event(tag, time, record, error)
end
def handle_emits_error(tag, es, error)
end
end
end