使用yabeda监控grpc
环境搭建见[[grpc生成ruby代码]]
拦截某些方法……
require 'yabeda'
require 'yabeda/prometheus'
module ::GRPC
class ActiveCall
module Interceptor
def send_status(*args, **opts)
yabeda_grpc_rpc_server_send_status_increment(args[0])
super
end
def server_unary_response(*args, **opts)
code = opts[:code] || ::GRPC::Core::StatusCodes::OK
yabeda_grpc_rpc_server_send_status_increment(code)
super
end
private
def yabeda_grpc_rpc_server_send_status_increment(code)
code_str = ::GRPC::Core::StatusCodes.code_map[code]
Yabeda.grpc_rpc_server_send_status.increment({code: code_str})
end
end
prepend Interceptor
end
module Core
module StatusCodes
def self.code_map
@code_map ||= constants.each_with_object({}) do |const, hash|
num = const_get(const)
hash[num] = const.to_s
end
end
code_map
end
end
end
Yabeda.configure do
group :grpc_rpc_server do
counter :send_status, comment: '::GRPC::Core::StatusCodes', tags: %i[code]
end
end
Yabeda.configure!
Yabeda::Prometheus::Exporter.start_metrics_server!