Class: Fluent::Plugin::MongoStorage

Inherits:
Storage
  • Object
show all
Includes:
LoggerSupport, MongoAuth, MongoAuthParams
Defined in:
lib/fluent/plugin/storage_mongo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerSupport

#configure_logger, included

Methods included from MongoAuth

#authenticate

Methods included from MongoAuthParams

included

Constructor Details

#initializeMongoStorage

Returns a new instance of MongoStorage.



47
48
49
50
51
52
53
# File 'lib/fluent/plugin/storage_mongo.rb', line 47

def initialize
  super

  @client_options = {}
  @collection_options = {capped: false}
  @store = {}
end

Instance Attribute Details

#client_optionsObject (readonly)

Returns the value of attribute client_options.



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

def client_options
  @client_options
end

#collection_optionsObject (readonly)

Returns the value of attribute collection_options.



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

def collection_options
  @collection_options
end

#storeObject (readonly)

for test



11
12
13
# File 'lib/fluent/plugin/storage_mongo.rb', line 11

def store
  @store
end

Instance Method Details

#configure(conf) ⇒ Object



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

def configure(conf)
  super

  unless @path
    if conf && !conf.arg.empty?
      @path = conf.arg
    else
      raise Fluent::ConfigError, "path or conf.arg for <storage> is required."
    end
  end

  if conf.has_key?('capped') and Fluent::Config.bool_value(conf['capped'])
    raise Fluent::ConfigError, "'capped_size' parameter is required on <storage> of Mongo storage" unless conf.has_key?('capped_size')
    @collection_options[:capped] = true
    @collection_options[:size] = Fluent::Config.size_value(conf['capped_size'])
    @collection_options[:max] = Fluent::Config.size_value(conf['capped_max']) if conf.has_key?('capped_max')
  end

  @client_options[:write] = {j: @journaled}
  @client_options[:write].merge!({w: @write_concern}) unless @write_concern.nil?
  @client_options[:ssl] = @ssl

  if @ssl
    @client_options[:ssl_cert] = @ssl_cert
    @client_options[:ssl_key] = @ssl_key
    @client_options[:ssl_key_pass_phrase] = @ssl_key_pass_phrase
    @client_options[:ssl_verify] = @ssl_verify
    @client_options[:ssl_ca_cert] = @ssl_ca_cert
  end
  configure_logger(@mongo_log_level)

  @client = client
  @client = authenticate(@client)
end

#delete(key) ⇒ Object



132
133
134
# File 'lib/fluent/plugin/storage_mongo.rb', line 132

def delete(key)
  @store.delete(key.to_s)
end

#fetch(key, defval) ⇒ Object



124
125
126
# File 'lib/fluent/plugin/storage_mongo.rb', line 124

def fetch(key, defval)
  @store.fetch(key.to_s, defval)
end

#get(key) ⇒ Object



120
121
122
# File 'lib/fluent/plugin/storage_mongo.rb', line 120

def get(key)
  @store[key.to_s]
end

#loadObject



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

def load
  begin
    value = {}
    documents = @client[format_collection_name(@collection)].find(_id: @path)
    if documents.count >= 1
      documents.each do |document|
        value.merge!(document)
      end
    end
    value.delete('_id') # just ignore '_id'

    unless value.is_a?(Hash)
      log.error "broken content for plugin storage (Hash required: ignored)", type: json.class
      log.debug "broken content", content: json_string
      return
    end
    @store = value
  rescue => e
    log.error "failed to load data for plugin storage from mongo", path: @path, error: e
  end
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/fluent/plugin/storage_mongo.rb', line 90

def multi_workers_ready?
  true
end

#put(key, value) ⇒ Object



128
129
130
# File 'lib/fluent/plugin/storage_mongo.rb', line 128

def put(key, value)
  @store[key.to_s] = value
end

#saveObject



116
117
118
# File 'lib/fluent/plugin/storage_mongo.rb', line 116

def save
  operate(format_collection_name(@collection), @store)
end

#update(key, &block) ⇒ Object



136
137
138
# File 'lib/fluent/plugin/storage_mongo.rb', line 136

def update(key, &block)
  @store[key.to_s] = block.call(@store[key.to_s])
end