Class: Skylight::Core::Instrumenter Private

Inherits:
Object
  • Object
show all
Includes:
Util::Logging
Defined in:
lib/skylight/core/instrumenter.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: TraceInfo

Constant Summary collapse

KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

:__skylight_current_trace
TOO_MANY_UNIQUES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"<too many unique descriptions>"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

#config_for_logging, #debug, #error, #fmt, #info, #log, #log_env_prefix, #raise_on_error?, #t, #trace?, #warn

Constructor Details

#initialize(uuid, config) ⇒ Instrumenter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Instrumenter.



47
48
49
50
51
52
53
54
55
# File 'lib/skylight/core/instrumenter.rb', line 47

def initialize(uuid, config)
  @uuid = uuid
  @gc = config.gc
  @config = config
  @subscriber = Subscriber.new(config, self)

  key = "#{KEY}_#{self.class.trace_class.name}".gsub(/\W/, '_')
  @trace_info = @config[:trace_info] || TraceInfo.new(key)
end

Instance Attribute Details

#configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/skylight/core/instrumenter.rb', line 28

def config
  @config
end

#gcObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/skylight/core/instrumenter.rb', line 28

def gc
  @gc
end

#trace_infoObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/skylight/core/instrumenter.rb', line 28

def trace_info
  @trace_info
end

#uuidObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/skylight/core/instrumenter.rb', line 28

def uuid
  @uuid
end

Class Method Details

.match?(string, regex) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


164
165
166
167
168
# File 'lib/skylight/core/instrumenter.rb', line 164

def self.match?(string, regex)
  @scanner ||= StringScanner.new('')
  @scanner.string = string
  @scanner.match?(regex)
end

.native_new(_uuid, _config_env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/skylight/core/instrumenter.rb', line 34

def self.native_new(_uuid, _config_env)
  raise "not implemented"
end

.new(config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
41
42
43
44
45
# File 'lib/skylight/core/instrumenter.rb', line 38

def self.new(config)
  config.validate!

  uuid = SecureRandom.uuid
  inst = native_new(uuid, config.to_native_env)
  inst.send(:initialize, uuid, config)
  inst
end

.trace_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/skylight/core/instrumenter.rb', line 30

def self.trace_class
  Trace
end

Instance Method Details

#broken!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



215
216
217
218
# File 'lib/skylight/core/instrumenter.rb', line 215

def broken!
  return unless trace = @trace_info.current
  trace.broken!
end

#check_install!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
# File 'lib/skylight/core/instrumenter.rb', line 86

def check_install!
  true
end

#current_traceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
# File 'lib/skylight/core/instrumenter.rb', line 77

def current_trace
  @trace_info.current
end

#current_trace=(trace) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
# File 'lib/skylight/core/instrumenter.rb', line 81

def current_trace=(trace)
  t { "setting current_trace=#{trace ? trace.uuid : "nil"}; thread=#{Thread.current.object_id}" }
  @trace_info.current = trace
end

#disableObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



153
154
155
156
157
158
# File 'lib/skylight/core/instrumenter.rb', line 153

def disable
  @disabled = true
  yield
ensure
  @disabled = false
end

#disabled?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


160
161
162
# File 'lib/skylight/core/instrumenter.rb', line 160

def disabled?
  @disabled
end

#done(span, meta = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



220
221
222
223
# File 'lib/skylight/core/instrumenter.rb', line 220

def done(span, meta=nil)
  return unless trace = @trace_info.current
  trace.done(span, meta)
end

#ignore?(trace) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


255
256
257
# File 'lib/skylight/core/instrumenter.rb', line 255

def ignore?(trace)
  config.ignored_endpoints.include?(trace.endpoint.sub(%r{<sk-segment>.+</sk-segment>}, ''))
end

#instrument(cat, title = nil, desc = nil, meta = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/skylight/core/instrumenter.rb', line 174

def instrument(cat, title=nil, desc=nil, meta=nil)
  raise ArgumentError, 'cat is required' unless cat

  unless trace = @trace_info.current
    return yield if block_given?
    return
  end

  cat = cat.to_s

  unless match?(cat, Skylight::CATEGORY_REGEX)
    warn "invalid skylight instrumentation category; trace=%s; value=%s", trace.uuid, cat
    return yield if block_given?
    return
  end

  cat = "other.#{cat}" unless match?(cat, Skylight::TIER_REGEX)

  unless sp = trace.instrument(cat, title, desc, meta)
    return yield if block_given?
    return
  end

  return sp unless block_given?

  meta = {}
  begin
    yield sp
  rescue Exception => e
    meta = { exception: [e.class.name, e.message], exception_object: e }
    raise e
  ensure
    trace.done(sp, meta)
  end
end

#limited_description(description) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/skylight/core/instrumenter.rb', line 225

def limited_description(description)
  endpoint = @trace_info.current.endpoint

  if description
    if native_track_desc(endpoint, description)
      description
    else
      TOO_MANY_UNIQUES
    end
  end
end

#log_contextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



57
58
59
# File 'lib/skylight/core/instrumenter.rb', line 57

def log_context
  @log_context ||= { inst: uuid }
end

#match?(string, regex) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


170
171
172
# File 'lib/skylight/core/instrumenter.rb', line 170

def match?(string, regex)
  self.class.match?(string, regex)
end

#native_startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
# File 'lib/skylight/core/instrumenter.rb', line 61

def native_start
  raise "not implemented"
end

#native_stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
# File 'lib/skylight/core/instrumenter.rb', line 65

def native_stop
  raise "not implemented"
end

#native_submit_trace(_trace) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
# File 'lib/skylight/core/instrumenter.rb', line 73

def native_submit_trace(_trace)
  raise "not implemented"
end

#native_track_desc(_endpoint, _description) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
# File 'lib/skylight/core/instrumenter.rb', line 69

def native_track_desc(_endpoint, _description)
  raise "not implemented"
end

#process(trace) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/skylight/core/instrumenter.rb', line 237

def process(trace)
  t { fmt "processing trace=#{trace.uuid}" }

  if ignore?(trace)
    t { fmt "ignoring trace=#{trace.uuid}" }
    return false
  end

  begin
    native_submit_trace(trace)
    true
  rescue => e
    warn "failed to submit trace to worker; trace=%s, err=%s", trace.uuid, e
    t { "BACKTRACE:\n#{e.backtrace.join("\n")}" }
    false
  end
end

#process_sql(sql) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return [title, sql]



260
261
262
# File 'lib/skylight/core/instrumenter.rb', line 260

def process_sql(sql)
  [nil, sql]
end

#shutdownObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



120
121
122
123
# File 'lib/skylight/core/instrumenter.rb', line 120

def shutdown
  @subscriber.unregister!
  native_stop
end

#span_correlation_header(span) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



210
211
212
213
# File 'lib/skylight/core/instrumenter.rb', line 210

def span_correlation_header(span)
  return unless trace = @trace_info.current
  trace.span_correlation_header(span)
end

#start!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/skylight/core/instrumenter.rb', line 90

def start!
  # We do this here since we can't report these issues via Gem install without stopping install entirely.
  check_install!

  t { "starting instrumenter" }

  unless config.validate_with_server
    log_error "invalid config"
    return
  end

  t { "starting native instrumenter" }
  unless native_start
    warn "failed to start instrumenter"
    return
  end

  config.gc.enable
  @subscriber.register!

  ActiveSupport::Notifications.instrument("started_instrumenter.skylight", instrumenter: self)

  self

rescue Exception => e
  log_error "failed to start instrumenter; msg=%s; config=%s", e.message, config.inspect
  t { e.backtrace.join("\n") }
  nil
end

#trace(endpoint, cat, title = nil, desc = nil, meta = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/skylight/core/instrumenter.rb', line 125

def trace(endpoint, cat, title=nil, desc=nil, meta=nil)
  # If a trace is already in progress, continue with that one
  if trace = @trace_info.current
    return yield(trace) if block_given?
    return trace
  end

  begin
    trace = self.class.trace_class.new(self, endpoint, Util::Clock.nanos, cat, title, desc, meta)
  rescue Exception => e
    log_error e.message
    t { e.backtrace.join("\n") }
    return
  end

  @trace_info.current = trace
  return trace unless block_given?

  begin
    yield trace

  ensure
    @trace_info.current = nil
    t { "instrumenter submitting trace; trace=#{trace.uuid}" }
    trace.submit
  end
end