class LogStash::Outputs::Gcs::PathFactory

PathFactory creates paths for rotating files.

Public Class Methods

new(directory, prefix, include_host, date_pattern, include_part, include_uuid, is_gzipped) click to toggle source
# File lib/logstash/outputs/gcs/path_factory.rb, line 9
def initialize(directory, prefix, include_host, date_pattern, include_part, include_uuid, is_gzipped)
  @path_lock = Mutex.new

  pattern = '%{prefix}'
  pattern += '_%{host}' if include_host
  pattern += '_%{date}'
  @base_pattern = pattern

  pattern += '.part%{partf}' if include_part
  pattern += '.%{uuid}' if include_uuid
  pattern += '.log'
  pattern += '.gz' if is_gzipped
  @pattern = pattern

  @prefix = prefix
  @directory = directory
  @date_pattern = date_pattern

  @part_number = starting_part
  @current = template_variables
end

Public Instance Methods

current_path(vars=nil) click to toggle source

Returns the full path to the current file including parent directory.

# File lib/logstash/outputs/gcs/path_factory.rb, line 53
def current_path(vars=nil)
  @path_lock.synchronize {
    filename = @pattern % (vars || @current)
    ::File.join(@directory, filename)
  }
end
rotate_path!() click to toggle source

Rotates the path to the next one in sequence. If the path has a part number and the base path (date/hostname) haven't changed the part number is incremented. Returns the path that was rotated out

# File lib/logstash/outputs/gcs/path_factory.rb, line 34
def rotate_path!
  last_path = current_path

  @path_lock.synchronize {
    @part_number = (next_base == current_base) ? @part_number + 1 : 0
    @current = template_variables
  }

  last_path
end
should_rotate?() click to toggle source

Checks if the file is ready to rotate because the timestamp changed.

# File lib/logstash/outputs/gcs/path_factory.rb, line 46
def should_rotate?
  @path_lock.synchronize {
    next_base != current_base
  }
end

Private Instance Methods

current_base() click to toggle source
# File lib/logstash/outputs/gcs/path_factory.rb, line 92
def current_base
  @base_pattern % @current
end
next_base() click to toggle source
# File lib/logstash/outputs/gcs/path_factory.rb, line 88
def next_base
  @base_pattern % template_variables
end
starting_part() click to toggle source

search through the directory for a file with the same base, and if it exists, set our part to be the max + 1 so we don't clobber existing files.

# File lib/logstash/outputs/gcs/path_factory.rb, line 64
def starting_part
  return 0 unless ::File.directory? @directory

  base_path = ::File.join(@directory, next_base)

  part_numbers = Dir.glob(base_path + '.part*').map do |item|
    match = /^.*\.part(?<part_num>\d+).*$/.match(item)
    next if match.nil?
    match[:part_num].to_i
  end

  part_numbers.any? ? part_numbers.max + 1 : 0
end
template_variables() click to toggle source
# File lib/logstash/outputs/gcs/path_factory.rb, line 78
def template_variables
  {
      prefix: @prefix,
      host: Socket.gethostname,
      date: Time.now.strftime(@date_pattern),
      partf: '%03d' % @part_number,
      uuid: SecureRandom.uuid
  }
end