2020-08-21 14:10:24 -04:00
---
type: reference, dev
stage: none
group: Development
info: "See the Technical Writers assigned to Development Guidelines: https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments-to-development-guidelines"
---
2019-10-11 14:06:15 -04:00
# Feature flag controls
2019-06-27 13:52:02 -04:00
2019-10-11 14:06:15 -04:00
## Access
To be able to turn on/off features behind feature flags in any of the
2019-06-27 13:52:02 -04:00
GitLab Inc. provided environments such as staging and production, you need to
2019-10-11 14:06:15 -04:00
have access to the [Chatops ](../chatops_on_gitlabcom.md ) bot. The Chatops bot
is currently running on the ops instance, which is different from < https: / / gitlab . com > or < https: / / dev . gitlab . org > .
2019-06-27 13:52:02 -04:00
2019-07-05 12:25:58 -04:00
Follow the Chatops document to [request access ](../chatops_on_gitlabcom.md#requesting-access ).
2019-06-27 13:52:02 -04:00
Once you are added to the project test if your access propagated,
run:
2020-03-25 02:07:58 -04:00
```shell
2019-06-27 13:52:02 -04:00
/chatops run feature --help
```
## Rolling out changes
When the changes are deployed to the environments it is time to start
rolling out the feature to our users. The exact procedure of rolling out a
change is unspecified, as this can vary from change to change. However, in
general we recommend rolling out changes incrementally, instead of enabling them
for everybody right away. We also recommend you to _not_ enable a feature
_before_ the code is being deployed.
This allows you to separate rolling out a feature from a deploy, making it
easier to measure the impact of both separately.
2020-12-09 22:09:53 -05:00
The GitLab feature library (using
2019-06-27 13:52:02 -04:00
[Flipper ](https://github.com/jnunemaker/flipper ), and covered in the [Feature
Flags process](process.md) guide) supports rolling out changes to a percentage of
2020-02-12 16:08:48 -05:00
time to users. This in turn can be controlled using [GitLab Chatops ](../../ci/chatops/README.md ).
2019-06-27 13:52:02 -04:00
For an up to date list of feature flag commands please see [the source
code](https://gitlab.com/gitlab-com/chatops/blob/master/lib/chatops/commands/feature.rb).
Note that all the examples in that file must be preceded by
`/chatops run` .
If you get an error "Whoops! This action is not allowed. This incident
will be reported." that means your Slack account is not allowed to
2019-10-11 14:06:15 -04:00
change feature flags or you do not [have access ](#access ).
2019-06-27 13:52:02 -04:00
2020-05-05 14:09:43 -04:00
### Enabling a feature for preproduction testing
2019-06-27 13:52:02 -04:00
2020-12-03 19:09:55 -05:00
As a first step in a feature rollout, you should enable the feature on < https: / / about . staging . gitlab . com >
2019-06-27 13:52:02 -04:00
and < https: / / dev . gitlab . org > .
These two environments have different scopes.
`dev.gitlab.org` is a production CE environment that has internal GitLab Inc.
traffic and is used for some development and other related work.
`staging.gitlab.com` has a smaller subset of GitLab.com database and repositories
and does not have regular traffic. Staging is an EE instance and can give you
a (very) rough estimate of how your feature will look/behave on GitLab.com.
Both of these instances are connected to Sentry so make sure you check the projects
there for any exceptions while testing your feature after enabling the feature flag.
2020-05-05 14:09:43 -04:00
For these preproduction environments, the commands should be run in a
Slack channel for the stage the feature is relevant to. For example, use the
`#s_monitor` channel for features developed by the Monitor stage, Health
group.
To enable a feature for 25% of all users, run the following in Slack:
```shell
/chatops run feature set new_navigation_bar 25 --dev
/chatops run feature set new_navigation_bar 25 --staging
```
2019-06-27 13:52:02 -04:00
2019-10-11 14:06:15 -04:00
### Enabling a feature for GitLab.com
2019-06-27 13:52:02 -04:00
2020-05-05 14:09:43 -04:00
When a feature has successfully been
[enabled on a preproduction ](#enabling-a-feature-for-preproduction-testing )
environment and verified as safe and working, you can roll out the
change to GitLab.com (production).
#### Communicate the change
Some feature flag changes on GitLab.com should be communicated with
parts of the company. The developer responsible needs to determine
whether this is necessary and the appropriate level of communication.
This depends on the feature and what sort of impact it might have.
2020-10-08 11:08:17 -04:00
Guidelines:
2020-05-05 14:09:43 -04:00
2020-10-08 11:08:17 -04:00
1. If the feature meets the requirements for creating a [Change Management ](https://about.gitlab.com/handbook/engineering/infrastructure/change-management/#feature-flags-and-the-change-management-process ) issue, create a Change Management issue per [criticality guidelines ](https://about.gitlab.com/handbook/engineering/infrastructure/change-management/#change-request-workflows ).
1. For simple, low-risk, easily reverted features, proceed and [enable the feature in `#production` ](#process ).
2020-10-21 14:08:58 -04:00
1. For features that impact the user experience, consider notifying `#support_gitlab-com` beforehand.
2020-05-05 14:09:43 -04:00
#### Process
Before toggling any feature flag, check that there are no ongoing
significant incidents on GitLab.com. You can do this by checking the
`#production` and `#incident-management` Slack channels, or looking for
2020-05-29 11:08:14 -04:00
[open incident issues ](https://gitlab.com/gitlab-com/gl-infra/production/-/issues/?scope=all&utf8=%E2%9C%93&state=opened&label_name[]=incident )
2020-05-05 14:09:43 -04:00
(although check the dates and times).
We do not want to introduce changes during an incident, as it can make
diagnosis and resolution of the incident much harder to achieve, and
also will largely invalidate your rollout process as you will be unable
to assess whether the rollout was without problems or not.
If there is any doubt, ask in `#production` .
The following `/chatops` commands should be performed in the Slack
`#production` channel.
When you begin to enable the feature, please link to the relevant
Feature Flag Rollout Issue within a Slack thread of the first `/chatops`
command you make so people can understand the change if they need to.
2020-05-22 08:08:15 -04:00
To enable a feature for 25% of the time, run the following in Slack:
2019-06-27 13:52:02 -04:00
2020-03-25 02:07:58 -04:00
```shell
2019-06-27 13:52:02 -04:00
/chatops run feature set new_navigation_bar 25
```
2020-05-22 08:08:15 -04:00
This sets a feature flag to `true` based on the following formula:
```ruby
feature_flag_state = rand < (25 / 100.0)
```
2019-06-27 13:52:02 -04:00
This will enable the feature for GitLab.com, with `new_navigation_bar` being the
name of the feature.
2020-04-08 02:09:54 -04:00
This command does *not* enable the feature for 25% of the total users.
Instead, when the feature is checked with `enabled?` , it will return `true` 25% of the time.
2019-06-27 13:52:02 -04:00
2020-05-22 08:08:15 -04:00
To enable a feature for 25% of actors such as users, projects, or groups,
run the following in Slack:
```shell
/chatops run feature set some_feature 25 --actors
```
This sets a feature flag to `true` based on the following formula:
```ruby
2020-08-10 08:09:55 -04:00
feature_flag_state = Zlib.crc32("some_feature< Actor > :#{actor.id}") % (100 * 1_000) < 25 * 1_000
2020-05-22 08:08:15 -04:00
# where <Actor>: is a `User`, `Group`, `Project` and actor is an instance
```
During development, based on the nature of the feature, an actor choice
should be made.
For user focused features:
```ruby
Feature.enabled?(:feature_cool_avatars, current_user)
```
For group or namespace level features:
```ruby
Feature.enabled?(:feature_cooler_groups, group)
```
For project level features:
```ruby
Feature.enabled?(:feature_ice_cold_projects, project)
```
2019-06-27 13:52:02 -04:00
If you are not certain what percentages to use, simply use the following steps:
1. 25%
1. 50%
1. 75%
1. 100%
Between every step you'll want to wait a little while and monitor the
appropriate graphs on < https: / / dashboards . gitlab . net > . The exact time to wait
may differ. For some features a few minutes is enough, while for others you may
want to wait several hours or even days. This is entirely up to you, just make
sure it is clearly communicated to your team, and the Production team if you
anticipate any potential problems.
Feature gates can also be actor based, for example a feature could first be
2019-09-18 14:06:14 -04:00
enabled for only the `gitlab` project. The project is passed by supplying a
2019-06-27 13:52:02 -04:00
`--project` flag:
2020-03-25 02:07:58 -04:00
```shell
2019-09-18 14:06:14 -04:00
/chatops run feature set --project=gitlab-org/gitlab some_feature true
2019-06-27 13:52:02 -04:00
```
For groups the `--group` flag is available:
2020-03-25 02:07:58 -04:00
```shell
2019-06-27 13:52:02 -04:00
/chatops run feature set --group=gitlab-org some_feature true
```
2020-01-20 10:09:18 -05:00
Note that actor-based gates are applied before percentages. For example, considering the
`group/project` as `gitlab-org/gitlab` and a given example feature as `some_feature` , if
you run these 2 commands:
2020-03-25 02:07:58 -04:00
```shell
2020-01-20 10:09:18 -05:00
/chatops run feature set --project=gitlab-org/gitlab some_feature true
2020-05-22 08:08:15 -04:00
/chatops run feature set some_feature 25 --actors
```
Then `some_feature` will be enabled for both 25% of actors and always when interacting with
`gitlab-org/gitlab` . This is a good idea if the feature flag development makes use of group
actors.
```ruby
Feature.enabled?(:some_feature, group)
2020-01-20 10:09:18 -05:00
```
2020-02-12 16:08:48 -05:00
**Percentage of time** rollout is not a good idea if what you want is to make sure a feature
2020-05-22 08:08:15 -04:00
is always on or off to the users. In that case, **Percentage of actors** rollout is a better method.
2020-01-20 10:09:18 -05:00
2020-06-24 02:09:01 -04:00
Lastly, to verify that the feature is deemed stable in as many cases as possible,
you should fully roll out the feature by enabling the flag **globally** by running:
```shell
/chatops run feature set some_feature true
```
This changes the feature flag state to be **enabled** always, which overrides the
existing gates (e.g. `--group=gitlab-org` ) in the above processes.
2020-05-05 14:09:43 -04:00
### Feature flag change logging
2020-12-01 22:09:38 -05:00
#### Chatops level
Any feature flag change that affects GitLab.com (production) via [Chatops ](https://gitlab.com/gitlab-com/chatops )
is automatically logged in an issue.
2020-05-05 14:09:43 -04:00
The issue is created in the
2020-05-29 11:08:14 -04:00
[gl-infra/feature-flag-log ](https://gitlab.com/gitlab-com/gl-infra/feature-flag-log/-/issues?scope=all&utf8=%E2%9C%93&state=closed )
2020-05-05 14:09:43 -04:00
project, and it will at minimum log the Slack handle of person enabling
a feature flag, the time, and the name of the flag being changed.
2020-12-09 22:09:53 -05:00
The issue is then also posted to the GitLab internal
2020-05-05 14:09:43 -04:00
[Grafana dashboard ](https://dashboards.gitlab.net/ ) as an annotation
marker to make the change even more visible.
Changes to the issue format can be submitted in the
[Chatops project ](https://gitlab.com/gitlab-com/chatops ).
2020-12-01 22:09:38 -05:00
#### Instance level
Any feature flag change that affects any GitLab instance is automatically logged in
[features_json.log ](../../administration/logs.md#features_jsonlog ).
You can search the change history in [Kibana ](https://about.gitlab.com/handbook/support/workflows/kibana.html ).
2019-06-27 13:52:02 -04:00
## Cleaning up
2020-10-14 08:08:58 -04:00
A feature flag should be removed as soon as it is no longer needed. Each additional
feature flag in the codebase increases the complexity of the application
and reduces confidence in our testing suite covering all possible combinations.
Additionally, a feature flag overwritten in some of the environments can result
in undefined and untested system behavior.
To remove a feature flag:
1. Open a new merge request with the ~"feature flag" label so
release managers are aware the changes are hidden behind a feature flag.
1. If the merge request has to be picked into a stable branch, add the
appropriate `~"Pick into X.Y"` label, for example `~"Pick into 13.0"` .
See [the feature flag process ](process.md#including-a-feature-behind-feature-flag-in-the-final-release )
for further details.
1. Remove all references to the feature flag from the codebase.
1. Remove the YAML definition for the feature from the repository.
1. Clean up the feature flag from all environments with `/chatops run feature delete some_feature` .
1. Close the rollout issue for the feature flag after the feature flag is removed from the codebase.
### Cleanup ChatOps
2019-06-27 13:52:02 -04:00
2020-12-11 01:10:17 -05:00
When a feature gate has been removed from the codebase, the feature
2019-10-11 14:06:15 -04:00
record still exists in the database that the flag was deployed too.
The record can be deleted once the MR is deployed to each environment:
2019-06-27 13:52:02 -04:00
2020-01-30 10:09:15 -05:00
```shell
2019-10-11 14:06:15 -04:00
/chatops run feature delete some_feature --dev
/chatops run feature delete some_feature --staging
2019-06-27 13:52:02 -04:00
```
2019-10-11 14:06:15 -04:00
Then, you can delete it from production after the MR is deployed to prod:
2020-01-30 10:09:15 -05:00
```shell
2019-06-27 13:52:02 -04:00
/chatops run feature delete some_feature
```