Class: ApplicationInsights::Channel::TelemetryChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/application_insights/channel/telemetry_channel.rb

Overview

The telemetry channel is responsible for constructing a Contracts::Envelope object from the passed in data and specified telemetry context.

Examples:

require 'application_insights'
channel = ApplicationInsights::Channel::TelemetryChannel.new
event = ApplicationInsights::Channel::Contracts::EventData.new name: 'My event'
channel.write event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil, queue = nil) ⇒ TelemetryChannel

Initializes a new instance of the class.

Parameters:



29
30
31
32
# File 'lib/application_insights/channel/telemetry_channel.rb', line 29

def initialize(context=nil, queue=nil)
  @context = context || TelemetryContext.new
  @queue = queue || SynchronousQueue.new(SynchronousSender.new)
end

Instance Attribute Details

#contextTelemetryContext (readonly)

The context associated with this channel. All Contracts::Envelope objects created by this channel will use this value if it’s present or if none is specified as part of the #write call.

Returns:

  • (TelemetryContext)

    the context instance (defaults to: TelemetryContext.new)



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

def context
  @context
end

#queueQueueBase (readonly)

The queue associated with this channel. All Contracts::Envelope objects created by this channel will be pushed to this queue.

Returns:

  • (QueueBase)

    the queue instance (defaults to: SynchronousQueue.new)



44
45
46
# File 'lib/application_insights/channel/telemetry_channel.rb', line 44

def queue
  @queue
end

Instance Method Details

#flushObject

Flushes the enqueued data by calling QueueBase#flush.



54
55
56
# File 'lib/application_insights/channel/telemetry_channel.rb', line 54

def flush
  @queue.flush
end

#senderSenderBase

The sender associated with this channel. This instance will be used to transmit telemetry to the service.

Returns:

  • (SenderBase)

    the sender instance (defaults to: SynchronousSender.new)



49
50
51
# File 'lib/application_insights/channel/telemetry_channel.rb', line 49

def sender
  @queue.sender
end

#write(data, context = nil, time = nil) ⇒ Object

Enqueues the passed in data to the #queue. If the caller specifies a context as well, it will take precedence over the instance in #context.

Parameters:

Raises:

  • (ArgumentError)


66
67
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
# File 'lib/application_insights/channel/telemetry_channel.rb', line 66

def write(data, context=nil, time=nil)
  local_context = context || @context
  raise ArgumentError, 'Context was required but not provided' unless local_context

  if time && time.is_a?(String)
    local_time = time
  elsif time && time.is_a?(Time)
    local_time = time.iso8601(7)
  else
    local_time = Time.now.iso8601(7)
  end

  data_type = data.class.name.gsub(/^.*::/, '')
  set_properties data, local_context
  data_attributes = {
      :base_type => data_type,
      :base_data => data
  }
  envelope_attributes = {
    :name => 'Microsoft.ApplicationInsights.' + data_type[0..-5],
    :time => local_time,
    :i_key => local_context.instrumentation_key,
    :tags => get_tags(local_context),
    :data => Contracts::Data.new(data_attributes)
  }
  envelope = Contracts::Envelope.new envelope_attributes
  @queue.push(envelope)
end