Class: ApplicationInsights::TelemetryClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(instrumentation_key = nil, telemetry_channel = nil) ⇒ TelemetryClient

Initializes a new instance of the class.

Parameters:

  • instrumentation_key (String) (defaults to: nil)

    to identify which Application Insights application this data is for.

  • telemetry_channel (Channel::TelemetryChannel) (defaults to: nil)

    the optional telemetry channel to be used instead of constructing a default one.



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

#channelChannel::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

Returns:



39
40
41
# File 'lib/application_insights/telemetry_client.rb', line 39

def channel
  @channel
end

#contextChannel::TelemetryContext (readonly)

The context associated with this client. All data objects created by this client will be accompanied by this value.

Returns:



33
34
35
# File 'lib/application_insights/telemetry_client.rb', line 33

def context
  @context
end

Instance Method Details

#flushObject

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.

Parameters:

  • name (String)

    the data to associate to this event.

  • options (Hash) (defaults to: {})

    the options to create the Channel::Contracts::EventData object.

Options Hash (options):

  • :properties (Hash)

    the set of custom properties the client wants attached to this data item. (defaults to: {})

  • :measurements (Hash)

    the set of custom measurements the client wants to attach to this data item (defaults to: {})



125
126
127
128
129
130
131
132
133
# File 'lib/application_insights/telemetry_client.rb', line 125

def track_event(name, options={})
  data = Channel::Contracts::EventData.new(
    :name => name || 'Null',
    :properties => options[:properties] || {},
    :measurements => options[:measurements] || {}
  )

  self.channel.write(data, self.context)
end

#track_exception(exception, options = {}) ⇒ Object

Send information about a single exception that occurred in the application.

Parameters:

  • exception (Exception)

    the exception that the client wants to send.

  • options (Hash) (defaults to: {})

    the options to create the Channel::Contracts::ExceptionData object.

Options Hash (options):

  • :handled_at (String)

    the type of exception (defaults to: ‘UserCode’)

  • :properties (Hash)

    the set of custom properties the client wants attached to this data item. (defaults to: {})

  • :measurements (Hash)

    the set of custom measurements the client wants to attach to this data item (defaults to: {})



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, options={})
  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.message,
    :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 => options.fetch(:handled_at, 'UserCode'),
    :exceptions => [details],
    :properties => options[:properties] || {},
    :measurements => options[: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.

Parameters:

  • name (String)

    the name of the metric that was captured.

  • value (Fixnum)

    the value of the metric that was captured.

  • options (Hash) (defaults to: {})

    the options to create the Channel::Contracts::MetricData object.

Options Hash (options):

  • :type (Channel::Contracts::DataPointType)

    the type of the metric (defaults to: Channel::Contracts::DataPointType::AGGREGATION)

  • :count (Fixnum)

    the number of metrics that were aggregated into this data point (defaults to: 0)

  • :min (Fixnum)

    the minimum of all metrics collected that were aggregated into this data point (defaults to: 0)

  • :max (Fixnum)

    the maximum of all metrics collected that were aggregated into this data point (defaults to: 0)

  • :std_dev (Fixnum)

    the standard deviation of all metrics collected that were aggregated into this data point (defaults to: 0)

  • :properties (Hash)

    the set of custom properties the client wants attached to this data item. (defaults to: {})

  • :measurements (Hash)

    the set of custom measurements the client wants to attach to this data item (defaults to: {})



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, options={})
  data_point = Channel::Contracts::DataPoint.new(
    :name => name || 'Null',
    :value => value || 0,
    :kind => options[:type] || Channel::Contracts::DataPointType::AGGREGATION,
    :count => options[:count],
    :min => options[:min],
    :max => options[:max],
    :std_dev => options[:std_dev]
  )

  data = Channel::Contracts::MetricData.new(
    :metrics => [data_point],
    :properties => options[: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).

Parameters:

  • name (String)

    the name of the page that was viewed.

  • url (String)

    the URL of the page that was viewed.

  • options (Hash) (defaults to: {})

    the options to create the Channel::Contracts::PageViewData object.

Options Hash (options):

  • :duration (Fixnum)

    the duration of the page view in milliseconds. (defaults to: 0)

  • :properties (Hash)

    the set of custom properties the client wants attached to this data item. (defaults to: {})

  • :measurements (Hash)

    the set of custom measurements the client wants to attach to this data item (defaults to: {})



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, options={})
  data_attributes = {
    :name => name || 'Null',
    :url => url,
    :duration => options[:duration],
    :properties => options[:properties] || {},
    :measurements => options[: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.

Parameters:

  • id (String)

    the unique identifier of the request.

  • start_time (String)

    the start time of the request.

  • duration (String)

    the duration to process the request.

  • response_code (String)

    the response code of the request.

  • success (Boolean)

    indicates whether the request succeeds or not.

  • options (Hash) (defaults to: {})

    the options to create the Channel::Contracts::RequestData object.

Options Hash (options):

  • :name (String)

    the name of the request.

  • :http_method (String)

    the http method used for the request.

  • :url (String)

    the url of the request.

  • :properties (Hash)

    the set of custom properties the client wants attached to this data item. (defaults to: {})

  • :measurements (Hash)

    the set of custom measurements the client wants to attach to this data item (defaults to: {})



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, options={})
  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 => options[:name],
    :http_method => options[:http_method],
    :url => options[:url],
    :properties => options[:properties] || {},
    :measurements => options[:measurements] || {}
  )

  self.channel.write(data, self.context, start_time)
end

#track_trace(name, severity_level = nil, options = {}) ⇒ Object

Sends a single trace statement.

Parameters:

Options Hash (options):

  • :properties (Hash)

    the set of custom properties the client wants attached to this data item. (defaults to: {})



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, options={})
  data = Channel::Contracts::MessageData.new(
    :message => name || 'Null',
    :severity_level => severity_level || Channel::Contracts::SeverityLevel::INFORMATION,
    :properties => options[:properties] || {}
  )

  self.channel.write(data, self.context)
end