diff options
author | Douwe Maan <douwe@gitlab.com> | 2017-05-12 17:15:31 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2017-05-12 17:15:31 +0000 |
commit | bec9ec9a6e8980d4354f2b577cfd2a96a83a73b7 (patch) | |
tree | 4a5c6ddb0915de712628e48dfdf7c8cbea7a17fa /app/models/project_services | |
parent | 4024200314144cd7aa0bda35f38817c8b198142a (diff) | |
parent | f38779c6f521e0d554303db0619bafb07ffeda29 (diff) | |
download | gitlab-ce-bec9ec9a6e8980d4354f2b577cfd2a96a83a73b7.tar.gz |
Merge branch '27439-performance-deltas' into 'master'
Expose memory deltas between app deployments and refactor prometheus queries to support more custom queries
See merge request !10981
Diffstat (limited to 'app/models/project_services')
-rw-r--r-- | app/models/project_services/monitoring_service.rb | 7 | ||||
-rw-r--r-- | app/models/project_services/prometheus_service.rb | 36 |
2 files changed, 16 insertions, 27 deletions
diff --git a/app/models/project_services/monitoring_service.rb b/app/models/project_services/monitoring_service.rb index 59776552540..ee9cd78327a 100644 --- a/app/models/project_services/monitoring_service.rb +++ b/app/models/project_services/monitoring_service.rb @@ -9,8 +9,11 @@ class MonitoringService < Service %w() end - # Environments have a number of metrics - def metrics(environment, timeframe_start: nil, timeframe_end: nil) + def environment_metrics(environment) + raise NotImplementedError + end + + def deployment_metrics(deployment) raise NotImplementedError end end diff --git a/app/models/project_services/prometheus_service.rb b/app/models/project_services/prometheus_service.rb index 6a4479c4dbc..ec72cb6856d 100644 --- a/app/models/project_services/prometheus_service.rb +++ b/app/models/project_services/prometheus_service.rb @@ -63,45 +63,31 @@ class PrometheusService < MonitoringService { success: false, result: err } end - def metrics(environment, timeframe_start: nil, timeframe_end: nil) - with_reactive_cache(environment.slug, timeframe_start, timeframe_end) do |data| - data - end + def environment_metrics(environment) + with_reactive_cache(Gitlab::Prometheus::Queries::EnvironmentQuery.name, environment.id, &:itself) + end + + def deployment_metrics(deployment) + metrics = with_reactive_cache(Gitlab::Prometheus::Queries::DeploymentQuery.name, deployment.id, &:itself) + metrics&.merge(deployment_time: created_at.to_i) || {} end # Cache metrics for specific environment - def calculate_reactive_cache(environment_slug, timeframe_start, timeframe_end) + def calculate_reactive_cache(query_class_name, *args) return unless active? && project && !project.pending_delete? - timeframe_start = Time.parse(timeframe_start) if timeframe_start - timeframe_end = Time.parse(timeframe_end) if timeframe_end - - timeframe_start ||= 8.hours.ago - timeframe_end ||= Time.now - - memory_query = %{(sum(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}) / count(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"})) /1024/1024} - cpu_query = %{sum(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}[2m])) / count(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}) * 100} + metrics = Kernel.const_get(query_class_name).new(client).query(*args) { success: true, - metrics: { - # Average Memory used in MB - memory_values: client.query_range(memory_query, start: timeframe_start, stop: timeframe_end), - memory_current: client.query(memory_query, time: timeframe_end), - memory_previous: client.query(memory_query, time: timeframe_start), - # Average CPU Utilization - cpu_values: client.query_range(cpu_query, start: timeframe_start, stop: timeframe_end), - cpu_current: client.query(cpu_query, time: timeframe_end), - cpu_previous: client.query(cpu_query, time: timeframe_start) - }, + metrics: metrics, last_update: Time.now.utc } - rescue Gitlab::PrometheusError => err { success: false, result: err.message } end def client - @prometheus ||= Gitlab::Prometheus.new(api_url: api_url) + @prometheus ||= Gitlab::PrometheusClient.new(api_url: api_url) end end |