Class: Aws::Xray::Context

Inherits:
Object
  • Object
show all
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

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.



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

#nameObject (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

.currentAws::Xray::Context

Returns:



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

Parameters:



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

Returns:

  • (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

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:



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.

Yields:

Returns:

  • (Object)

    A value which given block returns.



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.

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:



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

#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.



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!!

Parameters:

  • id (Symbol)

    must be unique between tracing methods.



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

Returns:

  • (Boolean)


141
142
143
# File 'lib/aws/xray/context.rb', line 141

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