Use ApplicationContext for FallbackOrganizationTracker
What does this MR do and why?
Gitlab::Current::Organization falls back to the default organization when it cannot resolve one from params, headers, or the current user. This fallback is temporary and must be removed eventually, so we need to track where and how often it happens.
Today, Gitlab::Organizations::FallbackOrganizationTracker.trigger sends an internal event (fallback_current_organization_to_default) through Snowplow. This data is hard to analyze, and the volume of events has become a problem.
This MR moves the tracking to Gitlab::ApplicationContext instead. The information then shows up as json.meta.organization_source on our log lines (for example in Kibana), which is easier to query and correlate with other request data.
The plan is to partly enable the feature flag and and try to bring the number of matches for json.meta.organization_source:fallback to zero.
Changes:
- Add a new
organization_sourcekey toGitlab::ApplicationContext(typeSymbol). This key is web-only, so it is not included in the Sidekiq job deletion matching logic or in the admindelete_jobsGraphQL mutation. FallbackOrganizationTracker.trigger(value)now pushesorganization_source: valueinto the application context instead of sending an internal event.Current#organizationcalls it with:fallback. We no longer need the old event label (caller_id), becausemeta.caller_idis already present on the same log line.- The Sidekiq middleware
Gitlab::SidekiqMiddleware::CurrentOrganization::Servernow pushesorganization_source: :contextwhen it setsCurrent.organizationfrom the job context that was propagated at enqueue time. Note: this value appears on log lines written while the job runs, not on the Sidekiqdoneline, because that line is built from the job payload captured at enqueue time. - Both of these pushes stay behind the existing ops feature flag
track_organization_fallback, which defaults to off. - The possible values right now are
:fallbackand:context. If the key is missing from a log line, the organization was resolved normally, from params, headers, or the user. - Remove
config/events/fallback_current_organization_to_default.yml. The related metric,counts.count_total_fallback_current_organization_to_default, is set tostatus: removed. We keep the metric definition file in the repository, as required by the metrics lifecycle documentation.
Resolves #606297 (closed)
How to verify
-
In a Rails console, enable the feature flag:
Feature.enable(:track_organization_fallback) -
Make an anonymous API request that does not specify an organization, for example:
curl http://gdk.test:3000/api/v4/versionAnonymous requests without organization params fall back to the default organization, so this should trigger the tracker.
-
Pass an Organization ID, for example:
curl -H "X-GitLab-Organization-ID: 1000" http://gdk.test:3000/api/v4/versionIt will print no organization source
-
Check
log/api_json.logfor the request's log line. It should contain"organization_source":"fallback"inside themetaobject.
If you have jq, you can run tail -f log/api_json.log | jq -c '.["meta.organization_source"]' and see
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.