diff --git a/lib/fluent/plugin/out_google_cloud.rb b/lib/fluent/plugin/out_google_cloud.rb index fefaa92d..5a713fdd 100644 --- a/lib/fluent/plugin/out_google_cloud.rb +++ b/lib/fluent/plugin/out_google_cloud.rb @@ -167,7 +167,21 @@ def write(chunk) 'entries' => [], } arr.each do |time, record| - if (record.has_key?('timeNanos')) + if (record.has_key?('timestamp') && + record['timestamp'].has_key?('seconds') && + record['timestamp'].has_key?('nanos')) + ts_secs = record['timestamp']['seconds'] + ts_nanos = record['timestamp']['nanos'] + record.delete('timestamp') + elsif (record.has_key?('timestampSeconds') && + record.has_key?('timestampNanos')) + ts_secs = record['timestampSeconds'] + ts_nanos = record['timestampNanos'] + record.delete('timestampSeconds') + record.delete('timestampNanos') + elsif (record.has_key?('timeNanos')) + # This is deprecated since the precision is insufficient. + # Use timestampSeconds/timestampNanos instead ts_secs = (record['timeNanos'] / 1000000000).to_i ts_nanos = record['timeNanos'] % 1000000000 record.delete('timeNanos') diff --git a/test/plugin/test_out_google_cloud.rb b/test/plugin/test_out_google_cloud.rb index 67d2c9c5..8f322cdf 100644 --- a/test/plugin/test_out_google_cloud.rb +++ b/test/plugin/test_out_google_cloud.rb @@ -270,7 +270,7 @@ def test_timestamps expected_ts = [] emit_index = 0 [Time.at(123456.789), Time.at(0), Time.now].each do |ts| - # Test both the "native" fluentd timestamp and timeNanos. + # Test the "native" fluentd timestamp as well as our nanosecond tags. d.emit({'message' => log_entry(emit_index)}, ts.to_f) # The native timestamp currently only supports second granularity # (fluentd issue #461), so strip nanoseconds from the expected value. @@ -280,6 +280,14 @@ def test_timestamps 'timeNanos' => ts.tv_sec * 1000000000 + ts.tv_nsec}) expected_ts.push(ts) emit_index += 1 + d.emit({'message' => log_entry(emit_index), + 'timestamp' => {'seconds' => ts.tv_sec, 'nanos' => ts.tv_nsec}}) + expected_ts.push(ts) + emit_index += 1 + d.emit({'message' => log_entry(emit_index), + 'timestampSeconds' => ts.tv_sec, 'timestampNanos' => ts.tv_nsec}) + expected_ts.push(ts) + emit_index += 1 end d.run verify_index = 0