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

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.



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

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.



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

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.



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

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.



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

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.



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

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)


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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



150
151
152
153
154
155
# File 'lib/skylight/core/instrumenter.rb', line 150

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)


157
158
159
# File 'lib/skylight/core/instrumenter.rb', line 157

def disabled?
  defined?(@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.



217
218
219
220
# File 'lib/skylight/core/instrumenter.rb', line 217

def done(span, meta = nil)
  return unless (trace = @trace_info.current)
  trace.done(span, meta)
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.



262
263
264
265
# File 'lib/skylight/core/instrumenter.rb', line 262

def finalize_endpoint_segment(trace)
  return unless trace.segment
  trace.endpoint += "<sk-segment>#{trace.segment}</sk-segment>"
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)


253
254
255
# File 'lib/skylight/core/instrumenter.rb', line 253

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)


171
172
173
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
# File 'lib/skylight/core/instrumenter.rb', line 171

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.



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/skylight/core/instrumenter.rb', line 222

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.



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

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)


167
168
169
# File 'lib/skylight/core/instrumenter.rb', line 167

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.



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

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.



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

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.



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

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.



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

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.



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

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 => 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]



258
259
260
# File 'lib/skylight/core/instrumenter.rb', line 258

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.



118
119
120
121
# File 'lib/skylight/core/instrumenter.rb', line 118

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.



207
208
209
210
# File 'lib/skylight/core/instrumenter.rb', line 207

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.



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

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



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

def trace(endpoint, cat, title = nil, desc = nil, meta: nil, segment: 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: meta, segment: segment)
  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