@@ -63,45 +63,30 @@ def test(*args)
63
63
{ success : false , result : err }
64
64
end
65
65
66
- def metrics ( environment , timeframe_start : nil , timeframe_end : nil )
67
- with_reactive_cache ( environment . slug , timeframe_start , timeframe_end ) do |data |
68
- data
69
- end
66
+ def environment_metrics ( environment , **args )
67
+ with_reactive_cache ( Gitlab ::Prometheus ::Queries ::EnvironmentQuery . name , environment . id , &:itself )
68
+ end
69
+
70
+ def deployment_metrics ( deployment )
71
+ with_reactive_cache ( Gitlab ::Prometheus ::Queries ::DeploymentQuery . name , deployment . id , &:itself )
70
72
end
71
73
72
74
# Cache metrics for specific environment
73
- def calculate_reactive_cache ( environment_slug , timeframe_start , timeframe_end )
75
+ def calculate_reactive_cache ( query_class_name , * args )
74
76
return unless active? && project && !project . pending_delete?
75
77
76
- timeframe_start = Time . parse ( timeframe_start ) if timeframe_start
77
- timeframe_end = Time . parse ( timeframe_end ) if timeframe_end
78
-
79
- timeframe_start ||= 8 . hours . ago
80
- timeframe_end ||= Time . now
81
-
82
- 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}
83
- 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}
78
+ metrics = Kernel . const_get ( query_class_name ) . new ( client ) . query ( *args )
84
79
85
80
{
86
81
success : true ,
87
- metrics : {
88
- # Average Memory used in MB
89
- memory_values : client . query_range ( memory_query , start : timeframe_start , stop : timeframe_end ) ,
90
- memory_current : client . query ( memory_query , time : timeframe_end ) ,
91
- memory_previous : client . query ( memory_query , time : timeframe_start ) ,
92
- # Average CPU Utilization
93
- cpu_values : client . query_range ( cpu_query , start : timeframe_start , stop : timeframe_end ) ,
94
- cpu_current : client . query ( cpu_query , time : timeframe_end ) ,
95
- cpu_previous : client . query ( cpu_query , time : timeframe_start )
96
- } ,
82
+ metrics : metrics ,
97
83
last_update : Time . now . utc
98
84
}
99
-
100
85
rescue Gitlab ::PrometheusError => err
101
86
{ success : false , result : err . message }
102
87
end
103
88
104
89
def client
105
- @prometheus ||= Gitlab ::Prometheus . new ( api_url : api_url )
90
+ @prometheus ||= Gitlab ::PrometheusClient . new ( api_url : api_url )
106
91
end
107
92
end
0 commit comments