Read about our upcoming Code of Conduct on this issue

prometheus_service.rb 3.37 KB
Newer Older
1
2
# frozen_string_literal: true

3
class PrometheusService < MonitoringService
4
  include PrometheusAdapter
5
6
7

  #  Access to prometheus is directly through the API
  prop_accessor :api_url
8
  boolean_accessor :manual_configuration
9

10
11
12
13
14
15
  # We need to allow the self-monitoring project to connect to the internal
  # Prometheus instance.
  # Since the internal Prometheus instance is usually a localhost URL, we need
  # to allow localhost URLs when the following conditions are true:
  # 1. project is the self-monitoring project.
  # 2. api_url is the internal Prometheus URL.
16
  with_options presence: true, if: :manual_configuration? do
17
18
    validates :api_url, public_url: true, unless: proc { |object| object.allow_local_api_url? }
    validates :api_url, url: true, if: proc { |object| object.allow_local_api_url? }
19
20
  end

21
  before_save :synchronize_service_state
22

23
24
  after_save :clear_reactive_cache!

25
26
  after_commit :track_events

27
28
  def initialize_properties
    if properties.nil?
Pawel Chojnacki's avatar
Pawel Chojnacki committed
29
      self.properties = {}
30
31
32
    end
  end

33
34
35
36
  def show_active_box?
    false
  end

37
  def editable?
38
    manual_configuration? || !prometheus_available?
39
40
  end

41
42
43
44
45
  def title
    'Prometheus'
  end

  def description
46
    s_('PrometheusService|Time-series monitoring service')
47
48
49
50
51
52
53
  end

  def self.to_param
    'prometheus'
  end

  def fields
54
    return [] unless editable?
55

56
    [
Pawel Chojnacki's avatar
Pawel Chojnacki committed
57
      {
58
        type: 'checkbox',
Pawel Chojnacki's avatar
Pawel Chojnacki committed
59
        name: 'manual_configuration',
60
61
        title: s_('PrometheusService|Active'),
        required: true
62
      },
63
64
65
66
      {
        type: 'text',
        name: 'api_url',
        title: 'API URL',
67
        placeholder: s_('PrometheusService|Prometheus API Base URL, like http://prometheus.example.com/'),
68
        required: true
69
70
71
72
73
74
      }
    ]
  end

  # Check we can connect to the Prometheus API
  def test(*args)
75
    prometheus_client.ping
76
    { success: true, result: 'Checked API endpoint' }
77
  rescue Gitlab::PrometheusClient::Error => err
78
79
80
    { success: false, result: err }
  end

81
  def prometheus_client
82
83
84
    return unless should_return_client?

    Gitlab::PrometheusClient.new(api_url)
Pawel Chojnacki's avatar
wip    
Pawel Chojnacki committed
85
86
  end

87
  def prometheus_available?
88
89
    return false if template?
    return false unless project
Pawel Chojnacki's avatar
Pawel Chojnacki committed
90

91
    project.clusters.enabled.any? { |cluster| cluster.application_prometheus_available? }
92
93
  end

94
95
96
97
  def allow_local_api_url?
    self_monitoring_project? && internal_prometheus_url?
  end

98
99
100
101
  def configured?
    should_return_client?
  end

102
103
  private

104
105
106
107
108
109
110
111
  def self_monitoring_project?
    project && project.id == current_settings.instance_administration_project_id
  end

  def internal_prometheus_url?
    api_url.present? && api_url == ::Gitlab::Prometheus::Internal.uri
  end

112
  def should_return_client?
113
    api_url.present? && manual_configuration? && active? && valid?
114
115
  end

116
117
118
119
  def current_settings
    Gitlab::CurrentSettings.current_application_settings
  end

120
  def synchronize_service_state
121
    self.active = prometheus_available? || manual_configuration?
122
123

    true
124
  end
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

  def track_events
    if enabled_manual_prometheus?
      Gitlab::Tracking.event('cluster:services:prometheus', 'enabled_manual_prometheus')
    elsif disabled_manual_prometheus?
      Gitlab::Tracking.event('cluster:services:prometheus', 'disabled_manual_prometheus')
    end

    true
  end

  def enabled_manual_prometheus?
    manual_configuration_changed? && manual_configuration?
  end

  def disabled_manual_prometheus?
    manual_configuration_changed? && !manual_configuration?
  end
143
end