Class: Fluent::Plugin::LabelRouterOutput::Route

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

Instance Method Summary collapse

Constructor Details

#initialize(rule, router, registry) ⇒ Route



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/out_label_router.rb', line 71

def initialize(rule, router, registry)
  @router = router
  @matches = rule['matches']
  @tag = rule['tag'].to_s
  @label = rule['@label']
  @metrics_labels = (rule['metrics_labels'].map { |k, v| [k.to_sym, v] }.to_h if rule['metrics_labels'])
  @counter = nil
  unless registry.nil?
      if registry.exist?(:fluentd_router_records_total)
        @counter = registry.get(:fluentd_router_records_total)
      else
        @counter = registry.counter(:fluentd_router_records_total, docstring: "Total number of events routed for the flow", labels: [:flow, :id])
      end
  end
end

Instance Method Details

#emit(tag, time, record) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/fluent/plugin/out_label_router.rb', line 134

def emit(tag, time, record)
  if @tag.empty?
    @router.emit(tag, time, record)
  else
    @router.emit(@tag, time, record)
  end
  @counter&.increment(by: 1, labels: get_labels)
end

#emit_es(tag, es) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/fluent/plugin/out_label_router.rb', line 143

def emit_es(tag, es)
  if @tag.empty?
    @router.emit_stream(tag, es)
  else
    @router.emit_stream(@tag, es)
  end
  # increment the counter for a given label set
  @counter&.increment(by: es.size, labels: get_labels)
end

#filter_select(match, metadata) ⇒ Object

Returns true if filter passes (filter match)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fluent/plugin/out_label_router.rb', line 109

def filter_select(match, )
  # Break on container_name mismatch
  unless match.hosts.empty? || match.hosts.include?([:host])
    return false
  end
  # Break on host mismatch
  unless match.container_names.empty? || match.container_names.include?([:container])
    return false
  end
  # Break if list of namespaces is not empty and does not include actual namespace
  unless match.namespaces.empty? || match.namespaces.include?([:namespace])
    return false
  end
  # Break if list of namespaces is not empty and does not contain any entry that match actual namespace
  unless match.namespaces_regex.empty? || match.namespaces_regex.any? { |pattern| Regexp.new(pattern).match?([:namespace]) }
    return false
  end
  # Break if list of namespace_labels is not empty and does not match actual namespace labels
  if !match.namespace_labels.empty? && !match_labels([:namespace_labels], match.namespace_labels)
    return false
  end

  match_labels([:labels], match.labels)
end

#get_labelsObject



87
88
89
90
# File 'lib/fluent/plugin/out_label_router.rb', line 87

def get_labels
  labels = { 'flow': @label, 'id': "default" }
  !@metrics_labels.nil? ? labels.merge(@metrics_labels) : labels
end

#match?(metadata) ⇒ Boolean

Evaluate selectors We evaluate <match> statements in order:

  1. If match == true and negate == false -> return true

  2. If match == true and negate == true -> return false

  3. If match == false and negate == false -> continue

  4. If match == false and negate == true -> continue

There is no match at all -> return false



99
100
101
102
103
104
105
106
# File 'lib/fluent/plugin/out_label_router.rb', line 99

def match?()
  @matches.each do |match|
    if filter_select(match, )
      return !match.negate
    end
  end
  false
end

#match_labels(input, match) ⇒ Object



153
154
155
# File 'lib/fluent/plugin/out_label_router.rb', line 153

def match_labels(input, match)
  (match.to_a - input.to_a).empty?
end