Class: Gitlab::Metrics::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/metrics/transaction.rb

Overview

Class for storing metrics information of a single transaction.

Constant Summary collapse

THREAD_KEY =
:_gitlab_metrics_transaction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action = nil) ⇒ Transaction

action - A String describing the action performed, usually the class

plus method name.

17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/metrics/transaction.rb', line 17

def initialize(action = nil)
  @metrics = []

  @started_at  = nil
  @finished_at = nil

  @values = Hash.new(0)
  @tags   = {}
  @action = action

  @memory_before = 0
  @memory_after  = 0
end

Instance Attribute Details

#actionObject

Returns the value of attribute action


9
10
11
# File 'lib/gitlab/metrics/transaction.rb', line 9

def action
  @action
end

#tagsObject (readonly)

Returns the value of attribute tags


7
8
9
# File 'lib/gitlab/metrics/transaction.rb', line 7

def tags
  @tags
end

#valuesObject (readonly)

Returns the value of attribute values


7
8
9
# File 'lib/gitlab/metrics/transaction.rb', line 7

def values
  @values
end

Class Method Details

.currentObject


11
12
13
# File 'lib/gitlab/metrics/transaction.rb', line 11

def self.current
  Thread.current[THREAD_KEY]
end

Instance Method Details

#add_metric(series, values, tags = {}) ⇒ Object


53
54
55
56
57
# File 'lib/gitlab/metrics/transaction.rb', line 53

def add_metric(series, values, tags = {})
  prefix = sidekiq? ? 'sidekiq_' : 'rails_'

  @metrics << Metric.new("#{prefix}#{series}", values, tags)
end

#add_tag(key, value) ⇒ Object


67
68
69
# File 'lib/gitlab/metrics/transaction.rb', line 67

def add_tag(key, value)
  @tags[key] = value
end

#allocated_memoryObject


35
36
37
# File 'lib/gitlab/metrics/transaction.rb', line 35

def allocated_memory
  @memory_after - @memory_before
end

#durationObject


31
32
33
# File 'lib/gitlab/metrics/transaction.rb', line 31

def duration
  @finished_at ? (@finished_at - @started_at) * 1000.0 : 0.0
end

#finishObject


71
72
73
74
# File 'lib/gitlab/metrics/transaction.rb', line 71

def finish
  track_self
  submit
end

#increment(name, value) ⇒ Object


59
60
61
# File 'lib/gitlab/metrics/transaction.rb', line 59

def increment(name, value)
  @values[name] += value
end

#runObject


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/metrics/transaction.rb', line 39

def run
  Thread.current[THREAD_KEY] = self

  @memory_before = System.memory_usage
  @started_at    = Time.now

  yield
ensure
  @memory_after = System.memory_usage
  @finished_at  = Time.now

  Thread.current[THREAD_KEY] = nil
end

#set(name, value) ⇒ Object


63
64
65
# File 'lib/gitlab/metrics/transaction.rb', line 63

def set(name, value)
  @values[name] = value
end

#sidekiq?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'lib/gitlab/metrics/transaction.rb', line 98

def sidekiq?
  Sidekiq.server?
end

#submitObject


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gitlab/metrics/transaction.rb', line 86

def submit
  metrics = @metrics.map do |metric|
    hash = metric.to_hash

    hash[:tags][:action] ||= @action if @action

    hash
  end

  Metrics.submit_metrics(metrics)
end

#track_selfObject


76
77
78
79
80
81
82
83
84
# File 'lib/gitlab/metrics/transaction.rb', line 76

def track_self
  values = { duration: duration, allocated_memory: allocated_memory }

  @values.each do |name, value|
    values[name] = value
  end

  add_metric('transactions', values, @tags)
end