Class: Aws::Xray::Segment
- Inherits:
-
Object
- Object
- Aws::Xray::Segment
- Defined in:
- lib/aws/xray/segment.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
-
#trace_id ⇒ Object
readonly
Returns the value of attribute trace_id.
Class Method Summary collapse
Instance Method Summary collapse
- #add_annotation(annotation) ⇒ Object (also: #set_annotation)
- #add_metadata(metadata) ⇒ Object (also: #set_metadata)
- #finish(now = Time.now) ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(name:, trace_id:, parent_id: nil) ⇒ Segment
constructor
A new instance of Segment.
- #set_error(error: false, throttle: false, fault: false, e: nil, remote: false, cause: nil) ⇒ Object
- #set_http_request(request) ⇒ Object
- #set_http_response(status, length) ⇒ Object
-
#set_http_response_with_error(status, length, remote:) ⇒ Object
Automatically set error according to status code.
- #start(now = Time.now) ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(name:, trace_id:, parent_id: nil) ⇒ Segment
Returns a new instance of Segment.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/aws/xray/segment.rb', line 21 def initialize(name:, trace_id:, parent_id: nil) @name = name @id = SecureRandom.hex(8) @trace_id = trace_id @parent_id = parent_id @version = Aws::Xray.config.version start @end_time = nil @http_request = nil @http_response = nil @error = nil @annotation = Aws::Xray.config.default_annotation @metadata = Aws::Xray.config. end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
19 20 21 |
# File 'lib/aws/xray/segment.rb', line 19 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/aws/xray/segment.rb', line 19 def name @name end |
#parent_id ⇒ Object (readonly)
Returns the value of attribute parent_id.
19 20 21 |
# File 'lib/aws/xray/segment.rb', line 19 def parent_id @parent_id end |
#trace_id ⇒ Object (readonly)
Returns the value of attribute trace_id.
19 20 21 |
# File 'lib/aws/xray/segment.rb', line 19 def trace_id @trace_id end |
Class Method Details
.build(name, trace) ⇒ Object
14 15 16 |
# File 'lib/aws/xray/segment.rb', line 14 def build(name, trace) new(name: name, trace_id: trace.root, parent_id: trace.parent) end |
Instance Method Details
#add_annotation(annotation) ⇒ Object Also known as: set_annotation
79 80 81 |
# File 'lib/aws/xray/segment.rb', line 79 def add_annotation(annotation) @annotation = @annotation.merge(AnnotationNormalizer.call(annotation)) end |
#add_metadata(metadata) ⇒ Object Also known as: set_metadata
85 86 87 |
# File 'lib/aws/xray/segment.rb', line 85 def () @metadata = @metadata.merge() end |
#finish(now = Time.now) ⇒ Object
94 95 96 |
# File 'lib/aws/xray/segment.rb', line 94 def finish(now = Time.now) @end_time = now.to_f end |
#finished? ⇒ Boolean
98 99 100 |
# File 'lib/aws/xray/segment.rb', line 98 def finished? !!@end_time end |
#set_error(error: false, throttle: false, fault: false, e: nil, remote: false, cause: nil) ⇒ Object
73 74 75 |
# File 'lib/aws/xray/segment.rb', line 73 def set_error(error: false, throttle: false, fault: false, e: nil, remote: false, cause: nil) @error = Error.new(error, throttle, fault, e, remote, cause) end |
#set_http_request(request) ⇒ Object
37 38 39 |
# File 'lib/aws/xray/segment.rb', line 37 def set_http_request(request) @http_request = request end |
#set_http_response(status, length) ⇒ Object
43 44 45 |
# File 'lib/aws/xray/segment.rb', line 43 def set_http_response(status, length) @http_response = Response.new(status, length) end |
#set_http_response_with_error(status, length, remote:) ⇒ Object
Automatically set error according to status code.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/aws/xray/segment.rb', line 51 def set_http_response_with_error(status, length, remote:) set_http_response(status, length) type = remote ? 'http_request_error' : 'http_response_error' case status.to_i when 429 cause = Cause.new(stack: caller, message: 'Got 429', type: type) set_error(error: true, remote: remote, throttle: true, cause: cause) when 400..499 cause = Cause.new(stack: caller, message: 'Got 4xx', type: type) set_error(error: true, remote: remote, cause: cause) when 500..599 cause = Cause.new(stack: caller, message: 'Got 5xx', type: type) set_error(fault: true, remote: remote, cause: cause) else # pass end end |
#start(now = Time.now) ⇒ Object
90 91 92 |
# File 'lib/aws/xray/segment.rb', line 90 def start(now = Time.now) @start_time = now.to_f end |
#to_h ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/aws/xray/segment.rb', line 106 def to_h h = { name: @name, id: @id, trace_id: @trace_id, start_time: @start_time, annotations: @annotation, metadata: @metadata, } if @version h[:service] = { version: @version } end if @http_request request_hash = @http_request.to_h # traced is Subsegment only request_hash.delete(:traced) h[:http] = { request: request_hash } end if @http_response h[:http] ||= {} h[:http][:response] = @http_response.to_h end if @end_time.nil? h[:in_progress] = true else h[:end_time] = @end_time end if @error h.merge!(@error.to_h) end h[:parent_id] = @parent_id if @parent_id h end |
#to_json ⇒ Object
102 103 104 |
# File 'lib/aws/xray/segment.rb', line 102 def to_json to_h.to_json end |