Class: LogStash::Outputs::ApplicationInsights

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/applicationinsights.rb

Instance Method Summary collapse

Instance Method Details

#closeObject

def multi_receive



59
60
61
# File 'lib/logstash/outputs/applicationinsights.rb', line 59

def close
  @client.flush
end

#create_clientObject

def close



63
64
65
66
67
68
# File 'lib/logstash/outputs/applicationinsights.rb', line 63

def create_client
  telemetry_context = TelemetryContext.new
  async_queue = AsynchronousQueue.new(AsynchronousSender.new)
  telemetry_channel = TelemetryChannel.new(telemetry_context, async_queue)
  @client = TelemetryClient.new(@ikey, telemetry_channel)
end

#get_ai_properties(event) ⇒ Object

def get_ai_message



79
80
81
82
83
84
85
86
# File 'lib/logstash/outputs/applicationinsights.rb', line 79

def get_ai_properties(event)
  ai_properties = event.to_hash.fetch(@ai_properties_field, nil)
  if !@ai_properties_field.nil? && ai_properties.nil?
    @logger.warn("#{@ai_properties_field} specified in ai_properties_field not found in event data. Will use all fields in event as AI properties.")
  end # if

  ai_properties || event.to_hash
end

#get_ai_severity(event) ⇒ Object

def get_ai_properties



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/logstash/outputs/applicationinsights.rb', line 88

def get_ai_severity(event)
  return nil if @ai_severity_level_field.nil? 

  severity_value = event.get(@ai_severity_level_field)

  if !@ai_severity_level_mapping.nil? && @ai_severity_level_mapping.any?
    ai_severity_level = @ai_severity_level_mapping.fetch(severity_value, nil)
  else
    ai_severity_level = severity_value
  end # unless

  if ai_severity_level.nil?
    @logger.warn("Cannot map value '#{severity_value}' from '#{@ai_severity_level_field}' to AI severity level. Will use default value.")
  else
    event.remove(@ai_severity_level_field)  # Removes the duplicated severity field.
  end # if
  
  ai_severity_level
end

#get_field(event, field_name) ⇒ Object

def create_client



70
71
72
73
74
75
76
77
# File 'lib/logstash/outputs/applicationinsights.rb', line 70

def get_field(event, field_name)
  return nil if field_name.nil?
  
  field = event.get(field_name) # Extracts specified field value as the AI Message.
  @logger.warn("#{field_name} not found in event data.") if field.nil?
  event.remove(field_name) unless field.nil?  # Removes the duplicated AI field.
  field
end

#multi_receive(events) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/logstash/outputs/applicationinsights.rb', line 28

def multi_receive(events)
  events.each do |event|
    begin
      ai_properties = get_ai_properties(event)
      if @ai_type == "trace"
        ai_message = get_field(event, @ai_message_field)
        ai_severity = get_ai_severity(event)
        @client.track_trace(ai_message, ai_severity, { :properties => ai_properties })
      elsif @ai_type == "metric"
        if !@ai_metrics_names.nil? && @ai_metrics_names.any?
          @ai_metrics_names.each do |metric_name|
            metric_value = get_field(event, metric_name)
            if metric_value.nil?
              @logger.warn("#{@metric_name} specified in ai_metrics_names not found in event data.")
            else
              @client.track_metric(metric_name, metric_value.to_f, { :properties => ai_properties })
            end  # if
          end # do
        end # if ai_metric_fields
      elsif @ai_type == "event"
        @client.track_event(@ai_event_name, { :properties => ai_properties }) if !@ai_event_name.nil?
      end # if ai_type

      @client.flush if @dev_mode

    rescue => e
      @logger.error("Error occurred sending data to AI.", :exception => e)
    end # begin
  end # do 
end

#registerObject



23
24
25
# File 'lib/logstash/outputs/applicationinsights.rb', line 23

def register
   create_client
end