Class: Aws::Xray::Context

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

Defined Under Namespace

Classes: NotSetError

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) ⇒ Context

Returns a new instance of Context.



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

def initialize(name, client, trace)
  @name = name
  @client = client
  @trace = trace
  @base_segment = Segment.build(@name, trace)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



53
54
55
# File 'lib/aws/xray/context.rb', line 53

def name
  @name
end

Class Method Details

.currentAws::Xray::Context

Returns:



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

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

.started?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/aws/xray/context.rb', line 22

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)

    Require this parameter because the socket inside client can live longer than this context. For example the life-cycle of context is HTTP request based but the socket can live over HTTP requests cycle, it is opened when application starts then is closed when application exits.

  • trace (Aws::Xray::Trace)

    newly generated trace or created with HTTP request header.

Yields:



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

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.



67
68
69
70
71
72
73
74
75
# File 'lib/aws/xray/context.rb', line 67

def base_trace
  res = yield @base_segment
  @client.send_segment(@base_segment)
  res
rescue => e
  @base_segment.set_error(fault: true, e: e)
  @client.send_segment(@base_segment)
  raise e
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.



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

def child_trace(remote:, name:)
  sub = SubSegment.build(@trace, @base_segment, remote: remote, name: name)
  res = yield sub
  @client.send_segment(sub)
  res
rescue => e
  sub.set_error(fault: true, e: e)
  @client.send_segment(sub)
  raise e
end