Class: Fluent::AliyunSlsOutput

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

Instance Method Summary collapse

Constructor Details

#initializeAliyunSlsOutput

Returns a new instance of AliyunSlsOutput.



17
18
19
20
21
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 17

def initialize
    super
    require "aliyun_sls_sdk/protobuf"
    require "aliyun_sls_sdk"
end

Instance Method Details

#clientObject



40
41
42
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 40

def client
    @_sls_con ||= AliyunSlsSdk::LogClient.new(@region_endpoint, @access_key_id, @access_key_secret, @ssl_verify)
end

#configure(conf) ⇒ Object



23
24
25
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 23

def configure(conf)
    super
end

#createLogStore(logstore_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 44

def createLogStore(logstore_name)
    retries = 2
    begin
        createLogStoreResp = client.create_logstore(@project, logstore_name, @create_logstore_ttl, @create_logstore_shard_count)
    rescue AliyunSlsSdk::LogException => e
        if e.errorCode == "LogStoreAlreadyExist"
            log.warn "logstore #{logstore_name} already exist"
        else
            raise
        end
    rescue => e
        if retries > 0
            log.warn "Error caught when creating logs store: #{e}"
            retries -= 1
            retry
        end
    end
end

#format(tag, time, record) ⇒ Object



36
37
38
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 36

def format(tag, time, record)
    [tag, time, record].to_msgpack
end

#getLogItem(record) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 63

def getLogItem(record)
    contents = {}
    record.each { |k, v|
        contents[k] = v
    }
    AliyunSlsSdk::LogItem.new(nil, contents)
end

#shutdownObject



32
33
34
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 32

def shutdown
    super
end

#startObject



28
29
30
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 28

def start
    super
end

#write(chunk) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fluent/plugin/out_aliyun_sls.rb', line 71

def write(chunk)
    log_itemlist = []
    chunk.each do |tag, time, record|
        log_itemlist << getLogItem(record)
    end

    log_itemlist.each_slice(4096) do |items|
        putLogRequest = AliyunSlsSdk::PutLogsRequest.new(@project, @logstore, nil, nil, items, nil, true)
        retries = 0
        begin
            client.put_logs(putLogRequest)
        rescue  => e
            if e.instance_of?(AliyunSlsSdk::LogException) && e.errorCode == "LogStoreNotExist" && @need_create_logstore
                createLogStore(@logstore)
                # wait up to 60 seconds to create the logstore
                if retries < 3
                    retries += 1
                    sleep(10 * retries)
                    retry
                end
            else
                log.warn "\tCaught in puts logs: #{e.message}"
                if retries < 3
                    client.http.shutdown
                    @_sls_con = nil
                    retries += 1
                    sleep(1 * retries)
                    retry
                end
                log.error "Could not puts logs to aliyun sls: #{e.message}"
            end
        end
    end
end