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.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fluent/plugin/prometheus.rb', line 122

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']

  @base_labels = Fluent::Plugin::Prometheus.parse_labels_elements(element)
  @base_labels = labels.merge(@base_labels)
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



120
121
122
# File 'lib/fluent/plugin/prometheus.rb', line 120

def desc
  @desc
end

#keyObject (readonly)

Returns the value of attribute key.



119
120
121
# File 'lib/fluent/plugin/prometheus.rb', line 119

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



118
119
120
# File 'lib/fluent/plugin/prometheus.rb', line 118

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



117
118
119
# File 'lib/fluent/plugin/prometheus.rb', line 117

def type
  @type
end

Class Method Details

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



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fluent/plugin/prometheus.rb', line 149

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

Instance Method Details

#labels(record, expander, placeholders) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fluent/plugin/prometheus.rb', line 137

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