Class: Fluent::DagOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDagOutput

Returns a new instance of DagOutput.



12
13
14
15
16
17
18
19
20
21
# File 'lib/fluent/plugin/out_dag.rb', line 12

def initialize
  super
  require 'aws-sdk-v1'
  require 'zlib'
  require 'time'
  require 'tempfile'
  require 'open3'

  @use_ssl = true
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



41
42
43
# File 'lib/fluent/plugin/out_dag.rb', line 41

def bucket
  @bucket
end

Instance Method Details

#configure(conf) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fluent/plugin/out_dag.rb', line 49

def configure(conf)
  super

  unless conf['flush_interval'].nil?
    unless valid_flush_interval?(conf['flush_interval'])
      raise "flush_interval is invalid. Please check your configuration"
    end
  end

  unless valid_dag_database?(conf['dag_database'])
    raise "dag_database is invalid. Please check your configuration"
  end

  unless valid_dag_table?(conf['dag_table'])
    raise "dag_table is invalid. Please check your configuration"
  end

  @path = "#{@dag_table}/date=%Y%m%d/hour=%-H/dag_%-M"
  @dag_object_key_format = "%{path}_%{index}.%{file_extension}"

  if use_ssl = conf['use_ssl']
    if use_ssl.empty?
      @use_ssl = true
    else
      @use_ssl = Config.bool_value(use_ssl)
      if @use_ssl.nil?
        raise ConfigError, "'true' or 'false' is required for use_ssl option on dag output"
      end
    end
  end

  @ext, @mime_type = ['gz', 'application/x-gzip']
  @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



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fluent/plugin/out_dag.rb', line 114

def format(tag, time, record)
  # copied from each mixin because current TimeSlicedOutput can't support mixins.
  if @include_tag_key
    record[@tag_key] = tag
  end

  dag_record = {}
  dag_record[:time] = time
  dag_record[:v] = record
  Yajl.dump(dag_record) + "\n"
end

#placeholdersObject



45
46
47
# File 'lib/fluent/plugin/out_dag.rb', line 45

def placeholders
  [:percent]
end

#startObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fluent/plugin/out_dag.rb', line 95

def start
  super
  options = {}
  if @dag_key_id && @dag_sec_key
    options[:access_key_id] = @dag_key_id
    options[:secret_access_key] = @dag_sec_key
  end
  options[:s3_endpoint] = @dag_endpoint
  options[:proxy_uri] = @proxy_uri if @proxy_uri
  options[:use_ssl] = @use_ssl
  options[:s3_force_path_style] = @dag_force_path_style

  @dag = AWS::S3.new(options)
  @bucket = @dag.buckets[@dag_database]

  ensure_bucket
  check_apikeys if @check_apikey_on_start
end

#write(chunk) ⇒ Object



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
# File 'lib/fluent/plugin/out_dag.rb', line 126

def write(chunk)
  i = 0

  begin
    path = @path_slicer.call(@path)
    values_for_dag_object_key = {
      "path" => path,
      "time_slice" => chunk.key,
      "file_extension" => @ext,
      "index" => i
    }
    dag_path = @dag_object_key_format.gsub(%r(%{[^}]+})) { |expr|
      values_for_dag_object_key[expr[2...expr.size-1]]
    }
    i += 1
  end while @bucket.objects[dag_path].exists?

  tmp = Tempfile.new("dag-")
  begin
    w = Zlib::GzipWriter.new(tmp)
    chunk.write_to(w)
    w.close
    @bucket.objects[dag_path].write(Pathname.new(tmp.path), {:content_type => @mime_type})
  ensure
    tmp.close(true) rescue nil
    w.close rescue nil
    w.unlink rescue nil
  end
end