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

Returns a new instance of Route.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fluent/plugin/out_label_router.rb', line 66

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



124
125
126
127
128
129
130
131
# File 'lib/fluent/plugin/out_label_router.rb', line 124

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



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

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)



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fluent/plugin/out_label_router.rb', line 107

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

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

#get_labelsObject



82
83
84
85
# File 'lib/fluent/plugin/out_label_router.rb', line 82

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

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fluent/plugin/out_label_router.rb', line 94

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

#match_labels(input, match) ⇒ Object



143
144
145
# File 'lib/fluent/plugin/out_label_router.rb', line 143

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