Class: Gruf::Instrumentation::Base

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gruf/instrumentation/base.rb

Overview

Base class for an instrumentation strategy. Define a call method to utilize functionality.

Direct Known Subclasses

OutputMetadataTimer, RequestLogging::Hook, Statsd

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(service, options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • service (Gruf::Service)

    The service to instrument

  • options (Hash) (defaults to: {})

    (Optional) Options to use when instrumenting the call



36
37
38
39
40
# File 'lib/gruf/instrumentation/base.rb', line 36

def initialize(service, options = {})
  @service = service
  @options = options
  setup
end

Instance Attribute Details

#optionsHash (readonly)

Returns options Options to use when instrumenting the call.

Returns:

  • (Hash)

    options Options to use when instrumenting the call



30
31
32
# File 'lib/gruf/instrumentation/base.rb', line 30

def options
  @options
end

#serviceGruf::Service (readonly)

Returns service The service to instrument.

Returns:



28
29
30
# File 'lib/gruf/instrumentation/base.rb', line 28

def service
  @service
end

Instance Method Details

#call(_rc) ⇒ Object

This method is abstract.

Abstract method that is required for implementing an instrumentation strategy.

Parameters:

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/gruf/instrumentation/base.rb', line 65

def call(_rc)
  raise NotImplementedError
end

#method_key(call_signature, delimiter: '.') ⇒ String

Parse the method signature into a service.method name format

Parameters:

  • call_signature (Symbol)

    The method call signature

  • delimiter (String) (defaults to: '.')

    The delimiter to separate service and method keys

Returns:

  • (String)

    The parsed service method name



109
110
111
# File 'lib/gruf/instrumentation/base.rb', line 109

def method_key(call_signature, delimiter: '.')
  "#{service_key}#{delimiter}#{call_signature}"
end

#outer_around(call_signature, request, active_call, &_block) ⇒ Object

Hook into the outer_around call to time the request, and pass that to the call method

Parameters:

  • call_signature (Symbol)

    The method being called

  • request (Object)

    The request object

  • active_call (GRPC::ActiveCall)

    The gRPC active call object

  • &_block (Proc)

    The execution block for the call

Returns:

  • (Object)

    result The result of the block that was called



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gruf/instrumentation/base.rb', line 78

def outer_around(call_signature, request, active_call, &_block)
  timed = Timer.time do
    yield
  end
  rc = RequestContext.new(
    service: service,
    request: request,
    response: timed.result,
    execution_time: timed.time,
    call_signature: call_signature,
    active_call: active_call
  )
  call(rc)
  raise rc.response unless rc.success?
  rc.response
end

#service_keyString

Returns the service name as a translated name separated by periods

Returns:

  • (String)

    Returns the service name as a translated name separated by periods



98
99
100
# File 'lib/gruf/instrumentation/base.rb', line 98

def service_key
  service.class.name.underscore.tr('/', '.')
end

#setupObject

Useful for setting up an instrumentation module post instantiation



45
46
47
# File 'lib/gruf/instrumentation/base.rb', line 45

def setup
  # noop
end

#success?(response) ⇒ Boolean

Was this call a success? If a response is a GRPC::BadStatus object, we assume that it was unsuccessful

Parameters:

  • response (Object)

    The gRPC response object

Returns:

  • (Boolean)

    True if was a successful call



55
56
57
# File 'lib/gruf/instrumentation/base.rb', line 55

def success?(response)
  !response.is_a?(GRPC::BadStatus)
end