Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Rename gem to appinsights
Browse files Browse the repository at this point in the history
  • Loading branch information
emancu committed Feb 11, 2015
1 parent 355a287 commit d981f42
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,2 +1,2 @@
# ai-rails
Microsoft Application Insights for Rails
# appinsights
Microsoft Application Insights AutoInstaller for Ruby frameworks
10 changes: 6 additions & 4 deletions ai_agent.gempsec → appinsights.gemspec
@@ -1,12 +1,12 @@
Gem::Specification.new do |s|
s.name = 'ai_agent'
s.name = 'appinsights'
s.version = '0.0.1'
s.date = Time.now.strftime('%Y-%m-%d')
s.summary = 'Application Insights Agent'
s.summary = 'Application Insights Auto-Installer'
s.description = 'Application Insights AutoInstaller for Ruby'
s.authors = ['Emiliano Mancuso']
s.email = ['emiliano.mancuso@gmail.com']
s.homepage = 'http://github.com/citrusbyte/ai-rails'
s.homepage = 'http://github.com/citrusbyte/appinsights'
s.license = 'MIT'

s.files = Dir[
Expand All @@ -17,6 +17,8 @@ Gem::Specification.new do |s|
'test/*.*'
]

s.add_dependency 'application_insights', '~> 0.5.0'
s.add_dependency 'toml-rb', '~> 0.2.1'
s.add_dependency 'application_insights', '~> 0.5.0'

s.add_development_dependency 'rack', '~> 1.6', '>= 1.6.0'
end
4 changes: 2 additions & 2 deletions lib/ai_agent.rb → lib/appinsights.rb
Expand Up @@ -3,13 +3,13 @@
require_relative 'middlewares'
require_relative 'config_loader'

module ApplicationInsightsInstaller
module AppInsights
if defined?(Rails::VERSION)
require_relative 'frameworks/rails'
else
puts <<-EOS
Config file not loaded.
Use ApplicationInsightsInstaller::ConfigLoader.new root, filename
Use AppInsights::ConfigLoader.new root, filename
to setup the Context and middlewares.
EOS

Expand Down
8 changes: 4 additions & 4 deletions lib/config_loader.rb
@@ -1,6 +1,6 @@
require 'toml'

module ApplicationInsightsInstaller
module AppInsights
class ConfigLoader
attr_reader :settings, :filename

Expand All @@ -10,13 +10,13 @@ def initialize(root, filename = nil)
@filename = File.join(@root, @filename) if @filename

unless @filename && File.exist?(@filename)
fail ApplicationInsightsInstaller::ConfigFileNotFound
fail AppInsights::ConfigFileNotFound
end

@settings = TOML.load_file @filename

Context.configure @settings['ai']
Middlewares.configure @settings['middleware']
AppInsights::Context.configure @settings['ai']
AppInsights::Middlewares.configure @settings['middleware']
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/context.rb
@@ -1,6 +1,6 @@
require 'application_insights'

module ApplicationInsightsInstaller
module AppInsights
class Context
class << self
def configure(config = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/errors.rb
@@ -1,4 +1,4 @@
module ApplicationInsightsInstaller
module AppInsights
class UnknownMiddleware < StandardError; end
class ConfigFileNotFound < StandardError; end
end
6 changes: 3 additions & 3 deletions lib/frameworks/rails.rb
@@ -1,9 +1,9 @@
module ApplicationInsightsInstaller
module AppInsights
class Railtie < Rails::Railtie
initializer 'ai_agent.start_plugin' do |_app|
ConfigLoader.new Rails.root
AppInsights::ConfigLoader.new Rails.root

Middlewares.enabled.each do |middleware, args|
AppInsights::Middlewares.enabled.each do |middleware, args|
config.app_middleware.use middleware, *args.values
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/middlewares.rb
@@ -1,6 +1,6 @@
require_relative 'middlewares/exception_handling'

module ApplicationInsightsInstaller
module AppInsights
class Middlewares
class << self
def configure(settings = {})
Expand Down Expand Up @@ -29,7 +29,7 @@ def constantize_middlewares
end
rescue NameError => e
# FIXME: Log, ignore or fail?
raise ApplicationInsightsInstaller::UnknownMiddleware, e.message
raise AppInsights::UnknownMiddleware, e.message
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/middlewares/exception_handling.rb
@@ -1,6 +1,6 @@
require 'application_insights'

module ApplicationInsightsInstaller
module AppInsights
class ExceptionHandling
def initialize(app)
@app = app
Expand All @@ -9,7 +9,7 @@ def initialize(app)
def call(env)
@app.call env
rescue Exception => exception
tc = ApplicationInsightsInstaller::Context.telemetry_client
tc = AppInsights::Context.telemetry_client
tc.track_exception exception

raise exception
Expand Down
2 changes: 1 addition & 1 deletion test/config/application_insights.toml
Expand Up @@ -15,7 +15,7 @@
# Enable/Disable Middlewares

[[middleware]]
name = 'ApplicationInsightsInstaller::ExceptionHandling'
name = 'AppInsights::ExceptionHandling'
enabled = true

[[middleware]]
Expand Down
2 changes: 1 addition & 1 deletion test/config/non_default_file.toml
Expand Up @@ -15,7 +15,7 @@
# Enable/Disable Middlewares

[[middleware]]
name = 'ApplicationInsightsInstaller::ExceptionHandling'
name = 'AppInsights::ExceptionHandling'
enabled = true

[[middleware]]
Expand Down
28 changes: 14 additions & 14 deletions test/config_loader_test.rb
@@ -1,52 +1,52 @@
require_relative 'helper'
require 'application_insights'

describe ApplicationInsightsInstaller::ConfigLoader do
describe AppInsights::ConfigLoader do
before do
ApplicationInsightsInstaller::Context.tap do |klass|
AppInsights::Context.tap do |klass|
klass.instance_variable_set :@context, nil
klass.instance_variable_set :@client, nil
end

ApplicationInsightsInstaller::Middlewares.tap do |klass|
AppInsights::Middlewares.tap do |klass|
klass.instance_variable_set :@settings, nil
klass.instance_variable_set :@enabled_middlewares, nil
end
end

describe 'initialize' do
it 'fails when there is no configuration file' do
assert_raises ApplicationInsightsInstaller::ConfigFileNotFound do
ApplicationInsightsInstaller::ConfigLoader.new '.'
assert_raises AppInsights::ConfigFileNotFound do
AppInsights::ConfigLoader.new '.'
end
end

it 'loads the config file from the default path' do
loader = ApplicationInsightsInstaller::ConfigLoader.new './test'
loader = AppInsights::ConfigLoader.new './test'

deny loader.settings.empty?
end

it 'loads the config file from the given filename' do
loader = ApplicationInsightsInstaller::ConfigLoader.new './test/config', 'non_default_file.toml'
loader = AppInsights::ConfigLoader.new './test/config', 'non_default_file.toml'

deny loader.settings.empty?
end

it 'autoconfigure the Context and middlewares' do
tc = ApplicationInsightsInstaller::Context.telemetry_client
settings = ApplicationInsightsInstaller::Middlewares.settings
middlewares = ApplicationInsightsInstaller::Middlewares.enabled
tc = AppInsights::Context.telemetry_client
settings = AppInsights::Middlewares.settings
middlewares = AppInsights::Middlewares.enabled

deny tc.context.instrumentation_key
assert settings.empty?
assert middlewares.empty?

ApplicationInsightsInstaller::ConfigLoader.new './test'
AppInsights::ConfigLoader.new './test'

tc = ApplicationInsightsInstaller::Context.telemetry_client
settings = ApplicationInsightsInstaller::Middlewares.settings
middlewares = ApplicationInsightsInstaller::Middlewares.enabled
tc = AppInsights::Context.telemetry_client
settings = AppInsights::Middlewares.settings
middlewares = AppInsights::Middlewares.enabled

assert tc.context.instrumentation_key
deny settings.empty?
Expand Down
10 changes: 5 additions & 5 deletions test/context_test.rb
@@ -1,6 +1,6 @@
require_relative 'helper'

describe ApplicationInsightsInstaller::Context do
describe AppInsights::Context do
before do
@configs = {
'instrumentation_key' => 'a_key',
Expand All @@ -18,13 +18,13 @@

describe 'configure' do
it 'returns a ApplicationInsights::Channel::TelemetryContext object' do
context = ApplicationInsightsInstaller::Context.configure
context = AppInsights::Context.configure

assert_equal ApplicationInsights::Channel::TelemetryContext, context.class
end

it 'accepts a hash to set Context values' do
context = ApplicationInsightsInstaller::Context.configure @configs
context = AppInsights::Context.configure @configs

assert_equal 'a_key', context.instrumentation_key
assert_equal '0.0.1', context.application.ver
Expand All @@ -33,9 +33,9 @@

describe 'telemetry_client' do
before do
ApplicationInsightsInstaller::Context.configure @configs
AppInsights::Context.configure @configs

@client = ApplicationInsightsInstaller::Context.telemetry_client
@client = AppInsights::Context.telemetry_client
end

it 'returns an instance of ApplicationInsights::TelemetryClient' do
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
@@ -1,7 +1,7 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))

require 'minitest/autorun'
require 'ai_agent'
require 'appinsights'

def deny(condition, message = 'Expected condition to be unsatisfied')
assert !condition, message
Expand Down
26 changes: 13 additions & 13 deletions test/middlewares_test.rb
@@ -1,10 +1,10 @@
require_relative 'helper'

describe ApplicationInsightsInstaller::Middlewares do
describe AppInsights::Middlewares do
before do
@configs = [
{
'name' => 'ApplicationInsightsInstaller::ExceptionHandling',
'name' => 'AppInsights::ExceptionHandling',
'enabled' => true
},
{
Expand All @@ -15,49 +15,49 @@
end

after do
ApplicationInsightsInstaller::Middlewares.tap do |klass|
AppInsights::Middlewares.tap do |klass|
klass.instance_variable_set :@settings, nil
klass.instance_variable_set :@enabled_middlewares, nil
end
end

describe 'enabled' do
it 'returns the middlewares enabled' do
ApplicationInsightsInstaller::Middlewares.configure @configs
AppInsights::Middlewares.configure @configs

enabled = ApplicationInsightsInstaller::Middlewares.enabled
expected = [[ApplicationInsightsInstaller::ExceptionHandling, {}]]
enabled = AppInsights::Middlewares.enabled
expected = [[AppInsights::ExceptionHandling, {}]]

deny enabled.empty?
assert_equal expected, enabled
end

it 'returns an empty Array if it was not configured' do
assert ApplicationInsightsInstaller::Middlewares.enabled.empty?
assert AppInsights::Middlewares.enabled.empty?
end
end

describe 'settings' do
it 'returns the settings loaded' do
ApplicationInsightsInstaller::Middlewares.configure @configs
AppInsights::Middlewares.configure @configs

settings = ApplicationInsightsInstaller::Middlewares.settings
settings = AppInsights::Middlewares.settings

deny settings.empty?
assert_equal @configs, settings
end

it 'returns an empty Hash if it was not configured' do
assert ApplicationInsightsInstaller::Middlewares.settings.empty?
assert AppInsights::Middlewares.settings.empty?
end
end

describe 'configure' do
it 'accepts one or zero params' do
ApplicationInsightsInstaller::Middlewares.configure
AppInsights::Middlewares.configure

assert_equal({}, ApplicationInsightsInstaller::Middlewares.settings)
assert_equal([], ApplicationInsightsInstaller::Middlewares.enabled)
assert_equal({}, AppInsights::Middlewares.settings)
assert_equal([], AppInsights::Middlewares.enabled)
end
end
end

0 comments on commit d981f42

Please sign in to comment.