Class: Aws::Xray::Context
- Inherits:
-
Object
- Object
- Aws::Xray::Context
- Defined in:
- lib/aws/xray/context.rb
Defined Under Namespace
Classes: BaseError, NotSetError, SegmentDidNotStartError
Constant Summary collapse
- VAR_NAME =
:_aws_xray_context_
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .current ⇒ Aws::Xray::Context
- .set_current(context) ⇒ Object
- .started? ⇒ Boolean
- .with_new_context(name, client, trace) {|Aws::Xray::Context| ... } ⇒ Object
Instance Method Summary collapse
-
#base_trace {|Aws::Xray::Segment| ... } ⇒ Object
Rescue standard errors and record the error to the segment.
-
#child_trace(remote:, name:) {|Aws::Xray::SubSegment| ... } ⇒ Object
Rescue standard errors and record the error to the sub segment.
-
#copy ⇒ Object
Curretly context object is thread safe, so copying is not necessary, but in case we need this, offer copy interface for multi threaded environment.
-
#disable_trace(id) ⇒ Object
Temporary disabling tracing for given id in given block.
- #disabled?(id) ⇒ Boolean
-
#initialize(name, client, trace, base_segment_id = nil) ⇒ Context
constructor
client and trace are frozen by default.
Constructor Details
#initialize(name, client, trace, base_segment_id = nil) ⇒ Context
client and trace are frozen by default.
65 66 67 68 69 70 71 72 |
# File 'lib/aws/xray/context.rb', line 65 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 = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
62 63 64 |
# File 'lib/aws/xray/context.rb', line 62 def name @name end |
Class Method Details
.current ⇒ Aws::Xray::Context
25 26 27 |
# File 'lib/aws/xray/context.rb', line 25 def current Thread.current.thread_variable_get(VAR_NAME) || raise(NotSetError) end |
.set_current(context) ⇒ Object
30 31 32 |
# File 'lib/aws/xray/context.rb', line 30 def set_current(context) Thread.current.thread_variable_set(VAR_NAME, context) end |
.started? ⇒ Boolean
35 36 37 |
# File 'lib/aws/xray/context.rb', line 35 def started? !!Thread.current.thread_variable_get(VAR_NAME) end |
.with_new_context(name, client, trace) {|Aws::Xray::Context| ... } ⇒ Object
44 45 46 47 48 49 |
# File 'lib/aws/xray/context.rb', line 44 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 standard errors and record the error to the segment. Then re-raise the error.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/aws/xray/context.rb', line 90 def base_trace base_segment = Segment.build(@name, @trace) @base_segment_id = base_segment.id begin yield base_segment rescue => 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
Rescue standard errors and record the error to the sub segment. Then re-raise the error.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/aws/xray/context.rb', line 112 def child_trace(remote:, name:) raise SegmentDidNotStartError unless @base_segment_id sub = SubSegment.build(@trace, @base_segment_id, remote: remote, name: name) begin yield sub rescue => e sub.set_error(fault: true, e: e) raise e ensure sub.finish @client.send_segment(sub) end end |
#copy ⇒ Object
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.
81 82 83 |
# File 'lib/aws/xray/context.rb', line 81 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!!
131 132 133 134 135 136 137 138 139 |
# File 'lib/aws/xray/context.rb', line 131 def disable_trace(id) @disabled_ids << id begin yield ensure @disabled_ids.delete(id) end end |
#disabled?(id) ⇒ Boolean
141 142 143 |
# File 'lib/aws/xray/context.rb', line 141 def disabled?(id) @disabled_ids.include?(id) end |