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