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, trace, base_segment_id = nil) ⇒ Context

Returns a new instance of Context.



52
53
54
55
56
57
58
59
# File 'lib/aws/xray/context.rb', line 52

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

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, trace) {|Aws::Xray::Context| ... } ⇒ Object

Parameters:

  • name (String)

    logical name of this tracing context.

  • trace (Aws::Xray::Trace)

    newly generated trace or created with HTTP request header.

Yields:



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

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

Instance Method Details

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

Trace should be imutable and thread-safe.

See README for example.



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

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

#disable_trace(id) ⇒ Object

Use ‘Aws::Xray.disable_trace` instead of this.

Parameters:

  • id (Symbol)

    must be unique between tracing methods.



114
115
116
117
118
119
120
121
122
# File 'lib/aws/xray/context.rb', line 114

def disable_trace(id)
  @disabled_ids << id

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

#disabled?(id) ⇒ Boolean

Use ‘Aws::Xray.disabled?` instead of this.

Parameters:

  • id (Symbol)

Returns:

  • (Boolean)


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

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

#overwrite(name:) ⇒ Object

Parameters:

  • name (String)


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/aws/xray/context.rb', line 132

def overwrite(name:)
  return yield if @subsegment_name

  @subsegment_name = name.to_s

  begin
    yield
  ensure
    @subsegment_name = nil
  end
end

#start_segment {|Aws::Xray::Segment| ... } ⇒ Object Also known as: base_trace

Use ‘Aws::Xray.trace` instead of this.

Yields:

Returns:

  • (Object)

    A value which given block returns.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/aws/xray/context.rb', line 75

def start_segment
  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 unless base_segment.finished?
    Client.send_segment(base_segment) if @trace.sampled?
  end
end

#start_subsegment(remote:, name:) {|Aws::Xray::Subsegment| ... } ⇒ Object Also known as: child_trace

Use ‘Aws::Xray.start_subsegment` instead of this.

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:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/aws/xray/context.rb', line 96

def start_subsegment(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 unless sub.finished?
    Client.send_segment(sub) if @trace.sampled?
  end
end