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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gruf/instrumentation/request_context.rb', line 41

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



34
35
36
# File 'lib/gruf/instrumentation/request_context.rb', line 34

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



31
32
33
# File 'lib/gruf/instrumentation/request_context.rb', line 31

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



36
37
38
# File 'lib/gruf/instrumentation/request_context.rb', line 36

def execution_time
  @execution_time
end

#requestObject (readonly)

Returns request The protobuf request object.

Returns:

  • (Object)

    request The protobuf request object



27
28
29
# File 'lib/gruf/instrumentation/request_context.rb', line 27

def request
  @request
end

#responseObject (readonly)

Returns response The protobuf response object.

Returns:

  • (Object)

    response The protobuf response object



29
30
31
# File 'lib/gruf/instrumentation/request_context.rb', line 29

def response
  @response
end

#serviceGruf::Service (readonly)

Returns service The service to instrument.

Returns:



25
26
27
# File 'lib/gruf/instrumentation/request_context.rb', line 25

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



77
78
79
# File 'lib/gruf/instrumentation/request_context.rb', line 77

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



67
68
69
# File 'lib/gruf/instrumentation/request_context.rb', line 67

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



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

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