lib/fluent/plugin/out_google_cloud.rb
# Constants for service names, resource types and etc.
Constants for service names, resource types and etc.
Constants for service names resource types and etc .
def compute_timestamp(resource_type, record, time)
      current_time = Time.now
      if record.key?('timestamp') &&
         record['timestamp'].is_a?(Hash) &&
         record['timestamp'].key?('seconds') &&
         record['timestamp'].key?('nanos')
        ts_secs = record['timestamp']['seconds']
        ts_nanos = record['timestamp']['nanos']
        record.delete('timestamp')
        timestamp = time_or_nil(ts_secs, ts_nanos)
      elsif record.key?('timestampSeconds') &&
            record.key?('timestampNanos')
        ts_secs = record.delete('timestampSeconds')
        ts_nanos = record.delete('timestampNanos')
        timestamp = time_or_nil(ts_secs, ts_nanos)
      elsif record.key?('timeNanos')
        nanos = record.delete('timeNanos')
        ts_secs = (nanos / 1_000_000_000).to_i
        ts_nanos = nanos % 1_000_000_000
        unless @timenanos_warning
          @timenanos_warning = true
          @log.warn 'timeNanos is deprecated - please use ' \
            'timestampSeconds and timestampNanos instead.'
        end
        timestamp = time_or_nil(ts_secs, ts_nanos)
      elsif resource_type == CLOUDFUNCTIONS_CONSTANTS[:resource_type] &&
            @cloudfunctions_log_match
        timestamp = DateTime.parse(
          @cloudfunctions_log_match['timestamp']).to_time
        ts_secs = timestamp.tv_sec
        ts_nanos = timestamp.tv_nsec
      elsif record.key?('time')
        begin
          timestamp = Time.iso8601(record.delete('time'))
        rescue
          timestamp = Time.at(time)
        end
        ts_secs = timestamp.tv_sec
        ts_nanos = timestamp.tv_nsec
      else
        timestamp = Time.at(time)
        ts_secs = timestamp.tv_sec
        ts_nanos = timestamp.tv_nsec
      end
      ts_secs = begin
                  Integer ts_secs
                rescue ArgumentError, TypeError
                  ts_secs
                end
      ts_nanos = begin
                   Integer ts_nanos
                 rescue ArgumentError, TypeError
                   ts_nanos
                 end
      if @adjust_invalid_timestamps && timestamp
         2. The parsed timestamp is past the end of the current year:
         Since the year is different from the current year, this isn't the
         missing year in system logs. It is unlikely that users explicitly
         write logs at a future date. This could result from an unsynchronized
         clock on a VM, or some random value being parsed as the timestamp.
         We reset the timestamp on those lines to the default value and let the
         downstream API handle it.
        next_year = Time.mktime(current_time.year + 1)
        one_day_later = current_time.to_datetime.next_day.to_time
        if timestamp >= next_year  Case 2.
          ts_secs = 0
          ts_nanos = 0
        elsif timestamp >= one_day_later  Case 1.
          adjusted_timestamp = timestamp.to_datetime.prev_year.to_time
          ts_secs = adjusted_timestamp.tv_sec
           The value of ts_nanos should not change when subtracting a year.
        end
      end
      [ts_secs, ts_nanos]
    end
def compute_timestamp ( resource_type , record , time ) current_time = Time . now if record . key? ( 'timestamp' ) && record [ 'timestamp' ] . is_a? ( Hash ) && record [ 'timestamp' ] . key? ( 'seconds' ) && record [ 'timestamp' ] . key? ( 'nanos' ) ts_secs = record [ 'timestamp' ] [ 'seconds' ] ts_nanos = record [ 'timestamp' ] [ 'nanos' ] record . delete ( 'timestamp' ) timestamp = time_or_nil ( ts_secs , ts_nanos ) elsif record . key? ( 'timestampSeconds' ) && record . key? ( 'timestampNanos' ) ts_secs = record . delete ( 'timestampSeconds' ) ts_nanos = record . delete ( 'timestampNanos' ) timestamp = time_or_nil ( ts_secs , ts_nanos ) elsif record . key? ( 'timeNanos' ) nanos = record . delete ( 'timeNanos' ) ts_secs = ( nanos / 1_000_000_000 ) . to_i ts_nanos = nanos % 1_000_000_000 unless @timenanos_warning @timenanos_warning = true @log . warn 'timeNanos is deprecated - please use ' 'timestampSeconds and timestampNanos instead.' end timestamp = time_or_nil ( ts_secs , ts_nanos ) elsif resource_type == CLOUDFUNCTIONS_CONSTANTS [ :resource_type ] && @cloudfunctions_log_match timestamp = DateTime . parse ( @cloudfunctions_log_match [ 'timestamp' ] ) . to_time ts_secs = timestamp . tv_sec ts_nanos = timestamp . tv_nsec elsif record . key? ( 'time' ) begin timestamp = Time . iso8601 ( record . delete ( 'time' ) ) rescue timestamp = Time . at ( time ) end ts_secs = timestamp . tv_sec ts_nanos = timestamp . tv_nsec else timestamp = Time . at ( time ) ts_secs = timestamp . tv_sec ts_nanos = timestamp . tv_nsec end ts_secs = begin Integer ts_secs rescue ArgumentError , TypeError ts_secs end ts_nanos = begin Integer ts_nanos rescue ArgumentError , TypeError ts_nanos end if @adjust_invalid_timestamps && timestamp next_year = Time . mktime ( current_time . year + 1 ) one_day_later = current_time . to_datetime . next_day . to_time if timestamp >= next_year ts_secs = 0 ts_nanos = 0 elsif timestamp >= one_day_later adjusted_timestamp = timestamp . to_datetime . prev_year . to_time ts_secs = adjusted_timestamp . tv_sec end end [ ts_secs , ts_nanos ] end
Constants for service names, resource types and etc.
Constants for service names resource types and etc .
# This is deprecated since the precision is insufficient. # Use timestampSeconds/timestampNanos instead # Warn the user this is deprecated, but only once to avoid spam. # k8s ISO8601 timestamp # Adjust timestamps from the future. # There are two cases: # 1. The parsed timestamp is later in the current year: # This can happen when system log lines from previous years are missing # the year, so the date parser assumes the current year. # We treat these lines as coming from last year. This could label # 2-year-old logs incorrectly, but this probably isn't super important. # # 2. The parsed timestamp is past the end of the current year: # Since the year is different from the current year, this isn't the # missing year in system logs. It is unlikely that users explicitly # write logs at a future date. This could result from an unsynchronized # clock on a VM, or some random value being parsed as the timestamp. # We reset the timestamp on those lines to the default value and let the # downstream API handle it. # Case 2. # Case 1. # The value of ts_nanos should not change when subtracting a year.
resource_type
record
time