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 write_request_via_grpc(entries:,
                               log_name: '',
                               resource: nil,
                               labels: {})
      client = api_client
      entries_count = entries.length
      client.write_log_entries(
        entries,
        log_name: log_name,
        resource: if resource
                    Google::Api::MonitoredResource.new(
                      type: resource.type,
                      labels: resource.labels.to_h
                    )
                  end,
        labels: labels.map do |k, v|
          [k.encode('utf-8'), convert_to_utf8(v)]
        end.to_h,
        partial_success: @partial_success
      )
      increment_successful_requests_count
      increment_ingested_entries_count(entries_count)
      unless @successful_call
        @successful_call = true
        @log.info 'Successfully sent gRPC to Stackdriver Logging API.'
      end
    rescue Google::Gax::GaxError => gax_error
      error = gax_error.cause
      case error
      when \
          GRPC::Internal,
          GRPC::Unimplemented,
          GRPC::Unavailable,
          GRPC::DeadlineExceeded
        increment_retried_entries_count(entries_count, error.code)
        @log.debug "Retrying #{entries_count} log message(s) later.",
                   error: error.to_s, error_code: error.code.to_s
        raise error
      when \
          GRPC::Unauthenticated,
          GRPC::NotFound,
          GRPC::Aborted,
          GRPC::FailedPrecondition,
          GRPC::ResourceExhausted,
          GRPC::Cancelled,
          GRPC::Unknown
        increment_failed_requests_count(error.code)
        increment_dropped_entries_count(entries_count, error.code)
        @log.warn "Dropping #{entries_count} log message(s)",
                  error: error.to_s, error_code: error.code.to_s
      when \
          GRPC::InvalidArgument,
          GRPC::PermissionDenied
        error_details_map = construct_error_details_map_grpc(gax_error)
        if error_details_map.empty?
          increment_failed_requests_count(error.code)
          increment_dropped_entries_count(entries_count, error.code)
          @log.warn "Dropping #{entries_count} log message(s)",
                    error: error.to_s, error_code: error.code.to_s
        else
          error_details_map.each do |(error_code, error_message), indexes|
            partial_errors_count = indexes.length
            increment_dropped_entries_count(partial_errors_count,
                                            error_code)
            entries_count -= partial_errors_count
            @log.warn "Dropping #{partial_errors_count} log message(s)",
                      error: error_message, error_code: error_code.to_s
          end
          increment_successful_requests_count
          increment_ingested_entries_count(entries_count)
        end
      else
        error_code = if error.respond_to?(:code)
                       error.code
                     else
                       GRPC::Core::StatusCodes::UNKNOWN
                     end
        increment_failed_requests_count(error_code)
        increment_dropped_entries_count(entries_count, error_code)
        @log.error "Unknown response code #{error_code} from the server," \
                   " dropping #{entries_count} log message(s)",
                   error: error.to_s, error_code: error_code.to_s
      end
    rescue StandardError => error
      increment_failed_requests_count(GRPC::Core::StatusCodes::UNKNOWN)
      increment_dropped_entries_count(entries_count,
                                      GRPC::Core::StatusCodes::UNKNOWN)
      @log.error "Unexpected error type #{error.class.name} from the client" \
                 " library, dropping #{entries_count} log message(s)",
                 error: error.to_s
    end
def write_request_via_grpc ( entries : , log_name : '' , resource : nil , labels : { } ) client = api_client entries_count = entries . length client . write_log_entries ( entries , log_name : log_name , resource : if resource Google :: Api :: MonitoredResource . new ( type : resource . type , labels : resource . labels . to_h ) end , labels : labels . map do | k , v | [ k . encode ( 'utf-8' ) , convert_to_utf8 ( v ) ] end . to_h , partial_success : @partial_success ) increment_successful_requests_count increment_ingested_entries_count ( entries_count ) unless @successful_call @successful_call = true @log . info 'Successfully sent gRPC to Stackdriver Logging API.' end rescue Google :: Gax :: GaxError => gax_error error = gax_error . cause case error when GRPC :: Internal , GRPC :: Unimplemented , GRPC :: Unavailable , GRPC :: DeadlineExceeded increment_retried_entries_count ( entries_count , error . code ) @log . debug "Retrying #{entries_count} log message(s) later." , error : error . to_s , error_code : error . code . to_s raise error when GRPC :: Unauthenticated , GRPC :: NotFound , GRPC :: Aborted , GRPC :: FailedPrecondition , GRPC :: ResourceExhausted , GRPC :: Cancelled , GRPC :: Unknown increment_failed_requests_count ( error . code ) increment_dropped_entries_count ( entries_count , error . code ) @log . warn "Dropping #{entries_count} log message(s)" , error : error . to_s , error_code : error . code . to_s when GRPC :: InvalidArgument , GRPC :: PermissionDenied error_details_map = construct_error_details_map_grpc ( gax_error ) if error_details_map . empty? increment_failed_requests_count ( error . code ) increment_dropped_entries_count ( entries_count , error . code ) @log . warn "Dropping #{entries_count} log message(s)" , error : error . to_s , error_code : error . code . to_s else error_details_map . each do | ( error_code , error_message ) , indexes | partial_errors_count = indexes . length increment_dropped_entries_count ( partial_errors_count , error_code ) entries_count -= partial_errors_count @log . warn "Dropping #{partial_errors_count} log message(s)" , error : error_message , error_code : error_code . to_s end increment_successful_requests_count increment_ingested_entries_count ( entries_count ) end else error_code = if error . respond_to? ( :code ) error . code else GRPC :: Core :: StatusCodes :: UNKNOWN end increment_failed_requests_count ( error_code ) increment_dropped_entries_count ( entries_count , error_code ) @log . error "Unknown response code #{error_code} from the server," " dropping #{entries_count} log message(s)" , error : error . to_s , error_code : error_code . to_s end rescue StandardError => error increment_failed_requests_count ( GRPC :: Core :: StatusCodes :: UNKNOWN ) increment_dropped_entries_count ( entries_count , GRPC :: Core :: StatusCodes :: UNKNOWN ) @log . error "Unexpected error type #{error.class.name} from the client" " library, dropping #{entries_count} log message(s)" , error : error . to_s end
Constants for service names, resource types and etc.
Constants for service names resource types and etc .
# Leave resource nil if it's nil. # Let the user explicitly know when the first call succeeded, to # aid with verification and troubleshooting. # GRPC::BadStatus is wrapped in error.cause. # See the mapping between HTTP status and gRPC status code at: # https://github.com/grpc/grpc/blob/master/src/core/lib/transport/status_conversion.cc # Server error, so retry via re-raising the error. # HTTP status 500 (Internal Server Error). # HTTP status 501 (Not Implemented). # HTTP status 503 (Service Unavailable). # HTTP status 504 (Gateway Timeout). # Most client errors indicate a problem with the request itself and # should not be retried. # HTTP status 401 (Unauthorized). # These are usually solved via a `gcloud auth` call, or by modifying # the permissions on the Google Cloud project. # HTTP status 404 (Not Found). # HTTP status 409 (Conflict). # HTTP status 412 (Precondition Failed). # HTTP status 429 (Too Many Requests). # HTTP status 499 (Client Closed Request). # the remaining http codes in both 4xx and 5xx category. # It's debatable whether to retry or drop these log entries. # This decision is made to avoid retrying forever due to # client errors. # If partial_success is enabled, valid entries should have be # written even if some other entries fail due to InvalidArgument or # PermissionDenied errors. Only invalid entries will be dropped. # HTTP status 400 (Bad Request). # HTTP status 403 (Forbidden). # Consider partially successful requests successful. # Assume it's a problem with the request itself and don't retry. # Got an unexpected error (not Google::Gax::GaxError) from the # google-cloud-logging lib.
entries
log_name
resource
labels