class THTP::Client::Instrumentation::Metrics

Automagic instrumentation for all outbound RPCs as a THTP::Client middleware

Constants

ERROR_TAG
EXCEPTION_TAG
INTERNAL_ERROR_TAG
OUTBOUND_RPC_STAT
SUCCESS_TAG

Public Class Methods

new(app, from:, to:, statsd:) click to toggle source
# File lib/thtp/client/instrumentation.rb, line 17
def initialize(app, from:, to:, statsd:)
  unless defined?(Datadog::Statsd) && statsd.is_a?(Datadog::Statsd)
    raise ArgumentError, "Only dogstatsd is supported, not #{statsd.class.name}"
  end
  @app = app
  @statsd = statsd
  @base_tags = ["rpc.from:#{from}", "rpc.to:#{to}"]
end

Public Instance Methods

call(rpc, *rpc_args, **rpc_opts) click to toggle source
# File lib/thtp/client/instrumentation.rb, line 26
def call(rpc, *rpc_args, **rpc_opts)
  start = Utils.get_time
  status_tag = SUCCESS_TAG
  error_tag = nil
  @app.call(rpc, *rpc_args, **rpc_opts)
rescue Thrift::Exception => e
  status_tag = EXCEPTION_TAG
  error_tag = "rpc.exception:#{e.class.name.underscore}"
  raise
rescue => e
  status_tag = ERROR_TAG
  error_tag = "rpc.error:#{e.class.name.underscore}"
  raise
ensure
  tags = ["rpc:#{rpc}", status_tag, error_tag, *@base_tags].compact
  @statsd.timing(OUTBOUND_RPC_STAT, Utils.elapsed_ms(start), tags: tags)
end