forked from DataDog/dd-trace-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profiler: support agent-based deployments and have those as default. (D…
…ataDog#668) Changes to profile to default to connecting through the agent, when an API key is not set.
- Loading branch information
Showing
11 changed files
with
450 additions
and
188 deletions.
There are no files selected for viewing
This file contains 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 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 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,63 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-2020 Datadog, Inc. | ||
|
||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
// cgroupPath is the path to the cgroup file where we can find the container id if one exists. | ||
cgroupPath = "/proc/self/cgroup" | ||
) | ||
|
||
var ( | ||
// expLine matches a line in the /proc/self/cgroup file. It has a submatch for the last element (path), which contains the container ID. | ||
expLine = regexp.MustCompile(`^\d+:[^:]*:(.+)$`) | ||
// expContainerID matches contained IDs and sources. Source: https://github.com/Qard/container-info/blob/master/index.js | ||
expContainerID = regexp.MustCompile(`([0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}|[0-9a-f]{64})(?:.scope)?$`) | ||
|
||
// containerID is the containerID read at init from /proc/self/cgroup | ||
containerID string | ||
) | ||
|
||
func init() { | ||
containerID = readContainerID(cgroupPath) | ||
} | ||
|
||
// parseContainerID finds the first container ID reading from r and returns it. | ||
func parseContainerID(r io.Reader) string { | ||
scn := bufio.NewScanner(r) | ||
for scn.Scan() { | ||
path := expLine.FindStringSubmatch(scn.Text()) | ||
if len(path) != 2 { | ||
// invalid entry, continue | ||
continue | ||
} | ||
if id := expContainerID.FindString(path[1]); id != "" { | ||
return id | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
// readContainerID attempts to return the container ID from the provided file path or empty on failure. | ||
func readContainerID(fpath string) string { | ||
f, err := os.Open(fpath) | ||
if err != nil { | ||
return "" | ||
} | ||
defer f.Close() | ||
return parseContainerID(f) | ||
} | ||
|
||
// ContainerID attempts to return the container ID from /proc/self/cgroup or empty on failure. | ||
func ContainerID() string { | ||
return containerID | ||
} |
This file contains 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,60 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-2020 Datadog, Inc. | ||
|
||
package internal | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadContainerID(t *testing.T) { | ||
for in, out := range map[string]string{ | ||
`other_line | ||
10:hugetlb:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
9:cpuset:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
8:pids:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
7:freezer:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
6:cpu,cpuacct:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
5:perf_event:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
4:blkio:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
3:devices:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
2:net_cls,net_prio:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa`: "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa", | ||
"10:hugetlb:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa": "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa", | ||
"10:hugetlb:/kubepods": "", | ||
} { | ||
id := parseContainerID(strings.NewReader(in)) | ||
if id != out { | ||
t.Fatalf("%q -> %q", in, out) | ||
} | ||
} | ||
} | ||
|
||
func TestReadContainerIDFromCgroup(t *testing.T) { | ||
cid := "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa" | ||
cgroupContents := "10:hugetlb:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/" + cid | ||
|
||
tmpFile, err := ioutil.TempFile(os.TempDir(), "fake-cgroup-") | ||
if err != nil { | ||
t.Fatalf("failed to create fake cgroup file: %v", err) | ||
} | ||
defer os.Remove(tmpFile.Name()) | ||
_, err = io.WriteString(tmpFile, cgroupContents) | ||
if err != nil { | ||
t.Fatalf("failed writing to fake cgroup file: %v", err) | ||
} | ||
err = tmpFile.Close() | ||
if err != nil { | ||
t.Fatalf("failed closing fake cgroup file: %v", err) | ||
} | ||
|
||
actualCID := readContainerID(tmpFile.Name()) | ||
assert.Equal(t, cid, actualCID) | ||
} |
This file contains 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
Oops, something went wrong.