Commit bb56a8dd authored by John Parent's avatar John Parent Committed by Bob Van Landuyt

Enable runner correlation logging

Setup log messages establishing enough info to correlate
runner actions to correlational_id
parent cd711f52
......@@ -628,6 +628,8 @@ Input type: `AdminSidekiqQueuesDeleteJobsInput`
| <a id="mutationadminsidekiqqueuesdeletejobsclientid"></a>`clientId` | [`String`](#string) | Delete jobs matching client_id in the context metadata. |
| <a id="mutationadminsidekiqqueuesdeletejobsclientmutationid"></a>`clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| <a id="mutationadminsidekiqqueuesdeletejobsfeaturecategory"></a>`featureCategory` | [`String`](#string) | Delete jobs matching feature_category in the context metadata. |
| <a id="mutationadminsidekiqqueuesdeletejobsjobid"></a>`jobId` | [`String`](#string) | Delete jobs matching job_id in the context metadata. |
| <a id="mutationadminsidekiqqueuesdeletejobspipelineid"></a>`pipelineId` | [`String`](#string) | Delete jobs matching pipeline_id in the context metadata. |
| <a id="mutationadminsidekiqqueuesdeletejobsproject"></a>`project` | [`String`](#string) | Delete jobs matching project in the context metadata. |
| <a id="mutationadminsidekiqqueuesdeletejobsqueuename"></a>`queueName` | [`String!`](#string) | Name of the queue to delete jobs from. |
| <a id="mutationadminsidekiqqueuesdeletejobsrelatedclass"></a>`relatedClass` | [`String`](#string) | Delete jobs matching related_class in the context metadata. |
......@@ -104,10 +104,7 @@ module API
def set_application_context
return unless current_job
Gitlab::ApplicationContext.push(
user: -> { current_job.user },
project: -> { current_job.project }
)
Gitlab::ApplicationContext.push(job: current_job)
end
def track_ci_minutes_usage!(_build, _runner)
......
......@@ -16,6 +16,8 @@ module Gitlab
:client_id,
:caller_id,
:remote_ip,
:job_id,
:pipeline_id,
:related_class,
:feature_category
].freeze
......@@ -28,6 +30,7 @@ module Gitlab
Attribute.new(:runner, ::Ci::Runner),
Attribute.new(:caller_id, String),
Attribute.new(:remote_ip, String),
Attribute.new(:job, ::Ci::Build),
Attribute.new(:related_class, String),
Attribute.new(:feature_category, String)
].freeze
......@@ -73,14 +76,16 @@ module Gitlab
def to_lazy_hash
{}.tap do |hash|
hash[:user] = -> { username } if set_values.include?(:user)
hash[:project] = -> { project_path } if set_values.include?(:project) || set_values.include?(:runner)
hash[:user] = -> { username } if include_user?
hash[:project] = -> { project_path } if include_project?
hash[:root_namespace] = -> { root_namespace_path } if include_namespace?
hash[:client_id] = -> { client } if include_client?
hash[:caller_id] = caller_id if set_values.include?(:caller_id)
hash[:remote_ip] = remote_ip if set_values.include?(:remote_ip)
hash[:related_class] = related_class if set_values.include?(:related_class)
hash[:feature_category] = feature_category if set_values.include?(:feature_category)
hash[:pipeline_id] = -> { job&.pipeline_id } if set_values.include?(:job)
hash[:job_id] = -> { job&.id } if set_values.include?(:job)
end
end
......@@ -103,32 +108,41 @@ module Gitlab
end
def project_path
associated_routable = project || runner_project
associated_routable = project || runner_project || job_project
associated_routable&.full_path
end
def username
user&.username
associated_user = user || job_user
associated_user&.username
end
def root_namespace_path
associated_routable = namespace || project || runner_project || runner_group
associated_routable = namespace || project || runner_project || runner_group || job_project
associated_routable&.full_path_components&.first
end
def include_namespace?
set_values.include?(:namespace) || set_values.include?(:project) || set_values.include?(:runner)
set_values.include?(:namespace) || set_values.include?(:project) || set_values.include?(:runner) || set_values.include?(:job)
end
def include_client?
set_values.include?(:user) || set_values.include?(:runner) || set_values.include?(:remote_ip)
end
def include_user?
set_values.include?(:user) || set_values.include?(:job)
end
def include_project?
set_values.include?(:project) || set_values.include?(:runner) || set_values.include?(:job)
end
def client
if user
"user/#{user.id}"
elsif runner
if runner
"runner/#{runner.id}"
elsif user
"user/#{user.id}"
else
"ip/#{remote_ip}"
end
......@@ -150,6 +164,18 @@ module Gitlab
runner.groups.first
end
end
def job_project
strong_memoize(:job_project) do
job&.project
end
end
def job_user
strong_memoize(:job_user) do
job&.user
end
end
end
end
......
......@@ -146,7 +146,8 @@ RSpec.describe Gitlab::ApplicationContext do
where(:provided_options, :client) do
[:remote_ip] | :remote_ip
[:remote_ip, :runner] | :runner
[:remote_ip, :runner, :user] | :user
[:remote_ip, :runner, :user] | :runner
[:remote_ip, :user] | :user
end
with_them do
......@@ -195,6 +196,16 @@ RSpec.describe Gitlab::ApplicationContext do
expect(result(context)).to include(project: nil)
end
end
context 'when using job context' do
let_it_be(:job) { create(:ci_build, :pending, :queued, user: user, project: project) }
it 'sets expected values' do
context = described_class.new(job: job)
expect(result(context)).to include(job_id: job.id, project: project.full_path, pipeline_id: job.pipeline_id)
end
end
end
describe '#use' do
......
......@@ -853,11 +853,11 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
subject { request_job(id: job.id) }
it_behaves_like 'storing arguments in the application context for the API' do
let(:expected_params) { { user: user.username, project: project.full_path, client_id: "user/#{user.id}" } }
let(:expected_params) { { user: user.username, project: project.full_path, client_id: "runner/#{runner.id}", job_id: job.id, pipeline_id: job.pipeline_id } }
end
it_behaves_like 'not executing any extra queries for the application context', 3 do
# Extra queries: User, Project, Route
it_behaves_like 'not executing any extra queries for the application context', 4 do
# Extra queries: User, Project, Route, Runner
let(:subject_proc) { proc { request_job(id: job.id) } }
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment