Class: Aws::Xray::Subsegment

Inherits:
Segment
  • Object
show all
Defined in:
lib/aws/xray/subsegment.rb

Overview

Constant Summary collapse

TYPE_NAME =
'subsegment'.freeze

Instance Attribute Summary

Attributes inherited from Segment

#id, #name, #parent_id, #trace_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Segment

#add_annotation, #add_metadata, #finish, #finished?, #set_error, #set_http_response, #set_http_response_with_error, #start, #to_json

Constructor Details

#initialize(name:, trace:, parent_id:, remote:) ⇒ Subsegment

Returns a new instance of Subsegment.



20
21
22
23
24
25
# File 'lib/aws/xray/subsegment.rb', line 20

def initialize(name:, trace:, parent_id:, remote:)
  super(name: name, trace_id: trace.root, parent_id: parent_id)
  @trace = trace
  @remote = !!remote
  @sql = nil
end

Class Method Details

.build(trace, parent_id, remote:, name:) ⇒ Object

Parameters:

  • remote (Boolean)


9
10
11
# File 'lib/aws/xray/subsegment.rb', line 9

def self.build(trace, parent_id, remote:, name:)
  new(name: name, trace: trace, parent_id: parent_id, remote: remote)
end

.build_nullObject

Build a subsegment as null object.



14
15
16
# File 'lib/aws/xray/subsegment.rb', line 14

def self.build_null
  build(Trace.generate, SecureRandom.hex(8), remote: true, name: '')
end

Instance Method Details

#generate_traceObject



53
54
55
# File 'lib/aws/xray/subsegment.rb', line 53

def generate_trace
  @trace.copy(parent: @id)
end

#set_http_request(env, traced: false) ⇒ Object

Set traced=false if the downstream call is not traced app. e.g. Third-party Web API call. docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html



30
31
32
33
# File 'lib/aws/xray/subsegment.rb', line 30

def set_http_request(env, traced: false)
  super(env)
  @http_request.traced = traced
end

#set_sql(sql) ⇒ Object

Parameters:



36
37
38
# File 'lib/aws/xray/subsegment.rb', line 36

def set_sql(sql)
  @sql = sql
end

#to_hObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aws/xray/subsegment.rb', line 40

def to_h
  h = super
  # x_forwarded_for is Segment only.
  if h[:http] && h[:http][:request]
    h[:http][:request].delete(:x_forwarded_for)
    h[:http][:request][:traced] = @http_request.traced
  end
  h[:type] = TYPE_NAME
  h[:namespace] = 'remote' if @remote
  h[:sql] = @sql.to_h if @sql
  h
end