Commit 70cc8857 authored by Sean McGivern's avatar Sean McGivern

Add feature category to structured logs

This won't be present in API logs yet because API endpoints don't yet
have feature categories.
parent d5e38a2e
......@@ -310,7 +310,7 @@ gem 'sentry-raven', '~> 3.0'
gem 'premailer-rails', '~> 1.10.3'
# LabKit: Tracing and Correlation
gem 'gitlab-labkit', '0.12.1'
gem 'gitlab-labkit', '0.12.2'
# I18n
gem 'ruby_parser', '~> 3.8', require: false
......
......@@ -428,7 +428,7 @@ GEM
fog-json (~> 1.2.0)
mime-types
ms_rest_azure (~> 0.12.0)
gitlab-labkit (0.12.1)
gitlab-labkit (0.12.2)
actionpack (>= 5.0.0, < 6.1.0)
activesupport (>= 5.0.0, < 6.1.0)
grpc (~> 1.19)
......@@ -1325,7 +1325,7 @@ DEPENDENCIES
github-markup (~> 1.7.0)
gitlab-chronic (~> 0.10.5)
gitlab-fog-azure-rm (~> 1.0)
gitlab-labkit (= 0.12.1)
gitlab-labkit (= 0.12.2)
gitlab-license (~> 1.0)
gitlab-mail_room (~> 0.0.6)
gitlab-markup (~> 1.7.1)
......
......@@ -466,7 +466,8 @@ class ApplicationController < ActionController::Base
user: -> { auth_user if strong_memoized?(:auth_user) },
project: -> { @project if @project&.persisted? },
namespace: -> { @group if @group&.persisted? },
caller_id: full_action_name) do
caller_id: caller_id,
feature_category: feature_category) do
yield
ensure
@current_context = Labkit::Context.current.to_h
......@@ -548,7 +549,7 @@ class ApplicationController < ActionController::Base
end
end
def full_action_name
def caller_id
"#{self.class.name}##{action_name}"
end
......
......@@ -29,6 +29,7 @@ DELETE /admin/sidekiq/queues/:queue_name
| `root_namespace` | string | no | The root namespace of the project |
| `subscription_plan` | string | no | The subscription plan of the root namespace (GitLab.com only) |
| `caller_id` | string | no | The endpoint or background job that schedule the job (for example: `ProjectsController#create`, `/api/:version/projects/:id`, `PostReceive`) |
| `feature_category` | string | no | The feature category of the background job (for example: `issue_tracking` or `code_review`) |
At least one attribute, other than `queue_name`, is required.
......
......@@ -114,6 +114,11 @@ input AdminSidekiqQueuesDeleteJobsInput {
"""
clientMutationId: String
"""
Delete jobs matching feature_category in the context metadata
"""
featureCategory: String
"""
Delete jobs matching project in the context metadata
"""
......
......@@ -381,6 +381,16 @@
},
"defaultValue": null
},
{
"name": "featureCategory",
"description": "Delete jobs matching feature_category in the context metadata",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
},
{
"name": "queueName",
"description": "The name of the queue to delete jobs from",
......@@ -12,7 +12,8 @@ module Gitlab
Attribute.new(:namespace, Namespace),
Attribute.new(:user, User),
Attribute.new(:caller_id, String),
Attribute.new(:related_class, String)
Attribute.new(:related_class, String),
Attribute.new(:feature_category, String)
].freeze
def self.with_context(args, &block)
......@@ -45,6 +46,7 @@ module Gitlab
hash[:root_namespace] = -> { root_namespace_path } if include_namespace?
hash[:caller_id] = caller_id if set_values.include?(:caller_id)
hash[:related_class] = related_class if set_values.include?(:related_class)
hash[:feature_category] = feature_category if set_values.include?(:feature_category)
end
end
......
......@@ -12,8 +12,10 @@ module Gitlab
# This is not a worker we know about, perhaps from a gem
return yield unless worker_class.respond_to?(:get_worker_context)
# Use the context defined on the class level as a base context
wrap_in_optional_context(worker_class.get_worker_context, &block)
Gitlab::ApplicationContext.with_context(feature_category: worker_class.get_feature_category.to_s) do
# Use the context defined on the class level as the more specific context
wrap_in_optional_context(worker_class.get_worker_context, &block)
end
end
end
end
......
......@@ -844,6 +844,8 @@ RSpec.describe ApplicationController do
describe '#set_current_context' do
controller(described_class) do
feature_category :issue_tracking
def index
Labkit::Context.with_context do |context|
render json: context.to_h
......@@ -893,6 +895,12 @@ RSpec.describe ApplicationController do
expect(json_response['meta.caller_id']).to eq('AnonymousController#index')
end
it 'sets the feature_category as defined in the controller' do
get :index, format: :json
expect(json_response['meta.feature_category']).to eq('issue_tracking')
end
it 'assigns the context to a variable for logging' do
get :index, format: :json
......
......@@ -14,6 +14,7 @@ RSpec.describe Gitlab::SidekiqMiddleware::WorkerContext::Server do
include ApplicationWorker
feature_category :foo
worker_context user: nil
def perform(identifier, *args)
......@@ -56,6 +57,12 @@ RSpec.describe Gitlab::SidekiqMiddleware::WorkerContext::Server do
expect(TestWorker.contexts['identifier'].keys).not_to include('meta.user')
end
it 'takes the feature category from the worker' do
TestWorker.perform_async('identifier', 1)
expect(TestWorker.contexts['identifier']).to include('meta.feature_category' => 'foo')
end
it "doesn't fail for unknown workers" do
expect { OtherWorker.perform_async }.not_to raise_error
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