Skip to content

Commit

Permalink
Release 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkreeftmeijer committed May 2, 2017
2 parents 66dfed7 + 3dffa95 commit 2aadeaa
Show file tree
Hide file tree
Showing 25 changed files with 673 additions and 448 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ priv
/doc
/cover
/tmp
/c_src/appsignal-agent
/c_src/appsignal.h
/c_src/appsignal.version
/c_src/libappsignal.a
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.2.1

* Allow nil transaction in instrumentation (#198)
* ErrorHandler handles errors in tuples (#201)
* Set `env: Mix.env` in generated config.exs (#203)
* Improve registry lookup performance (#205)

# 1.2.0

* Catch and handle errors in the Plug using Plug.ErrorHandler instead of
Expand Down
4 changes: 0 additions & 4 deletions lib/appsignal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ defmodule Appsignal do
Supervisor.start_link(children, [strategy: :one_for_one, name: Appsignal.Supervisor])
end

def started? do
Application.get_application(Appsignal) != nil
end

def phoenix? do
Code.ensure_loaded?(Phoenix)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/appsignal/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ defmodule Appsignal.ErrorHandler do
msg = Exception.message(r)
{"#{inspect r.__struct__}", prefixed(message, msg)}
end
def extract_reason_and_message({r = %{}, _}, message) do
extract_reason_and_message(r, message)
end
def extract_reason_and_message({kind, _} = reason, message) do
{inspect(kind), prefixed(message, inspect(reason))}
end
Expand Down
14 changes: 7 additions & 7 deletions lib/appsignal/instrumentation/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Appsignal.Instrumentation.Helpers do

alias Appsignal.{Transaction, TransactionRegistry}

@type instrument_arg :: Transaction.t | Plug.Conn.t | pid()
@type instrument_arg :: Transaction.t | Plug.Conn.t | pid() | nil

@doc """
Execute the given function in start / finish event calls in the current
Expand Down Expand Up @@ -53,12 +53,8 @@ defmodule Appsignal.Instrumentation.Helpers do
"""
@spec instrument(instrument_arg, String.t, String.t, String.t, integer, function) :: any
def instrument(pid, name, title, body, body_format, function) when is_pid(pid) do
case TransactionRegistry.lookup(pid) do
nil ->
function.()
t = %Transaction{} ->
instrument(t, name, title, body, body_format, function)
end
t = TransactionRegistry.lookup(pid)
instrument(t, name, title, body, body_format, function)
end

def instrument(%Transaction{} = transaction, name, title, body, body_format, function) do
Expand All @@ -67,4 +63,8 @@ defmodule Appsignal.Instrumentation.Helpers do
Transaction.finish_event(transaction, name, title, body, body_format)
result
end

def instrument(nil, _name, _title, _body, _body_format, function) do
function.()
end
end
2 changes: 1 addition & 1 deletion lib/appsignal/phoenix/template_instrumenter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if Appsignal.phoenix? do
Appsignal.Instrumentation.Helpers.instrument(
self(),
"render.phoenix_template",
unquote(name),
unquote(path),
fn() -> unquote(expr) end
)
end
Expand Down
7 changes: 4 additions & 3 deletions lib/appsignal/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,10 @@ defmodule Appsignal.Transaction do
|> Transaction.set_sample_data("environment", request_environment(conn))

# Add session data
if not config()[:skip_session_data] and conn.private[:plug_session_fetch] == :done do
session_data = conn.private[:plug_session]
Transaction.set_sample_data(transaction, "session_data", session_data)
if !config()[:skip_session_data] and conn.private[:plug_session_fetch] == :done do
Transaction.set_sample_data(
transaction, "session_data", conn.private[:plug_session]
)
else
transaction
end
Expand Down
20 changes: 11 additions & 9 deletions lib/appsignal/transaction/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ defmodule Appsignal.TransactionRegistry do
@spec register(Transaction.t) :: :ok
def register(transaction) do
pid = self()
case Appsignal.started? do
true ->
true = :ets.insert(@table, {pid, transaction})
GenServer.cast(__MODULE__, {:monitor, pid})
false ->
Logger.debug("Appsignal was not started, skipping transaction registration.")
nil
if registry_alive?() do
true = :ets.insert(@table, {pid, transaction})
GenServer.cast(__MODULE__, {:monitor, pid})
else
Logger.debug("Appsignal was not started, skipping transaction registration.")
nil
end
end

Expand All @@ -49,7 +48,7 @@ defmodule Appsignal.TransactionRegistry do
"""
@spec lookup(pid, boolean) :: Transaction.t | nil
def lookup(pid, return_removed \\ false) do
case Appsignal.started? && :ets.lookup(@table, pid) do
case registry_alive?() && :ets.lookup(@table, pid) do
[{^pid, :removed}] ->
case return_removed do
false -> nil
Expand Down Expand Up @@ -120,5 +119,8 @@ defmodule Appsignal.TransactionRegistry do
{:noreply, state}
end


defp registry_alive? do
pid = Process.whereis(__MODULE__)
!is_nil(pid) && Process.alive?(pid)
end
end
49 changes: 36 additions & 13 deletions lib/mix/tasks/appsignal.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ defmodule Mix.Tasks.Appsignal.Install do
:file ->
write_config_file(config)
link_config_file()
deactivate_config_in_test_env()
activate_config_for_env("dev")
activate_config_for_env("stag")
activate_config_for_env("prod")
:env ->
output_config_environment_variables(config)
end
Expand Down Expand Up @@ -152,25 +154,36 @@ defmodule Mix.Tasks.Appsignal.Install do

# Contents for the config/appsignal.exs file.
defp appsignal_config_file_contents(config) do
options = [
~s( name: "#{config[:name]}"),
~s( push_api_key: "#{config[:push_api_key]}"),
~s( env: Mix.env)
]

options_with_active = case has_environment_configuration_files?() do
false -> [~s( active: true)] ++ options
true -> options
end

"use Mix.Config\n\n" <>
"config :appsignal, :config,\n" <>
~s( active: true,\n) <>
~s( name: "#{config[:name]}",\n) <>
~s( push_api_key: "#{config[:push_api_key]}"\n)
Enum.join(options_with_active, ",\n") <>
"\n"
end

# Append a line to Mix configuration environment files which deactivates
# AppSignal for the test environment.
defp deactivate_config_in_test_env do
env_file = Path.join("config", "test.exs")
# Append a line to Mix configuration environment files which activate
# AppSignal. This is done for development, staging and production
# environments if they are present.
defp activate_config_for_env(env) do
env_file = config_path_for_env(env)
if File.exists? env_file do
IO.write "Deactivating AppSignal in the test environment: "
IO.write "Activating #{env} environment: "

deactivation = "\nconfig :appsignal, :config, active: false\n"
case file_contains?(env_file, deactivation) do
:ok -> IO.puts "Success! (Already deactivated)"
active_content = "\nconfig :appsignal, :config, active: true\n"
case file_contains?(env_file, active_content) do
:ok -> IO.puts "Success! (Already active?)"
{:error, :not_found} ->
case append_to_file(env_file, deactivation) do
case append_to_file(env_file, active_content) do
:ok -> IO.puts "Success!"
{:error, reason} ->
IO.puts "Failure! #{reason}"
Expand Down Expand Up @@ -230,4 +243,14 @@ defmodule Mix.Tasks.Appsignal.Install do
IO.puts "Demonstration sample data sent!"
IO.puts "It may take about a minute for the data to appear on https://appsignal.com/accounts"
end

defp has_environment_configuration_files? do
"dev" |> config_path_for_env |> File.exists? or
"stag" |> config_path_for_env |> File.exists? or
"prod" |> config_path_for_env |> File.exists?
end

defp config_path_for_env(env) do
Path.join("config", "#{env}.exs")
end
end
10 changes: 7 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ defmodule Mix.Tasks.Compile.Appsignal do
:ok = Mix.Appsignal.Helper.ensure_downloaded(arch)
:ok = Mix.Appsignal.Helper.compile
{:error, {:unsupported, arch}} ->
Mix.Shell.IO.error("Unsupported target platform #{arch}, AppSignal integration disabled!\nPlease check http://docs.appsignal.com/support/operating-systems.html")
Mix.Shell.IO.error(
"Unsupported target platform #{arch}, AppSignal integration " <>
"disabled!\nPlease check " <>
"http://docs.appsignal.com/support/operating-systems.html"
)
:ok
end
end
Expand All @@ -25,7 +29,7 @@ defmodule Appsignal.Mixfile do

def project do
[app: :appsignal,
version: "1.2.0",
version: "1.2.1",
name: "AppSignal",
description: description(),
package: package(),
Expand Down Expand Up @@ -80,7 +84,7 @@ defmodule Appsignal.Mixfile do
[
{:httpoison, "~> 0.11"},
{:decorator, "~> 1.0"},
{:phoenix, ">= 1.2.0", optional: true, only: [:prod, :test_phoenix]},
{:phoenix, ">= 1.2.0", optional: true, only: [:prod, :test_phoenix, :dev]},
{:mock, "~> 0.2.1", only: [:test, :test_phoenix, :test_no_nif]},
{:bypass, "~> 0.5", only: [:test, :test_phoenix, :test_no_nif]},
{:ex_doc, "~> 0.12", only: :dev, runtime: false}
Expand Down
62 changes: 42 additions & 20 deletions mix_helpers.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,37 @@ defmodule Mix.Appsignal.Helper do

System.put_env("LIB_DIR", priv_dir())

unless has_files?() and has_correct_agent_version?() do
version = Appsignal.Agent.version
if has_local_release_files?() do
IO.puts "AppSignal: Using local agent release."
File.rm_rf!(priv_dir())
File.mkdir_p!(priv_dir())
try do
download_and_extract(arch_config[:download_url], version, arch_config[:checksum])
catch
{:checksum_mismatch, filename, _, _} ->
File.rm!(filename)
try do
download_and_extract(arch_config[:download_url], version, arch_config[:checksum])
catch
{:checksum_mismatch, filename, calculated, expected} ->
raise Mix.Error, message: """
Checksum verification of #{filename} failed!
Calculated: #{calculated}
Expected: #{expected}
"""
end
end

Enum.each(["appsignal.h", "appsignal-agent", "appsignal.version", "libappsignal.a"], fn(file) ->
File.cp(project_ext_path(file), priv_path(file))
end)
else
:ok
unless has_files?() and has_correct_agent_version?() do
version = Appsignal.Agent.version
File.rm_rf!(priv_dir())
File.mkdir_p!(priv_dir())
try do
download_and_extract(arch_config[:download_url], version, arch_config[:checksum])
catch
{:checksum_mismatch, filename, _, _} ->
File.rm!(filename)
try do
download_and_extract(arch_config[:download_url], version, arch_config[:checksum])
catch
{:checksum_mismatch, filename, calculated, expected} ->
raise Mix.Error, message: """
Checksum verification of #{filename} failed!
Calculated: #{calculated}
Expected: #{expected}
"""
end
end
else
:ok
end
end
end

Expand Down Expand Up @@ -134,16 +142,30 @@ defmodule Mix.Appsignal.Helper do
Path.join(priv_dir(), filename)
end

defp project_ext_path(filename) do
Path.join([__DIR__, "c_src", filename])
end

defp has_file(filename) do
filename |> priv_path |> File.exists?
end

defp has_local_ext_file(filename) do
filename |> project_ext_path |> File.exists?
end

defp has_files? do
has_file("appsignal-agent") and
has_file("appsignal.h") and
has_file("appsignal_extension.so")
end

defp has_local_release_files? do
has_local_ext_file("appsignal-agent") and
has_local_ext_file("appsignal.h") and
has_local_ext_file("libappsignal.a")
end

defp has_correct_agent_version? do
path = priv_path("appsignal.version")
File.read(path) == {:ok, "#{Appsignal.Agent.version}\n"}
Expand Down
20 changes: 8 additions & 12 deletions test/appsignal/appsignal_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AppsignalTest do
use ExUnit.Case
import Mock
import AppsignalTest.Utils

test "set gauge" do
Appsignal.set_gauge("key", 10.0)
Expand All @@ -17,21 +18,16 @@ defmodule AppsignalTest do
Appsignal.add_distribution_value("dist_key", 10)
end

test "started?" do
assert Appsignal.started?
end

test "Agent environment variables" do
System.put_env("APPSIGNAL_APP_ENV", "test")
Application.put_env(:appsignal, :config, env: :test)

Appsignal.Config.initialize()
with_env(%{"APPSIGNAL_APP_ENV" => "test"}, fn() ->
Appsignal.Config.initialize()

env = Appsignal.Config.get_system_env()
assert "test" = env["APPSIGNAL_APP_ENV"]
env = Appsignal.Config.get_system_env()
assert "test" = env["APPSIGNAL_APP_ENV"]

config = Application.get_env :appsignal, :config
assert :test = config[:env]
config = Application.get_env :appsignal, :config
assert :test = config[:env]
end)
end

alias Appsignal.{Transaction, TransactionRegistry}
Expand Down
Loading

0 comments on commit 2aadeaa

Please sign in to comment.