Skip to content

Commit b2b816f

Browse files
authored
Merge pull request #402 from newellista/add-error-reason-to-logs
Add error reason to logs
2 parents a2e0cbb + 0abd9be commit b2b816f

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

lib/protobuf/rpc/connectors/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ def data_callback(data)
6363
# @param [String] message The error message
6464
def failure(code, message)
6565
@error = ClientError.new
66-
@error.code = ::Protobuf::Socketrpc::ErrorReason.fetch(code)
66+
@stats.status = @error.code = ::Protobuf::Socketrpc::ErrorReason.fetch(code)
6767
@error.message = message
68+
6869
logger.debug { sign_message("Server failed request (invoking on_failure): #{@error.inspect}") }
6970

7071
@failure_cb.call(@error) unless @failure_cb.nil?

lib/protobuf/rpc/stat.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
require 'date'
22
require 'time'
33
require 'protobuf/logging'
4+
require 'protobuf/rpc/rpc.pb'
45

56
module Protobuf
67
module Rpc
78
class Stat
89
attr_accessor :mode, :start_time, :end_time, :request_size, :dispatcher
9-
attr_accessor :response_size, :client, :service, :method_name
10+
attr_accessor :response_size, :client, :service, :method_name, :status
1011
attr_reader :server
1112

1213
MODES = [:SERVER, :CLIENT].freeze
1314

15+
ERROR_TRANSLATIONS = {
16+
::Protobuf::Socketrpc::ErrorReason::BAD_REQUEST_DATA => "BAD_REQUEST_DATA",
17+
::Protobuf::Socketrpc::ErrorReason::BAD_REQUEST_PROTO => "BAD_REQUEST_PROTO",
18+
::Protobuf::Socketrpc::ErrorReason::SERVICE_NOT_FOUND => "SERVICE_NOT_FOUND",
19+
::Protobuf::Socketrpc::ErrorReason::METHOD_NOT_FOUND => "METHOD_NOT_FOUND",
20+
::Protobuf::Socketrpc::ErrorReason::RPC_ERROR => "RPC_ERROR",
21+
::Protobuf::Socketrpc::ErrorReason::RPC_FAILED => "RPC_FAILED",
22+
::Protobuf::Socketrpc::ErrorReason::INVALID_REQUEST_PROTO => "INVALID_REQUEST_PROTO",
23+
::Protobuf::Socketrpc::ErrorReason::BAD_RESPONSE_PROTO => "BAD_RESPONSE_PROTO",
24+
::Protobuf::Socketrpc::ErrorReason::UNKNOWN_HOST => "UNKNOWN_HOST",
25+
::Protobuf::Socketrpc::ErrorReason::IO_ERROR => "IO_ERROR",
26+
}.freeze
27+
1428
def initialize(mode = :SERVER)
1529
@mode = mode
1630
@request_size = 0
@@ -78,6 +92,12 @@ def client?
7892
@mode == :CLIENT
7993
end
8094

95+
def status_string
96+
return "OK" if status.nil?
97+
98+
ERROR_TRANSLATIONS.fetch(status, "UNKNOWN_ERROR")
99+
end
100+
81101
def to_s
82102
[
83103
server? ? "[SRV]" : "[CLT]",
@@ -86,14 +106,14 @@ def to_s
86106
rpc,
87107
sizes,
88108
elapsed_time,
109+
status_string,
89110
@end_time.try(:iso8601),
90111
].compact.join(' - ')
91112
end
92113

93114
def trace_id
94115
::Thread.current.object_id.to_s(16)
95116
end
96-
97117
end
98118
end
99119
end

spec/lib/protobuf/rpc/connectors/base_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@
187187
shared_examples "a ConnectorDisposition" do |meth, cb, *args|
188188

189189
it "calls #complete before exit" do
190-
subject.stats = double("Object", :stop => true)
190+
subject.stats = ::Protobuf::Rpc::Stat.new(:stop => true)
191191

192192
expect(subject).to receive(:complete)
193193
subject.method(meth).call(*args)
194194
end
195195

196196
it "calls the #{cb} callback when provided" do
197-
stats = double("Object")
197+
stats = ::Protobuf::Rpc::Stat.new
198198
allow(stats).to receive(:stop).and_return(true)
199199
subject.stats = stats
200200
some_cb = double("Object")
@@ -205,7 +205,7 @@
205205
end
206206

207207
it "calls the complete callback when provided" do
208-
stats = double("Object")
208+
stats = ::Protobuf::Rpc::Stat.new
209209
allow(stats).to receive(:stop).and_return(true)
210210
subject.stats = stats
211211
comp_cb = double("Object")
@@ -217,8 +217,8 @@
217217

218218
end
219219

220-
it_behaves_like("a ConnectorDisposition", :failure, "failure_cb", "code", "message")
221-
it_behaves_like("a ConnectorDisposition", :failure, "complete_cb", "code", "message")
220+
it_behaves_like("a ConnectorDisposition", :failure, "failure_cb", :RPC_ERROR, "message")
221+
it_behaves_like("a ConnectorDisposition", :failure, "complete_cb", :RPC_ERROR, "message")
222222
it_behaves_like("a ConnectorDisposition", :succeed, "complete_cb", "response")
223223
it_behaves_like("a ConnectorDisposition", :succeed, "success_cb", "response")
224224
it_behaves_like("a ConnectorDisposition", :complete, "complete_cb")

spec/lib/protobuf/rpc/stat_spec.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
::Timecop.freeze(1.62.seconds.from_now) do
3535
stats.stop
36-
expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/1302B - 1.62s - #{::Time.now.iso8601}"
36+
expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/1302B - 1.62s - OK - #{::Time.now.iso8601}"
3737
end
3838
end
3939
end
@@ -44,7 +44,7 @@
4444
stats.client = 'myserver1'
4545
stats.dispatcher = double('dispatcher', :service => BarService.new(:find_bars))
4646
stats.request_size = 43
47-
expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/-"
47+
expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/- - OK"
4848
end
4949
end
5050
end
@@ -61,20 +61,39 @@
6161

6262
::Timecop.freeze(0.832.seconds.from_now) do
6363
stats.stop
64-
expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - #{::Time.now.iso8601}"
64+
expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - OK - #{::Time.now.iso8601}"
6565
end
6666

6767
end
6868
end
6969

70+
describe 'error log' do
71+
it 'resolves error to a string' do
72+
::Timecop.freeze(10.minutes.ago) do
73+
stats = ::Protobuf::Rpc::Stat.new(:CLIENT)
74+
stats.server = ['30000', 'myserver1.myhost.com']
75+
stats.service = 'Foo::BarService'
76+
stats.status = ::Protobuf::Socketrpc::ErrorReason::RPC_ERROR
77+
stats.method_name = 'find_bars'
78+
stats.request_size = 37
79+
stats.response_size = 12345
80+
81+
::Timecop.freeze(0.832.seconds.from_now) do
82+
stats.stop
83+
expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - RPC_ERROR - #{::Time.now.iso8601}"
84+
end
85+
end
86+
end
87+
end
88+
7089
context 'when request is still running' do
7190
it 'omits response size, duration, and timestamp' do
7291
stats = ::Protobuf::Rpc::Stat.new(:CLIENT)
7392
stats.server = ['30000', 'myserver1.myhost.com']
7493
stats.service = 'Foo::BarService'
7594
stats.method_name = 'find_bars'
7695
stats.request_size = 37
77-
expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/-"
96+
expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/- - OK"
7897
end
7998
end
8099
end

0 commit comments

Comments
 (0)