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, FacadeSegment

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.



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

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

#serviceObject

Returns the value of attribute service.



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

def service
  @service
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



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

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

#decrement_ref_counterObject



49
50
51
# File 'lib/aws-xray-sdk/model/segment.rb', line 49

def decrement_ref_counter
  @ref_counter -= 1
end

#ready_to_send?Boolean

Returns:

  • (Boolean)


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

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

#remove_subsegment(subsegment:) ⇒ Object



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

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

#sampling_rule_name=(v) ⇒ Object



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

def sampling_rule_name=(v)
  @aws ||= {}
  @aws[:xray] ||= {}
  @aws[:xray][:sampling_rule_name] = v
end

#segmentObject



67
68
69
# File 'lib/aws-xray-sdk/model/segment.rb', line 67

def segment
  self
end

#to_hObject



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

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[:service] = service if service
  h
end

#trace_idObject



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

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