Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report sum of times for each deploy #3295

Merged
merged 1 commit into from Mar 29, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+95 −4
Diff settings

Always

Just for now

@@ -146,7 +146,7 @@ def execute(dir)
production: stage&.production?
}

ActiveSupport::Notifications.instrument("execute_job.samson", payload) do
Samson::TimeSum.instrument "execute_job.samson", payload do
payload[:success] =
if kubernetes?
@executor = Kubernetes::DeployExecutor.new(@job, @output)
@@ -155,8 +155,6 @@ def execute(dir)
@executor.execute(*cmds)
end
end

payload[:success]
end

# ideally the plugin should handle this, but that was even hackier
@@ -11,6 +11,7 @@ class << Samson

Samson.statsd.event "Startup", "Samson startup" if ENV['SERVER_MODE']

# tested via test/lib/samson/time_sum_test.rb
ActiveSupport::Notifications.subscribe("execute_job.samson") do |_, start, finish, _, payload|
duration = 1000.0 * (finish - start)
tags = [
@@ -22,7 +23,13 @@ class << Samson
production = payload.fetch(:production)
tags << "production:#{production}" unless production.nil?

Samson.statsd.timing "execute_shell.time", duration, tags: tags
Samson.statsd.batch do |statsd|
statsd.timing "execute_shell.time", duration, tags: tags
(payload[:parts] || {}).each do |part, time|
statsd.timing "execute_shell.parts", time, tags: tags + ["part:#{part}"]
end
end
Rails.logger.info(payload.merge(total: duration, message: "Job execution finished"))
end

# report and log timing, plain names so git-grep works
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# summarize activesupport notification duration into user defined buckets
module Samson
module TimeSum
BASE = {
"execute.command_executor.samson" => :shell,
"execute.terminal_executor.samson" => :shell,
"request.rest_client.samson" => :kubeclient,
"request.vault.samson" => :vault,
"request.faraday.samson" => :http,
"sql.active_record" => :db
}.freeze

def self.record(metrics = BASE, &block)
sum = metrics.each_value.each_with_object({}) { |key, h| h[key] = 0.0 }
summarize = ->(metric, start, finish, *) { sum[metrics[metric]] += 1000 * (finish - start) }
metrics.each_key.inject(block) do |inner, name|
-> { ActiveSupport::Notifications.subscribed(summarize, name, &inner) }
end.call
sum
end

def self.instrument(notification, payload)
ActiveSupport::Notifications.instrument(notification, payload) do
result = nil
payload[:parts] = record(BASE) { result = yield }
result
end
end
end
end
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require_relative '../../test_helper'

SingleCov.covered!

describe Samson::TimeSum do
describe ".record" do
it "records empty" do
calls = []
Samson::TimeSum.record({}) { calls << 1 }.keys.must_equal []
calls.must_equal [1]
end

it "records single" do
result = Samson::TimeSum.record("sql.active_record" => :db) { User.first }
result.keys.must_equal [:db]
assert result[:db].between?(0, 5), result
end

it "records 0 when nothing happened" do
calls = []
Samson::TimeSum.record("sql.active_record" => :db) { calls << 1 }[:db].must_equal 0
calls.must_equal [1]
end
end

describe ".instrument" do
it "logs" do
Rails.logger.expects(:info).with do |payload|
payload[:message].must_equal "Job execution finished"
assert payload[:parts][:db].between?(0, 5)
true
end
result = Samson::TimeSum.instrument("execute_job.samson", project: "foo", stage: "bar", production: false) do
User.first
end
result.must_equal User.first
end

it "does not log parts when crashing" do
Rails.logger.expects(:info).with do |payload|
payload[:message].must_equal "Job execution finished"
refute payload[:parts]
true
end
assert_raises ArgumentError do
Samson::TimeSum.instrument("execute_job.samson", project: "foo", stage: "bar", production: false) do
raise ArgumentError
end
end
end
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.