Class: PrometheusService

Inherits:
MonitoringService show all
Includes:
PrometheusAdapter
Defined in:
app/models/project_services/prometheus_service.rb

Constant Summary

Constants included from PrometheusAdapter

PrometheusAdapter::DEFAULT_PROMETHEUS_REQUEST_TIMEOUT_SEC

Constants inherited from Service

Service::DEV_SERVICE_NAMES, Service::SERVICE_NAMES

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MonitoringService

#can_query?, #query, supported_events

Methods inherited from Service

#activated?, #api_field_names, #async_execute, available_services_names, available_services_types, boolean_accessor, build_from_integration, #can_test?, #category, #configurable_event_actions, #configurable_events, default_integration, dev_services_names, #editable?, #event_channel_names, event_description, #event_field, event_names, #event_names, #execute, find_or_create_templates, find_or_initialize_all, find_or_initialize_integration, #global_fields, #help, instance_exists_for?, #issue_tracker?, #json_fields, #operating?, prop_accessor, #reset_updated_properties, services_names, services_types, supported_event_actions, supported_events, #supported_events, #supports_data_fields?, #to_data_fields_hash, #to_param, #to_service_hash, #updated_properties

Methods included from ProjectServicesLoggable

#build_message, #log_error, #log_info, #logger

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.to_paramObject


52
53
54
# File 'app/models/project_services/prometheus_service.rb', line 52

def self.to_param
  'prometheus'
end

Instance Method Details

#allow_local_api_url?Boolean

Returns:

  • (Boolean)

121
122
123
124
# File 'app/models/project_services/prometheus_service.rb', line 121

def allow_local_api_url?
  allow_local_requests_from_web_hooks_and_services? ||
  (self_monitoring_project? && internal_prometheus_url?)
end

#configured?Boolean

Returns:

  • (Boolean)

126
127
128
# File 'app/models/project_services/prometheus_service.rb', line 126

def configured?
  should_return_client?
end

#descriptionObject


48
49
50
# File 'app/models/project_services/prometheus_service.rb', line 48

def description
  s_('PrometheusService|Time-series monitoring service')
end

#fieldsObject


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/project_services/prometheus_service.rb', line 56

def fields
  [
    {
      type: 'checkbox',
      name: 'manual_configuration',
      title: s_('PrometheusService|Active'),
      required: true
    },
    {
      type: 'text',
      name: 'api_url',
      title: 'API URL',
      placeholder: s_('PrometheusService|Prometheus API Base URL, like http://prometheus.example.com/'),
      required: true
    },
    {
      type: 'text',
      name: 'google_iap_audience_client_id',
      title: 'Google IAP Audience Client ID',
      placeholder: s_('PrometheusService|Client ID of the IAP secured resource (looks like IAP_CLIENT_ID.apps.googleusercontent.com)'),
      autocomplete: 'off',
      required: false
    },
    {
      type: 'textarea',
      name: 'google_iap_service_account_json',
      title: 'Google IAP Service Account JSON',
      placeholder: s_('PrometheusService|Contents of the credentials.json file of your service account, like: { "type": "service_account", "project_id": ... }'),
      required: false
    }
  ]
end

#initialize_propertiesObject


34
35
36
37
38
# File 'app/models/project_services/prometheus_service.rb', line 34

def initialize_properties
  if properties.nil?
    self.properties = {}
  end
end

#prometheus_available?Boolean

Returns:

  • (Boolean)

112
113
114
115
116
117
118
119
# File 'app/models/project_services/prometheus_service.rb', line 112

def prometheus_available?
  return false if template?
  return false unless project

  project.all_clusters.enabled.eager_load(:application_prometheus).any? do |cluster|
    cluster.application_prometheus&.available?
  end
end

#prometheus_clientObject


97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/project_services/prometheus_service.rb', line 97

def prometheus_client
  return unless should_return_client?

  options = prometheus_client_default_options.merge(
    allow_local_requests: allow_local_api_url?
  )

  if behind_iap?
    # Adds the Authorization header
    options[:headers] = iap_client.apply({})
  end

  Gitlab::PrometheusClient.new(api_url, options)
end

#show_active_box?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'app/models/project_services/prometheus_service.rb', line 40

def show_active_box?
  false
end

#test(*args) ⇒ Object

Check we can connect to the Prometheus API


90
91
92
93
94
95
# File 'app/models/project_services/prometheus_service.rb', line 90

def test(*args)
  prometheus_client.ping
  { success: true, result: 'Checked API endpoint' }
rescue Gitlab::PrometheusClient::Error => err
  { success: false, result: err }
end

#titleObject


44
45
46
# File 'app/models/project_services/prometheus_service.rb', line 44

def title
  'Prometheus'
end