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.



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

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.



48
49
50
# File 'lib/aws/xray/context.rb', line 48

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

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



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

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.



62
63
64
65
66
67
68
69
70
# File 'lib/aws/xray/context.rb', line 62

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.



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

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