Class: ApplicationInsights::TelemetryClient
- Inherits:
-
Object
- Object
- ApplicationInsights::TelemetryClient
- Defined in:
- lib/application_insights/telemetry_client.rb
Overview
The telemetry client used for sending all types of telemetry. It serves as the main entry point for interacting with the Application Insights service.
Instance Attribute Summary collapse
-
#channel ⇒ Channel::TelemetryChannel
readonly
The channel associated with this telemetry client.
-
#context ⇒ Channel::TelemetryContext
readonly
The context associated with this client.
Instance Method Summary collapse
-
#flush ⇒ Object
Flushes data in the queue.
-
#initialize(instrumentation_key = nil, telemetry_channel = nil) ⇒ TelemetryClient
constructor
Initializes a new instance of the class.
-
#track_event(name, options = {}) ⇒ Object
Send information about a single event that has occurred in the context of the application.
-
#track_exception(exception, options = {}) ⇒ Object
Send information about a single exception that occurred in the application.
-
#track_metric(name, value, options = {}) ⇒ Object
Send information about a single metric data point that was captured for the application.
-
#track_page_view(name, url, options = {}) ⇒ Object
Send information about the page viewed in the application (a web page for instance).
-
#track_request(id, start_time, duration, response_code, success, options = {}) ⇒ Object
Sends a single request.
-
#track_trace(name, severity_level = Channel::Contracts::SeverityLevel::INFORMATION, options = {}) ⇒ Object
Sends a single trace statement.
Constructor Details
#initialize(instrumentation_key = nil, telemetry_channel = nil) ⇒ TelemetryClient
Initializes a new instance of the class.
23 24 25 26 27 |
# File 'lib/application_insights/telemetry_client.rb', line 23 def initialize(instrumentation_key = nil, telemetry_channel = nil) @context = Channel::TelemetryContext.new @context.instrumentation_key = instrumentation_key @channel = telemetry_channel || Channel::TelemetryChannel.new end |
Instance Attribute Details
#channel ⇒ Channel::TelemetryChannel (readonly)
The channel associated with this telemetry client. All data created by this client will be passed along with the #context object to Channel::TelemetryChannel#write
37 38 39 |
# File 'lib/application_insights/telemetry_client.rb', line 37 def channel @channel end |
#context ⇒ Channel::TelemetryContext (readonly)
The context associated with this client. All data objects created by this client will be accompanied by this value.
32 33 34 |
# File 'lib/application_insights/telemetry_client.rb', line 32 def context @context end |
Instance Method Details
#flush ⇒ Object
Flushes data in the queue. Data in the queue will be sent either immediately irrespective of what sender is being used.
213 214 215 |
# File 'lib/application_insights/telemetry_client.rb', line 213 def flush self.channel.flush end |
#track_event(name, options = {}) ⇒ Object
Send information about a single event that has occurred in the context of the application.
116 117 118 119 120 121 122 123 124 |
# File 'lib/application_insights/telemetry_client.rb', line 116 def track_event(name, ={}) data_attributes = { :name => name || 'Null', :properties => .fetch(:properties) { {} }, :measurements => .fetch(:measurements) { {} } } data = Channel::Contracts::EventData.new data_attributes self.channel.write(data, self.context) end |
#track_exception(exception, options = {}) ⇒ Object
Send information about a single exception that occurred in the application.
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 |
# File 'lib/application_insights/telemetry_client.rb', line 68 def track_exception(exception, ={}) if exception.is_a? Exception parsed_stack = [] if exception.backtrace frame_pattern = /^(?<file>.*):(?<line>\d+)(\.|:in `((?<method>.*)'$))/ counter = 0; exception.backtrace.each do |frame| match = frame_pattern.match frame stack_frame = Channel::Contracts::StackFrame.new stack_frame.assembly = 'Unknown' stack_frame.file_name = match['file'] stack_frame.level = counter stack_frame.line = match['line'] stack_frame.method = match['method'] parsed_stack << stack_frame counter += 1 end end details_attributes = { :id => 1, :outer_id => 0, :type_name => exception.class.name, :message => exception., :has_full_stack => exception.backtrace != nil, :stack => (exception.backtrace.join("\n") if exception.backtrace), :parsed_stack => parsed_stack } details = Channel::Contracts::ExceptionDetails.new details_attributes data_attributes = { :handled_at => .fetch(:handled_at,'UserCode'), :exceptions => [ details ], :properties => .fetch(:properties) { {} }, :measurements => .fetch(:measurements) { {} } } data = Channel::Contracts::ExceptionData.new data_attributes self.channel.write(data, self.context) end end |
#track_metric(name, value, options = {}) ⇒ Object
Send information about a single metric data point that was captured for the application.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/application_insights/telemetry_client.rb', line 144 def track_metric(name, value, ={}) data_point_attributes = { :name => name || 'Null', :value => value || 0, :kind => .fetch(:type) { Channel::Contracts::DataPointType::AGGREGATION }, :count => [:count], :min => [:min], :max => [:max], :std_dev => [:std_dev] } data_point = Channel::Contracts::DataPoint.new data_point_attributes data_attributes = { :metrics => [ data_point ], :properties => .fetch(:properties) { {} } } data = Channel::Contracts::MetricData.new data_attributes self.channel.write(data, self.context) end |
#track_page_view(name, url, options = {}) ⇒ Object
Send information about the page viewed in the application (a web page for instance).
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/application_insights/telemetry_client.rb', line 48 def track_page_view(name, url, ={}) data_attributes = { :name => name || 'Null', :url => url, :duration => [:duration], :properties => .fetch(:properties) { {} }, :measurements => .fetch(:measurements) { {} } } data = Channel::Contracts::PageViewData.new data_attributes self.channel.write(data, self.context) end |
#track_request(id, start_time, duration, response_code, success, options = {}) ⇒ Object
Sends a single request.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/application_insights/telemetry_client.rb', line 194 def track_request(id, start_time, duration, response_code, success, ={}) data_attributes = { :id => id || 'Null', :start_time => start_time || Time.now.iso8601(7), :duration => duration || '0:00:00:00.0000000', :response_code => response_code || 200, :success => success = nil ? true : success, :name => [:name], :http_method => [:http_method], :url => [:url], :properties => .fetch(:properties) { {} }, :measurements => .fetch(:measurements) { {} } } data = Channel::Contracts::RequestData.new data_attributes self.channel.write(data, self.context) end |
#track_trace(name, severity_level = Channel::Contracts::SeverityLevel::INFORMATION, options = {}) ⇒ Object
Sends a single trace statement.
170 171 172 173 174 175 176 177 178 |
# File 'lib/application_insights/telemetry_client.rb', line 170 def track_trace(name, severity_level = Channel::Contracts::SeverityLevel::INFORMATION, ={}) data_attributes = { :message => name || 'Null', :severity_level => severity_level || Channel::Contracts::SeverityLevel::INFORMATION, :properties => .fetch(:properties) { {} } } data = Channel::Contracts::MessageData.new data_attributes self.channel.write(data, self.context) end |