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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Metrics::Subscribers::ExternalHttp, :request_store do
let(:transaction) { Gitlab::Metrics::Transaction.new }
let(:subscriber) { described_class.new }
let(:event_1) do
double(:event, payload: {
method: 'POST', code: "200", duration: 0.321,
scheme: 'https', host: 'gitlab.com', port: 80, path: '/api/v4/projects',
query: 'current=true'
})
end
let(:event_2) do
double(:event, payload: {
method: 'GET', code: "301", duration: 0.12,
scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2',
query: 'current=true'
})
end
let(:event_3) do
double(:event, payload: {
method: 'POST', duration: 5.3,
scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2/issues',
query: 'current=true',
exception_object: Net::ReadTimeout.new
})
end
describe '.detail_store' do
context 'when external HTTP detail store is empty' do
before do
Gitlab::SafeRequestStore[:peek_enabled] = true
end
it 'returns an empty array' do
expect(described_class.detail_store).to eql([])
end
end
context 'when the performance bar is not enabled' do
it 'returns an empty array' do
expect(described_class.detail_store).to eql([])
end
end
context 'when external HTTP detail store has some values' do
before do
Gitlab::SafeRequestStore[:peek_enabled] = true
Gitlab::SafeRequestStore[:external_http_detail_store] = [{
method: 'POST', code: "200", duration: 0.321
}]
end
it 'returns the external http detailed store' do
expect(described_class.detail_store).to eql([{ method: 'POST', code: "200", duration: 0.321 }])
end
end
end
describe '.payload' do
context 'when SafeRequestStore does not have any item from external HTTP' do
it 'returns an empty array' do
expect(described_class.payload).to eql(external_http_count: 0, external_http_duration_s: 0.0)
end
end
context 'when external HTTP recorded some values' do
before do
Gitlab::SafeRequestStore[:external_http_count] = 7
Gitlab::SafeRequestStore[:external_http_duration_s] = 1.2
end
it 'returns the external http detailed store' do
expect(described_class.payload).to eql(external_http_count: 7, external_http_duration_s: 1.2)
end
end
end
describe '#request' do
before do
Gitlab::SafeRequestStore[:peek_enabled] = true
allow(subscriber).to receive(:current_transaction).and_return(transaction)
end
it 'tracks external HTTP request count' do
expect(transaction).to receive(:increment)
.with(:gitlab_external_http_total, 1, { code: "200", method: "POST" })
expect(transaction).to receive(:increment)
.with(:gitlab_external_http_total, 1, { code: "301", method: "GET" })
subscriber.request(event_1)
subscriber.request(event_2)
end
it 'tracks external HTTP duration' do
expect(transaction).to receive(:observe)
.with(:gitlab_external_http_duration_seconds, 0.321)
expect(transaction).to receive(:observe)
.with(:gitlab_external_http_duration_seconds, 0.12)
expect(transaction).to receive(:observe)
.with(:gitlab_external_http_duration_seconds, 5.3)
subscriber.request(event_1)
subscriber.request(event_2)
subscriber.request(event_3)
end
it 'tracks external HTTP exceptions' do
expect(transaction).to receive(:increment)
.with(:gitlab_external_http_total, 1, { code: 'undefined', method: "POST" })
expect(transaction).to receive(:increment)
.with(:gitlab_external_http_exception_total, 1)
subscriber.request(event_3)
end
it 'stores per-request counters' do
subscriber.request(event_1)
subscriber.request(event_2)
subscriber.request(event_3)
expect(Gitlab::SafeRequestStore[:external_http_count]).to eq(3)
expect(Gitlab::SafeRequestStore[:external_http_duration_s]).to eq(5.741) # 0.321 + 0.12 + 5.3
end
it 'stores a portion of events into the detail store' do
subscriber.request(event_1)
subscriber.request(event_2)
subscriber.request(event_3)
expect(Gitlab::SafeRequestStore[:external_http_detail_store].length).to eq(3)
expect(Gitlab::SafeRequestStore[:external_http_detail_store][0]).to include(
method: 'POST', code: "200", duration: 0.321,
scheme: 'https', host: 'gitlab.com', port: 80, path: '/api/v4/projects',
query: 'current=true', exception_object: nil,
backtrace: be_a(Array)
)
expect(Gitlab::SafeRequestStore[:external_http_detail_store][1]).to include(
method: 'GET', code: "301", duration: 0.12,
scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2',
query: 'current=true', exception_object: nil,
backtrace: be_a(Array)
)
expect(Gitlab::SafeRequestStore[:external_http_detail_store][2]).to include(
method: 'POST', duration: 5.3,
scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2/issues',
query: 'current=true',
exception_object: be_a(Net::ReadTimeout),
backtrace: be_a(Array)
)
end
context 'when the performance bar is not enabled' do
before do
Gitlab::SafeRequestStore.delete(:peek_enabled)
end
it 'does not capture detail store' do
subscriber.request(event_1)
subscriber.request(event_2)
subscriber.request(event_3)
expect(Gitlab::SafeRequestStore[:external_http_detail_store]).to be(nil)
end
end
end
end
|