-
Notifications
You must be signed in to change notification settings - Fork 140
Add origin detection fields #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
b09df3b
Add origin detection
StephenWakely cf25b16
Add external env
StephenWakely 7dee16a
Add tag cardinality option
StephenWakely c28b06a
Serialize fields
StephenWakely 4df07b8
Update event serializer params
StephenWakely ff41013
Merge remote-tracking branch 'origin/master' into stephen/origin-dete…
StephenWakely 5b48dd2
Add fields to service check
StephenWakely abb3b79
Use older fakefs version to test against Ruby v2
StephenWakely a50fafb
Ruby 2.1 compatible multiline strings
StephenWakely 3e0ad4f
Only use old fakefs on old versions
StephenWakely deee154
Missed multiline
StephenWakely 84f505a
Further fixes for windows
StephenWakely 9c182b2
Fix content logic
StephenWakely 6111742
Fudge allocation test
StephenWakely 6282d13
Increase timing for macos
StephenWakely 280b919
Try avoiding allocation
StephenWakely 571458d
Trace the allocations
StephenWakely b7ed78a
Trace initialization
StephenWakely afb698b
Return block result
StephenWakely 035cb88
Try warming up
StephenWakely 4289183
Origin detection doesn't need to be a class
StephenWakely e7d35dd
Remove field serializer from event
StephenWakely 922cd2e
Remove windows specific
StephenWakely 7e1bb67
Return origin detection
StephenWakely dd1eb6a
Remove all linux specific alloction test
StephenWakely 76486f8
Attempt to bound regex
StephenWakely 166fd8b
Take substring of path.
StephenWakely cd89b8c
Replace regex with parser
StephenWakely 556bfe1
Ruby <= 2.3 compliance
StephenWakely fcbd7f7
Remove trace allocations
StephenWakely 29f4f08
Only turn origin detection off for linux
StephenWakely a4bd599
Make origin detection methods private
StephenWakely 1c58afa
Remove unused instance vars
StephenWakely ecbbf3d
Add origin fields to telemetry metrics
StephenWakely 373be59
Add delay serializer tests
StephenWakely cea7499
Fix field order
StephenWakely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # frozen_string_literal: true | ||
| module Datadog | ||
| class Statsd | ||
| private | ||
|
|
||
| CGROUPV1BASECONTROLLER = "memory" | ||
| HOSTCGROUPNAMESPACEINODE = 0xEFFFFFFB | ||
|
|
||
| def host_cgroup_namespace? | ||
| stat = File.stat("/proc/self/ns/cgroup") rescue nil | ||
| return false unless stat | ||
| stat.ino == HOSTCGROUPNAMESPACEINODE | ||
| end | ||
|
|
||
| def parse_cgroup_node_path(lines) | ||
| res = {} | ||
| lines.split("\n").each do |line| | ||
| tokens = line.split(':') | ||
| next unless tokens.length == 3 | ||
|
|
||
| controller = tokens[1] | ||
| path = tokens[2] | ||
|
|
||
| if controller == CGROUPV1BASECONTROLLER || controller == '' | ||
| res[controller] = path | ||
| end | ||
| end | ||
|
|
||
| res | ||
| end | ||
|
|
||
| def get_cgroup_inode(cgroup_mount_path, proc_self_cgroup_path) | ||
| content = File.read(proc_self_cgroup_path) rescue nil | ||
| return nil unless content | ||
|
|
||
| controllers = parse_cgroup_node_path(content) | ||
|
|
||
| [CGROUPV1BASECONTROLLER, ''].each do |controller| | ||
| next unless controllers[controller] | ||
|
|
||
| segments = [ | ||
| cgroup_mount_path.chomp('/'), | ||
| controller.strip, | ||
| controllers[controller].sub(/^\//, '') | ||
| ] | ||
| path = segments.reject(&:empty?).join("/") | ||
| inode = inode_for_path(path) | ||
| return inode unless inode.nil? | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def inode_for_path(path) | ||
| stat = File.stat(path) rescue nil | ||
| return nil unless stat | ||
| "in-#{stat.ino}" | ||
| end | ||
|
|
||
| def parse_container_id(handle) | ||
| exp_line = /^\d+:[^:]*:(.+)$/ | ||
| uuid = /[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}/ | ||
| container = /[0-9a-f]{64}/ | ||
| task = /[0-9a-f]{32}-\d+/ | ||
| exp_container_id = /(#{uuid}|#{container}|#{task})(?:\.scope)?$/ | ||
|
|
||
| handle.each_line do |line| | ||
| match = line.match(exp_line) | ||
| next unless match && match[1] | ||
| id_match = match[1].match(exp_container_id) | ||
|
|
||
| return id_match[1] if id_match && id_match[1] | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def read_container_id(fpath) | ||
| handle = File.open(fpath, 'r') rescue nil | ||
| return nil unless handle | ||
|
|
||
| id = parse_container_id(handle) | ||
| handle.close | ||
| id | ||
| end | ||
|
|
||
| # Extracts the final container info from a line in mount info | ||
| def extract_container_info(line) | ||
| parts = line.strip.split("/") | ||
| return nil unless parts.last == "hostname" | ||
|
|
||
| # Expected structure: [..., <group>, <container_id>, ..., "hostname"] | ||
| container_id = nil | ||
| group = nil | ||
|
|
||
| parts.each_with_index do |part, idx| | ||
| # Match the container id and include the section prior to it. | ||
| if part.length == 64 && !!(part =~ /\A[0-9a-f]{64}\z/) | ||
| group = parts[idx - 1] if idx >= 1 | ||
| container_id = part | ||
| elsif part.length > 32 && !!(part =~ /\A[0-9a-f]{32}-\d+\z/) | ||
| group = parts[idx - 1] if idx >= 1 | ||
| container_id = part | ||
| elsif !!(part =~ /\A[0-9a-f]{8}(-[0-9a-f]{4}){4}\z/) | ||
| group = parts[idx - 1] if idx >= 1 | ||
| container_id = part | ||
| end | ||
| end | ||
|
|
||
| return container_id unless group == "sandboxes" | ||
| end | ||
|
|
||
| # Parse /proc/self/mountinfo to extract the container id. | ||
| # Often container runtimes embed the container id in the mount paths. | ||
| # We parse the mount with a final `hostname` component, which is part of | ||
| # the containers `etc/hostname` bind mount. | ||
| def parse_mount_info(handle) | ||
| handle.each_line do |line| | ||
| split = line.split(" ") | ||
| mnt1 = split[3] | ||
| mnt2 = split[4] | ||
| [mnt1, mnt2].each do |line| | ||
| container_id = extract_container_info(line) | ||
| return container_id unless container_id.nil? | ||
| end | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def read_mount_info(path) | ||
| handle = File.open(path, 'r') rescue nil | ||
| return nil unless handle | ||
|
|
||
| info = parse_mount_info(handle) | ||
| handle.close | ||
| info | ||
| end | ||
|
|
||
| def get_container_id(user_provided_id, cgroup_fallback) | ||
| return user_provided_id unless user_provided_id.nil? | ||
| return nil unless cgroup_fallback | ||
|
|
||
| container_id = read_container_id("/proc/self/cgroup") | ||
| return container_id unless container_id.nil? | ||
|
|
||
| container_id = read_mount_info("/proc/self/mountinfo") | ||
| return container_id unless container_id.nil? | ||
|
|
||
| return nil if host_cgroup_namespace? | ||
|
|
||
| get_cgroup_inode("/sys/fs/cgroup", "/proc/self/cgroup") | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.