Class: Fluent::Plugin::Prometheus::Metric
- Inherits:
-
Object
- Object
- Fluent::Plugin::Prometheus::Metric
- Defined in:
- lib/fluent/plugin/prometheus.rb
Defined Under Namespace
Classes: LastModifiedStore
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#retention ⇒ Object
readonly
Returns the value of attribute retention.
-
#retention_check_interval ⇒ Object
readonly
Returns the value of attribute retention_check_interval.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #has_retention? ⇒ Boolean
-
#initialize(element, registry, labels) ⇒ Metric
constructor
A new instance of Metric.
- #instrument(record, expander) ⇒ Object
- #labels(record, expander) ⇒ Object
- #remove_expired_metrics(registry, log) ⇒ Object
- #set_value?(value) ⇒ Boolean
Constructor Details
#initialize(element, registry, labels) ⇒ Metric
Returns a new instance of Metric.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/fluent/plugin/prometheus.rb', line 174 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'] @retention = element['retention'].to_i @retention_check_interval = element.fetch('retention_check_interval', 60).to_i if has_retention? @last_modified_store = LastModifiedStore.new end @topk = element['topk'].to_i @base_labels = Fluent::Plugin::Prometheus.parse_labels_elements(element) @base_labels = labels.merge(@base_labels) end |
Instance Attribute Details
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
170 171 172 |
# File 'lib/fluent/plugin/prometheus.rb', line 170 def desc @desc end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
169 170 171 |
# File 'lib/fluent/plugin/prometheus.rb', line 169 def key @key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
168 169 170 |
# File 'lib/fluent/plugin/prometheus.rb', line 168 def name @name end |
#retention ⇒ Object (readonly)
Returns the value of attribute retention.
171 172 173 |
# File 'lib/fluent/plugin/prometheus.rb', line 171 def retention @retention end |
#retention_check_interval ⇒ Object (readonly)
Returns the value of attribute retention_check_interval.
172 173 174 |
# File 'lib/fluent/plugin/prometheus.rb', line 172 def retention_check_interval @retention_check_interval end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
167 168 169 |
# File 'lib/fluent/plugin/prometheus.rb', line 167 def type @type end |
Class Method Details
.get(registry, name, type, docstring) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/fluent/plugin/prometheus.rb', line 207 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
#has_retention? ⇒ Boolean
239 240 241 |
# File 'lib/fluent/plugin/prometheus.rb', line 239 def has_retention? @retention > 0 end |
#instrument(record, expander) ⇒ Object
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/fluent/plugin/prometheus.rb', line 228 def instrument(record, ) value = self.value(record) if self.set_value?(value) labels = labels(record, ) set_value(value, labels) if has_retention? @last_modified_store.set_last_updated(labels) end end end |
#labels(record, expander) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/fluent/plugin/prometheus.rb', line 195 def labels(record, ) label = {} @base_labels.each do |k, v| if v.is_a?(String) label[k] = .(v) else label[k] = v.call(record) end end label end |
#remove_expired_metrics(registry, log) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/fluent/plugin/prometheus.rb', line 243 def remove_expired_metrics(registry, log) if has_retention? metric = registry.get(@name) expiration_time = Time.now - @retention expired_label_sets = @last_modified_store.get_labels_not_modified_since(expiration_time) expired_label_sets.each { |expired_label_set| log.debug "Metric #{@name} with labels #{expired_label_set} expired. Removing..." metric.remove(expired_label_set) # this method is supplied by the require at the top of this method @last_modified_store.remove(expired_label_set) } else log.warn('remove_expired_metrics should not be called when retention is not set for this metric!') end end |
#set_value?(value) ⇒ Boolean
221 222 223 224 225 226 |
# File 'lib/fluent/plugin/prometheus.rb', line 221 def set_value?(value) if value return true end false end |