Class: Skylight::Instrumenter Private

Inherits:
Object
  • Object
show all
Includes:
Util::Logging
Defined in:
lib/skylight/instrumenter.rb,
lib/skylight/native.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

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



63
64
65
66
67
68
69
70
71
72
# File 'lib/skylight/instrumenter.rb', line 63

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

  @trace_info = @config[:trace_info] || TraceInfo.new(KEY)
  @mutex = Mutex.new
  @extensions = Skylight::Extensions::Collection.new(@config)
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.



48
49
50
# File 'lib/skylight/instrumenter.rb', line 48

def config
  @config
end

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



48
49
50
# File 'lib/skylight/instrumenter.rb', line 48

def extensions
  @extensions
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.



48
49
50
# File 'lib/skylight/instrumenter.rb', line 48

def gc
  @gc
end

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



48
49
50
# File 'lib/skylight/instrumenter.rb', line 48

def subscriber
  @subscriber
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.



48
49
50
# File 'lib/skylight/instrumenter.rb', line 48

def uuid
  @uuid
end

Class Method Details

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



96
97
98
# File 'lib/skylight/native.rb', line 96

def self.native_new(*_args)
  allocate
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.



54
55
56
57
58
59
60
61
# File 'lib/skylight/instrumenter.rb', line 54

def self.new(config)
  config.validate!

  uuid = SecureRandom.uuid
  inst = native_new(uuid, config.to_native_env)
  inst.send(:initialize, uuid, config)
  inst
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.



303
304
305
306
307
# File 'lib/skylight/instrumenter.rb', line 303

def broken!
  return unless (trace = @trace_info.current)

  trace.broken!
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.



110
111
112
# File 'lib/skylight/instrumenter.rb', line 110

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.



114
115
116
117
# File 'lib/skylight/instrumenter.rb', line 114

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

#disable_extension!(name) ⇒ 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.



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

def disable_extension!(name)
  @mutex.synchronize { extensions.disable!(name) }
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.



317
318
319
320
321
# File 'lib/skylight/instrumenter.rb', line 317

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

  trace.done(span, meta)
end

#enable_extension!(name) ⇒ 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.



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

def enable_extension!(name)
  @mutex.synchronize { extensions.enable!(name) }
end

#endpoint_assignment_muted?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)


144
145
146
# File 'lib/skylight/instrumenter.rb', line 144

def endpoint_assignment_muted?
  tracing_muted? || muted == :endpoint_assignment
end

#extension_enabled?(name) ⇒ 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)


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

def extension_enabled?(name)
  extensions.enabled?(name)
end

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

Because GraphQL can return multiple results, each of which may have their own success/error states, we need to set the skylight segment as follows:

  • when all queries have errors: “error”

  • when some queries have errors: “<rendered format>+error”

  • when no queries have errors: “<rendered format>”

<rendered format> will be determined by the Rails controller as usual. See Instrumenter#finalize_endpoint_segment for the actual segment/error assignment.



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/skylight/instrumenter.rb', line 363

def finalize_endpoint_segment(trace)
  return unless (segment = trace.segment)

  segment =
    case trace.compound_response_error_status
    when :all
      "error"
    when :partial
      "#{segment}+error"
    else
      segment
    end

  trace.endpoint += "<sk-segment>#{segment}</sk-segment>"
end

#handle_instrumenter_error(trace, err) ⇒ 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.



340
341
342
343
344
345
346
347
# File 'lib/skylight/instrumenter.rb', line 340

def handle_instrumenter_error(trace, err)
  poison! if err.is_a?(Skylight::InstrumenterUnrecoverableError)

  warn "failed to submit trace to worker; trace=%s, err=%s", trace.uuid, err
  t { "BACKTRACE:\n#{err.backtrace.join("\n")}" }

  false
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)


349
350
351
# File 'lib/skylight/instrumenter.rb', line 349

def ignore?(trace)
  config.ignored_endpoints.include?(trace.endpoint)
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)


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/skylight/instrumenter.rb', line 257

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

  if tracing_muted?
    return yield if block_given?

    return
  end

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

    return
  end

  cat = cat.to_s

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

    return
  end

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

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

    return
  end

  return sp unless block_given?

  begin
    yield sp
  rescue Exception => e
    meta ||= {}
    meta[:exception] = [e.class.name, e.message]
    meta[:exception_object] = e
    raise e
  ensure
    trace.done(sp, meta)
  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.



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

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

#mute(ignore: :all) ⇒ Object Also known as: disable

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.

possible values: ignore: :all ignore: :endpoint_assignment ignore: false | nil



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

def mute(ignore: :all)
  old_muted = muted
  self.muted = ignore
  yield if block_given?
ensure
  self.muted = old_muted
end

#mutedObject

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.



136
137
138
# File 'lib/skylight/instrumenter.rb', line 136

def muted
  @trace_info.muted
end

#muted=(val) ⇒ 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.



132
133
134
# File 'lib/skylight/instrumenter.rb', line 132

def muted=(val)
  @trace_info.muted = val
end

#native_flushObject

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.



98
99
100
# File 'lib/skylight/instrumenter.rb', line 98

def native_flush
  raise "not implemented"
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.



90
91
92
# File 'lib/skylight/instrumenter.rb', line 90

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.



94
95
96
# File 'lib/skylight/instrumenter.rb', line 94

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.



106
107
108
# File 'lib/skylight/instrumenter.rb', line 106

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.



102
103
104
# File 'lib/skylight/instrumenter.rb', line 102

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

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



309
310
311
# File 'lib/skylight/instrumenter.rb', line 309

def poison!
  @poisoned = true
end

#poisoned?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)


313
314
315
# File 'lib/skylight/instrumenter.rb', line 313

def poisoned?
  @poisoned
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.



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/skylight/instrumenter.rb', line 323

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

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

  begin
    finalize_endpoint_segment(trace)
    native_submit_trace(trace)
    true
  rescue StandardError => e
    handle_instrumenter_error(trace, e)
  end
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.



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

def shutdown
  @subscriber.unregister!
  native_stop
end

#silence_warnings(context) ⇒ 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.



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

def silence_warnings(context)
  @warnings_silenced || @mutex.synchronize { @warnings_silenced ||= {} }

  @warnings_silenced[context] = true
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.



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
209
# File 'lib/skylight/instrumenter.rb', line 181

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

  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

  enable_extension!(:source_location) if @config.enable_source_locations?
  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, segment: nil, component: 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.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/skylight/instrumenter.rb', line 216

def trace(endpoint, cat, title = nil, desc = nil, meta: nil, segment: nil, component: 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
    meta ||= {}
    extensions.process_trace_meta(meta)
    trace =
      Trace.new(
        self,
        endpoint,
        Skylight::Util::Clock.nanos,
        cat,
        title,
        desc,
        meta: meta,
        segment: segment,
        component: component
      )
  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

#tracing_muted?Boolean Also known as: disabled?

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)


140
141
142
# File 'lib/skylight/instrumenter.rb', line 140

def tracing_muted?
  muted == :all || muted == true
end

#unmuteObject

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.



160
161
162
163
164
165
166
# File 'lib/skylight/instrumenter.rb', line 160

def unmute
  old_muted = muted
  self.muted = false
  yield if block_given?
ensure
  self.muted = old_muted
end

#validate_installationObject

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.



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/skylight/instrumenter.rb', line 119

def validate_installation
  # Warn if there was an error installing Skylight.

  Skylight.check_install_errors(config) if defined?(Skylight.check_install_errors)

  if !Skylight.native? && defined?(Skylight.warn_skylight_native_missing)
    Skylight.warn_skylight_native_missing(config)
    return false
  end

  true
end

#warnings_silenced?(context) ⇒ 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)


174
175
176
# File 'lib/skylight/instrumenter.rb', line 174

def warnings_silenced?(context)
  @warnings_silenced && @warnings_silenced[context]
end