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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(telemetry_channel = nil) ⇒ TelemetryClient

Initializes a new instance of the TelemetryClient class.



16
17
18
19
# File 'lib/application_insights/telemetry_client.rb', line 16

def initialize(telemetry_channel = nil)
  @context = Channel::TelemetryContext.new
  @channel = telemetry_channel || Channel::TelemetryChannel.new
end

Instance Attribute Details

#channelObject (readonly)

Gets the channel associated with this telemetry client.



25
26
27
# File 'lib/application_insights/telemetry_client.rb', line 25

def channel
  @channel
end

#contextObject (readonly)

Gets the context associated with this telemetry client.



22
23
24
# File 'lib/application_insights/telemetry_client.rb', line 22

def context
  @context
end

Instance Method Details

#flushObject

Flushes the current queue.



107
108
109
# File 'lib/application_insights/telemetry_client.rb', line 107

def flush
  self.channel.flush
end

#track_event(name, options = {}) ⇒ Object

Send an EventTelemetry object for display in Diagnostic Search and aggregation in Metrics Explorer.



65
66
67
68
69
70
71
72
73
# File 'lib/application_insights/telemetry_client.rb', line 65

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 an ExceptionTelemetry object for display in Diagnostic Search.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/application_insights/telemetry_client.rb', line 41

def track_exception(exception, options={})
  if exception.is_a? Exception
    details_attributes = {
      :id => 1,
      :outer_id => 0,
      :type_name => exception.class,
      :message => exception.message,
      :has_full_stack => true,
      :stack => exception.backtrace.join("\n")
    }
    details = Channel::Contracts::ExceptionDetails.new details_attributes

    data_attributes = {
      :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 a MetricTelemetry object for aggregation in Metric Explorer.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/application_insights/telemetry_client.rb', line 76

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



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/application_insights/telemetry_client.rb', line 28

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_trace(name, options = {}) ⇒ Object

Send a trace message for display in Diagnostic Search.



97
98
99
100
101
102
103
104
# File 'lib/application_insights/telemetry_client.rb', line 97

def track_trace(name, options={})
  data_attributes = {
    :message => name || 'Null',
    :properties => options.fetch(:measurements) { {} }
  }
  data = Channel::Contracts::MessageData.new data_attributes
  self.channel.write(data, self.context)
end