Class: Gruf::Instrumentation::RequestContext

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/instrumentation/request_context.rb

Overview

Represents a request context for a given incoming gRPC request. This represents an injection layer that is used to pass to instrumentation strategies safely across thread boundaries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service:, request:, response:, call_signature:, active_call:, execution_time: 0.00) ⇒ RequestContext

Initialize the request context given the request and response



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gruf/instrumentation/request_context.rb', line 25

def initialize(
  service:,
  request:,
  response:,
  call_signature:,
  active_call:,
  execution_time: 0.00
)
  @service = service
  @request = request
  @response = response
  @call_signature = call_signature
  @active_call = active_call
  @execution_time = execution_time
end

Instance Attribute Details

#active_callGRPC::ActiveCall (readonly)

the call itself

Returns:

  • (GRPC::ActiveCall)

    active_call The gRPC core active call object, which represents marshalled data for



18
19
20
# File 'lib/gruf/instrumentation/request_context.rb', line 18

def active_call
  @active_call
end

#call_signatureSymbol (readonly)

Returns call_signature The gRPC method on the service that was called.

Returns:

  • (Symbol)

    call_signature The gRPC method on the service that was called



15
16
17
# File 'lib/gruf/instrumentation/request_context.rb', line 15

def call_signature
  @call_signature
end

#execution_timeTime (readonly)

Returns execution_time The execution time, in ms, of the request.

Returns:

  • (Time)

    execution_time The execution time, in ms, of the request



20
21
22
# File 'lib/gruf/instrumentation/request_context.rb', line 20

def execution_time
  @execution_time
end

#requestObject (readonly)

Returns request The protobuf request object.

Returns:

  • (Object)

    request The protobuf request object



11
12
13
# File 'lib/gruf/instrumentation/request_context.rb', line 11

def request
  @request
end

#responseObject (readonly)

Returns response The protobuf response object.

Returns:

  • (Object)

    response The protobuf response object



13
14
15
# File 'lib/gruf/instrumentation/request_context.rb', line 13

def response
  @response
end

#serviceGruf::Service (readonly)

Returns service The service to instrument.

Returns:



9
10
11
# File 'lib/gruf/instrumentation/request_context.rb', line 9

def service
  @service
end

Instance Method Details

#execution_time_rounded(precision: 2) ⇒ Float

Return the execution time rounded to a specified precision

Parameters:

  • precision (Integer) (defaults to: 2)

    The amount of decimal places to round to

Returns:

  • (Float)

    The execution time rounded to the appropriate decimal point



61
62
63
# File 'lib/gruf/instrumentation/request_context.rb', line 61

def execution_time_rounded(precision: 2)
  execution_time.to_f.round(precision)
end

#response_class_nameString

Return the response class name

Returns:

  • (String)

    Return the response class name



51
52
53
# File 'lib/gruf/instrumentation/request_context.rb', line 51

def response_class_name
  response.class.name
end

#success?Boolean

Returns True if the response is successful.

Returns:

  • (Boolean)

    True if the response is successful



44
45
46
# File 'lib/gruf/instrumentation/request_context.rb', line 44

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