Class: XRay::Subsegment

Inherits:
Object
  • Object
show all
Includes:
Entity
Defined in:
lib/aws-xray-sdk/model/subsegment.rb

Overview

The work done in a single segment can be broke down into subsegments. Subsegments provide more granular timing information and details about downstream calls that your application made to fulfill the original request. A subsegment can contain additional details about a call to an AWS service, an external HTTP API, or an SQL database.

Direct Known Subclasses

DummySubsegment

Constant Summary

Constants included from Entity

Entity::HTTP_REQUEST_KEY, Entity::HTTP_RESPONSE_KEY

Instance Attribute Summary collapse

Attributes included from Entity

#aws, #cause, #end_time, #error, #exception, #fault, #http_request, #http_response, #name, #namespace, #parent, #sampled, #start_time, #throttle

Instance Method Summary collapse

Methods included from Entity

#add_exception, #annotations, #apply_status_code, #cause_id, #closed?, #id, #merge_http_request, #merge_http_response, #metadata, #subsegments, #to_json

Constructor Details

#initialize(name:, segment:, namespace: 'local') ⇒ Subsegment

Returns a new instance of Subsegment.

Parameters:

  • name (String)

    The subsegment name.

  • segment (Segment)

    The root parent segment. This segment may not be its direct parent.

  • namespace (String) (defaults to: 'local')

    Currently supported namespaces are ‘remote’, ‘aws’, ‘local’.



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

def initialize(name:, segment:, namespace: 'local')
  @name = name
  @segment = segment
  @namespace = namespace
  @start_time = Time.now.to_f
  @sampled = true
end

Instance Attribute Details

#segmentObject (readonly)

Returns the value of attribute segment.



12
13
14
# File 'lib/aws-xray-sdk/model/subsegment.rb', line 12

def segment
  @segment
end

#sqlObject

Returns the value of attribute sql.



13
14
15
# File 'lib/aws-xray-sdk/model/subsegment.rb', line 13

def sql
  @sql
end

Instance Method Details

#add_subsegment(subsegment:) ⇒ Object



28
29
30
31
32
# File 'lib/aws-xray-sdk/model/subsegment.rb', line 28

def add_subsegment(subsegment:)
  super subsegment: subsegment
  segment.ref_counter += 1
  segment.subsegment_size += 1
end

#all_children_countObject

Returns the number of its direct and indirect children. This is useful when we remove the reference to a subsegment and need to keep remaining subsegment size accurate.



52
53
54
55
56
# File 'lib/aws-xray-sdk/model/subsegment.rb', line 52

def all_children_count
  size = subsegments.count
  subsegments.each { |v| size += v.all_children_count }
  size
end

#close(end_time: nil) ⇒ Object



40
41
42
43
# File 'lib/aws-xray-sdk/model/subsegment.rb', line 40

def close(end_time: nil)
  super end_time: end_time
  segment.decrement_ref_counter
end

#remove_subsegment(subsegment:) ⇒ Object



34
35
36
37
38
# File 'lib/aws-xray-sdk/model/subsegment.rb', line 34

def remove_subsegment(subsegment:)
  super subsegment: subsegment
  cur = segment.subsegment_size
  segment.subsegment_size = cur - subsegment.all_children_count - 1
end

#to_hObject



58
59
60
61
62
63
64
65
# File 'lib/aws-xray-sdk/model/subsegment.rb', line 58

def to_h
  h = super
  h[:trace_id] = segment.trace_id
  h[:sql] = sql unless sql.empty?
  h[:type] = 'subsegment'
  h[:namespace] = namespace if namespace
  h
end