Class: Kuroko2::ExecutionLogger::CloudWatchLogs
- Inherits:
-
Object
- Object
- Kuroko2::ExecutionLogger::CloudWatchLogs
- Defined in:
- lib/autoload/kuroko2/execution_logger/cloud_watch_logs.rb
Constant Summary collapse
- MAX_RETRY_COUNT =
5
- RETRY_ERRORS =
[ Aws::CloudWatchLogs::Errors::InvalidSequenceTokenException, Aws::CloudWatchLogs::Errors::ThrottlingException, Aws::CloudWatchLogs::Errors::ResourceNotFoundException, ]
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #get_logs(token = @get_log_token) ⇒ Object
-
#initialize(stream_name:, group_name:, region: 'ap-northeast-1') ⇒ CloudWatchLogs
constructor
A new instance of CloudWatchLogs.
- #put_logs(events) ⇒ Object
- #send_log(message) ⇒ Object
Constructor Details
#initialize(stream_name:, group_name:, region: 'ap-northeast-1') ⇒ CloudWatchLogs
Returns a new instance of CloudWatchLogs.
12 13 14 15 16 17 18 19 |
# File 'lib/autoload/kuroko2/execution_logger/cloud_watch_logs.rb', line 12 def initialize(stream_name:, group_name:, region: 'ap-northeast-1') @client = Aws::CloudWatchLogs::Client.new(region: region) @group_name = group_name @stream_name = stream_name @put_log_token = nil @get_log_token = nil end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/autoload/kuroko2/execution_logger/cloud_watch_logs.rb', line 10 def client @client end |
Instance Method Details
#get_logs(token = @get_log_token) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/autoload/kuroko2/execution_logger/cloud_watch_logs.rb', line 65 def get_logs(token = @get_log_token) response = client.get_log_events({ log_group_name: @group_name, log_stream_name: @stream_name, next_token: token, start_from_head: true, }) @get_log_token = response.next_forward_token response rescue Aws::CloudWatchLogs::Errors::ResourceNotFoundException raise ExecutionLogger::NotFound end |
#put_logs(events) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/autoload/kuroko2/execution_logger/cloud_watch_logs.rb', line 25 def put_logs(events) exception_cb = lambda do |exception| Kuroko2.logger.warn("#{exception.class} #{exception.} #{events}") case exception when Aws::CloudWatchLogs::Errors::InvalidSequenceTokenException old_token = @put_log_token new_token = exception..match(%r{\AThe given sequenceToken is invalid. The next expected sequenceToken is:\s*(\w+)\z})[1] if new_token @put_log_token = new_token Kuroko2.logger.warn("Refreshed sequenceToken from '#{old_token}' to '#{@put_log_token}'") end when Aws::CloudWatchLogs::Errors::ResourceNotFoundException create_log_stream when Aws::CloudWatchLogs::Errors::ThrottlingException sleep(0.5) end end = { exception_cb: exception_cb, on: RETRY_ERRORS, tries: MAX_RETRY_COUNT, sleep: 0, } Retryable.retryable() do response = client.put_log_events( log_group_name: @group_name, log_stream_name: @stream_name, log_events: events, sequence_token: @put_log_token, ) @put_log_token = response.data[:next_sequence_token] Kuroko2.logger.debug("Put logs: #{@group_name} #{@stream_name} / #{response.data}") response end end |
#send_log(message) ⇒ Object
21 22 23 |
# File 'lib/autoload/kuroko2/execution_logger/cloud_watch_logs.rb', line 21 def send_log() put_logs([{ timestamp: , message: .to_json }]) end |