lib/fluent/plugin/out_google_cloud.rb
# Take a locally unique resource id and convert it to the globally unique
# monitored resource.
Take a locally unique resource id and convert it to the globally unique
monitored resource.
Take a locally unique resource id and convert it to the globally unique monitored resource .
def monitored_resource_from_local_resource_id(local_resource_id)
      return unless
        /^
          (?<resource_type>k8s_container)
          \.(?<namespace_name>[0-9a-z-]+)
          \.(?<pod_name>[.0-9a-z-]+)
          \.(?<container_name>[0-9a-z-]+)$/x =~ local_resource_id ||
        /^
          (?<resource_type>k8s_pod)
          \.(?<namespace_name>[0-9a-z-]+)
          \.(?<pod_name>[.0-9a-z-]+)$/x =~ local_resource_id ||
        /^
          (?<resource_type>k8s_node)
          \.(?<node_name>[0-9a-z-]+)$/x =~ local_resource_id
      @k8s_cluster_name = nil if @k8s_cluster_name == ''
      @k8s_cluster_location = nil if @k8s_cluster_location == ''
      begin
        @k8s_cluster_name ||= @utils.fetch_gce_metadata(
          @platform, 'instance/attributes/cluster-name')
        @k8s_cluster_location ||= @utils.fetch_gce_metadata(
          @platform, 'instance/attributes/cluster-location')
      rescue StandardError => e
        @log.error 'Failed to retrieve k8s cluster name and location.', \
                   error: e
      end
      case resource_type
      when K8S_CONTAINER_CONSTANTS[:resource_type]
        labels = {
          'namespace_name' => namespace_name,
          'pod_name' => pod_name,
          'container_name' => container_name,
          'cluster_name' => @k8s_cluster_name,
          'location' => @k8s_cluster_location
        }
        fallback_resource = GKE_CONSTANTS[:resource_type]
      when K8S_POD_CONSTANTS[:resource_type]
        labels = {
          'namespace_name' => namespace_name,
          'pod_name' => pod_name,
          'cluster_name' => @k8s_cluster_name,
          'location' => @k8s_cluster_location
        }
        fallback_resource = GKE_CONSTANTS[:resource_type]
      when K8S_NODE_CONSTANTS[:resource_type]
        labels = {
          'node_name' => node_name,
          'cluster_name' => @k8s_cluster_name,
          'location' => @k8s_cluster_location
        }
        fallback_resource = COMPUTE_CONSTANTS[:resource_type]
      end
      unless @k8s_cluster_name && @k8s_cluster_location
        @log.error "Failed to construct #{resource_type} resource locally." \
                   ' Falling back to writing logs against' \
                   " #{fallback_resource} resource.", error: e
        return
      end
      constructed_resource = Google::Apis::LoggingV2::MonitoredResource.new(
        type: resource_type,
        labels: labels)
      @log.debug("Constructed #{resource_type} resource locally: " \
                 "#{constructed_resource.inspect}")
      constructed_resource
    end
def monitored_resource_from_local_resource_id ( local_resource_id ) return unless / ^
          (?<resource_type>k8s_container)
           \. (?<namespace_name>[0-9a-z-]+)
           \. (?<pod_name>[.0-9a-z-]+)
           \. (?<container_name>[0-9a-z-]+)$ /x =~ local_resource_id || / ^
          (?<resource_type>k8s_pod)
           \. (?<namespace_name>[0-9a-z-]+)
           \. (?<pod_name>[.0-9a-z-]+)$ /x =~ local_resource_id || / ^
          (?<resource_type>k8s_node)
           \. (?<node_name>[0-9a-z-]+)$ /x =~ local_resource_id @k8s_cluster_name = nil if @k8s_cluster_name == '' @k8s_cluster_location = nil if @k8s_cluster_location == '' begin @k8s_cluster_name ||= @utils . fetch_gce_metadata ( @platform , 'instance/attributes/cluster-name' ) @k8s_cluster_location ||= @utils . fetch_gce_metadata ( @platform , 'instance/attributes/cluster-location' ) rescue StandardError => e @log . error 'Failed to retrieve k8s cluster name and location.' , error : e end case resource_type when K8S_CONTAINER_CONSTANTS [ :resource_type ] labels = { 'namespace_name' => namespace_name , 'pod_name' => pod_name , 'container_name' => container_name , 'cluster_name' => @k8s_cluster_name , 'location' => @k8s_cluster_location } fallback_resource = GKE_CONSTANTS [ :resource_type ] when K8S_POD_CONSTANTS [ :resource_type ] labels = { 'namespace_name' => namespace_name , 'pod_name' => pod_name , 'cluster_name' => @k8s_cluster_name , 'location' => @k8s_cluster_location } fallback_resource = GKE_CONSTANTS [ :resource_type ] when K8S_NODE_CONSTANTS [ :resource_type ] labels = { 'node_name' => node_name , 'cluster_name' => @k8s_cluster_name , 'location' => @k8s_cluster_location } fallback_resource = COMPUTE_CONSTANTS [ :resource_type ] end unless @k8s_cluster_name && @k8s_cluster_location @log . error "Failed to construct #{resource_type} resource locally." ' Falling back to writing logs against' " #{fallback_resource} resource." , error : e return end constructed_resource = Google :: Apis :: LoggingV2 :: MonitoredResource . new ( type : resource_type , labels : labels ) @log . debug ( "Constructed #{resource_type} resource locally: " "#{constructed_resource.inspect}" ) constructed_resource end
Take a locally unique resource id and convert it to the globally unique
monitored resource.
Take a locally unique resource id and convert it to the globally unique monitored resource .
# Clear name and location if they're explicitly set to empty.
local_resource_id
<not_specific>