Class: Fluent::CloudSearchOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_cloudsearch.rb

Constant Summary collapse

MAX_SIZE_LIMIT =

message packをJSONにした時に5MBを超えないように

4.5 * 1024 * 1024
INVALID_CHAR_REGEX =
/[^\u0009\u000a\u000d\u0020-\uD7FF\uE000-\uFFFD]/

Instance Method Summary collapse

Constructor Details

#initializeCloudSearchOutput

Returns a new instance of CloudSearchOutput.



21
22
23
24
25
26
# File 'lib/fluent/plugin/out_cloudsearch.rb', line 21

def initialize
  super

  require 'aws-sdk'
  require 'json'
end

Instance Method Details

#configure(conf) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fluent/plugin/out_cloudsearch.rb', line 28

def configure(conf)

  # override config. (config_set_default can't override it)
  conf['buffer_chunk_limit'] ||= MAX_SIZE_LIMIT

  super

  unless @endpoint
    raise ConfigError, "'endpoint' parameter is required"
  end
  if @buffer.buffer_chunk_limit > MAX_SIZE_LIMIT
    raise ConfigError, "buffer_chunk_limit must be less than #{MAX_SIZE_LIMIT}"
  end
end

#format(tag, time, record) ⇒ Object



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

def format(tag, time, record)
  if !record.key?('id') then
    log.warn "id is required #{record.to_s}"
    return ''
  elsif !record.key?('type') then
    log.warn "type is required #{record.to_s}"
    return ''
  elsif record['type'] == 'add' then
    if !record.key?('fields') then
        log.warn "fields is required when type is add. #{record.to_s}"
        return ''
    end
  elsif record['type'] != 'delete' then
    log.warn "type is add or delete #{record.to_s}"
    return ''
  end

  r = record.dup
  f = r['fields']
  if f.kind_of? Hash
    # replace invalid char to white space
    f.each do |key, value|
      if value.kind_of? String
        f[key] = value.gsub(INVALID_CHAR_REGEX, ' ')
      else
        f[key] = value
      end
    end
  end

  return "#{r.to_json},"
end

#setup_credentialsObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/plugin/out_cloudsearch.rb', line 100

def setup_credentials
  options = {}
  if @access_key_id && @secret_access_key
    options[:credentials] = Aws::Credentials.new(@access_key_id, @secret_access_key)
  elsif @profile_name
    options[:credentials] = Aws::SharedCredentials.new(
      :profile_name => @profile_name
    )
  end
  options
end

#shutdownObject



51
52
53
54
# File 'lib/fluent/plugin/out_cloudsearch.rb', line 51

def shutdown
  super

end

#startObject



43
44
45
46
47
48
49
# File 'lib/fluent/plugin/out_cloudsearch.rb', line 43

def start
  super
  options = setup_credentials
  options[:endpoint] = @endpoint
  options[:region] = @region if @region
  @client = Aws::CloudSearchDomain::Client.new(options)
end

#write(chunk) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/fluent/plugin/out_cloudsearch.rb', line 89

def write(chunk)
  documents = '['
  documents << chunk.read.chop  # chop last ','
  documents << ']'
  resp = @client.upload_documents(
    :documents => documents,
    :content_type => "application/json"
  )
end