Class: Aws::Xray::Context

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

Constant Summary collapse

VAR_NAME =
:_aws_xray_context_

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, client, trace, base_segment_id = nil) ⇒ Context

client and trace are frozen by default.



54
55
56
57
58
59
60
61
62
# File 'lib/aws/xray/context.rb', line 54

def initialize(name, client, trace, base_segment_id = nil)
  raise 'name is required' unless name
  @name = name
  @client = client
  @trace = trace
  @base_segment_id = base_segment_id
  @disabled_ids = []
  @sub_segment_name = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.currentAws::Xray::Context

Returns:



11
12
13
# File 'lib/aws/xray/context.rb', line 11

def current
  Thread.current.thread_variable_get(VAR_NAME) || raise(NotSetError)
end

.started?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/aws/xray/context.rb', line 24

def started?
  !!Thread.current.thread_variable_get(VAR_NAME)
end

.with_given_context(context) ⇒ Object

Parameters:



16
17
18
19
20
21
# File 'lib/aws/xray/context.rb', line 16

def with_given_context(context)
  Thread.current.thread_variable_set(VAR_NAME, context)
  yield
ensure
  remove_current
end

.with_new_context(name, client, trace) {|Aws::Xray::Context| ... } ⇒ Object

Parameters:

  • name (String)

    logical name of this tracing context.

  • client (Aws::Xray::Client)
  • trace (Aws::Xray::Trace)

    newly generated trace or created with HTTP request header.

Yields:



33
34
35
36
37
38
# File 'lib/aws/xray/context.rb', line 33

def with_new_context(name, client, trace)
  build_current(name, client, trace)
  yield
ensure
  remove_current
end

Instance Method Details

#base_trace {|Aws::Xray::Segment| ... } ⇒ Object

Rescue all exceptions and record the exception to the segment. Then re-raise the exception.

Yields:

Returns:

  • (Object)

    A value which given block returns.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aws/xray/context.rb', line 80

def base_trace
  base_segment = Segment.build(@name, @trace)
  @base_segment_id = base_segment.id

  begin
    yield base_segment
  rescue Exception => e
    base_segment.set_error(fault: true, e: e)
    raise e
  ensure
    base_segment.finish
    @client.send_segment(base_segment)
  end
end

#child_trace(remote:, name:) {|Aws::Xray::SubSegment| ... } ⇒ Object

Returns A value which given block returns.

Parameters:

  • remote (Boolean)
  • name (String)

    Arbitrary name of the sub segment. e.g. “funccall_f”.

Yields:

Returns:

  • (Object)

    A value which given block returns.

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aws/xray/context.rb', line 99

def child_trace(remote:, name:)
  raise SegmentDidNotStartError unless @base_segment_id
  sub = SubSegment.build(@trace, @base_segment_id, remote: remote, name: overwrite_name(name))

  begin
    yield sub
  rescue Exception => e
    sub.set_error(fault: true, e: e)
    raise e
  ensure
    sub.finish
    @client.send_segment(sub)
  end
end

#copyObject

Curretly context object is thread safe, so copying is not necessary, but in case we need this, offer copy interface for multi threaded environment.

client and trace should be imutable and thread-safe.

See README for example.



71
72
73
# File 'lib/aws/xray/context.rb', line 71

def copy
  self.class.new(@name.dup, @client.copy, @trace.copy, @base_segment_id ? @base_segment_id.dup : nil)
end

#disable_trace(id) ⇒ Object

Temporary disabling tracing for given id in given block. CAUTION: the disabling will NOT be propagated between threads!!

Parameters:

  • id (Symbol)

    must be unique between tracing methods.



118
119
120
121
122
123
124
125
126
# File 'lib/aws/xray/context.rb', line 118

def disable_trace(id)
  @disabled_ids << id

  begin
    yield
  ensure
    @disabled_ids.delete(id)
  end
end

#disabled?(id) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/aws/xray/context.rb', line 128

def disabled?(id)
  @disabled_ids.include?(id)
end

#overwrite(name:) ⇒ Object

Temporary overwrite sub segment with given data in given block. The overwriting will be occured only one time. CAUTION: the injection will NOT be propagated between threads!!

Parameters:

  • name (String)


137
138
139
140
141
142
143
144
145
# File 'lib/aws/xray/context.rb', line 137

def overwrite(name:)
  @sub_segment_name = name.to_s

  begin
    yield
  ensure
    @sub_segment_name = nil
  end
end