Class: Fluent::Plugin::LabelRouterOutput

Inherits:
BareOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_label_router.rb

Defined Under Namespace

Classes: Route

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/fluent/plugin/out_label_router.rb', line 174

def configure(conf)
  super
  @route_map = Hash.new { |h, k| h[k] = Array.new }
  @routers = []
  @default_router = nil
  @routes.each do |rule|
    route_router = event_emitter_router(rule['@label'])
    puts rule
    @routers << Route.new(rule.matches, rule.tag.to_s, route_router)
  end

  if @default_route != '' or @default_tag != ''
    @default_router = Route.new(nil, @default_tag, event_emitter_router(@default_route))
  end

  @access_to_labels = record_accessor_create("$.kubernetes.labels")
  @access_to_namespace = record_accessor_create("$.kubernetes.namespace_name")
  @access_to_host = record_accessor_create("$.kubernetes.host")
  @access_to_container_name = record_accessor_create("$.kubernetes.container_name")

  @batch = @emit_mode == :batch
end

#process(tag, es) ⇒ Object



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
# File 'lib/fluent/plugin/out_label_router.rb', line 126

def process(tag, es)
  if @sticky_tags
    if @route_map.has_key?(tag)
      # We already matched with this tag send events to the routers
      @route_map[tag].each do |r|
        r.emit_es(tag, es.dup)
      end
      return
    end
  end
  event_stream = Hash.new {|h, k| h[k] = Fluent::MultiEventStream.new }
  es.each do |time, record|
     = { labels: @access_to_labels.call(record).to_h,
                       namespace: @access_to_namespace.call(record).to_s,
                       container: @access_to_container_name.call(record).to_s,
                       host: @access_to_host.call(record).to_s}
    orphan_record = true
    @routers.each do |r|
      if r.match?()
        orphan_record = false
        if @sticky_tags
          @route_map[tag].push(r)
        end
        if @batch
          event_stream[r].add(time, record)
        else
          r.emit(tag, time, record.dup)
        end
      end
    end
    if !@default_router.nil? && orphan_record
      if @sticky_tags
        @route_map[tag].push(@default_router)
      end
      if @batch
        event_stream[@default_router].add(time, record)
      else
        @default_router.emit(tag, time, record.dup)
      end
    end
    if @batch
      event_stream.each do |r, es|
        r.emit_es(tag, es.dup)
      end
    end
  end
end