Class: Aws::Xray::Segment

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

Overview

Direct Known Subclasses

SubSegment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

TODO: securerandom?



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

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/aws/xray/segment.rb', line 18

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/aws/xray/segment.rb', line 18

def name
  @name
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



18
19
20
# File 'lib/aws/xray/segment.rb', line 18

def parent_id
  @parent_id
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



18
19
20
# File 'lib/aws/xray/segment.rb', line 18

def trace_id
  @trace_id
end

Class Method Details

.build(name, trace) ⇒ Object



13
14
15
# File 'lib/aws/xray/segment.rb', line 13

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

Parameters:

  • annotation (Hash)

    Keys must consist of only alphabets and underscore. Values must be one of String or Integer or Boolean values.



57
58
59
# File 'lib/aws/xray/segment.rb', line 57

def add_annotation(annotation)
  @annotation = @annotation.merge(AnnotationNormalizer.call(annotation))
end

#add_metadata(metadata) ⇒ Object Also known as: set_metadata

Parameters:

  • metadata (Hash)


63
64
65
# File 'lib/aws/xray/segment.rb', line 63

def ()
  @metadata = @metadata.merge()
end

#finish(now = Time.now) ⇒ Object



68
69
70
# File 'lib/aws/xray/segment.rb', line 68

def finish(now = Time.now)
  @end_time = now.to_f
end

#set_error(error: false, throttle: false, fault: false, e: nil, remote: false, cause: nil) ⇒ Object

Parameters:

  • error (Boolean) (defaults to: false)

    Indicating that a client error occurred (response status code was 4XX Client Error).

  • throttle (Boolean) (defaults to: false)

    Indicating that a request was throttled (response status code was 429 Too Many Requests).

  • fault (Boolean) (defaults to: false)

    Indicating that a server error occurred (response status code was 5XX Server Error).

  • e (Exception) (defaults to: nil)

    An Exception object



51
52
53
# File 'lib/aws/xray/segment.rb', line 51

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

Parameters:



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

Parameters:

  • status (Integer)

    HTTP status

  • length (Integer)

    Size of HTTP response body



43
44
45
# File 'lib/aws/xray/segment.rb', line 43

def set_http_response(status, length)
  @http_response = Response.new(status, length)
end

#to_hObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/aws/xray/segment.rb', line 76

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_jsonObject



72
73
74
# File 'lib/aws/xray/segment.rb', line 72

def to_json
  to_h.to_json
end