gitlab-org--gitlab-foss/lib/gitlab/metrics/transaction.rb
Yorick Peterse d345591fc8
Tracking of custom events
GitLab Performance Monitoring is now able to track custom events not
directly related to application performance. These events include the
number of tags pushed, repositories created, builds registered, etc.

The use of these events is to get a better overview of how a GitLab
instance is used and how that may affect performance. For example, a
large number of Git pushes may have a negative impact on the underlying
storage engine.

Events are stored in the "events" measurement and are not prefixed with
"rails_" or "sidekiq_", this makes it easier to query events with the
same name triggered from different parts of the application. All events
being stored in the same measurement also makes it easier to downsample
data.

Currently the following events are tracked:

* Creating repositories
* Removing repositories
* Changing the default branch of a repository
* Pushing a new tag
* Removing an existing tag
* Pushing a commit (along with the branch being pushed to)
* Pushing a new branch
* Removing an existing branch
* Importing a repository (along with the URL we're importing)
* Forking a repository (along with the source/target path)
* CI builds registered (and when no build could be found)
* CI builds being updated
* Rails and Sidekiq exceptions

Fixes gitlab-org/gitlab-ce#13720
2016-08-17 10:04:04 +02:00

130 lines
3 KiB
Ruby

module Gitlab
module Metrics
# Class for storing metrics information of a single transaction.
class Transaction
THREAD_KEY = :_gitlab_metrics_transaction
# The series to store events (e.g. Git pushes) in.
EVENT_SERIES = 'events'
attr_reader :tags, :values, :method, :metrics
attr_accessor :action
def self.current
Thread.current[THREAD_KEY]
end
# action - A String describing the action performed, usually the class
# plus method name.
def initialize(action = nil)
@metrics = []
@methods = {}
@started_at = nil
@finished_at = nil
@values = Hash.new(0)
@tags = {}
@action = action
@memory_before = 0
@memory_after = 0
end
def duration
@finished_at ? (@finished_at - @started_at) : 0.0
end
def allocated_memory
@memory_after - @memory_before
end
def run
Thread.current[THREAD_KEY] = self
@memory_before = System.memory_usage
@started_at = System.monotonic_time
yield
ensure
@memory_after = System.memory_usage
@finished_at = System.monotonic_time
Thread.current[THREAD_KEY] = nil
end
def add_metric(series, values, tags = {})
@metrics << Metric.new("#{Metrics.series_prefix}#{series}", values, tags)
end
# Tracks a business level event
#
# Business level events including events such as Git pushes, Emails being
# sent, etc.
#
# event_name - The name of the event (e.g. "git_push").
# tags - A set of tags to attach to the event.
def add_event(event_name, tags = {})
@metrics << Metric.new(EVENT_SERIES,
{ count: 1 },
{ event: event_name }.merge(tags),
:event)
end
# Returns a MethodCall object for the given name.
def method_call_for(name)
unless method = @methods[name]
@methods[name] = method = MethodCall.new(name, Instrumentation.series)
end
method
end
def increment(name, value)
@values[name] += value
end
def set(name, value)
@values[name] = value
end
def add_tag(key, value)
@tags[key] = value
end
def finish
track_self
submit
end
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
def submit
submit = @metrics.dup
@methods.each do |name, method|
submit << method.to_metric if method.above_threshold?
end
submit_hashes = submit.map do |metric|
hash = metric.to_hash
hash[:tags][:action] ||= @action if @action && !metric.event?
hash
end
Metrics.submit_metrics(submit_hashes)
end
end
end
end