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



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

def configure(conf)
  super
  @registry = (::Prometheus::Client.registry if @metrics)
  @route_map = Hash.new { |h, k| h[k] = Set.new }
  @rwlock = Concurrent::ReadWriteLock.new
  @routers = []
  @default_router = nil
  @routes.each do |rule|
    route_router = event_emitter_router(rule['@label'])
    @routers << Route.new(rule, route_router, @registry)
  end

  if @default_route != '' or @default_tag != ''
    default_rule = { 'matches' => nil, 'tag' => @default_tag, '@label' => @default_route, 'metrics_labels' => @default_metrics_labels }
    @default_router = Route.new(default_rule, event_emitter_router(@default_route), @registry)
  end

  @access_to_labels = record_accessor_create("$.kubernetes.labels")
  @access_to_namespace_labels = record_accessor_create("$.kubernetes.namespace_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



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

def process(tag, es)
  if @sticky_tags
    @rwlock.with_read_lock {
      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,
                       namespace_labels: @access_to_namespace_labels.call(record).to_h,
                       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
          @rwlock.with_write_lock {
            @route_map[tag].add(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
        @rwlock.with_write_lock {
          @route_map[tag].add(@default_router)
        }
      end
      if @batch
        event_stream[@default_router].add(time, record)
      else
        @default_router.emit(tag, time, record.dup)
      end
    end
  end
  if @batch
    event_stream.each do |r, es|
      r.emit_es(tag, es.dup)
    end
  end
end