Module: Datadog::Core::Environment::Container

Defined in:
lib/datadog/core/environment/container.rb

Overview

For container environments

Defined Under Namespace

Classes: Descriptor

Constant Summary collapse

UUID_PATTERN =
'[0-9a-f]{8}[-_]?[0-9a-f]{4}[-_]?[0-9a-f]{4}[-_]?[0-9a-f]{4}[-_]?[0-9a-f]{12}'.freeze
CONTAINER_PATTERN =
'[0-9a-f]{64}'.freeze
PLATFORM_REGEX =
/(?<platform>.*?)(?:.slice)?$/.freeze
POD_REGEX =
/(?<pod>(pod)?#{UUID_PATTERN})(?:.slice)?$/.freeze
CONTAINER_REGEX =
/(?<container>#{UUID_PATTERN}|#{CONTAINER_PATTERN})(?:.scope)?$/.freeze
FARGATE_14_CONTAINER_REGEX =
/(?<container>[0-9a-f]{32}-[0-9]{1,10})/.freeze

Class Method Summary collapse

Class Method Details

.container_idObject



28
29
30
# File 'lib/datadog/core/environment/container.rb', line 28

def container_id
  descriptor.container_id
end

.descriptorObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/datadog/core/environment/container.rb', line 36

def descriptor
  @descriptor ||= Descriptor.new.tap do |descriptor|
    begin
      Cgroup.descriptors.each do |cgroup_descriptor|
        # Parse container data from cgroup descriptor
        path = cgroup_descriptor.path
        next if path.nil?

        # Split path into parts
        parts = path.split('/')
        parts.shift # Remove leading empty part

        # Read info from path
        next if parts.empty?

        platform = parts[0][PLATFORM_REGEX, :platform]
        container_id, task_uid = nil

        case parts.length
        when 0..1
          next
        when 2
          container_id = parts[-1][CONTAINER_REGEX, :container] \
                        || parts[-1][FARGATE_14_CONTAINER_REGEX, :container]
        else
          if (container_id = parts[-1][CONTAINER_REGEX, :container])
            task_uid = parts[-2][POD_REGEX, :pod] || parts[1][POD_REGEX, :pod]
          else
            container_id = parts[-1][FARGATE_14_CONTAINER_REGEX, :container]
          end
        end

        # If container ID wasn't found, ignore.
        # Path might describe a non-container environment.
        next if container_id.nil?

        descriptor.platform = platform
        descriptor.container_id = container_id
        descriptor.task_uid = task_uid

        break
      end
    rescue StandardError => e
      Datadog.logger.error(
        "Error while parsing container info. Cause: #{e.class.name} #{e.message} " \
        "Location: #{Array(e.backtrace).first}"
      )
    end
  end
end

.platformObject



24
25
26
# File 'lib/datadog/core/environment/container.rb', line 24

def platform
  descriptor.platform
end

.task_uidObject



32
33
34
# File 'lib/datadog/core/environment/container.rb', line 32

def task_uid
  descriptor.task_uid
end