Class: THTP::Server::Instrumentation::Metrics

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/thtp/server/instrumentation.rb

Overview

A THTP::Server Server subscriber for RPC metrics reporting

Constant Summary collapse

INBOUND_RPC_STAT =
'rpc.incoming'
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

Methods included from Utils

#args_class, #canonical_name, #deserialize, #deserialize_buffer, #deserialize_stream, #elapsed_ms, #extract_rpcs, #get_time, #jsonify, #result_class, #serialize, #serialize_buffer, #serialize_stream

Constructor Details

#initialize(statsd) ⇒ Metrics

Returns a new instance of Metrics.



21
22
23
24
25
26
# File 'lib/thtp/server/instrumentation.rb', line 21

def initialize(statsd)
  unless defined?(Datadog::Statsd) && statsd.is_a?(Datadog::Statsd)
    raise ArgumentError, 'Only dogstatsd is supported'
  end
  @statsd = statsd
end

Instance Method Details

#internal_error(request:, error:, time:) ⇒ Object

An unknown error occurred

Parameters:

  • request (Rack::Request)

    The inbound HTTP request

  • error (Exception)

    The to-be-serialized exception

  • time (Integer)

    Milliseconds of execution wall time



65
66
67
68
# File 'lib/thtp/server/instrumentation.rb', line 65

def internal_error(request:, error:, time:)
  tags = [INTERNAL_ERROR_TAG, "rpc.error:#{canonical_name(error.class)}"]
  @statsd.timing(INBOUND_RPC_STAT, time, tags: tags)
end

#rpc_error(request:, rpc:, args:, error:, time:) ⇒ Object

Handler raised an unexpected error

Parameters:

  • request (Rack::Request)

    The inbound HTTP request

  • rpc (Symbol)

    The name of the RPC

  • args (Thrift::Struct?)

    The deserialized thrift args

  • error (THTP::ServerError)

    The to-be-serialized exception

  • time (Integer)

    Milliseconds of execution wall time



56
57
58
59
# File 'lib/thtp/server/instrumentation.rb', line 56

def rpc_error(request:, rpc:, args:, error:, time:)
  tags = ["rpc:#{rpc}", ERROR_TAG, "rpc.error:#{canonical_name(error.class)}"]
  @statsd.timing(INBOUND_RPC_STAT, time, tags: tags)
end

#rpc_exception(request:, rpc:, args:, exception:, time:) ⇒ Object

Handler raised an exception defined in the schema

Parameters:

  • request (Rack::Request)

    The inbound HTTP request

  • rpc (Symbol)

    The name of the RPC

  • args (Thrift::Struct)

    The deserialized thrift args

  • exception (Thrift::Struct)

    The to-be-serialized thrift exception

  • time (Integer)

    Milliseconds of execution wall time



45
46
47
48
# File 'lib/thtp/server/instrumentation.rb', line 45

def rpc_exception(request:, rpc:, args:, exception:, time:)
  tags = ["rpc:#{rpc}", EXCEPTION_TAG, "rpc.error:#{canonical_name(exception.class)}"]
  @statsd.timing(INBOUND_RPC_STAT, time, tags: tags)
end

#rpc_success(request:, rpc:, args:, result:, time:) ⇒ Object

Everything went according to plan

Parameters:

  • request (Rack::Request)

    The inbound HTTP request

  • rpc (Symbol)

    The name of the RPC

  • args (Thrift::Struct)

    The deserialized thrift args

  • result (Thrift::Struct)

    The to-be-serialized thrift response

  • time (Integer)

    Milliseconds of execution wall time



34
35
36
37
# File 'lib/thtp/server/instrumentation.rb', line 34

def rpc_success(request:, rpc:, args:, result:, time:)
  tags = ["rpc:#{rpc}", SUCCESS_TAG]
  @statsd.timing(INBOUND_RPC_STAT, time, tags: tags)
end