Class: Grafana::PrometheusDatasource

Inherits:
AbstractDatasource show all
Defined in:
lib/grafana/prometheus_datasource.rb

Overview

Implements the interface to Prometheus datasources.

Instance Attribute Summary

Attributes inherited from AbstractDatasource

#model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractDatasource

build_instance, #category, #id, inherited, #initialize, #name, #replace_variables, #type, #uid

Constructor Details

This class inherits a constructor from Grafana::AbstractDatasource

Class Method Details

.handles?(model) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



7
8
9
10
# File 'lib/grafana/prometheus_datasource.rb', line 7

def self.handles?(model)
  tmp = new(model)
  tmp.type == 'prometheus'
end

Instance Method Details

#default_variable_formatObject



46
47
48
# File 'lib/grafana/prometheus_datasource.rb', line 46

def default_variable_format
    'regex'
end

#raw_query_from_panel_model(panel_query_target) ⇒ Object



40
41
42
43
# File 'lib/grafana/prometheus_datasource.rb', line 40

def raw_query_from_panel_model(panel_query_target)
  { query: panel_query_target['expr'], instant: panel_query_target['instant'],
    interval: panel_query_target['step'] }
end

#request(query_description) ⇒ Object

:raw_query needs to contain a Prometheus query as String



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/grafana/prometheus_datasource.rb', line 14

def request(query_description)
  raise MissingSqlQueryError if query_description[:raw_query].nil?

  query_hash = query_description[:raw_query].is_a?(Hash) ? query_description[:raw_query] : {}

  # read instant value and convert instant value to boolean value
  instant = query_description[:variables].delete('instant') || query_hash[:instant] || false
  instant = instant.raw_value if instant.is_a?(Variable)
  instant = instant.to_s.downcase == 'true'
  interval = query_description[:variables].delete('interval') || query_hash[:interval] || 15
  interval = interval.raw_value if interval.is_a?(Variable)
  query = query_hash[:query] || query_description[:raw_query]

  ver = query_description[:grafana_version].split('.').map{|x| x.to_i}
  request = nil
  if (ver[0] == 7 and ver[1] < 5) or ver[0] < 7
    request = prepare_get_request({query_description: query_description, instant: instant, interval: interval, query: query})
  else
    request = prepare_post_request({query_description: query_description, instant: instant, interval: interval, query: query})
  end

  result = request.execute(query_description[:timeout])
  preformat_response(result.body)
end