Class: Fluent::Plugin::Prometheus::Metric

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

Direct Known Subclasses

Counter, Gauge, Histogram, Summary

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, registry, labels) ⇒ Metric

Returns a new instance of Metric.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/fluent/plugin/prometheus.rb', line 218

def initialize(element, registry, labels)
  ['name', 'desc'].each do |key|
    if element[key].nil?
      raise ConfigError, "metric requires '#{key}' option"
    end
  end
  @type = element['type']
  @name = element['name']
  @key = element['key']
  @desc = element['desc']
  element['initialized'].nil? ? @initialized = false : @initialized = element['initialized'] == 'true'
  
  @base_labels = Fluent::Plugin::Prometheus.parse_labels_elements(element)
  @base_labels = labels.merge(@base_labels)

  if @initialized
    @base_initlabels = Fluent::Plugin::Prometheus.parse_initlabels_elements(element, @base_labels)
  end
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



216
217
218
# File 'lib/fluent/plugin/prometheus.rb', line 216

def desc
  @desc
end

#keyObject (readonly)

Returns the value of attribute key.



215
216
217
# File 'lib/fluent/plugin/prometheus.rb', line 215

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



214
215
216
# File 'lib/fluent/plugin/prometheus.rb', line 214

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



213
214
215
# File 'lib/fluent/plugin/prometheus.rb', line 213

def type
  @type
end

Class Method Details

.get(registry, name, type, docstring) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/fluent/plugin/prometheus.rb', line 263

def self.get(registry, name, type, docstring)
  metric = registry.get(name)

  # should have same type, docstring
  if metric.type != type
    raise AlreadyRegisteredError, "#{name} has already been registered as #{type} type"
  end
  if metric.docstring != docstring
    raise AlreadyRegisteredError, "#{name} has already been registered with different docstring"
  end

  metric
end

.init_label_set(metric, base_initlabels, base_labels) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/fluent/plugin/prometheus.rb', line 238

def self.init_label_set(metric, base_initlabels, base_labels)
  base_initlabels.each { |initlabels|
    # Should never happen, but handy test should code evolution break current implementation
    if initlabels.keys.sort != base_labels.keys.sort
      raise ConfigError, "initlabels for metric #{metric.name} must have the same signature than labels " \
                        "(initlabels given: #{initlabels.keys} vs." \
                        " expected from labels: #{base_labels.keys})"
    end

    metric.init_label_set(initlabels)
  }
end

Instance Method Details

#labels(record, expander) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/fluent/plugin/prometheus.rb', line 251

def labels(record, expander)
  label = {}
  @base_labels.each do |k, v|
    if v.is_a?(String)
      label[k] = expander.expand(v)
    else
      label[k] = v.call(record)
    end
  end
  label
end