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.



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

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



37
38
39
# File 'lib/application_insights/telemetry_client.rb', line 37

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:



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

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.



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.

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: {})



116
117
118
119
120
121
122
123
124
# File 'lib/application_insights/telemetry_client.rb', line 116

def track_event(name, options={})
  data_attributes = {
    :name => name || 'Null',
    :properties => options.fetch(:properties) { {} },
    :measurements => options.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.

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: {})



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

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: {})



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

  data_attributes = {
    :metrics => [ data_point ],
    :properties => options.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).

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: {})



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

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: {})



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

Parameters:

Options Hash (options):

  • :properties (Hash)

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



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, options={})
  data_attributes = {
      :message => name || 'Null',
      :severity_level => severity_level || Channel::Contracts::SeverityLevel::INFORMATION,
      :properties => options.fetch(:properties) { {} }
  }
  data = Channel::Contracts::MessageData.new data_attributes
  self.channel.write(data, self.context)
end