Sha256: 565eda69f27b1f1ef90472823fd5e6cc0cfbe9a5eca45a04e58cf5fd9cddd507

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

# typed: true
require 'datadog/core/environment/ext'

module Datadog
  module Core
    module Environment
      # Reads information from Linux cgroups.
      # This information is used to extract information
      # about the current Linux container identity.
      # @see https://man7.org/linux/man-pages/man7/cgroups.7.html
      module Cgroup
        include Kernel # Ensure that kernel methods are always available (https://sorbet.org/docs/error-reference#7003)

        LINE_REGEX = /^(\d+):([^:]*):(.+)$/.freeze

        Descriptor = Struct.new(
          :id,
          :groups,
          :path,
          :controllers
        )

        module_function

        def descriptors(process = 'self')
          [].tap do |descriptors|
            begin
              filepath = "/proc/#{process}/cgroup"

              if File.exist?(filepath)
                File.foreach("/proc/#{process}/cgroup") do |line|
                  line = line.strip
                  descriptors << parse(line) unless line.empty?
                end
              end
            rescue StandardError => e
              Datadog.logger.error("Error while parsing cgroup. Cause: #{e.message} Location: #{Array(e.backtrace).first}")
            end
          end
        end

        def parse(line)
          id, groups, path = line.scan(LINE_REGEX).first

          Descriptor.new(id, groups, path).tap do |descriptor|
            descriptor.controllers = groups.split(',') unless groups.nil?
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ddtrace-1.0.0.beta1 lib/datadog/core/environment/cgroup.rb
ddtrace-0.54.2 lib/datadog/core/environment/cgroup.rb
ddtrace-0.54.1 lib/datadog/core/environment/cgroup.rb
ddtrace-0.54.0 lib/datadog/core/environment/cgroup.rb
ddtrace-0.53.0 lib/datadog/core/environment/cgroup.rb
ddtrace-0.52.0 lib/datadog/core/environment/cgroup.rb