Class: Skywalking::Tracing::SpanContext

Inherits:
Object
  • Object
show all
Includes:
Log::Logging
Defined in:
lib/skywalking/tracing/span_context.rb

Direct Known Subclasses

IgnoredContext

Defined Under Namespace

Classes: PrimaryEndpoint

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log::Logging

#debug, #error, #info, #log, #warn

Constructor Details

#initializeSpanContext

Returns a new instance of SpanContext.



29
30
31
32
33
34
35
36
# File 'lib/skywalking/tracing/span_context.rb', line 29

def initialize
  @segment = Tracing::Segment.new
  @span_id = -1
  @correlation = {}
  @n_spans = 0
  @create_time = (Process.clock_gettime(Process::CLOCK_REALTIME) * 1000).to_i
  @primary_endpoint = nil
end

Instance Attribute Details

#correlationObject

Returns the value of attribute correlation.



27
28
29
# File 'lib/skywalking/tracing/span_context.rb', line 27

def correlation
  @correlation
end

#create_timeObject

Returns the value of attribute create_time.



27
28
29
# File 'lib/skywalking/tracing/span_context.rb', line 27

def create_time
  @create_time
end

#n_spansObject

Returns the value of attribute n_spans.



27
28
29
# File 'lib/skywalking/tracing/span_context.rb', line 27

def n_spans
  @n_spans
end

#segmentObject

Returns the value of attribute segment.



27
28
29
# File 'lib/skywalking/tracing/span_context.rb', line 27

def segment
  @segment
end

#span_idObject

Returns the value of attribute span_id.



27
28
29
# File 'lib/skywalking/tracing/span_context.rb', line 27

def span_id
  @span_id
end

Instance Method Details

#active_spanObject



157
158
159
# File 'lib/skywalking/tracing/span_context.rb', line 157

def active_span
  peek
end

#cfgObject



38
39
40
# File 'lib/skywalking/tracing/span_context.rb', line 38

def cfg
  @config ||= ::Skywalking::Agent.agent_config
end

#ignore_check(operation, carrier: nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/skywalking/tracing/span_context.rb', line 42

def ignore_check(operation, carrier: nil)
  if cfg[:re_ignore_operation].match?(operation) || carrier&.suppressed?
    return Tracing::NoopSpan
  end

  nil
end

#new_entry_span(operation, carrier: nil, inherit: nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/skywalking/tracing/span_context.rb', line 85

def new_entry_span(operation, carrier: nil, inherit: nil)
  span = ignore_check(operation)
  return span if span

  parent = peek
  debug 'create new entry span'
  if parent && parent.kind == Kind::Entry && inherit == parent.component
    span = parent
    span.operation = operation
  else
    span = new_span(EntrySpan, parent, operation: operation)
    span.extract(carrier) if carrier&.valid?
  end

  span
end

#new_exit_span(operation, peer, component: nil, inherit: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/skywalking/tracing/span_context.rb', line 111

def new_exit_span(operation, peer, component: nil, inherit: nil)
  span = ignore_check(operation)
  return span if span

  parent = peek
  debug 'create new exit span'

  if parent && parent.kind == Kind::Exit && inherit == parent.inherit
    span = parent
    span.operation = operation
    span.peer = peer
    span.component = component
  else
    span = new_span(ExitSpan, parent, operation: operation, peer: peer, component: component)
  end
  span.inherit = inherit if inherit

  span
end

#new_local_span(operation) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/skywalking/tracing/span_context.rb', line 102

def new_local_span(operation)
  span = ignore_check(operation)
  return span if span

  parent = peek
  debug 'create new local span'
  new_span(Span, parent, operation: operation, kind: Kind::Local)
end

#new_span(span_klass, parent, **kwargs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/skywalking/tracing/span_context.rb', line 57

def new_span(span_klass, parent, **kwargs)
  finished = parent && !parent.stack_depth
  context = finished ? SpanContext.new : self

  span = span_klass.new(
    span_id: context.span_id += 1,
    parent_id: parent && !finished ? parent.span_id : -1,
    context: context,
    **kwargs
  )

  if finished
    carrier = Carrier.new(
      trace_id: parent.context.segment.related_traces[0],
      segment_id: parent.context.segment.segment_id,
      span_id: parent.span_id,
      service: cfg[:service_name],
      service_instance: cfg[:instance_name],
      endpoint: parent.operation,
      peer: parent.peer,
      correlation: parent.context.correlation
    )
    span.extract(carrier)
  end

  span
end

#peekObject



50
51
52
53
54
55
# File 'lib/skywalking/tracing/span_context.rb', line 50

def peek
  spans = ContextManager.spans
  return spans.last unless spans.empty?

  nil
end

#start(span) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/skywalking/tracing/span_context.rb', line 131

def start(span)
  @n_spans += 1
  spans = ContextManager.spans_dup
  unless spans.include?(span)
    spans << span
    if @primary_endpoint.nil?
      @primary_endpoint = PrimaryEndpoint.new(span)
    else
      @primary_endpoint.set_primary_endpoint(span)
    end
  end
end

#stop?(span) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/skywalking/tracing/span_context.rb', line 144

def stop?(span)
  spans = ContextManager.spans_dup
  span.finish?(@segment)
  spans.delete(span)
  @n_spans -= 1
  if @n_spans.zero?
    Reporter::Report.trigger << @segment
    return true
  end

  false
end