Skip to content

Instantly share code, notes, and snippets.

@quixoten
Created February 27, 2014 16:56
Show Gist options
  • Select an option

  • Save quixoten/9254205 to your computer and use it in GitHub Desktop.

Select an option

Save quixoten/9254205 to your computer and use it in GitHub Desktop.
module Protobuf
module Rpc
class LogSubscriber < ::ActiveSupport::Subscriber
include ::Protobuf::Logging
TIMING_FORMAT = '%s: %.1fms'.freeze
RECEIVED_FORMAT = '[%s] Received %s#%s from %s'.freeze
COMPLETED_FORMAT = '[%s] Completed in %.1fms (%s)'.freeze
def decode_request(event)
add_timing('Decode', event)
end
def dispatch_request(event)
add_timing('Dispatch', event)
end
def encode_request(event)
add_timing('Encode', event)
end
def handle_request(event)
logger.info do
COMPLETED_FORMAT % [
thread_id,
event.duration,
event.children.map { |child| child.payload['timing'] }.compact.join(' | ')
]
end
end
def finish(name, id, env)
super
if name == "decode_request.protobuf"
logger.info do
RECEIVED_FORMAT % [
thread_id,
env['service_name'] || "Unknown",
env['method_name'] || "unknown",
env['client_host']
]
end
end
end
private
def add_timing(label, event)
event.payload['timing'] = TIMING_FORMAT % [label, event.duration]
end
def thread_id
Thread.current.object_id.to_s(36)
end
end
end
end
::Protobuf::Rpc::LogSubscriber.attach_to 'protobuf'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment