Class: HeimdallApm::TrackedTransaction

Inherits:
Object
  • Object
show all
Defined in:
lib/heimdall_apm/tracked_transaction.rb

Overview

A TrackedTransaction is a collection of segments.

Constant Summary collapse

WEB_MODE =
1
JOB_MODE =
2
VISITORS =
{
  metrics: ::HeimdallApm::Visitors::RequestMetricsVisitor
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ TrackedTransaction

Returns a new instance of TrackedTransaction.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/heimdall_apm/tracked_transaction.rb', line 24

def initialize(context)
  @context      = context
  @root_segment = nil
  @segments     = []
  @scope        = nil
  @stopped      = false
  @mode         = nil
  @annotations  = {}

  @recorder     = context.recorder
  @vault        = context.vault
end

Instance Attribute Details

#annotationsObject (readonly)

Miscellaneous annotations made to the transaction. Must be a Hash.



22
23
24
# File 'lib/heimdall_apm/tracked_transaction.rb', line 22

def annotations
  @annotations
end

#recorderObject (readonly)

Recorder used to process transaction data



16
17
18
# File 'lib/heimdall_apm/tracked_transaction.rb', line 16

def recorder
  @recorder
end

#root_segmentObject (readonly)

First segment added to the transaction



13
14
15
# File 'lib/heimdall_apm/tracked_transaction.rb', line 13

def root_segment
  @root_segment
end

#scopeObject

Scope of this transaction (controller routes / job id)



19
20
21
# File 'lib/heimdall_apm/tracked_transaction.rb', line 19

def scope
  @scope
end

Instance Method Details

#annotate(hsh) ⇒ Object



88
89
90
# File 'lib/heimdall_apm/tracked_transaction.rb', line 88

def annotate(hsh)
  @annotations.merge!(hsh)
end

#current_segmentObject

Grab the currently running segment. Will be ‘nil` for a finalized transaction



66
67
68
# File 'lib/heimdall_apm/tracked_transaction.rb', line 66

def current_segment
  @segments[-1]
end

#custom_series_nameObject

Allows InfluxDB’s series name to be customize via annotations



93
94
95
# File 'lib/heimdall_apm/tracked_transaction.rb', line 93

def custom_series_name
  annotations[:series_name]
end

#job?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/heimdall_apm/tracked_transaction.rb', line 105

def job?
  @mode == JOB_MODE
end

#recordObject



74
75
76
77
78
79
80
81
82
# File 'lib/heimdall_apm/tracked_transaction.rb', line 74

def record
  return unless root_segment

  VISITORS.each do |_, klass|
    visitor = klass.new(@vault, self)
    root_segment.accept(visitor)
    visitor.store_in_vault
  end
end

#start_segment(segment) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/heimdall_apm/tracked_transaction.rb', line 37

def start_segment(segment)
  @root_segment = segment unless @root_segment
  @segments.push(segment)

  # TODO: maybe use a visitor to check that at the end of the request intead
  @mode ||=
    case segment.type
    when 'Controller' then WEB_MODE
    when 'Job'        then JOB_MODE
    else
      nil
    end

  segment.start
end

#stop_segmentObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/heimdall_apm/tracked_transaction.rb', line 53

def stop_segment
  segment = @segments.pop
  segment.record_stop_time

  if finalized?
    stop_request
  else
    @segments[-1].add_child(segment)
  end
end

#stopped?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/heimdall_apm/tracked_transaction.rb', line 84

def stopped?
  @stopped
end

#tagsObject



97
98
99
# File 'lib/heimdall_apm/tracked_transaction.rb', line 97

def tags
  annotations[:tags]
end

#web?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/heimdall_apm/tracked_transaction.rb', line 101

def web?
  @mode == WEB_MODE
end