application_insights 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/.travis.yml +21 -9
- data/CONTRIBUTING.md +30 -0
- data/Gemfile +4 -4
- data/LICENSE.txt +11 -11
- data/README.md +13 -14
- data/Rakefile +14 -14
- data/application_insights.gemspec +28 -29
- data/lib/application_insights.rb +9 -9
- data/lib/application_insights/channel/asynchronous_queue.rb +58 -51
- data/lib/application_insights/channel/asynchronous_sender.rb +132 -123
- data/lib/application_insights/channel/contracts/application.rb +14 -53
- data/lib/application_insights/channel/contracts/cloud.rb +14 -0
- data/lib/application_insights/channel/contracts/data.rb +14 -48
- data/lib/application_insights/channel/contracts/data_point.rb +24 -130
- data/lib/application_insights/channel/contracts/data_point_type.rb +7 -16
- data/lib/application_insights/channel/contracts/dependency_kind.rb +9 -19
- data/lib/application_insights/channel/contracts/dependency_source_type.rb +9 -19
- data/lib/application_insights/channel/contracts/device.rb +28 -257
- data/lib/application_insights/channel/contracts/envelope.rb +40 -266
- data/lib/application_insights/channel/contracts/event_data.rb +28 -77
- data/lib/application_insights/channel/contracts/exception_data.rb +37 -140
- data/lib/application_insights/channel/contracts/exception_details.rb +28 -129
- data/lib/application_insights/channel/contracts/internal.rb +14 -53
- data/lib/application_insights/channel/contracts/json_serializable.rb +59 -59
- data/lib/application_insights/channel/contracts/location.rb +16 -36
- data/lib/application_insights/channel/contracts/message_data.rb +24 -77
- data/lib/application_insights/channel/contracts/metric_data.rb +27 -60
- data/lib/application_insights/channel/contracts/operation.rb +19 -121
- data/lib/application_insights/channel/contracts/page_view_data.rb +30 -111
- data/lib/application_insights/channel/contracts/remote_dependency_data.rb +56 -260
- data/lib/application_insights/channel/contracts/request_data.rb +36 -176
- data/lib/application_insights/channel/contracts/session.rb +15 -70
- data/lib/application_insights/channel/contracts/severity_level.rb +13 -25
- data/lib/application_insights/channel/contracts/stack_frame.rb +17 -94
- data/lib/application_insights/channel/contracts/user.rb +19 -104
- data/lib/application_insights/channel/event.rb +68 -64
- data/lib/application_insights/channel/queue_base.rb +65 -62
- data/lib/application_insights/channel/sender_base.rb +79 -72
- data/lib/application_insights/channel/synchronous_queue.rb +45 -39
- data/lib/application_insights/channel/synchronous_sender.rb +17 -15
- data/lib/application_insights/channel/telemetry_channel.rb +120 -102
- data/lib/application_insights/channel/telemetry_context.rb +85 -68
- data/lib/application_insights/rack/track_request.rb +87 -84
- data/lib/application_insights/telemetry_client.rb +229 -217
- data/lib/application_insights/unhandled_exception.rb +49 -47
- data/lib/application_insights/version.rb +3 -3
- data/test/application_insights.rb +8 -9
- data/test/application_insights/channel/contracts/test_application.rb +44 -44
- data/test/application_insights/channel/contracts/test_cloud.rb +44 -0
- data/test/application_insights/channel/contracts/test_data.rb +44 -44
- data/test/application_insights/channel/contracts/test_data_point.rb +109 -109
- data/test/application_insights/channel/contracts/test_device.rb +200 -200
- data/test/application_insights/channel/contracts/test_envelope.rb +209 -209
- data/test/application_insights/channel/contracts/test_event_data.rb +62 -62
- data/test/application_insights/channel/contracts/test_exception_data.rb +111 -111
- data/test/application_insights/channel/contracts/test_exception_details.rb +106 -106
- data/test/application_insights/channel/contracts/test_internal.rb +44 -44
- data/test/application_insights/channel/contracts/test_location.rb +70 -31
- data/test/application_insights/channel/contracts/test_message_data.rb +66 -66
- data/test/application_insights/channel/contracts/test_metric_data.rb +50 -50
- data/test/application_insights/channel/contracts/test_operation.rb +109 -96
- data/test/application_insights/channel/contracts/test_page_view_data.rb +88 -88
- data/test/application_insights/channel/contracts/test_remote_dependency_data.rb +209 -209
- data/test/application_insights/channel/contracts/test_request_data.rb +153 -153
- data/test/application_insights/channel/contracts/test_session.rb +57 -57
- data/test/application_insights/channel/contracts/test_stack_frame.rb +83 -83
- data/test/application_insights/channel/contracts/test_user.rb +96 -83
- data/test/application_insights/channel/test_asynchronous_queue.rb +47 -47
- data/test/application_insights/channel/test_asynchronous_sender.rb +80 -80
- data/test/application_insights/channel/test_event.rb +52 -52
- data/test/application_insights/channel/test_queue_base.rb +88 -88
- data/test/application_insights/channel/test_sender_base.rb +87 -87
- data/test/application_insights/channel/test_synchronous_queue.rb +27 -27
- data/test/application_insights/channel/test_synchronous_sender.rb +10 -10
- data/test/application_insights/channel/test_telemetry_channel.rb +126 -102
- data/test/application_insights/channel/test_telemetry_context.rb +82 -74
- data/test/application_insights/mock_sender.rb +37 -37
- data/test/application_insights/rack/test_track_request.rb +142 -139
- data/test/application_insights/test_telemetry_client.rb +133 -123
- data/test/application_insights/test_unhandled_exception.rb +23 -24
- metadata +23 -33
@@ -1,217 +1,229 @@
|
|
1
|
-
require_relative 'channel/telemetry_context'
|
2
|
-
require_relative 'channel/telemetry_channel'
|
3
|
-
require_relative 'channel/contracts/page_view_data'
|
4
|
-
require_relative 'channel/contracts/exception_data'
|
5
|
-
require_relative 'channel/contracts/exception_details'
|
6
|
-
require_relative 'channel/contracts/event_data'
|
7
|
-
require_relative 'channel/contracts/data_point'
|
8
|
-
require_relative 'channel/contracts/data_point_type'
|
9
|
-
require_relative 'channel/contracts/metric_data'
|
10
|
-
require_relative 'channel/contracts/message_data'
|
11
|
-
require_relative 'channel/contracts/stack_frame'
|
12
|
-
require_relative 'channel/contracts/request_data'
|
13
|
-
require_relative 'channel/contracts/severity_level'
|
14
|
-
|
15
|
-
module ApplicationInsights
|
16
|
-
# The telemetry client used for sending all types of telemetry. It serves as
|
17
|
-
# interacting with the Application Insights service.
|
18
|
-
class TelemetryClient
|
19
|
-
# Initializes a new instance of the class.
|
20
|
-
# @param [String] instrumentation_key to identify which Application Insights
|
21
|
-
#
|
22
|
-
#
|
23
|
-
|
24
|
-
|
25
|
-
@context
|
26
|
-
@
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# this
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
#
|
36
|
-
#
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# @
|
44
|
-
# @
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
#
|
66
|
-
# @
|
67
|
-
#
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
# @
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
:
|
158
|
-
:
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
#
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
#
|
192
|
-
# @
|
193
|
-
#
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
data = Channel::Contracts::RequestData.new
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
1
|
+
require_relative 'channel/telemetry_context'
|
2
|
+
require_relative 'channel/telemetry_channel'
|
3
|
+
require_relative 'channel/contracts/page_view_data'
|
4
|
+
require_relative 'channel/contracts/exception_data'
|
5
|
+
require_relative 'channel/contracts/exception_details'
|
6
|
+
require_relative 'channel/contracts/event_data'
|
7
|
+
require_relative 'channel/contracts/data_point'
|
8
|
+
require_relative 'channel/contracts/data_point_type'
|
9
|
+
require_relative 'channel/contracts/metric_data'
|
10
|
+
require_relative 'channel/contracts/message_data'
|
11
|
+
require_relative 'channel/contracts/stack_frame'
|
12
|
+
require_relative 'channel/contracts/request_data'
|
13
|
+
require_relative 'channel/contracts/severity_level'
|
14
|
+
|
15
|
+
module ApplicationInsights
|
16
|
+
# The telemetry client used for sending all types of telemetry. It serves as
|
17
|
+
# the main entry point for interacting with the Application Insights service.
|
18
|
+
class TelemetryClient
|
19
|
+
# Initializes a new instance of the class.
|
20
|
+
# @param [String] instrumentation_key to identify which Application Insights
|
21
|
+
# application this data is for.
|
22
|
+
# @param [Channel::TelemetryChannel] telemetry_channel the optional telemetry
|
23
|
+
# channel to be used instead of constructing a default one.
|
24
|
+
def initialize(instrumentation_key = nil, telemetry_channel = nil)
|
25
|
+
@context = Channel::TelemetryContext.new
|
26
|
+
@context.instrumentation_key = instrumentation_key
|
27
|
+
@channel = telemetry_channel || Channel::TelemetryChannel.new
|
28
|
+
end
|
29
|
+
|
30
|
+
# The context associated with this client. All data objects created by this
|
31
|
+
# client will be accompanied by this value.
|
32
|
+
# @return [Channel::TelemetryContext] the context instance.
|
33
|
+
attr_reader :context
|
34
|
+
|
35
|
+
# The channel associated with this telemetry client. All data created by this
|
36
|
+
# client will be passed along with the {#context} object to
|
37
|
+
# {Channel::TelemetryChannel#write}
|
38
|
+
# @return [Channel::TelemetryChannel] the channel instance.
|
39
|
+
attr_reader :channel
|
40
|
+
|
41
|
+
# Send information about the page viewed in the application (a web page for
|
42
|
+
# instance).
|
43
|
+
# @param [String] name the name of the page that was viewed.
|
44
|
+
# @param [String] url the URL of the page that was viewed.
|
45
|
+
# @param [Hash] options the options to create the
|
46
|
+
# {Channel::Contracts::PageViewData} object.
|
47
|
+
# @option options [Fixnum] :duration the duration of the page view in
|
48
|
+
# milliseconds. (defaults to: 0)
|
49
|
+
# @option options [Hash] :properties the set of custom properties the client
|
50
|
+
# wants attached to this data item. (defaults to: {})
|
51
|
+
# @option options [Hash] :measurements the set of custom measurements the
|
52
|
+
# client wants to attach to this data item (defaults to: {})
|
53
|
+
def track_page_view(name, url, options={})
|
54
|
+
data_attributes = {
|
55
|
+
:name => name || 'Null',
|
56
|
+
:url => url,
|
57
|
+
:duration => options[:duration],
|
58
|
+
:properties => options[:properties] || {},
|
59
|
+
:measurements => options[:measurements] || {}
|
60
|
+
}
|
61
|
+
data = Channel::Contracts::PageViewData.new data_attributes
|
62
|
+
self.channel.write(data, self.context)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Send information about a single exception that occurred in the application.
|
66
|
+
# @param [Exception] exception the exception that the client wants to send.
|
67
|
+
# @param [Hash] options the options to create the
|
68
|
+
# {Channel::Contracts::ExceptionData} object.
|
69
|
+
# @option options [String] :handled_at the type of exception
|
70
|
+
# (defaults to: 'UserCode')
|
71
|
+
# @option options [Hash] :properties the set of custom properties the client
|
72
|
+
# wants attached to this data item. (defaults to: {})
|
73
|
+
# @option options [Hash] :measurements the set of custom measurements the
|
74
|
+
# client wants to attach to this data item (defaults to: {})
|
75
|
+
def track_exception(exception, options={})
|
76
|
+
return unless exception.is_a? Exception
|
77
|
+
|
78
|
+
parsed_stack = []
|
79
|
+
if exception.backtrace
|
80
|
+
frame_pattern = /^(?<file>.*):(?<line>\d+)(\.|:in `((?<method>.*)'$))/
|
81
|
+
|
82
|
+
exception.backtrace.each_with_index do |frame, counter|
|
83
|
+
match = frame_pattern.match frame
|
84
|
+
stack_frame = Channel::Contracts::StackFrame.new(
|
85
|
+
:assembly => 'Unknown',
|
86
|
+
:file_name => match['file'],
|
87
|
+
:level => counter,
|
88
|
+
:line => match['line'],
|
89
|
+
:method => match['method']
|
90
|
+
)
|
91
|
+
|
92
|
+
parsed_stack << stack_frame
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
details = Channel::Contracts::ExceptionDetails.new(
|
97
|
+
:id => 1,
|
98
|
+
:outer_id => 0,
|
99
|
+
:type_name => exception.class.name,
|
100
|
+
:message => exception.message,
|
101
|
+
:has_full_stack => exception.backtrace != nil,
|
102
|
+
:stack => (exception.backtrace.join("\n") if exception.backtrace),
|
103
|
+
:parsed_stack => parsed_stack
|
104
|
+
)
|
105
|
+
|
106
|
+
data = Channel::Contracts::ExceptionData.new(
|
107
|
+
:handled_at => options.fetch(:handled_at, 'UserCode'),
|
108
|
+
:exceptions => [details],
|
109
|
+
:properties => options[:properties] || {},
|
110
|
+
:measurements => options[:measurements] || {}
|
111
|
+
)
|
112
|
+
|
113
|
+
self.channel.write(data, self.context)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Send information about a single event that has occurred in the context of
|
117
|
+
# the application.
|
118
|
+
# @param [String] name the data to associate to this event.
|
119
|
+
# @param [Hash] options the options to create the
|
120
|
+
# {Channel::Contracts::EventData} object.
|
121
|
+
# @option options [Hash] :properties the set of custom properties the client
|
122
|
+
# wants attached to this data item. (defaults to: {})
|
123
|
+
# @option options [Hash] :measurements the set of custom measurements the
|
124
|
+
# client wants to attach to this data item (defaults to: {})
|
125
|
+
def track_event(name, options={})
|
126
|
+
data = Channel::Contracts::EventData.new(
|
127
|
+
:name => name || 'Null',
|
128
|
+
:properties => options[:properties] || {},
|
129
|
+
:measurements => options[:measurements] || {}
|
130
|
+
)
|
131
|
+
|
132
|
+
self.channel.write(data, self.context)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Send information about a single metric data point that was captured for
|
136
|
+
# the application.
|
137
|
+
# @param [String] name the name of the metric that was captured.
|
138
|
+
# @param [Fixnum] value the value of the metric that was captured.
|
139
|
+
# @param [Hash] options the options to create the
|
140
|
+
# {Channel::Contracts::MetricData} object.
|
141
|
+
# @option options [Channel::Contracts::DataPointType] :type the type of the
|
142
|
+
# metric (defaults to: {Channel::Contracts::DataPointType::AGGREGATION})
|
143
|
+
# @option options [Fixnum] :count the number of metrics that were aggregated
|
144
|
+
# into this data point (defaults to: 0)
|
145
|
+
# @option options [Fixnum] :min the minimum of all metrics collected that
|
146
|
+
# were aggregated into this data point (defaults to: 0)
|
147
|
+
# @option options [Fixnum] :max the maximum of all metrics collected that
|
148
|
+
# were aggregated into this data point (defaults to: 0)
|
149
|
+
# @option options [Fixnum] :std_dev the standard deviation of all metrics
|
150
|
+
# collected that were aggregated into this data point (defaults to: 0)
|
151
|
+
# @option options [Hash] :properties the set of custom properties the client
|
152
|
+
# wants attached to this data item. (defaults to: {})
|
153
|
+
# @option options [Hash] :measurements the set of custom measurements the
|
154
|
+
# client wants to attach to this data item (defaults to: {})
|
155
|
+
def track_metric(name, value, options={})
|
156
|
+
data_point = Channel::Contracts::DataPoint.new(
|
157
|
+
:name => name || 'Null',
|
158
|
+
:value => value || 0,
|
159
|
+
:kind => options[:type] || Channel::Contracts::DataPointType::AGGREGATION,
|
160
|
+
:count => options[:count],
|
161
|
+
:min => options[:min],
|
162
|
+
:max => options[:max],
|
163
|
+
:std_dev => options[:std_dev]
|
164
|
+
)
|
165
|
+
|
166
|
+
data = Channel::Contracts::MetricData.new(
|
167
|
+
:metrics => [data_point],
|
168
|
+
:properties => options[:properties] || {}
|
169
|
+
)
|
170
|
+
|
171
|
+
self.channel.write(data, self.context)
|
172
|
+
end
|
173
|
+
|
174
|
+
# Sends a single trace statement.
|
175
|
+
# @param [String] name the trace statement.
|
176
|
+
# @param [Channel::Contracts::SeverityLevel] severity_level the severity level.
|
177
|
+
# @param [Hash] options the options to create the
|
178
|
+
# {Channel::Contracts::EventData} object.
|
179
|
+
# @option options [Hash] :properties the set of custom properties the client
|
180
|
+
# wants attached to this data item. (defaults to: {})
|
181
|
+
def track_trace(name, severity_level = nil, options={})
|
182
|
+
data = Channel::Contracts::MessageData.new(
|
183
|
+
:message => name || 'Null',
|
184
|
+
:severity_level => severity_level || Channel::Contracts::SeverityLevel::INFORMATION,
|
185
|
+
:properties => options[:properties] || {}
|
186
|
+
)
|
187
|
+
|
188
|
+
self.channel.write(data, self.context)
|
189
|
+
end
|
190
|
+
|
191
|
+
# Sends a single request.
|
192
|
+
# @param [String] id the unique identifier of the request.
|
193
|
+
# @param (String) start_time the start time of the request.
|
194
|
+
# @param [String] duration the duration to process the request.
|
195
|
+
# @param [String] response_code the response code of the request.
|
196
|
+
# @param [Boolean] success indicates whether the request succeeds or not.
|
197
|
+
# @param [Hash] options the options to create the
|
198
|
+
# {Channel::Contracts::RequestData} object.
|
199
|
+
# @option options [String] :name the name of the request.
|
200
|
+
# @option options [String] :http_method the http method used for the request.
|
201
|
+
# @option options [String] :url the url of the request.
|
202
|
+
# @option options [Hash] :properties the set of custom properties the client
|
203
|
+
# wants attached to this data item. (defaults to: {})
|
204
|
+
# @option options [Hash] :measurements the set of custom measurements the
|
205
|
+
# client wants to attach to this data item (defaults to: {})
|
206
|
+
def track_request(id, start_time, duration, response_code, success, options={})
|
207
|
+
data = Channel::Contracts::RequestData.new(
|
208
|
+
:id => id || 'Null',
|
209
|
+
:start_time => start_time || Time.now.iso8601(7),
|
210
|
+
:duration => duration || '0:00:00:00.0000000',
|
211
|
+
:response_code => response_code || 200,
|
212
|
+
:success => success = nil ? true : success,
|
213
|
+
:name => options[:name],
|
214
|
+
:http_method => options[:http_method],
|
215
|
+
:url => options[:url],
|
216
|
+
:properties => options[:properties] || {},
|
217
|
+
:measurements => options[:measurements] || {}
|
218
|
+
)
|
219
|
+
|
220
|
+
self.channel.write(data, self.context)
|
221
|
+
end
|
222
|
+
|
223
|
+
# Flushes data in the queue. Data in the queue will be sent either immediately
|
224
|
+
# irrespective of what sender is being used.
|
225
|
+
def flush
|
226
|
+
self.channel.flush
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
@@ -1,47 +1,49 @@
|
|
1
|
-
require_relative 'telemetry_client'
|
2
|
-
require_relative 'channel/telemetry_channel'
|
3
|
-
require_relative 'channel/synchronous_queue'
|
4
|
-
require_relative 'channel/synchronous_sender'
|
5
|
-
|
6
|
-
include ApplicationInsights
|
7
|
-
|
8
|
-
module ApplicationInsights
|
9
|
-
module UnhandledException
|
10
|
-
@sender = nil
|
11
|
-
|
12
|
-
# Auto collects unhandled exception and send to the Application Insights service.
|
13
|
-
# @param (string) instrumentation_key used to identify which Application
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
@sender =
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
1
|
+
require_relative 'telemetry_client'
|
2
|
+
require_relative 'channel/telemetry_channel'
|
3
|
+
require_relative 'channel/synchronous_queue'
|
4
|
+
require_relative 'channel/synchronous_sender'
|
5
|
+
|
6
|
+
include ApplicationInsights
|
7
|
+
|
8
|
+
module ApplicationInsights
|
9
|
+
module UnhandledException
|
10
|
+
@sender = nil
|
11
|
+
|
12
|
+
# Auto collects unhandled exception and send to the Application Insights service.
|
13
|
+
# @param (string) instrumentation_key used to identify which Application
|
14
|
+
# Insights application this data is for.
|
15
|
+
# @example
|
16
|
+
# require 'application_insights'
|
17
|
+
# ApplicationInsights::UnhandledException.collect('<YOUR INSTRUMENTATION KEY GOES HERE>')
|
18
|
+
# raise Exception, 'Boom!'
|
19
|
+
def self.collect(instrumentation_key)
|
20
|
+
at_exit do
|
21
|
+
# Avoid sending exception more than once if this method got invoked multiple times
|
22
|
+
send(instrumentation_key) unless @sender
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# @api private
|
27
|
+
# Send the last raised exception to the Application Insights service if
|
28
|
+
# telemetry_sender is not customized.
|
29
|
+
# @param (string) instrumentation_key used to identify which Application
|
30
|
+
# Insights application this data is for.
|
31
|
+
# @param (SenderBase) telemetry_sender used to send the last raised exception.
|
32
|
+
def self.send(instrumentation_key, telemetry_sender = nil)
|
33
|
+
if $! && !$!.is_a?(SystemExit) && !$!.is_a?(SignalException)
|
34
|
+
if telemetry_sender
|
35
|
+
@sender = telemetry_sender
|
36
|
+
elsif !@sender
|
37
|
+
# Use a synchronized sender to guarantee the data would be sent out once flush
|
38
|
+
@sender = Channel::SynchronousSender.new
|
39
|
+
end
|
40
|
+
|
41
|
+
queue = Channel::SynchronousQueue.new @sender
|
42
|
+
channel = Channel::TelemetryChannel.new nil, queue
|
43
|
+
client = TelemetryClient.new instrumentation_key, channel
|
44
|
+
client.track_exception($!, handled_at: 'Unhandled')
|
45
|
+
client.flush
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|