Skip to content

Commit 608186d

Browse files
committed
Add per deployment additional metrics
1 parent 4d8f397 commit 608186d

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

app/controllers/projects/deployments_controller.rb

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ def metrics
2222
render_404
2323
end
2424

25+
def additional_metrics
26+
return render_404 unless deployment.has_additional_metrics?
27+
metrics = deployment.additional_metrics
28+
29+
if metrics&.any?
30+
render json: metrics, status: :ok
31+
else
32+
head :no_content
33+
end
34+
end
35+
2536
private
2637

2738
def deployment

app/models/deployment.rb

+10
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,22 @@ def has_metrics?
103103
project.monitoring_service.present?
104104
end
105105

106+
def has_additional_metrics?
107+
has_metrics? && project.monitoring_service&.respond_to?(:reactive_query)
108+
end
109+
106110
def metrics
107111
return {} unless has_metrics?
108112

109113
project.monitoring_service.deployment_metrics(self)
110114
end
111115

116+
def additional_metrics
117+
return {} unless has_additional_metrics?
118+
metrics = project.monitoring_service.reactive_query(Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery.name, id, &:itself)
119+
metrics&.merge(deployment_time: created_at.to_i) || {}
120+
end
121+
112122
private
113123

114124
def ref_path

config/routes/project.rb

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
resources :deployments, only: [:index] do
168168
member do
169169
get :metrics
170+
get :additional_metrics
170171
end
171172
end
172173
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Gitlab::Prometheus::Queries
2+
class AdditionalMetricsDeploymentQuery < AdditionalMetricsQuery
3+
4+
def query(deployment_id)
5+
deployment = Deployment.find_by(id: deployment_id)
6+
query_context = {
7+
environment_slug: deployment.environment.slug,
8+
environment_filter: %{container_name!="POD",environment="#{deployment.environment.slug}"},
9+
timeframe_start: (deployment.created_at - 30.minutes).to_f,
10+
timeframe_end: (deployment.created_at + 30.minutes).to_f
11+
}
12+
13+
query_metrics(query_context)
14+
end
15+
end
16+
end
17+

lib/gitlab/prometheus/queries/additional_metrics_query.rb

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
module Gitlab::Prometheus::Queries
22
class AdditionalMetricsQuery < BaseQuery
33
def query(environment_id)
4-
query_processor = method(:process_query).curry[query_context(environment_id)]
4+
environment = Environment.find_by(id: environment_id)
5+
query_context = {
6+
environment_slug: environment.slug,
7+
environment_filter: %{container_name!="POD",environment="#{environment.slug}"},
8+
timeframe_start: 8.hours.ago.to_f,
9+
timeframe_end: Time.now.to_f
10+
}
11+
12+
query_metrics(query_context)
13+
end
14+
15+
protected
16+
17+
def query_metrics(query_context)
18+
query_processor = method(:process_query).curry[query_context]
519

620
matched_metrics.map do |group|
721
metrics = group.metrics.map do |metric|
@@ -22,16 +36,6 @@ def query(environment_id)
2236

2337
private
2438

25-
def query_context(environment_id)
26-
environment = Environment.find_by(id: environment_id)
27-
{
28-
environment_slug: environment.slug,
29-
environment_filter: %{container_name!="POD",environment="#{environment.slug}"},
30-
timeframe_start: 8.hours.ago.to_f,
31-
timeframe_end: Time.now.to_f
32-
}
33-
end
34-
3539
def process_query(context, query)
3640
query_with_result = query.dup
3741
query_with_result[:result] =

0 commit comments

Comments
 (0)