Class: THTP::Client::Instrumentation::Metrics

Inherits:
Object
  • Object
show all
Defined in:
lib/thtp/client/instrumentation.rb

Overview

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

Constant Summary collapse

OUTBOUND_RPC_STAT =
'rpc.outgoing'
SUCCESS_TAG =

everything is ok

'rpc.status:success'
EXCEPTION_TAG =

schema-defined (expected) exception

'rpc.status:exception'
ERROR_TAG =

unexpected error

'rpc.status:error'
INTERNAL_ERROR_TAG =
'rpc.status:internal_error'

Instance Method Summary collapse

Constructor Details

#initialize(app, from:, to:, statsd:) ⇒ Metrics

Returns a new instance of Metrics.



17
18
19
20
21
22
23
24
# 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

Instance Method Details

#call(rpc, *rpc_args, **rpc_opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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