Skip to content

Commit 1dbe7f7

Browse files
authored
Reduce logcache timeout from 250 to 10s (#4187)
* Reduce logcache timeout from 250 to 10s * Adjust unittest * Add response time in a parsable formation ` * Optimize based on review
1 parent 4c6c470 commit 1dbe7f7

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

lib/logcache/client.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def initialize(host:, port:, client_ca_path:, client_cert_path:, client_key_path
1616
"#{host}:#{port}",
1717
GRPC::Core::ChannelCredentials.new(client_ca, client_key, client_cert),
1818
channel_args: { GRPC::Core::Channel::SSL_TARGET => tls_subject_name },
19-
timeout: 250
19+
timeout: 10
2020
)
2121
else
2222
@service = Logcache::V1::Egress::Stub.new(
2323
"#{host}:#{port}",
2424
:this_channel_is_insecure,
25-
timeout: 250
25+
timeout: 10
2626
)
2727
end
2828
end
@@ -44,9 +44,16 @@ def container_metrics(source_guid:, start_time:, end_time:, envelope_limit: DEFA
4444

4545
private
4646

47-
def with_request_error_handling(_source_guid)
47+
def with_request_error_handling(source_guid)
4848
tries ||= 3
49-
yield
49+
start_time = Time.now
50+
51+
result = yield
52+
time_taken_in_ms = ((Time.now - start_time) * 1000).to_i # convert to milliseconds to get more precise information
53+
logger.info('logcache.response',
54+
{ source_id: source_guid,
55+
time_taken_in_ms: time_taken_in_ms })
56+
result
5057
rescue StandardError => e
5158
raise CloudController::Errors::ApiError.new_from_details('ServiceUnavailable', 'Connection to Log Cache timed out') if e.is_a?(GRPC::DeadlineExceeded)
5259

spec/logcache/client_spec.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Logcache
2626
let(:client_ca) { File.read(client_ca_path) }
2727
let(:client_key) { File.read(client_key_path) }
2828
let(:client_cert) { File.read(client_cert_path) }
29+
let(:mock_logger) { double(Steno::Logger, info: nil) }
2930

3031
describe '#container_metrics' do
3132
let(:instance_count) { 2 }
@@ -36,9 +37,10 @@ module Logcache
3637
with(client_ca, client_key, client_cert).
3738
and_return(credentials)
3839
expect(Logcache::V1::Egress::Stub).to receive(:new).
39-
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 250).
40+
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 10).
4041
and_return(logcache_service)
4142
allow(Logcache::V1::ReadRequest).to receive(:new).and_return(logcache_request)
43+
allow_any_instance_of(Client).to receive(:logger).and_return(mock_logger)
4244
end
4345

4446
it 'calls Logcache with the correct parameters and returns envelopes' do
@@ -56,6 +58,17 @@ module Logcache
5658
)
5759
expect(logcache_service).to have_received(:read).with(logcache_request)
5860
end
61+
62+
it 'logs the response time and metadata' do
63+
client.container_metrics(source_guid: process.guid, envelope_limit: 1000, start_time: 100, end_time: 101)
64+
expect(mock_logger).to have_received(:info).with(
65+
a_string_matching(/logcache.response/),
66+
hash_including(
67+
source_id: process.guid,
68+
time_taken_in_ms: be_a(Integer)
69+
)
70+
)
71+
end
5972
end
6073

6174
describe 'when logcache is unavailable' do
@@ -68,7 +81,7 @@ module Logcache
6881
with(client_ca, client_key, client_cert).
6982
and_return(credentials)
7083
expect(Logcache::V1::Egress::Stub).to receive(:new).
71-
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 250).
84+
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 10).
7285
and_return(logcache_service)
7386
allow(client).to receive(:sleep)
7487
allow(Logcache::V1::ReadRequest).to receive(:new).and_return(logcache_request)
@@ -99,7 +112,7 @@ module Logcache
99112
with(client_ca, client_key, client_cert).
100113
and_return(credentials)
101114
expect(Logcache::V1::Egress::Stub).to receive(:new).
102-
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 250).
115+
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 10).
103116
and_return(logcache_service)
104117
allow(client).to receive(:sleep)
105118
allow(Logcache::V1::ReadRequest).to receive(:new).and_return(logcache_request)
@@ -130,7 +143,7 @@ module Logcache
130143
with(client_ca, client_key, client_cert).
131144
and_return(credentials)
132145
expect(Logcache::V1::Egress::Stub).to receive(:new).
133-
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 250).
146+
with("#{host}:#{port}", credentials, channel_args: channel_arg_hash, timeout: 10).
134147
and_return(logcache_service)
135148
allow(client).to receive(:sleep)
136149
allow(Logcache::V1::ReadRequest).to receive(:new).and_return(logcache_request)
@@ -164,7 +177,7 @@ module Logcache
164177
before do
165178
expect(GRPC::Core::ChannelCredentials).not_to receive(:new)
166179
expect(Logcache::V1::Egress::Stub).to receive(:new).
167-
with("#{host}:#{port}", :this_channel_is_insecure, timeout: 250).
180+
with("#{host}:#{port}", :this_channel_is_insecure, timeout: 10).
168181
and_return(logcache_service)
169182
allow(Logcache::V1::ReadRequest).to receive(:new).and_return(logcache_request)
170183
end

0 commit comments

Comments
 (0)