Skip to content

Commit

Permalink
Merge pull request #6552 from ChrisBr/influxdb-beta-new
Browse files Browse the repository at this point in the history
Improve influxdb
  • Loading branch information
ChrisBr committed Dec 11, 2018
2 parents aa96047 + 9871d97 commit 8ffe06c
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 94 deletions.
4 changes: 4 additions & 0 deletions src/api/lib/backend/connection_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'remember_location'

module Backend
# Module that holds the wrapping methods for http requests, are mainly used for simplify the calculation of urls.
#
Expand Down Expand Up @@ -40,6 +42,8 @@ module Backend
#

module ConnectionHelper
include Backend::RememberLocation

private

# Performs a http get request to the configured OBS Backend server.
Expand Down
49 changes: 49 additions & 0 deletions src/api/lib/backend/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Backend
class Instrumentation
def initialize(http_method, host, http_status_code, runtime)
@http_method = http_method
@host = host
@http_status_code = http_status_code
@runtime = runtime
end

def instrument
return unless enabled?

ActiveSupport::Notifications.instrument('obs.backend.process_response', data)
end

private

attr_accessor :http_method, :host, :http_status_code, :runtime

def enabled?
CONFIG['influxdb_hosts'].present?
end

def data
{
http_method: http_method,
http_status_code: http_status_code,
host: host,
runtime: runtime,
controller_location: controller_location,
backend_location: backend_location
}
end

def controller_location
[
Thread.current[:_influxdb_rails_controller],
Thread.current[:_influxdb_rails_action]
].reject(&:blank?).join('#')
end

def backend_location
[
Thread.current[:_influxdb_obs_backend_api_module],
Thread.current[:_influxdb_obs_backend_api_method]
].reject(&:blank?).join('#')
end
end
end
19 changes: 2 additions & 17 deletions src/api/lib/backend/logger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../influxdb_obs/obs/normalizer/location_normalizer'
require_relative 'instrumentation'

module Backend
# Class that implements a logger to write output in the backend logs
Expand All @@ -20,7 +20,7 @@ def self.info(method, host, port, path, response, start_time)
@backend_logger.info "#{now} #{method} #{host}:#{port}#{path} #{response.code} #{time_delta}"
@backend_time += time_delta
Rails.logger.debug "request took #{time_delta} #{@backend_time}"
instrument_notification(method, host, response.code, time_delta)
Backend::Instrumentation.new(method, host, response.code, time_delta).instrument

return unless CONFIG['extended_backend_log']

Expand All @@ -33,20 +33,5 @@ def self.info(method, host, port, path, response, start_time)
@backend_logger.info "(non-XML data) #{data.class}"
end
end

def self.instrument_notification(method, host, code, runtime)
return if CONFIG['influxdb_hosts'].blank?

location = InfluxDB::OBS::Normalizer::LocationNormalizer.new(caller_locations(4, 8))
data = {
http_method: method,
http_status: code,
host: host,
runtime: runtime,
controller: location.controller_name,
backend: location.backend_name
}
ActiveSupport::Notifications.instrument('obs.backend.process_response', data)
end
end
end
25 changes: 25 additions & 0 deletions src/api/lib/backend/remember_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Backend
module RememberLocation
def singleton_method_added(method_name)
return if filtering.include?(method_name)
return if method_name == :singleton_method_added
return if method_name == :initialize

original_method = singleton_method(method_name)

filtering << method_name

define_singleton_method(method_name) do |*args, &block|
Thread.current[:_influxdb_obs_backend_api_method] = method_name.to_s
Thread.current[:_influxdb_obs_backend_api_module] = name
original_method.call(*args, &block)
end
end

private

def filtering
@filtering ||= []
end
end
end
8 changes: 4 additions & 4 deletions src/api/lib/influxdb_obs/obs/middleware/backend_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def call(_name, _started, finished, _unique_id, data)
attr_reader :series_name, :logger

def enabled?
series_name.present?
CONFIG['influxdb_hosts'].present?
end

def values(runtime)
Expand All @@ -35,10 +35,10 @@ def values(runtime)
def tags(data)
{
http_method: data[:http_method],
http_status: data[:http_status],
http_status_code: data[:http_status_code],
host: data[:host],
controller: data[:controller],
backend: data[:backend]
controller_location: data[:controller_location],
backend_location: data[:backend_location]
}.reject { |_, value| value.blank? }
end
end
Expand Down
39 changes: 0 additions & 39 deletions src/api/lib/influxdb_obs/obs/normalizer/location_normalizer.rb

This file was deleted.

20 changes: 20 additions & 0 deletions src/api/spec/lib/backend/remember_location_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails_helper'

RSpec.describe Backend::RememberLocation do
let(:mod) { WithRememberLocation }

class WithRememberLocation
extend Backend::RememberLocation

def self.run_after; end
end

describe 'it remembers the location' do
before do
mod.run_after
end

it { expect(Thread.current[:_influxdb_obs_backend_api_module]).to eq(mod.to_s) }
it { expect(Thread.current[:_influxdb_obs_backend_api_method]).to eq('run_after') }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
let(:finish_time) { Time.at(1_517_567_370) }
let(:data) do
{
http_status: 200,
http_status_code: 200,
http_method: 'GET',
host: 'backend',
controller: 'UsersController#index',
backend: 'Cloud#upload',
controller_location: 'UsersController#index',
backend_location: 'Cloud#upload',
runtime: 2
}
end
Expand All @@ -25,16 +25,20 @@
value: 2000
},
tags: {
http_status: 200,
http_status_code: 200,
http_method: 'GET',
host: 'backend',
controller: 'UsersController#index',
backend: 'Cloud#upload'
controller_location: 'UsersController#index',
backend_location: 'Cloud#upload'
},
timestamp: 1_517_567_370
}
end

before do
stub_const('CONFIG', CONFIG.merge('influxdb_hosts' => ['localhost']))
end

it 'writes to InfluxDB' do
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(series_name, result)
subject.call('_name', start_time, finish_time, '_unique_id', data)
Expand Down

This file was deleted.

0 comments on commit 8ffe06c

Please sign in to comment.