有个需求,每天创建一个索引,然后从日志文件中把一天的数据都存入索引中
要求:
每个索引存储0-24点的日志
如果fluentd宕机,重启后仍然能正常分析日志并按照日期对应的索引存储
配置
<filter test.elasticsearch.**>
@type record_transformer
enable_ruby true
<record>
# fluentd 不支持毫秒级的时间戳,因此需要将
# 毫秒级时间戳变成秒级
createTimeSecond ${record['createTime']/1000}
</record>
# remove_keys message
</filter>
<match test.elasticsearch.**>
@type elasticsearch
host 127.0.0.1
port 9200
logstash_format true
logstash_prefix logstash
logstash_dateformat %Y.%m.%d
time_key_format "%Y-%m-%dT%H:%M:%S.%N%+08:00"
time_key createTimeSecond
utc_index:false
type_name test
with_transporter_log true
@log_level info
ssl_verify false
emit_error_for_missing_id true
validate_client_version true
reconnect_on_error true
reload_on_failure true
<buffer>
@type file
path /var/log/fluentd-buffers/kubernetes.system.buffer
flush_mode interval
retry_type exponential_backoff
flush_thread_count 2
flush_interval 10s
retry_forever true
retry_max_interval 30
chunk_limit_size 32M
queue_limit_length 50
overflow_action block
</buffer>
</match>
标红的三个配置表示
time_key_format 表示time_key按照东八区时间进行转换,
time_key 表示按照要存储的日志中的createTime字段(体现为es中source中的字段)进行赋值
utc_index false 创建每天的索引的时候不按照utc的时区来,按照系统的时区进行创建索引,utc_index默认为true,如果不设置,fluentd创建索引的时候会按照0时区创建,导致每天生成索引中的数据时间间隔错8个小时,也就是每天的日志是从当天早上八点到第二天早上八点
createTimeSecond ${record['createTime']/1000 表示把业务字段中的createTIme 从毫秒转化为秒,并新增字段进行存储
配置对应源码
if @logstash_format
if record.has_key?("@timestamp")
time = Time.parse record["@timestamp"]
elsif record.has_key?(@time_key)
time = Time.parse record[@time_key]
record['@timestamp'] = record[@time_key]
else
record.merge!({"@timestamp" => Time.at(time).to_datetime.to_s})
end
if @utc_index
target_index = "#{@logstash_prefix}-#{Time.at(time).getutc.strftime("#{@logstash_dateformat}")}"
else
target_index = "#{@logstash_prefix}-#{Time.at(time).strftime("#{@logstash_dateformat}")}"
end
else
target_index = @index_name
end