2020-10-30 14:08:56 -04:00
---
stage: none
group: unassigned
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 14:08:56 -04:00
---
2020-06-02 17:08:00 -04:00
# GitLab Developers Guide to service measurement
You can enable service measurement in order to debug any slow service's execution time, number of SQL calls, garbage collection stats, memory usage, etc.
## Measuring module
The measuring module is a tool that allows to measure a service's execution, and log:
- Service class name
- Execution time
2020-06-11 17:08:37 -04:00
- Number of SQL calls
- Detailed `gc` stats and diffs
2020-06-02 17:08:00 -04:00
- RSS memory usage
- Server worker ID
2020-12-11 13:09:57 -05:00
The measuring module logs these measurements into a structured log called [`service_measurement.log` ](../administration/logs.md#service_measurementlog ),
2020-06-02 17:08:00 -04:00
as a single entry for each service execution.
For GitLab.com, `service_measurement.log` is ingested in Elasticsearch and Kibana as part of our monitoring solution.
## How to use it
The measuring module allows you to easily measure and log execution of any service,
by just prepending `Measurable` in any Service class, on the last line of the file that the class resides in.
For example, to prepend a module into the `DummyService` class, you would use the following approach:
```ruby
class DummyService
def execute
# ...
end
end
DummyService.prepend(Measurable)
```
In case when you are prepending a module from the `EE` namespace with EE features, you need to prepend Measurable after prepending the `EE` module.
2020-12-11 13:09:57 -05:00
This way, `Measurable` is at the bottom of the ancestor chain, in order to measure execution of `EE` features as well:
2020-06-02 17:08:00 -04:00
```ruby
class DummyService
def execute
# ...
end
end
2021-05-11 17:10:21 -04:00
DummyService.prepend_mod_with('DummyService')
2020-06-02 17:08:00 -04:00
DummyService.prepend(Measurable)
```
### Log additional attributes
In case you need to log some additional attributes, it is possible to define `extra_attributes_for_measurement` in the service class:
```ruby
def extra_attributes_for_measurement
{
project_path: @project .full_path,
user: current_user.name
}
end
```
2020-12-11 13:09:57 -05:00
After the measurement module is injected in the service, it is behind a generic feature flag.
2020-11-03 19:09:12 -05:00
To actually use it, you need to enable measuring for the desired service by enabling the feature flag.
2020-06-02 17:08:00 -04:00
### Enabling measurement using feature flags
In the following example, the `:gitlab_service_measuring_projects_import_service`
2021-03-11 16:09:09 -05:00
[feature flag ](feature_flags/index.md#enabling-a-feature-flag-locally-in-development ) is used to enable the measuring feature
2020-06-02 17:08:00 -04:00
for `Projects::ImportService` .
2020-06-11 17:08:37 -04:00
From ChatOps:
2020-06-02 17:08:00 -04:00
```shell
/chatops run feature set gitlab_service_measuring_projects_import_service true
```