Class: Gitlab::Prometheus::Queries::DeploymentQuery

Inherits:
BaseQuery
  • Object
show all
Defined in:
lib/gitlab/prometheus/queries/deployment_query.rb

Instance Attribute Summary

Attributes inherited from BaseQuery

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseQuery

#initialize, #raw_cpu_usage_query, #raw_memory_usage_query

Constructor Details

This class inherits a constructor from Gitlab::Prometheus::Queries::BaseQuery

Class Method Details

.transform_reactive_result(result) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord


33
34
35
36
# File 'lib/gitlab/prometheus/queries/deployment_query.rb', line 33

def self.transform_reactive_result(result)
  result[:metrics] = result.delete :data
  result
end

Instance Method Details

#query(deployment_id) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/prometheus/queries/deployment_query.rb', line 8

def query(deployment_id)
  Deployment.find_by(id: deployment_id).try do |deployment|
    environment_slug = deployment.environment.slug

    memory_query = raw_memory_usage_query(environment_slug)
    memory_avg_query = %{avg(avg_over_time(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}[30m]))}
    cpu_query = raw_cpu_usage_query(environment_slug)
    cpu_avg_query = %{avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}[30m])) * 100}

    timeframe_start = (deployment.created_at - 30.minutes).to_f
    timeframe_end = (deployment.created_at + 30.minutes).to_f

    {
      memory_values: client_query_range(memory_query, start_time: timeframe_start, end_time: timeframe_end),
      memory_before: client_query(memory_avg_query, time: deployment.created_at.to_f),
      memory_after: client_query(memory_avg_query, time: timeframe_end),

      cpu_values: client_query_range(cpu_query, start_time: timeframe_start, end_time: timeframe_end),
      cpu_before: client_query(cpu_avg_query, time: deployment.created_at.to_f),
      cpu_after: client_query(cpu_avg_query, time: timeframe_end)
    }
  end
end