Skip to content

Commit 331715b

Browse files
committed
talk to Insights via http
1 parent b2c64dc commit 331715b

File tree

6 files changed

+115
-6
lines changed

6 files changed

+115
-6
lines changed

lib/travis/addons/handlers/insights.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'travis/addons/handlers/base'
22
require 'travis/addons/handlers/task'
3-
require 'travis/addons/model/broadcast'
3+
require 'travis/addons/support/insights'
4+
require 'travis/rollout'
45

56
module Travis
67
module Addons
@@ -20,12 +21,24 @@ def handle?
2021
end
2122

2223
def handle
23-
Travis::Sidekiq.insights(event, payload)
24+
post? ? post : enqueue
2425
end
2526

2627
private
2728

28-
def payload
29+
def post?
30+
Rollout.matches?(:insights_http, owner: owner_name)
31+
end
32+
33+
def post
34+
insights.post(event: event, data: data)
35+
end
36+
37+
def enqueue
38+
Travis::Sidekiq.insights(event, data)
39+
end
40+
41+
def data
2942
{
3043
type: object.class.name,
3144
id: object.id,
@@ -39,6 +52,7 @@ def payload
3952
finished_at: object.finished_at
4053
}
4154
end
55+
alias payload data
4256

4357
def state
4458
case event
@@ -48,6 +62,14 @@ def state
4862
end
4963
end
5064

65+
def owner_name
66+
object.owner&.login
67+
end
68+
69+
def insights
70+
Travis::Insights.new(Hub.context.config)
71+
end
72+
5173
class EventHandler < Addons::Instrument
5274
def notify_completed
5375
publish

lib/travis/addons/support/insights.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'faraday'
2+
3+
module Travis
4+
class Insights < Struct.new(:config)
5+
def post(data)
6+
http.post do |r|
7+
r.url '/events'
8+
r.params['source'] = 'hub'
9+
r.headers['Content-Type'] = 'application/json'
10+
r.body = JSON.dump(data)
11+
end
12+
end
13+
14+
private
15+
16+
def http
17+
@http ||= Faraday.new(compact(url: url, ssl: ssl)) do |c|
18+
c.request :authorization, :Token, %(token="#{token}")
19+
c.request :retry, max: 5, methods: [:post], exceptions: [Faraday::Error]
20+
c.response :raise_error
21+
c.adapter :net_http
22+
end
23+
end
24+
25+
def url
26+
config[:insights][:url]
27+
end
28+
29+
def token
30+
config[:insights][:token]
31+
end
32+
33+
def ssl
34+
config[:ssl].to_h
35+
end
36+
37+
def compact(hash)
38+
hash.reject { |_, value| value.nil? || value&.empty? }
39+
end
40+
end
41+
end

lib/travis/hub/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def http_basic_auth
1818
database: { adapter: 'postgresql', database: "travis_#{env}", encoding: 'unicode', min_messages: 'warning', pool: 25, reaping_frequency: 60, variables: { statement_timeout: 10000 } },
1919
logs_api: { url: 'https://travis-logs-notset.example.com:1234', token: 'notset', retries: { max: 5, interval: 3, max_interval: 60, interval_randomness: 0.5, backoff_factor: 2 } },
2020
job_board: { url: 'https://not:[email protected]', site: 'org' },
21+
insights: { url: ENV['INSIGHTS_URL'], token: ENV['INSIGHTS_TOKEN'] },
2122
redis: { url: ENV['TRAVIS_REDIS_URL'] || 'redis://localhost:6379', insights_url: ENV['INSIGHTS_REDIS_URL'] || 'redis://localhost:6379' },
2223
sidekiq: { namespace: 'sidekiq', pool_size: 1 },
2324
lock: { strategy: :redis, ttl: 30000 },
@@ -34,6 +35,7 @@ def http_basic_auth
3435
limit: { resets: { max: 50, after: 6 * 60 * 60 } },
3536
notifications: [],
3637
auth: { jwt_public_key: ENV['JWT_RSA_PUBLIC_KEY'], http_basic_auth: http_basic_auth }
38+
3739
def metrics
3840
# TODO cleanup keychain?
3941
super.to_h.merge(librato: librato.to_h.merge(source: librato_source), graphite: graphite)

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'travis/hub/api'
77
require 'date'
88

9+
require 'support/config'
910
require 'support/context'
1011
require 'support/database_cleaner'
1112
require 'support/env'
@@ -28,6 +29,7 @@
2829
RSpec.configure do |c|
2930
c.mock_with :mocha
3031
c.filter_run_excluding pending: true
32+
c.include Support::Config
3133
c.include Support::Context
3234
c.include Support::DatabaseCleaner
3335
c.include Support::Env

spec/support/config.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Support
2+
module Config
3+
def self.included(base)
4+
base.send(:extend, ClassMethods)
5+
end
6+
7+
module ClassMethods
8+
def config(vars)
9+
before { define_config(vars) }
10+
after { undefine_config(vars) }
11+
end
12+
end
13+
14+
def define_config(vars)
15+
vars.each { |key, value| context.config[key] = value }
16+
end
17+
18+
def undefine_config(vars)
19+
vars.each { |key, _| context.config[key] = nil }
20+
end
21+
end
22+
end
23+

spec/travis/addons/handlers/insights_spec.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
describe Travis::Addons::Handlers::Insights do
2-
let(:build) { FactoryGirl.create(:build, repository: repository) }
3-
let(:job) { FactoryGirl.create(:job, owner_id: 1, owner_type: 'User', repository: repository) }
2+
let(:owner) { FactoryGirl.create(:user, login: 'user') }
3+
let(:build) { FactoryGirl.create(:build, owner: owner, repository: repository) }
4+
let(:job) { FactoryGirl.create(:job, owner: owner, repository: repository) }
45
let(:repository) { FactoryGirl.create(:repository) }
56

67
describe 'subscription' do
@@ -69,7 +70,7 @@
6970
}
7071
end
7172

72-
describe 'job:created' do
73+
describe 'job:created via Sidekiq' do
7374
let(:event) { 'job:created' }
7475
it 'handle' do
7576
::Sidekiq::Client.any_instance.expects(:push).with(
@@ -82,6 +83,24 @@
8283
end
8384
end
8485

86+
describe 'job:created via HTTP' do
87+
config insights: { url: 'https://insights.travis-ci.com', token: 'token' }
88+
89+
env ROLLOUT: 'insights_http'
90+
env ROLLOUT_INSIGHTS_HTTP_OWNERS: 'user'
91+
92+
let(:event) { 'job:created' }
93+
94+
it 'handle' do
95+
stub_request(:post, 'https://insights.travis-ci.com/events?source=hub').with do |r|
96+
expect(JSON.parse(r.body)['event']).to eq 'job:created'
97+
expect(r.headers['Authorization']).to eq 'Token token="token"'
98+
expect(r.headers['Content-Type']).to eq 'application/json'
99+
end
100+
handler.handle
101+
end
102+
end
103+
85104
describe 'job:canceled' do
86105
let(:event) { 'job:canceled' }
87106
it 'handle' do

0 commit comments

Comments
 (0)