Class: Fluent::SwiftOutput

Inherits:
TimeSlicedOutput
  • Object
show all
Includes:
Mixin::ConfigPlaceholders, SetTagKeyMixin, SetTimeKeyMixin
Defined in:
lib/fluent/plugin/out_swift.rb

Instance Method Summary collapse

Constructor Details

#initializeSwiftOutput

Returns a new instance of SwiftOutput.



8
9
10
11
12
13
14
15
# File 'lib/fluent/plugin/out_swift.rb', line 8

def initialize
  super
  require 'fog/openstack'
  require 'zlib'
  require 'time'
  require 'tempfile'
  require 'open3'
end

Instance Method Details

#configure(conf) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/out_swift.rb', line 48

def configure(conf)
  super

  if format_json = conf['format_json']
    @format_json = true
  else
    @format_json = false
  end

  @ext, @mime_type = case @store_as
    when 'gzip' then ['gz', 'application/x-gzip']
    when 'lzo' then
      begin
        Open3.capture3('lzop -V')
      rescue Errno::ENOENT
        raise ConfigError, "'lzop' utility must be in PATH for LZO compression"
      end
      ['lzo', 'application/x-lzop']
    when 'json' then ['json', 'application/json']
    else ['txt', 'text/plain']
  end

  @timef = TimeFormatter.new(@time_format, @localtime)

  if @localtime
    @path_slicer = Proc.new {|path|
      Time.now.strftime(path)
    }
  else
    @path_slicer = Proc.new {|path|
      Time.now.utc.strftime(path)
    }
  end
end

#format(tag, time, record) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fluent/plugin/out_swift.rb', line 98

def format(tag, time, record)
  if @include_time_key || !@format_json
    time_str = @timef.format(time)
  end

  # copied from each mixin because current TimeSlicedOutput can't support mixins.
  if @include_tag_key
    record[@tag_key] = tag
  end
  if @include_time_key
    record[@time_key] = time_str
  end

  if @format_json
    Yajl.dump(record) + "\n"
  else
    "#{time_str}\t#{tag}\t#{Yajl.dump(record)}\n"
  end
end

#placeholdersObject



44
45
46
# File 'lib/fluent/plugin/out_swift.rb', line 44

def placeholders
  [:percent]
end

#startObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fluent/plugin/out_swift.rb', line 83

def start
  super

  Excon.defaults[:ssl_verify_peer] = @ssl_verify

  @storage = Fog::Storage::OpenStack.new :openstack_auth_url => @auth_url,
                    :openstack_username => @auth_user,
                    :openstack_tenant => @auth_tenant,
                    :openstack_api_key => @auth_api_key,
                    :openstack_region => @auth_region
  @storage. @swift_account if @swift_account

  check_container
end

#write(chunk) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fluent/plugin/out_swift.rb', line 118

def write(chunk)
  i = 0

  begin
    path = @path_slicer.call(@path)
    values_for_swift_object_key = {
      "path" => path,
      "time_slice" => chunk.key,
      "file_extension" => @ext,
      "index" => i
    }
    swift_path = @swift_object_key_format.gsub(%r(%{[^}]+})) { |expr|
      values_for_swift_object_key[expr[2...expr.size-1]]
    }
    i += 1
  end while check_object_exists(@swift_container, swift_path)

  tmp = Tempfile.new("swift-")
  begin
    if @store_as == "gzip"
      w = Zlib::GzipWriter.new(tmp)
      chunk.write_to(w)
      w.close
    elsif @store_as == "lzo"
      w = Tempfile.new("chunk-tmp")
      chunk.write_to(w)
      w.close
      tmp.close
      # We don't check the return code because we can't recover lzop failure.
      system "lzop -qf1 -o #{tmp.path} #{w.path}"
    else
      chunk.write_to(tmp)
      tmp.close
    end
    File.open(tmp.path) do |file|
      @storage.put_object(@swift_container, swift_path, file, {:content_type => @mime_type})
    end
    $log.info "Put Log to Swift. container=#{@swift_container} object=#{swift_path}"
  ensure
    tmp.close(true) rescue nil
    w.close rescue nil
    w.unlink rescue nil
  end
end