Module: Fluent::PluginHelper::Metrics

Includes:
SystemConfig::Mixin
Defined in:
lib/fluent/plugin_helper/metrics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Instance Attribute Details

#_metricsObject (readonly)

For tests.



31
32
33
# File 'lib/fluent/plugin_helper/metrics.rb', line 31

def _metrics
  @_metrics
end

Instance Method Details

#after_shutdownObject



112
113
114
115
# File 'lib/fluent/plugin_helper/metrics.rb', line 112

def after_shutdown
  metrics_operate(:after_shutdown)
  super
end

#before_shutdownObject



102
103
104
105
# File 'lib/fluent/plugin_helper/metrics.rb', line 102

def before_shutdown
  metrics_operate(:before_shutdown)
  super
end

#closeObject



117
118
119
120
# File 'lib/fluent/plugin_helper/metrics.rb', line 117

def close
  metrics_operate(:close)
  super
end

#configure(conf) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fluent/plugin_helper/metrics.rb', line 39

def configure(conf)
  super

  @plugin_type_or_id = if self.plugin_id_configured?
                         self.plugin_id
                       else
                         if type = (conf["@type"] || conf["type"])
                           "#{type}.#{self.plugin_id}"
                         else
                           "#{self.class.to_s.split("::").last.downcase}.#{self.plugin_id}"
                         end
                       end
end

#initializeObject



33
34
35
36
37
# File 'lib/fluent/plugin_helper/metrics.rb', line 33

def initialize
  super
  @_metrics_started = false
  @_metrics = {} # usage => metrics_state
end

#metrics_create(namespace: "fluentd", subsystem: "metrics", name:, help_text:, labels: {}, prefer_gauge: false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fluent/plugin_helper/metrics.rb', line 53

def metrics_create(namespace: "fluentd", subsystem: "metrics", name:, help_text:, labels: {}, prefer_gauge: false)
  metrics = if system_config.metrics
              Fluent::Plugin.new_metrics(system_config.metrics[:@type], parent: self)
            else
              Fluent::Plugin.new_metrics(Fluent::Plugin::Metrics::DEFAULT_TYPE, parent: self)
            end
  config = if system_config.metrics
             system_config.metrics.corresponding_config_element
           else
             Fluent::Config::Element.new('metrics', '', {'@type' => Fluent::Plugin::Metrics::DEFAULT_TYPE}, [])
           end
  metrics.use_gauge_metric = prefer_gauge
  metrics.configure(config)
  # For multi workers environment, cmetrics should be distinguish with static labels.
  if Fluent::Engine.system_config.workers > 1
    labels.merge!(worker_id: fluentd_worker_id.to_s)
  end
  labels.merge!(plugin: @plugin_type_or_id)
  metrics.create(namespace: namespace, subsystem: subsystem, name: name, help_text: help_text, labels: labels)

  @_metrics["#{@plugin_type_or_id}_#{namespace}_#{subsystem}_#{name}"] = metrics

  metrics
end

#metrics_operate(method_name, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/fluent/plugin_helper/metrics.rb', line 78

def metrics_operate(method_name, &block)
  @_metrics.each_pair do |key, m|
    begin
      block.call(s) if block_given?
      m.__send__(method_name)
    rescue => e
      log.error "unexpected error while #{method_name}", key: key, metrics: m, error: e
    end
  end
end

#shutdownObject



107
108
109
110
# File 'lib/fluent/plugin_helper/metrics.rb', line 107

def shutdown
  metrics_operate(:shutdown)
  super
end

#startObject



89
90
91
92
93
94
# File 'lib/fluent/plugin_helper/metrics.rb', line 89

def start
  super

  metrics_operate(:start)
  @_metrics_started = true
end

#stopObject



96
97
98
99
100
# File 'lib/fluent/plugin_helper/metrics.rb', line 96

def stop
  super
  # timer stops automatically in super
  metrics_operate(:stop)
end

#terminateObject



122
123
124
125
126
# File 'lib/fluent/plugin_helper/metrics.rb', line 122

def terminate
  metrics_operate(:terminate)
  @_metrics = {}
  super
end