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



119
120
121
122
# File 'lib/fluent/plugin_helper/metrics.rb', line 119

def after_shutdown
  metrics_operate(:after_shutdown)
  super
end

#before_shutdownObject



109
110
111
112
# File 'lib/fluent/plugin_helper/metrics.rb', line 109

def before_shutdown
  metrics_operate(:before_shutdown)
  super
end

#closeObject



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

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
77
78
79
80
81
82
83
# 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[:worker_id] = fluentd_worker_id.to_s
  end
  labels[: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

  # define the getter method for the calling instance.
  singleton_class.module_eval do
    unless method_defined?(name)
      define_method(name) { metrics.get }
    end
  end

  metrics
end

#metrics_operate(method_name, &block) ⇒ Object



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

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



114
115
116
117
# File 'lib/fluent/plugin_helper/metrics.rb', line 114

def shutdown
  metrics_operate(:shutdown)
  super
end

#startObject



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

def start
  super

  metrics_operate(:start)
  @_metrics_started = true
end

#stopObject



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

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

#terminateObject



129
130
131
132
133
# File 'lib/fluent/plugin_helper/metrics.rb', line 129

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