application_insights 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +40 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +106 -0
  6. data/Rakefile +10 -0
  7. data/application_insights.gemspec +25 -0
  8. data/lib/application_insights.rb +2 -0
  9. data/lib/application_insights/channel/contracts/application.rb +35 -0
  10. data/lib/application_insights/channel/contracts/data.rb +47 -0
  11. data/lib/application_insights/channel/contracts/data_point.rb +125 -0
  12. data/lib/application_insights/channel/contracts/data_point_type.rb +16 -0
  13. data/lib/application_insights/channel/contracts/dependency_kind.rb +22 -0
  14. data/lib/application_insights/channel/contracts/dependency_source_type.rb +19 -0
  15. data/lib/application_insights/channel/contracts/device.rb +243 -0
  16. data/lib/application_insights/channel/contracts/envelope.rb +254 -0
  17. data/lib/application_insights/channel/contracts/event_data.rb +77 -0
  18. data/lib/application_insights/channel/contracts/exception_data.rb +105 -0
  19. data/lib/application_insights/channel/contracts/exception_details.rb +125 -0
  20. data/lib/application_insights/channel/contracts/internal.rb +51 -0
  21. data/lib/application_insights/channel/contracts/json_serializable.rb +59 -0
  22. data/lib/application_insights/channel/contracts/location.rb +35 -0
  23. data/lib/application_insights/channel/contracts/message_data.rb +76 -0
  24. data/lib/application_insights/channel/contracts/metric_data.rb +60 -0
  25. data/lib/application_insights/channel/contracts/operation.rb +83 -0
  26. data/lib/application_insights/channel/contracts/page_view_data.rb +109 -0
  27. data/lib/application_insights/channel/contracts/remote_dependency_data.rb +218 -0
  28. data/lib/application_insights/channel/contracts/request_data.rb +173 -0
  29. data/lib/application_insights/channel/contracts/session.rb +67 -0
  30. data/lib/application_insights/channel/contracts/severity_level.rb +25 -0
  31. data/lib/application_insights/channel/contracts/stack_frame.rb +91 -0
  32. data/lib/application_insights/channel/contracts/user.rb +83 -0
  33. data/lib/application_insights/channel/queue_base.rb +48 -0
  34. data/lib/application_insights/channel/sender_base.rb +46 -0
  35. data/lib/application_insights/channel/synchronous_queue.rb +28 -0
  36. data/lib/application_insights/channel/synchronous_sender.rb +13 -0
  37. data/lib/application_insights/channel/telemetry_channel.rb +81 -0
  38. data/lib/application_insights/channel/telemetry_context.rb +49 -0
  39. data/lib/application_insights/telemetry_client.rb +111 -0
  40. data/lib/application_insights/version.rb +3 -0
  41. data/test/application_insights.rb +9 -0
  42. data/test/application_insights/channel/contracts/test_application.rb +31 -0
  43. data/test/application_insights/channel/contracts/test_data.rb +44 -0
  44. data/test/application_insights/channel/contracts/test_data_point.rb +109 -0
  45. data/test/application_insights/channel/contracts/test_device.rb +200 -0
  46. data/test/application_insights/channel/contracts/test_envelope.rb +209 -0
  47. data/test/application_insights/channel/contracts/test_event_data.rb +62 -0
  48. data/test/application_insights/channel/contracts/test_exception_data.rb +85 -0
  49. data/test/application_insights/channel/contracts/test_exception_details.rb +106 -0
  50. data/test/application_insights/channel/contracts/test_internal.rb +44 -0
  51. data/test/application_insights/channel/contracts/test_location.rb +31 -0
  52. data/test/application_insights/channel/contracts/test_message_data.rb +66 -0
  53. data/test/application_insights/channel/contracts/test_metric_data.rb +50 -0
  54. data/test/application_insights/channel/contracts/test_operation.rb +70 -0
  55. data/test/application_insights/channel/contracts/test_page_view_data.rb +88 -0
  56. data/test/application_insights/channel/contracts/test_remote_dependency_data.rb +183 -0
  57. data/test/application_insights/channel/contracts/test_request_data.rb +153 -0
  58. data/test/application_insights/channel/contracts/test_session.rb +57 -0
  59. data/test/application_insights/channel/contracts/test_stack_frame.rb +83 -0
  60. data/test/application_insights/channel/contracts/test_user.rb +70 -0
  61. data/test/application_insights/channel/test_queue_base.rb +88 -0
  62. data/test/application_insights/channel/test_sender_base.rb +96 -0
  63. data/test/application_insights/channel/test_synchronous_queue.rb +42 -0
  64. data/test/application_insights/channel/test_synchronous_sender.rb +11 -0
  65. data/test/application_insights/channel/test_telemetry_channel.rb +102 -0
  66. data/test/application_insights/channel/test_telemetry_context.rb +72 -0
  67. data/test/application_insights/test_telemetry_client.rb +107 -0
  68. metadata +166 -0
@@ -0,0 +1,83 @@
1
+ require_relative '../../../../lib/application_insights/channel/contracts/stack_frame'
2
+ require 'test/unit'
3
+
4
+ include ApplicationInsights::Channel
5
+
6
+ class TestStackFrame < Test::Unit::TestCase
7
+ def test_initialize
8
+ item = Contracts::StackFrame.new
9
+ assert_not_nil item
10
+ end
11
+
12
+ def test_level_works_as_expected
13
+ expected = 42
14
+ item = Contracts::StackFrame.new
15
+ item.level = expected
16
+ actual = item.level
17
+ assert_equal expected, actual
18
+ expected = 13
19
+ item.level = expected
20
+ actual = item.level
21
+ assert_equal expected, actual
22
+ end
23
+
24
+ def test_method_works_as_expected
25
+ expected = 'Test string'
26
+ item = Contracts::StackFrame.new
27
+ item.method = expected
28
+ actual = item.method
29
+ assert_equal expected, actual
30
+ expected = 'Other string'
31
+ item.method = expected
32
+ actual = item.method
33
+ assert_equal expected, actual
34
+ end
35
+
36
+ def test_assembly_works_as_expected
37
+ expected = 'Test string'
38
+ item = Contracts::StackFrame.new
39
+ item.assembly = expected
40
+ actual = item.assembly
41
+ assert_equal expected, actual
42
+ expected = 'Other string'
43
+ item.assembly = expected
44
+ actual = item.assembly
45
+ assert_equal expected, actual
46
+ end
47
+
48
+ def test_file_name_works_as_expected
49
+ expected = 'Test string'
50
+ item = Contracts::StackFrame.new
51
+ item.file_name = expected
52
+ actual = item.file_name
53
+ assert_equal expected, actual
54
+ expected = 'Other string'
55
+ item.file_name = expected
56
+ actual = item.file_name
57
+ assert_equal expected, actual
58
+ end
59
+
60
+ def test_line_works_as_expected
61
+ expected = 42
62
+ item = Contracts::StackFrame.new
63
+ item.line = expected
64
+ actual = item.line
65
+ assert_equal expected, actual
66
+ expected = 13
67
+ item.line = expected
68
+ actual = item.line
69
+ assert_equal expected, actual
70
+ end
71
+
72
+ def test_to_json_works_as_expected
73
+ item = Contracts::StackFrame.new
74
+ item.level = 42
75
+ item.method = 'Test string'
76
+ item.assembly = 'Test string'
77
+ item.file_name = 'Test string'
78
+ item.line = 42
79
+ actual = item.to_json
80
+ expected = '{"level":42,"method":"Test string","assembly":"Test string","fileName":"Test string","line":42}'
81
+ assert_equal expected, actual
82
+ end
83
+ end
@@ -0,0 +1,70 @@
1
+ require_relative '../../../../lib/application_insights/channel/contracts/user'
2
+ require 'test/unit'
3
+
4
+ include ApplicationInsights::Channel
5
+
6
+ class TestUser < Test::Unit::TestCase
7
+ def test_initialize
8
+ item = Contracts::User.new
9
+ assert_not_nil item
10
+ end
11
+
12
+ def test_account_acquisition_date_works_as_expected
13
+ expected = 'Test string'
14
+ item = Contracts::User.new
15
+ item.account_acquisition_date = expected
16
+ actual = item.account_acquisition_date
17
+ assert_equal expected, actual
18
+ expected = 'Other string'
19
+ item.account_acquisition_date = expected
20
+ actual = item.account_acquisition_date
21
+ assert_equal expected, actual
22
+ end
23
+
24
+ def test_account_id_works_as_expected
25
+ expected = 'Test string'
26
+ item = Contracts::User.new
27
+ item.account_id = expected
28
+ actual = item.account_id
29
+ assert_equal expected, actual
30
+ expected = 'Other string'
31
+ item.account_id = expected
32
+ actual = item.account_id
33
+ assert_equal expected, actual
34
+ end
35
+
36
+ def test_user_agent_works_as_expected
37
+ expected = 'Test string'
38
+ item = Contracts::User.new
39
+ item.user_agent = expected
40
+ actual = item.user_agent
41
+ assert_equal expected, actual
42
+ expected = 'Other string'
43
+ item.user_agent = expected
44
+ actual = item.user_agent
45
+ assert_equal expected, actual
46
+ end
47
+
48
+ def test_id_works_as_expected
49
+ expected = 'Test string'
50
+ item = Contracts::User.new
51
+ item.id = expected
52
+ actual = item.id
53
+ assert_equal expected, actual
54
+ expected = 'Other string'
55
+ item.id = expected
56
+ actual = item.id
57
+ assert_equal expected, actual
58
+ end
59
+
60
+ def test_to_json_works_as_expected
61
+ item = Contracts::User.new
62
+ item.account_acquisition_date = 'Test string'
63
+ item.account_id = 'Test string'
64
+ item.user_agent = 'Test string'
65
+ item.id = 'Test string'
66
+ actual = item.to_json
67
+ expected = '{"ai.user.accountAcquisitionDate":"Test string","ai.user.accountId":"Test string","ai.user.userAgent":"Test string","ai.user.id":"Test string"}'
68
+ assert_equal expected, actual
69
+ end
70
+ end
@@ -0,0 +1,88 @@
1
+ require_relative '../../../lib/application_insights/channel/queue_base'
2
+ require_relative '../../../lib/application_insights/channel/sender_base'
3
+ require 'test/unit'
4
+
5
+ include ApplicationInsights::Channel
6
+
7
+ class TestQueueBase < Test::Unit::TestCase
8
+ def test_initialize_raises_exception_when_sender_is_nil
9
+ begin
10
+ QueueBase.new nil
11
+ assert false
12
+ rescue ArgumentError => e
13
+ assert_equal 'Sender was required but not provided', e.message
14
+ end
15
+ end
16
+
17
+ def test_initialize
18
+ sender = SenderBase.new 'http://tempuri.org'
19
+ item = QueueBase.new sender
20
+ assert_equal sender.queue, item
21
+ assert_equal 500, item.max_queue_length
22
+ assert_equal sender, item.sender
23
+ end
24
+
25
+ def test_max_queue_length_works_as_expected
26
+ sender = SenderBase.new 'http://tempuri.org'
27
+ item = QueueBase.new sender
28
+ assert_equal 500, item.max_queue_length
29
+ item.max_queue_length = 42
30
+ assert_equal 42, item.max_queue_length
31
+ end
32
+
33
+ def test_sender_works_as_expected
34
+ sender = SenderBase.new 'http://tempuri.org'
35
+ item = QueueBase.new sender
36
+ assert_equal sender, item.sender
37
+ end
38
+
39
+ def test_push_will_not_enqueue_nil
40
+ sender = SenderBase.new 'http://tempuri.org'
41
+ item = QueueBase.new sender
42
+ queue = item.instance_variable_get('@queue')
43
+ item.push nil
44
+ assert queue.empty?
45
+ end
46
+
47
+ def test_push_works_as_expected
48
+ sender = SenderBase.new 'http://tempuri.org'
49
+ item = MockQueueBase.new sender
50
+ item.max_queue_length = 2
51
+ queue = item.instance_variable_get('@queue')
52
+ [[7, 0], [8, 1], [9, 2]].each do |i, count|
53
+ item.push i
54
+ assert_equal count, item.flush_count
55
+ end
56
+ assert_equal 3, queue.length
57
+ temp = []
58
+ queue.length.times do |i|
59
+ temp.push queue.pop
60
+ end
61
+ assert_equal [7, 8, 9], temp
62
+ end
63
+
64
+ def test_pop_works_as_expected
65
+ sender = SenderBase.new 'http://tempuri.org'
66
+ item = QueueBase.new sender
67
+ queue = item.instance_variable_get('@queue')
68
+ [7, 8, 9].each do |i|
69
+ queue.push i
70
+ end
71
+ [7, 8, 9, nil].each do |i|
72
+ assert_equal i, item.pop
73
+ end
74
+ end
75
+ end
76
+
77
+ class MockQueueBase < QueueBase
78
+ def initialize(sender)
79
+ super sender
80
+ @flush_count = 0
81
+ end
82
+
83
+ attr_accessor :flush_count
84
+
85
+ def flush
86
+ @flush_count += 1
87
+ end
88
+ end
@@ -0,0 +1,96 @@
1
+ require_relative '../../../lib/application_insights/channel/queue_base'
2
+ require_relative '../../../lib/application_insights/channel/sender_base'
3
+ require 'socket'
4
+ require 'test/unit'
5
+ require 'thread'
6
+
7
+ include ApplicationInsights::Channel
8
+
9
+ class TestSenderBase < Test::Unit::TestCase
10
+ def test_initialize_raises_exception_when_service_endpoint_uri_is_nil
11
+ begin
12
+ SenderBase.new nil
13
+ assert false
14
+ rescue ArgumentError => e
15
+ assert_equal 'Service endpoint URI was required but not provided', e.message
16
+ end
17
+ end
18
+
19
+ def test_initialize
20
+ sender = SenderBase.new 'http://tempuri.org'
21
+ assert_equal 'http://tempuri.org', sender.service_endpoint_uri
22
+ assert_nil sender.queue
23
+ assert_equal 100, sender.send_buffer_size
24
+ end
25
+
26
+ def test_service_endpoint_uri_works_as_expected
27
+ sender = SenderBase.new 'http://tempuri.org'
28
+ assert_equal 'http://tempuri.org', item.service_endpoint_uri
29
+ item.service_endpoint_uri = 'http://live.com'
30
+ assert_equal 'http://live.com', item.service_endpoint_uri
31
+ end
32
+
33
+ def test_service_endpoint_uri_works_as_expected
34
+ sender = SenderBase.new 'http://tempuri.org'
35
+ assert_nil sender.queue
36
+ temp = Object.new
37
+ sender.queue = temp
38
+ assert_equal temp, sender.queue
39
+ end
40
+
41
+ def test_send_buffer_size_works_as_expected
42
+ sender = SenderBase.new 'http://tempuri.org'
43
+ assert_equal 100, sender.send_buffer_size
44
+ sender.send_buffer_size = 42
45
+ assert_equal 42, sender.send_buffer_size
46
+ end
47
+
48
+ def test_send_works_as_expected_with_400_code
49
+ thread, port = execute_server '400 BadRequest'
50
+ sender = SenderBase.new 'http://localhost:' + port.to_s + '/track'
51
+ sender.queue = []
52
+ sender.send([1, 2])
53
+ thread.join
54
+ assert_equal [], sender.queue
55
+ end
56
+
57
+ def test_send_works_as_expected_with_500_code
58
+ thread, port = execute_server '500 InternalServerError'
59
+ sender = SenderBase.new 'http://localhost:' + port.to_s + '/track'
60
+ sender.queue = []
61
+ sender.send([1, 2])
62
+ thread.join
63
+ assert_equal [1, 2], sender.queue
64
+ end
65
+
66
+ def execute_server(code)
67
+ port = 50000 + Random.rand(10000)
68
+ thread = Thread.new {
69
+ server = TCPServer.new(port)
70
+ client = server.accept
71
+ request = ''
72
+ read_buffer_size = 64
73
+ while TRUE
74
+ temp = client.recv(read_buffer_size)
75
+ request += temp
76
+ break if temp.length < read_buffer_size
77
+ end
78
+ request = request.split(/\n/)[-1]
79
+ response = request
80
+ headers = [
81
+ "HTTP/1.1 " + code,
82
+ "Content-Type: application/json",
83
+ "Content-Length: #{response.length}\r\n\r\n"
84
+ ].join("\r\n")
85
+ client.puts headers
86
+ client.puts response
87
+ client.close
88
+ }
89
+
90
+ while thread.status != 'sleep'
91
+ sleep 0.1
92
+ end
93
+
94
+ return thread, port
95
+ end
96
+ end
@@ -0,0 +1,42 @@
1
+ require_relative '../../../lib/application_insights/channel/sender_base'
2
+ require_relative '../../../lib/application_insights/channel/synchronous_queue'
3
+ require 'test/unit'
4
+
5
+ include ApplicationInsights::Channel
6
+
7
+ class TestSynchronousQueue < Test::Unit::TestCase
8
+ def test_initialize
9
+ SynchronousQueue.new(MockSynchronousSender.new)
10
+ end
11
+
12
+ def test_flush_works_as_expected
13
+ sender = MockSynchronousSender.new
14
+ queue = SynchronousQueue.new sender
15
+ queue.max_queue_length = 3
16
+ (1..7).to_a.each do |i|
17
+ queue.push i
18
+ end
19
+ assert_equal [[1, 2], [3], [4, 5], [6]], sender.data
20
+ temp = []
21
+ queue.instance_variable_get('@queue').length.times do |i|
22
+ temp.push queue.instance_variable_get('@queue').pop
23
+ end
24
+ assert_equal [7], temp
25
+ end
26
+ end
27
+
28
+ class MockSynchronousSender
29
+ def initialize
30
+ @send_buffer_size = 2
31
+ @data = []
32
+ @queue = nil
33
+ end
34
+
35
+ attr_accessor :send_buffer_size
36
+ attr_accessor :data
37
+ attr_accessor :queue
38
+
39
+ def send(data_to_send)
40
+ @data.push data_to_send
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../../../lib/application_insights/channel/synchronous_sender'
2
+ require 'test/unit'
3
+
4
+ include ApplicationInsights::Channel
5
+
6
+ class TestSynchronousSender < Test::Unit::TestCase
7
+ def test_initialize
8
+ sender = SynchronousSender.new
9
+ assert_equal 'http://dc.services.visualstudio.com/v2/track', sender.service_endpoint_uri
10
+ end
11
+ end
@@ -0,0 +1,102 @@
1
+ require_relative '../../../lib/application_insights/channel/telemetry_channel'
2
+ require_relative '../../../lib/application_insights/channel/telemetry_context'
3
+ require_relative '../../../lib/application_insights/channel/synchronous_queue'
4
+ require_relative '../../../lib/application_insights/channel/synchronous_sender'
5
+ require 'test/unit'
6
+
7
+ include ApplicationInsights::Channel
8
+
9
+ class TestTelemetryChannel < Test::Unit::TestCase
10
+ def test_initialize
11
+ channel = TelemetryChannel.new
12
+ assert_not_nil channel.context
13
+ assert_not_nil channel.queue
14
+ assert_not_nil channel.sender
15
+ end
16
+
17
+ def test_context_works_as_expected
18
+ context = TelemetryContext.new
19
+ channel = TelemetryChannel.new
20
+ assert_not_nil channel.context
21
+ assert_not_same context, channel.context
22
+ channel = TelemetryChannel.new context
23
+ assert_same context, channel.context
24
+ end
25
+
26
+ def test_queue_works_as_expected
27
+ queue = SynchronousQueue.new SynchronousSender.new
28
+ channel = TelemetryChannel.new
29
+ assert_not_nil channel.queue
30
+ assert_not_same queue, channel.queue
31
+ channel = TelemetryChannel.new nil, queue
32
+ assert_same queue, channel.queue
33
+ end
34
+
35
+ def test_sender_works_as_expected
36
+ queue = SynchronousQueue.new SynchronousSender.new
37
+ channel = TelemetryChannel.new
38
+ assert_not_nil channel.sender
39
+ assert_not_same queue.sender, channel.sender
40
+ channel = TelemetryChannel.new nil, queue
41
+ assert_same queue.sender, channel.sender
42
+ end
43
+
44
+ def test_flush_works_as_expected
45
+ queue = MockTelemetryChannelQueue.new SynchronousSender.new
46
+ channel = TelemetryChannel.new nil, queue
47
+ assert_equal 0, queue.flush_count
48
+ channel.flush
49
+ assert_equal 1, queue.flush_count
50
+ end
51
+
52
+ def test_write_works_as_expected
53
+ queue = MockTelemetryChannelQueue.new SynchronousSender.new
54
+ context = TelemetryContext.new
55
+ context.instrumentation_key = 'instrumentation key'
56
+ channel = TelemetryChannel.new context, queue
57
+ expected = MockTelemetryItemData.new
58
+ channel.write expected
59
+ assert_equal 1, queue.queue.count
60
+ actual = queue.queue[0]
61
+ assert_not_nil actual
62
+ assert_equal 1, actual.ver
63
+ assert_equal 100, actual.sample_rate
64
+ assert_equal 'Microsoft.ApplicationInsights.MockTelemetryItem', actual.name
65
+ assert_not_nil actual.time
66
+ assert_equal 'instrumentation key', actual.i_key
67
+ assert_not_nil actual.tags
68
+ assert_equal 1, actual.tags.count
69
+ assert_equal 'rb:0.1.0', actual.tags['ai.internal.sdkVersion']
70
+ assert_not_nil actual.data
71
+ assert_equal 'MockTelemetryItemData', actual.data.base_type
72
+ assert_same expected, actual.data.base_data
73
+ end
74
+ end
75
+
76
+ class MockTelemetryItemData
77
+ def initialize
78
+ @properties = {}
79
+ end
80
+
81
+ attr_accessor :properties
82
+ end
83
+
84
+ class MockTelemetryChannelQueue < QueueBase
85
+ def initialize(sender)
86
+ super sender
87
+ @queue = []
88
+ @flush_count = 0
89
+ end
90
+
91
+ attr_accessor :flush_count
92
+
93
+ attr_accessor :queue
94
+
95
+ def push(data)
96
+ @queue.push data
97
+ end
98
+
99
+ def flush
100
+ @flush_count += 1
101
+ end
102
+ end