Class: Fluent::Plugin::ContainiqOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::Plugin::ContainiqOutput
- Defined in:
- lib/fluent/plugin/out_containiq.rb
Instance Method Summary collapse
- #compress(string) ⇒ Object
- #configure(conf) ⇒ Object
- #encode_chunk(chunk) ⇒ Object
- #format(tag, time, record) ⇒ Object
- #formatted_to_msgpack_binary? ⇒ Boolean
- #get_api_key(api_key) ⇒ Object
- #get_endpoint_url ⇒ Object
- #multi_workers_ready? ⇒ Boolean
- #send_bulk(bulk_records, bulk_size) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #write(chunk) ⇒ Object
Instance Method Details
#compress(string) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/fluent/plugin/out_containiq.rb', line 163 def compress(string) wio = StringIO.new("w") w_gz = Zlib::GzipWriter.new(wio) w_gz.write(string) w_gz.close wio.string end |
#configure(conf) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/fluent/plugin/out_containiq.rb', line 14 def configure(conf) super # ensure endpoint url is configured correctly, otherwise raise an error get_endpoint_url() get_api_key(@api_key) end |
#encode_chunk(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 105 106 |
# File 'lib/fluent/plugin/out_containiq.rb', line 71 def encode_chunk(chunk) records = [] bulk_size = 0 chunk.each { |tag, time, record| record['timestamp'] ||= Time.at(time).iso8601(3) begin json_record = Yajl.dump(record) record_size = json_record.size + (1 if !records.empty?).to_i # Accounting for trailing "\n" rescue log.error "Adding record #{record} to buffer failed. Exception: #{$!}" next end if record_size > @bulk_limit if @bulk_limit_warning_limit.is_a?(Integer) log.warn "Record with size #{record_size} exceeds #{@bulk_limit} and can't be sent to ContainIQ. Record starts with (truncated at #{@bulk_limit_warning_limit} characters): #{json_record[0,@bulk_limit_warning_limit]}" # Send the full message to debug facility log.debug "Record with size #{record_size} exceeds #{@bulk_limit} and can't be sent to ContainIQ. Record is: #{json_record}" else log.warn "Record with size #{record_size} exceeds #{@bulk_limit} and can't be sent to ContainIQ. Record is: #{json_record}" end next end if bulk_size + record_size > @bulk_limit yield(records, bulk_size) records = [] bulk_size = 0 end records.push(json_record) bulk_size += record_size } if records yield(records, bulk_size) end end |
#format(tag, time, record) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/fluent/plugin/out_containiq.rb', line 56 def format(tag, time, record) if time.is_a?(Fluent::EventTime) sec_frac = time.to_f else sec_frac = time * 1.0 end [tag, sec_frac, record].to_msgpack end |
#formatted_to_msgpack_binary? ⇒ Boolean
48 49 50 |
# File 'lib/fluent/plugin/out_containiq.rb', line 48 def formatted_to_msgpack_binary? true end |
#get_api_key(api_key) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/fluent/plugin/out_containiq.rb', line 177 def get_api_key(api_key) # if api key is null, get it from a local file called "NOTIFICATION_FILE_LOCATION" if api_key.nil? file = ENV["NOTIFICATION_FILE_LOCATION"] raise Fluent::ConfigError, 'missing environment variable: NOTIFICATION_FILE_LOCATION' if file.nil? fileFirstLine = File.open(file, &:readline) scan = fileFirstLine.gsub("\n",'').scan(/key: (.+)/i) raise Fluent::ConfigError, 'unable to parse secret key' if scan.empty? api_key = scan.first.first end return api_key end |
#get_endpoint_url ⇒ Object
171 172 173 174 175 |
# File 'lib/fluent/plugin/out_containiq.rb', line 171 def get_endpoint_url endpoint_url = ENV["INGEST_LOGS_ENDPOINT_URL"] raise 'missing environment variable: INGEST_LOGS_ENDPOINT_URL' if endpoint_url.nil? endpoint_url end |
#multi_workers_ready? ⇒ Boolean
52 53 54 |
# File 'lib/fluent/plugin/out_containiq.rb', line 52 def multi_workers_ready? true end |
#send_bulk(bulk_records, bulk_size) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 155 156 157 158 159 160 161 |
# File 'lib/fluent/plugin/out_containiq.rb', line 108 def send_bulk(bulk_records, bulk_size) log.debug "Sending a bulk of #{bulk_records.size} records, size #{bulk_size}B to ContainIQ" # Setting our request post = Net::HTTP::Post.new @uri.request_uri body = "[#{bulk_records.join(",")}]" if gzip post.body = compress(body) else post.body = body end retry_count = 4 # How many times to resend failed bulks sleep_interval = 2 # How long to sleep initially between retries begin retry_count.times do |counter| should_retry = true begin response = @http.request @uri, post if response.code != '200' if response.code == '401' log.error "You are not authorized with ContainIQ! Token OK? dropping logs..." should_retry = false elsif response.code == '400' log.info "Got 400 code from ContainIQ. This means that some of your logs are too big, or badly formatted. Response: #{response.body}" should_retry = false else log.warn "Got HTTP #{response.code} from ContainIQ, not giving up just yet (Try #{counter + 1}/#{retry_count})" end else log.debug "Successfully sent bulk of #{bulk_records.size} records, size #{bulk_size}B to ContainIQ" should_retry = false end rescue StandardError => e log.warn "Error connecting to ContainIQ. Got exception: #{e} (Try #{counter + 1}/#{retry_count})" end if should_retry if counter == retry_count - 1 log.error "Could not send your bulk after #{retry_count} tries Sorry! Your bulk is: #{post.body}" break end sleep(sleep_interval) sleep_interval *= 2 else return end end rescue Exception => e log.error "Got unexpected exception! Here: #{e}" end end |
#shutdown ⇒ Object
44 45 46 |
# File 'lib/fluent/plugin/out_containiq.rb', line 44 def shutdown super end |
#start ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fluent/plugin/out_containiq.rb', line 22 def start super endpoint_url = get_endpoint_url() @uri = URI endpoint_url log.debug "ContainIQ URL #{endpoint_url}" @http = Net::HTTP::Persistent.new name: 'fluent-plugin-containiq' api_key = get_api_key(@api_key) @http.headers['Authorization'] = "Bearer #{api_key}" @http.headers['Content-Type'] = 'text/plain' if @gzip @http.headers['Content-Encoding'] = 'gzip' end @http.idle_timeout = @http_idle_timeout @http. << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1] log.debug "Started ContainIQ shipper.." end |
#write(chunk) ⇒ Object
65 66 67 68 69 |
# File 'lib/fluent/plugin/out_containiq.rb', line 65 def write(chunk) encode_chunk(chunk) { |bulk_records, bulk_size| send_bulk(bulk_records, bulk_size) } end |