appsignal 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,7 +44,9 @@ describe Appsignal do
44
44
  it "should create a config from the env" do
45
45
  ENV['APPSIGNAL_PUSH_API_KEY'] = 'something'
46
46
  Appsignal::Extension.should_receive(:start)
47
- Appsignal.start
47
+ expect(Appsignal.logger).not_to receive(:error)
48
+ silence { Appsignal.start }
49
+ expect(Appsignal.config[:push_api_key]).to eq('something')
48
50
  end
49
51
  end
50
52
 
@@ -551,7 +553,8 @@ describe Appsignal do
551
553
  end
552
554
 
553
555
  describe ".start_logger" do
554
- let(:out_stream) { StringIO.new }
556
+ let(:out_stream) { std_stream }
557
+ let(:output) { out_stream.read }
555
558
  let(:log_path) { File.join(tmp_dir, 'log') }
556
559
  let(:log_file) { File.join(log_path, 'appsignal.log') }
557
560
 
@@ -564,28 +567,28 @@ describe Appsignal do
564
567
  :log_path => log_path
565
568
  )
566
569
  end
567
- around do |example|
568
- recognize_as_container(:none) do
569
- capture_stdout(out_stream) { example.run }
570
- end
571
- end
570
+ around { |example| recognize_as_container(:none) { example.run } }
572
571
  after { FileUtils.rm_rf(log_path) }
573
572
 
574
573
  context "when the log path is writable" do
575
574
  context "when the log file is writable" do
576
575
  let(:log_file_contents) { File.open(log_file).read }
576
+
577
577
  before do
578
- Appsignal.start_logger
579
- Appsignal.logger.error('Log to file')
578
+ capture_stdout(out_stream) do
579
+ Appsignal.start_logger
580
+ Appsignal.logger.error('Log to file')
581
+ end
580
582
  end
581
583
 
582
584
  it "logs to file" do
583
585
  expect(File.exist?(log_file)).to be_true
584
- expect(log_file_contents).to include 'Log to file'
586
+ expect(log_file_contents).to include '[ERROR] Log to file'
587
+ expect(output).to be_empty
585
588
  end
586
589
 
587
590
  it "amends in memory log to log file" do
588
- expect(log_file_contents).to include 'Log in memory'
591
+ expect(log_file_contents).to include '[ERROR] appsignal: Log in memory'
589
592
  end
590
593
  end
591
594
 
@@ -594,46 +597,54 @@ describe Appsignal do
594
597
  FileUtils.touch log_file
595
598
  FileUtils.chmod 0444, log_file
596
599
 
597
- Appsignal.start_logger
598
- Appsignal.logger.error('Log to not writable log file')
600
+ capture_stdout(out_stream) do
601
+ Appsignal.start_logger
602
+ Appsignal.logger.error('Log to not writable log file')
603
+ end
599
604
  end
600
605
 
601
606
  it "logs to stdout" do
602
607
  expect(File.writable?(log_file)).to be_false
603
- expect(out_stream.string).to include 'Log to not writable log file'
608
+ expect(output).to include '[ERROR] appsignal: Log to not writable log file'
604
609
  end
605
610
 
606
611
  it "amends in memory log to stdout" do
607
- expect(out_stream.string).to include 'Log in memory'
612
+ expect(output).to include '[ERROR] appsignal: Log in memory'
608
613
  end
609
614
 
610
615
  it "outputs a warning" do
611
- expect(out_stream.string).to include \
612
- "appsignal: Unable to start logger with log path '#{log_file}'.",
613
- "appsignal: Permission denied"
616
+ expect(output).to include \
617
+ "[WARN] appsignal: Unable to start logger with log path '#{log_file}'.",
618
+ "[WARN] appsignal: Permission denied"
614
619
  end
615
620
  end
616
621
  end
617
622
 
618
- context "when the log path is not writable" do
623
+ context "when the log path and fallback path are not writable" do
619
624
  before do
620
625
  FileUtils.chmod 0444, log_path
626
+ FileUtils.chmod 0444, Appsignal::Config::SYSTEM_TMP_DIR
621
627
 
622
- Appsignal.start_logger
623
- Appsignal.logger.error('Log to not writable log path')
628
+ capture_stdout(out_stream) do
629
+ Appsignal.start_logger
630
+ Appsignal.logger.error('Log to not writable log path')
631
+ end
632
+ end
633
+ after do
634
+ FileUtils.chmod 0755, Appsignal::Config::SYSTEM_TMP_DIR
624
635
  end
625
636
 
626
637
  it "logs to stdout" do
627
638
  expect(File.writable?(log_path)).to be_false
628
- expect(out_stream.string).to include 'Log to not writable log path'
639
+ expect(output).to include '[ERROR] appsignal: Log to not writable log path'
629
640
  end
630
641
 
631
642
  it "amends in memory log to stdout" do
632
- expect(out_stream.string).to include 'Log in memory'
643
+ expect(output).to include '[ERROR] appsignal: Log in memory'
633
644
  end
634
645
 
635
646
  it "outputs a warning" do
636
- expect(out_stream.string).to include \
647
+ expect(output).to include \
637
648
  "appsignal: Unable to log to '#{log_path}' "\
638
649
  "or the '#{Appsignal::Config::SYSTEM_TMP_DIR}' fallback."
639
650
  end
@@ -641,17 +652,19 @@ describe Appsignal do
641
652
 
642
653
  context "when on Heroku" do
643
654
  before do
644
- Appsignal.start_logger
645
- Appsignal.logger.error('Log to stdout')
655
+ capture_stdout(out_stream) do
656
+ Appsignal.start_logger
657
+ Appsignal.logger.error('Log to stdout')
658
+ end
646
659
  end
647
660
  around { |example| recognize_as_heroku { example.run } }
648
661
 
649
662
  it "logs to stdout" do
650
- expect(out_stream.string).to include 'appsignal: Log to stdout'
663
+ expect(output).to include '[ERROR] appsignal: Log to stdout'
651
664
  end
652
665
 
653
666
  it "amends in memory log to stdout" do
654
- expect(out_stream.string).to include 'Log in memory'
667
+ expect(output).to include '[ERROR] appsignal: Log in memory'
655
668
  end
656
669
  end
657
670
 
@@ -661,7 +674,9 @@ describe Appsignal do
661
674
  context "when there is no config" do
662
675
  before do
663
676
  Appsignal.config = nil
664
- Appsignal.start_logger
677
+ capture_stdout(out_stream) do
678
+ Appsignal.start_logger
679
+ end
665
680
  end
666
681
 
667
682
  it "sets the log level to info" do
@@ -673,7 +688,9 @@ describe Appsignal do
673
688
  context "when log level is configured to debug" do
674
689
  before do
675
690
  Appsignal.config.config_hash[:debug] = true
676
- Appsignal.start_logger
691
+ capture_stdout(out_stream) do
692
+ Appsignal.start_logger
693
+ end
677
694
  end
678
695
 
679
696
  it "sets the log level to debug" do
@@ -685,11 +702,21 @@ describe Appsignal do
685
702
  end
686
703
 
687
704
  describe ".log_formatter" do
688
- subject { Appsignal.log_formatter }
705
+ subject { Appsignal.log_formatter.call('Debug', Time.parse('2015-07-08'), nil, 'log line') }
706
+
707
+ it "formats a log" do
708
+ expect(subject).to eq "[2015-07-08T00:00:00 (process) ##{Process.pid}][Debug] log line\n"
709
+ end
710
+
711
+ context "with prefix" do
712
+ subject do
713
+ Appsignal.log_formatter("prefix").call('Debug', Time.parse('2015-07-08'), nil, 'log line')
714
+ end
689
715
 
690
- it "should format a log line" do
691
- Process.stub(:pid => 100)
692
- subject.call('Debug', Time.parse('2015-07-08'), nil, 'log line').should eq "[2015-07-08T00:00:00 (process) #100][Debug] log line\n"
716
+ it "adds a prefix" do
717
+ expect(subject)
718
+ .to eq "[2015-07-08T00:00:00 (process) ##{Process.pid}][Debug] prefix: log line\n"
719
+ end
693
720
  end
694
721
  end
695
722
 
@@ -792,40 +819,36 @@ describe Appsignal do
792
819
  end
793
820
 
794
821
  describe ".instrument" do
795
- it "should instrument through the transaction" do
796
- stub = double
797
- stub.should_receive(:method_call).and_return('return value')
822
+ before do
823
+ expect(Appsignal::Transaction).to receive(:current).at_least(:once).and_return(transaction)
824
+ end
798
825
 
799
- transaction.should_receive(:start_event)
800
- transaction.should_receive(:finish_event).with(
801
- 'name',
802
- 'title',
803
- 'body',
804
- 0
805
- )
826
+ it "should instrument through the transaction" do
827
+ expect(transaction).to receive(:start_event)
828
+ expect(transaction).to receive(:finish_event)
829
+ .with('name', 'title', 'body', Appsignal::EventFormatter::DEFAULT)
806
830
 
807
- Appsignal.instrument 'name', 'title', 'body' do
808
- stub.method_call
809
- end.should eq 'return value'
831
+ result = Appsignal.instrument 'name', 'title', 'body' do
832
+ 'return value'
833
+ end
834
+ expect(result).to eq 'return value'
810
835
  end
811
836
  end
812
837
 
813
838
  describe ".instrument_sql" do
814
- it "should instrument sql through the transaction" do
815
- stub = double
816
- stub.should_receive(:method_call).and_return('return value')
839
+ before do
840
+ expect(Appsignal::Transaction).to receive(:current).at_least(:once).and_return(transaction)
841
+ end
817
842
 
818
- transaction.should_receive(:start_event)
819
- transaction.should_receive(:finish_event).with(
820
- 'name',
821
- 'title',
822
- 'body',
823
- 1
824
- )
843
+ it "should instrument sql through the transaction" do
844
+ expect(transaction).to receive(:start_event)
845
+ expect(transaction).to receive(:finish_event)
846
+ .with('name', 'title', 'body', Appsignal::EventFormatter::SQL_BODY_FORMAT)
825
847
 
826
- Appsignal.instrument_sql 'name', 'title', 'body' do
827
- stub.method_call
828
- end.should eq 'return value'
848
+ result = Appsignal.instrument_sql 'name', 'title', 'body' do
849
+ 'return value'
850
+ end
851
+ expect(result).to eq 'return value'
829
852
  end
830
853
  end
831
854
 
@@ -48,22 +48,23 @@ RSpec.configure do |config|
48
48
  config.extend DependencyHelper
49
49
 
50
50
  config.before :all do
51
- FileUtils.rm_rf(tmp_dir)
52
- FileUtils.mkdir_p(tmp_dir)
53
-
54
- # Use modifiable SYSTEM_TMP_DIR
51
+ # Use modified SYSTEM_TMP_DIR
55
52
  Appsignal::Config.send :remove_const, :SYSTEM_TMP_DIR
56
53
  Appsignal::Config.send :const_set, :SYSTEM_TMP_DIR,
57
54
  File.join(tmp_dir, 'system-tmp')
55
+
56
+ FileUtils.rm_rf(tmp_dir)
57
+ FileUtils.mkdir_p(Appsignal::Config::SYSTEM_TMP_DIR)
58
58
  end
59
59
 
60
60
  config.before do
61
- ENV['RAILS_ENV'] = 'test'
62
- ENV['PADRINO_ENV'] = 'test'
61
+ ENV['RAILS_ENV'] ||= 'test'
62
+ ENV['RACK_ENV'] ||= 'test'
63
+ ENV['PADRINO_ENV'] ||= 'test'
63
64
 
64
65
  # Clean environment
65
66
  ENV.keys.select { |key| key.start_with?('APPSIGNAL_') }.each do |key|
66
- ENV[key] = nil
67
+ ENV.delete(key)
67
68
  end
68
69
  end
69
70
 
@@ -14,4 +14,13 @@ module CLIHelpers
14
14
  end
15
15
  end
16
16
  end
17
+
18
+ def set_input(value)
19
+ $stdin.puts value
20
+ end
21
+
22
+ def prepare_input
23
+ # Prepare the input by rewinding the pointer in the StringIO
24
+ $stdin.rewind
25
+ end
17
26
  end
@@ -1,35 +1,66 @@
1
1
  module StdStreamsHelper
2
+ def std_stream
3
+ Tempfile.new SecureRandom.uuid
4
+ end
5
+
2
6
  # Capture STDOUT in a variable
3
7
  #
8
+ # Given tempfiles are rewinded and unlinked after yield, so no cleanup
9
+ # required. You can read from the stream using `stdout.read`.
10
+ #
4
11
  # Usage
5
12
  #
6
- # out_stream = StringIO.new
13
+ # out_stream = Tempfile.new
7
14
  # capture_stdout(out_stream) { do_something }
8
15
  def capture_stdout(stdout)
9
- original_stdout = $stdout
10
- $stdout = stdout
16
+ original_stdout = $stdout.dup
17
+ $stdout.reopen stdout
11
18
 
12
19
  yield
13
-
14
- $stdout = original_stdout
20
+ ensure
21
+ $stdout.reopen original_stdout
22
+ stdout.rewind
23
+ stdout.unlink
15
24
  end
16
25
 
17
26
  # Capture STDOUT and STDERR in variables
18
27
  #
28
+ # Given tempfiles are rewinded and unlinked after yield, so no cleanup
29
+ # required. You can read from the stream using `stdout.read`.
30
+ #
19
31
  # Usage
20
32
  #
21
- # out_stream = StringIO.new
22
- # err_stream = StringIO.new
33
+ # out_stream = Tempfile.new
34
+ # err_stream = Tempfile.new
23
35
  # capture_std_streams(out_stream, err_stream) { do_something }
24
36
  def capture_std_streams(stdout, stderr)
25
- original_stdout = $stdout
26
- $stdout = stdout
27
- original_stderr = $stderr
28
- $stderr = stderr
37
+ original_stdout = $stdout.dup
38
+ $stdout.reopen stdout
39
+ original_stderr = $stderr.dup
40
+ $stderr.reopen stderr
29
41
 
30
42
  yield
43
+ ensure
44
+ $stdout.reopen original_stdout
45
+ $stderr.reopen original_stderr
46
+ stdout.rewind
47
+ stdout.unlink
48
+ stderr.rewind
49
+ stderr.unlink
50
+ end
31
51
 
32
- $stdout = original_stdout
33
- $stderr = original_stderr
52
+ def silence
53
+ std_stream = Tempfile.new(SecureRandom.uuid)
54
+ original_stdout = $stdout.dup
55
+ original_stderr = $stderr.dup
56
+ $stdout.reopen std_stream
57
+ $stderr.reopen std_stream
58
+
59
+ yield
60
+ ensure
61
+ $stdout.reopen original_stdout
62
+ $stderr.reopen original_stderr
63
+ std_stream.rewind
64
+ std_stream.unlink
34
65
  end
35
66
  end
@@ -8,7 +8,7 @@ module TransactionHelpers
8
8
  end
9
9
 
10
10
  def background_job_transaction(args={})
11
- Appsignal::Transaction.create(
11
+ Appsignal::Transaction.new(
12
12
  '1',
13
13
  Appsignal::Transaction::BACKGROUND_JOB,
14
14
  Appsignal::Transaction::GenericRequest.new({
@@ -19,7 +19,7 @@ module TransactionHelpers
19
19
  end
20
20
 
21
21
  def http_request_transaction(args={})
22
- Appsignal::Transaction.create(
22
+ Appsignal::Transaction.new(
23
23
  '1',
24
24
  Appsignal::Transaction::HTTP_REQUEST,
25
25
  Appsignal::Transaction::GenericRequest.new({
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appsignal
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Beekman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-17 00:00:00.000000000 Z
12
+ date: 2016-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -43,16 +43,16 @@ dependencies:
43
43
  name: rake
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: '11'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: '11'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rspec
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -153,6 +153,7 @@ files:
153
153
  - lib/appsignal/cli.rb
154
154
  - lib/appsignal/cli/demo.rb
155
155
  - lib/appsignal/cli/diagnose.rb
156
+ - lib/appsignal/cli/helpers.rb
156
157
  - lib/appsignal/cli/install.rb
157
158
  - lib/appsignal/cli/notify_of_deploy.rb
158
159
  - lib/appsignal/config.rb
@@ -220,6 +221,7 @@ files:
220
221
  - spec/lib/appsignal/capistrano3_spec.rb
221
222
  - spec/lib/appsignal/cli/demo_spec.rb
222
223
  - spec/lib/appsignal/cli/diagnose_spec.rb
224
+ - spec/lib/appsignal/cli/helpers_spec.rb
223
225
  - spec/lib/appsignal/cli/install_spec.rb
224
226
  - spec/lib/appsignal/cli/notify_of_deploy_spec.rb
225
227
  - spec/lib/appsignal/cli_spec.rb
@@ -340,6 +342,7 @@ test_files:
340
342
  - spec/lib/appsignal/capistrano3_spec.rb
341
343
  - spec/lib/appsignal/cli/demo_spec.rb
342
344
  - spec/lib/appsignal/cli/diagnose_spec.rb
345
+ - spec/lib/appsignal/cli/helpers_spec.rb
343
346
  - spec/lib/appsignal/cli/install_spec.rb
344
347
  - spec/lib/appsignal/cli/notify_of_deploy_spec.rb
345
348
  - spec/lib/appsignal/cli_spec.rb