Class: XRay::Segment

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

Overview

The compute resources running your application logic send data about their work as segments. A segment provides the resource’s name, details about the request, and details about the work done.

Direct Known Subclasses

DummySegment

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, #close, #closed?, #id, #merge_http_request, #merge_http_response, #metadata, #subsegments, #to_json

Constructor Details

#initialize(trace_id: nil, name: nil, parent_id: nil) ⇒ Segment

Returns a new instance of Segment.

Parameters:

  • trace_id (String) (defaults to: nil)

    Manually crafted trace id.

  • name (String) (defaults to: nil)

    Must be specified either on object creation or on environment variable ‘AWS_TRACING_NAME`. The latter has higher precedence.

  • parent_id (String) (defaults to: nil)

    ID of the segment/subsegment representing the upstream caller.



15
16
17
18
19
20
21
22
23
# File 'lib/aws-xray-sdk/model/segment.rb', line 15

def initialize(trace_id: nil, name: nil, parent_id: nil)
  @trace_id = trace_id
  @name = ENV['AWS_TRACING_NAME'] || name
  @parent_id = parent_id
  @start_time = Time.now.to_f
  @ref_counter = 0
  @subsegment_size = 0
  @sampled = true
end

Instance Attribute Details

#originObject

Returns the value of attribute origin.



9
10
11
# File 'lib/aws-xray-sdk/model/segment.rb', line 9

def origin
  @origin
end

#ref_counterObject

Returns the value of attribute ref_counter.



9
10
11
# File 'lib/aws-xray-sdk/model/segment.rb', line 9

def ref_counter
  @ref_counter
end

#subsegment_sizeObject

Returns the value of attribute subsegment_size.



9
10
11
# File 'lib/aws-xray-sdk/model/segment.rb', line 9

def subsegment_size
  @subsegment_size
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/aws-xray-sdk/model/segment.rb', line 9

def user
  @user
end

Instance Method Details

#add_subsegment(subsegment:) ⇒ Object



31
32
33
34
35
# File 'lib/aws-xray-sdk/model/segment.rb', line 31

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

#decrement_ref_counterObject



42
43
44
# File 'lib/aws-xray-sdk/model/segment.rb', line 42

def decrement_ref_counter
  @ref_counter -= 1
end

#ready_to_send?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/aws-xray-sdk/model/segment.rb', line 46

def ready_to_send?
  closed? && ref_counter.zero?
end

#remove_subsegment(subsegment:) ⇒ Object



37
38
39
40
# File 'lib/aws-xray-sdk/model/segment.rb', line 37

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

#segmentObject



59
60
61
# File 'lib/aws-xray-sdk/model/segment.rb', line 59

def segment
  self
end

#to_hObject



50
51
52
53
54
55
56
57
# File 'lib/aws-xray-sdk/model/segment.rb', line 50

def to_h
  h = super
  h[:trace_id] = trace_id
  h[:origin] = origin if origin
  h[:parent_id] = @parent_id if @parent_id
  h[:user] = user if user
  h
end

#trace_idObject



25
26
27
28
29
# File 'lib/aws-xray-sdk/model/segment.rb', line 25

def trace_id
  @trace_id ||= begin
    %[1-#{Time.now.to_i.to_s(16)}-#{SecureRandom.hex(12)}]
  end
end