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 = nil, options = {}) ⇒ Object
Sends a single trace statement.
Constructor Details
#initialize(instrumentation_key = nil, telemetry_channel = nil) ⇒ TelemetryClient
Initializes a new instance of the class.
24 25 26 27 28 |
# File 'lib/application_insights/telemetry_client.rb', line 24 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
39 40 41 |
# File 'lib/application_insights/telemetry_client.rb', line 39 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.
33 34 35 |
# File 'lib/application_insights/telemetry_client.rb', line 33 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.
225 226 227 |
# File 'lib/application_insights/telemetry_client.rb', line 225 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.
125 126 127 128 129 130 131 132 133 |
# File 'lib/application_insights/telemetry_client.rb', line 125 def track_event(name, ={}) data = Channel::Contracts::EventData.new( :name => name || 'Null', :properties => [:properties] || {}, :measurements => [:measurements] || {} ) self.channel.write(data, self.context) end |
#track_exception(exception, options = {}) ⇒ Object
Send information about a single exception that occurred in the application.
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 |
# File 'lib/application_insights/telemetry_client.rb', line 75 def track_exception(exception, ={}) return unless exception.is_a? Exception parsed_stack = [] if exception.backtrace frame_pattern = /^(?<file>.*):(?<line>\d+)(\.|:in `((?<method>.*)'$))/ exception.backtrace.each_with_index do |frame, counter| match = frame_pattern.match frame stack_frame = Channel::Contracts::StackFrame.new( :assembly => 'Unknown', :file_name => match['file'], :level => counter, :line => match['line'], :method => match['method'] ) parsed_stack << stack_frame end end details = Channel::Contracts::ExceptionDetails.new( :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 ) data = Channel::Contracts::ExceptionData.new( :handled_at => .fetch(:handled_at, 'UserCode'), :exceptions => [details], :properties => [:properties] || {}, :measurements => [:measurements] || {} ) self.channel.write(data, self.context) end |
#track_metric(name, value, options = {}) ⇒ Object
Send information about a single metric data point that was captured for the application.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/application_insights/telemetry_client.rb', line 155 def track_metric(name, value, ={}) data_point = Channel::Contracts::DataPoint.new( :name => name || 'Null', :value => value || 0, :kind => [:type] || Channel::Contracts::DataPointType::AGGREGATION, :count => [:count], :min => [:min], :max => [:max], :std_dev => [:std_dev] ) data = Channel::Contracts::MetricData.new( :metrics => [data_point], :properties => [:properties] || {} ) 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).
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/application_insights/telemetry_client.rb', line 53 def track_page_view(name, url, ={}) data_attributes = { :name => name || 'Null', :url => url, :duration => [:duration], :properties => [:properties] || {}, :measurements => [: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.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/application_insights/telemetry_client.rb', line 206 def track_request(id, start_time, duration, response_code, success, ={}) data = Channel::Contracts::RequestData.new( :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 => [:properties] || {}, :measurements => [:measurements] || {} ) self.channel.write(data, self.context, start_time) end |
#track_trace(name, severity_level = nil, options = {}) ⇒ Object
Sends a single trace statement.
181 182 183 184 185 186 187 188 189 |
# File 'lib/application_insights/telemetry_client.rb', line 181 def track_trace(name, severity_level = nil, ={}) data = Channel::Contracts::MessageData.new( :message => name || 'Null', :severity_level => severity_level || Channel::Contracts::SeverityLevel::INFORMATION, :properties => [:properties] || {} ) self.channel.write(data, self.context) end |