summaryrefslogtreecommitdiff
path: root/doc/development/service_measurement.md
blob: 895ac540838114ccd42125e9a7de246801164bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
stage: none
group: unassigned
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
---

# 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
- Number of SQL calls
- Detailed `gc` stats and diffs
- RSS memory usage
- Server worker ID

The measuring module logs these measurements into a structured log called [`service_measurement.log`](../administration/logs.md#service_measurementlog),
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.

This way, `Measurable` is at the bottom of the ancestor chain, in order to measure execution of `EE` features as well:

```ruby
class DummyService
  def execute
  # ...
  end
end

DummyService.prepend_if_ee('EE::DummyService')
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
```

After the measurement module is injected in the service, it is behind a generic feature flag.
To actually use it, you need to enable measuring for the desired service by enabling the feature flag.

### Enabling measurement using feature flags

In the following example, the `:gitlab_service_measuring_projects_import_service`
[feature flag](feature_flags/index.md#enabling-a-feature-flag-locally-in-development) is used to enable the measuring feature
for `Projects::ImportService`.

From ChatOps:

```shell
/chatops run feature set gitlab_service_measuring_projects_import_service true
```