Class: Skylight::Trace

Inherits:
Object show all
Includes:
Util::Logging
Defined in:
lib/skylight/trace.rb,
ext/skylight_native.c

Constant Summary collapse

GC_CAT =
'noise.gc'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

#debug, #error, #info, #log, #t, #trace, trace?, #warn

Constructor Details

#initialize(instrumenter, cat, title, desc, annot) ⇒ Trace

Returns a new instance of Trace.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/skylight/trace.rb', line 23

def initialize(instrumenter, cat, title, desc, annot)
  raise ArgumentError, 'instrumenter is required' unless instrumenter

  @instrumenter = instrumenter
  @submitted = false
  @broken = false
  @traced = false

  @notifications = []

  if Hash === title
    annot = title
    title = desc = nil
  elsif Hash === desc
    annot = desc
    desc = nil
  end

  # create the root node
  @root = native_start_span(native_get_started_at, cat)
  native_span_set_title(@root, title) if title
  native_span_set_description(@root, desc) if desc

  @gc = config.gc.track unless ENV.key?("SKYLIGHT_DISABLE_GC_TRACKING")
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



7
8
9
# File 'lib/skylight/trace.rb', line 7

def endpoint
  @endpoint
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



7
8
9
# File 'lib/skylight/trace.rb', line 7

def notifications
  @notifications
end

Class Method Details

.native_new(start, uuid, endpoint) ⇒ Object

class Skylight::Trace



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/skylight_native.c', line 245

static VALUE
trace_new(VALUE klass, VALUE start, VALUE uuid, VALUE endpoint) {
  sky_trace_t* trace;

  UNUSED(klass);
  CHECK_NUMERIC(start);
  CHECK_TYPE(uuid, T_STRING);
  CHECK_TYPE(endpoint, T_STRING);

  CHECK_FFI(
      sky_trace_new(NUM2ULL(start), STR2BUF(uuid), STR2BUF(endpoint), &trace),
      "native Trace#new failed");

  sky_clear_allocation_count();

  return Data_Wrap_Struct(rb_cTrace, NULL, sky_trace_free, trace);
}

.new(instrumenter, endpoint, start, cat, title = nil, desc = nil, annot = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/skylight/trace.rb', line 9

def self.new(instrumenter, endpoint, start, cat, title = nil, desc = nil, annot = nil)
  inst = native_new(normalize_time(start), "TODO", endpoint)
  inst.send(:initialize, instrumenter, cat, title, desc, annot)
  inst.endpoint = endpoint
  inst
end

.normalize_time(time) ⇒ Object

TODO: Move this into native



17
18
19
20
21
# File 'lib/skylight/trace.rb', line 17

def self.normalize_time(time)
  # At least one customer has extensions that cause integer division to produce rationals.
  # Since the native code expects an integer, we force it again.
  (time.to_i / 100_000).to_i
end

Instance Method Details

#configObject



55
56
57
# File 'lib/skylight/trace.rb', line 55

def config
  @instrumenter.config
end

#done(span) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/skylight/trace.rb', line 118

def done(span)
  return unless span
  return if @broken
  stop(span, Util::Clock.nanos - gc_time)
rescue => e
  error "failed to close span; msg=%s", e.message
  @broken = true
  nil
end

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



86
87
88
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/trace.rb', line 86

def instrument(cat, title=nil, desc=nil, annot=nil)
  return if @broken
  t { "instrument: #{cat}, #{title}" }

  if Hash === title
    annot = title
    title = desc = nil
  elsif Hash === desc
    annot = desc
    desc = nil
  end

  title.freeze if title.is_a?(String)
  desc.freeze  if desc.is_a?(String)

  original_desc = desc
  now           = Util::Clock.nanos
  desc          = @instrumenter.limited_description(desc)

  if desc == Instrumenter::TOO_MANY_UNIQUES
    debug "[SKYLIGHT] [#{Skylight::VERSION}] A payload description produced <too many uniques>"
    debug "original desc=%s", original_desc
    debug "cat=%s, title=%s, desc=%s, annot=%s", cat, title, desc, annot.inspect
  end

  start(now - gc_time, cat, title, desc, annot)
rescue => e
  error "failed to instrument span; msg=%s", e.message
  @broken = true
  nil
end

#native_get_endpointObject



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/skylight_native.c', line 277

static VALUE
trace_get_endpoint(VALUE self) {
  sky_trace_t* trace;
  sky_buf_t endpoint;

  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_FFI(
      sky_trace_endpoint(trace, &endpoint),
      "native Trace#endpoint failed");

  return BUF2STR(endpoint);
}

#native_get_started_atObject



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'ext/skylight_native.c', line 263

static VALUE
trace_get_started_at(VALUE self) {
  uint64_t start;
  sky_trace_t* trace;

  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_FFI(
      sky_trace_start(trace, &start),
      "native Trace#started_at failed");

  return ULL2NUM(start);
}

#native_get_uuidObject



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/skylight_native.c', line 305

static VALUE
trace_get_uuid(VALUE self) {
  sky_trace_t* trace;
  sky_buf_t uuid;

  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_FFI(
      sky_trace_uuid(trace, &uuid),
      "native Trace#uuid failed");

  return BUF2STR(uuid);
}

#native_set_endpoint(endpoint) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'ext/skylight_native.c', line 291

static VALUE
trace_set_endpoint(VALUE self, VALUE endpoint) {
  sky_trace_t* trace;

  CHECK_TYPE(endpoint, T_STRING);
  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_FFI(
      sky_trace_set_endpoint(trace, STR2BUF(endpoint)),
      "native Trace#set_endpoint failed");

  return Qnil;
}

#native_span_set_description(span, desc) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'ext/skylight_native.c', line 376

static VALUE
trace_span_set_description(VALUE self, VALUE span, VALUE desc) {
  sky_trace_t* trace;

  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_TYPE(span, T_FIXNUM);
  CHECK_TYPE(desc, T_STRING);

  CHECK_FFI(
      sky_trace_span_set_desc(trace, FIX2UINT(span), STR2BUF(desc)),
      "native Trace#span_set_description failed");

  return Qnil;
}

#native_span_set_title(span, title) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/skylight_native.c', line 360

static VALUE
trace_span_set_title(VALUE self, VALUE span, VALUE title) {
  sky_trace_t* trace;

  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_TYPE(span, T_FIXNUM);
  CHECK_TYPE(title, T_STRING);

  CHECK_FFI(
      sky_trace_span_set_title(trace, FIX2UINT(span), STR2BUF(title)),
      "native Trace#span_set_title failed");

  return Qnil;
}

#native_start_span(time, category) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'ext/skylight_native.c', line 319

static VALUE
trace_start_span(VALUE self, VALUE time, VALUE category) {
  sky_trace_t* trace;
  uint32_t span;

  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_NUMERIC(time);
  CHECK_TYPE(category, T_STRING);

  CHECK_FFI(
      sky_trace_instrument(trace, NUM2ULL(time), STR2BUF(category), &span),
      "native Trace#start_span failed");

  if (sky_have_memprof()) {
    sky_trace_span_add_uint_annotation(trace, span, 2, sky_consume_allocations());
  }

  return UINT2NUM(span);
}

#native_stop_span(span, time) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'ext/skylight_native.c', line 340

static VALUE
trace_stop_span(VALUE self, VALUE span, VALUE time) {
  sky_trace_t* trace;

  My_Struct(trace, sky_trace_t, consumed_trace_msg);

  CHECK_NUMERIC(time);
  CHECK_TYPE(span, T_FIXNUM);

  if (sky_have_memprof()) {
    sky_trace_span_add_uint_annotation(trace, FIX2UINT(span), 1, sky_consume_allocations());
  }

  CHECK_FFI(
      sky_trace_span_done(trace, FIX2UINT(span), NUM2ULL(time)),
      "native Trace#stop_span failed");

  return Qnil;
}

#record(cat, title = nil, desc = nil, annot = nil) ⇒ Object



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
84
# File 'lib/skylight/trace.rb', line 59

def record(cat, title=nil, desc=nil, annot=nil)
  @return if @broken

  if Hash === title
    annot = title
    title = desc = nil
  elsif Hash === desc
    annot = desc
    desc = nil
  end

  title.freeze if title.is_a?(String)
  desc.freeze  if desc.is_a?(String)

  desc = @instrumenter.limited_description(desc)

  time = Util::Clock.nanos - gc_time

  stop(start(time, cat, title, desc), time)

  nil
rescue => e
  error "failed to record span; msg=%s", e.message
  @broken = true
  nil
end

#releaseObject



128
129
130
131
# File 'lib/skylight/trace.rb', line 128

def release
  return unless @instrumenter.current_trace == self
  @instrumenter.current_trace = nil
end

#submitObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/skylight/trace.rb', line 146

def submit
  return if @broken

  t { "submitting trace" }

  if @submitted
    t { "already submitted" }
    return
  end

  release
  @submitted = true

  traced unless @traced

  @instrumenter.process(self)
rescue Exception => e
  error e
  t { e.backtrace.join("\n") }
end

#tracedObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/skylight/trace.rb', line 133

def traced
  @traced = true
  time = gc_time
  now = Util::Clock.nanos

  if time > 0
    t { fmt "tracking GC time; duration=%d", time }
    stop(start(now - time, GC_CAT, nil, nil, {}), now)
  end

  stop(@root, now)
end