Class: LogStash::Outputs::MicrosoftSentinelOutputInternal::SampleFileCreator

Inherits:
EventsHandler
  • Object
show all
Defined in:
lib/logstash/sentinel/sampleFileCreator.rb

Instance Method Summary collapse

Methods inherited from EventsHandler

#create_event_document

Constructor Details

#initialize(logstashLogAnalyticsConfiguration) ⇒ SampleFileCreator

Returns a new instance of SampleFileCreator.



10
11
12
13
14
15
16
# File 'lib/logstash/sentinel/sampleFileCreator.rb', line 10

def initialize(logstashLogAnalyticsConfiguration)
  @events_buffer = Concurrent::Array.new
  @maximum_events_to_sample = 10
  @was_file_written = false
  @writing_mutex = Mutex.new
  super
end

Instance Method Details

#closeObject



28
29
30
# File 'lib/logstash/sentinel/sampleFileCreator.rb', line 28

def close
  try_writing_events_to_file(true)
end

#handle_events(events) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/logstash/sentinel/sampleFileCreator.rb', line 18

def handle_events(events)
  events.each do |event|
    if !@was_file_written
      filtered_event = create_event_document(event)
      @events_buffer.push(filtered_event)
    end
  end
  try_writing_events_to_file
end

#try_writing_events_to_file(force = false) ⇒ Object



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
# File 'lib/logstash/sentinel/sampleFileCreator.rb', line 32

def try_writing_events_to_file(force = false)
  if @was_file_written
    return
  end

  @writing_mutex.synchronize do
    #check if file was written during the wait
    if @was_file_written ||
       @events_buffer.length == 0 ||
       (@events_buffer.length <= @maximum_events_to_sample && !force) 
      return
    end

    output_path = @logstashLogAnalyticsConfiguration.sample_file_path
    output_file_name = "sampleFile#{Time.now.to_i}.json"
    file = java.io.File.new(output_path,output_file_name)
    fw = java.io.FileWriter.new(file)
    fw.write(@events_buffer.take(@maximum_events_to_sample).to_json)
    fw.flush
    fw.close

    @was_file_written = true
    @logger.info("Sample file was written in path: #{file.getAbsolutePath}")
  end
end