Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions statsd/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestBufferGauge(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeGauge("namespace.", []string{"tag:tag"}, "metric", 1, []string{}, 1, noTimestamp, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -44,6 +45,7 @@ func TestBufferGauge(t *testing.T) {
}

func TestBufferCount(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeCount("namespace.", []string{"tag:tag"}, "metric", 1, []string{}, 1, noTimestamp, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -81,6 +83,7 @@ func TestBufferCount(t *testing.T) {
}

func TestBufferHistogram(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeHistogram("namespace.", []string{"tag:tag"}, "metric", 1, []string{}, 1, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -112,6 +115,7 @@ func TestBufferHistogram(t *testing.T) {
}

func TestBufferDistribution(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeDistribution("namespace.", []string{"tag:tag"}, "metric", 1, []string{}, 1, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -142,6 +146,7 @@ func TestBufferDistribution(t *testing.T) {
assert.Equal(t, "namespace.metric:1|d|#tag:tag|c:container-id|e:external-env|card:low\n", string(buffer.bytes()))
}
func TestBufferSet(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeSet("namespace.", []string{"tag:tag"}, "metric", "value", []string{}, 1, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -173,6 +178,7 @@ func TestBufferSet(t *testing.T) {
}

func TestBufferTiming(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeTiming("namespace.", []string{"tag:tag"}, "metric", 1, []string{}, 1, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -204,6 +210,7 @@ func TestBufferTiming(t *testing.T) {
}

func TestBufferEvent(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeEvent(&Event{Title: "title", Text: "text"}, []string{"tag:tag"}, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -235,6 +242,7 @@ func TestBufferEvent(t *testing.T) {
}

func TestBufferServiceCheck(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
err := buffer.writeServiceCheck(&ServiceCheck{Name: "name", Status: Ok}, []string{"tag:tag"}, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down Expand Up @@ -266,6 +274,7 @@ func TestBufferServiceCheck(t *testing.T) {
}

func TestBufferFullSize(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(30, 10)
err := buffer.writeGauge("namespace.", []string{"tag:tag"}, "metric", 1, []string{}, 1, noTimestamp, true, CardinalityNotSet)
assert.Nil(t, err)
Expand All @@ -275,6 +284,7 @@ func TestBufferFullSize(t *testing.T) {
}

func TestBufferSeparator(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 10)
err := buffer.writeGauge("namespace.", []string{"tag:tag"}, "metric", 1, []string{}, 1, noTimestamp, true, CardinalityNotSet)
assert.Nil(t, err)
Expand All @@ -284,6 +294,7 @@ func TestBufferSeparator(t *testing.T) {
}

func TestBufferAggregated(t *testing.T) {
withoutOriginGlobals(t)
buffer := newStatsdBuffer(1024, 1)
pos, err := buffer.writeAggregated([]byte("h"), "namespace.", []string{"tag:tag"}, "metric", []float64{1}, "", 12, -1, 1, true, CardinalityNotSet)
assert.Nil(t, err)
Expand Down
25 changes: 19 additions & 6 deletions statsd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ package statsd

import (
"sync"
"sync/atomic"
)

var (
// containerID holds the container ID.
containerID = ""

initOnce sync.Once
containerID atomic.Value
initOnce sync.Once
)

// getContainerID returns the container ID configured at the client creation
func init() {
// Pre-seed with an empty string so Load never returns nil and the
// type-assertion in the read path is unconditional.
containerID.Store("")
}

// getContainerID returns the container ID configured at the client creation.
// It can either be auto-discovered with origin detection or provided by the user.
// User-defined container ID is prioritized.
func getContainerID() string {
return containerID
return containerID.Load().(string)
}

func storeContainerID(id string) {
containerID.Store(id)
}

func setContainerIDForTest(id string) {
containerID.Store(id)
}
14 changes: 7 additions & 7 deletions statsd/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,27 @@ func internalInitContainerID(userProvidedID string, cgroupFallback, isHostCgroup
// readCIDOrInode reads the container ID from the user provided ID, cgroups or mountinfo.
func readCIDOrInode(userProvidedID, cgroupPath, selfMountInfoPath, defaultCgroupMountPath string, cgroupFallback, isHostCgroupNs bool) {
if userProvidedID != "" {
containerID = userProvidedID
storeContainerID(userProvidedID)
return
}

if cgroupFallback {
containerID = readContainerID(cgroupPath)
if containerID != "" {
if id := readContainerID(cgroupPath); id != "" {
storeContainerID(id)
return
}

containerID = readMountinfo(selfMountInfoPath)
if containerID != "" {
if id := readMountinfo(selfMountInfoPath); id != "" {
storeContainerID(id)
return
}

// If we're in the host cgroup namespace, the cid should be retrievable in /proc/self/cgroup
// In private cgroup namespace, we can retrieve the cgroup controller inode.
if containerID == "" && isHostCgroupNs {
if isHostCgroupNs {
return
}

containerID = getCgroupInode(defaultCgroupMountPath, cgroupPath)
storeContainerID(getCgroupInode(defaultCgroupMountPath, cgroupPath))
}
}
3 changes: 1 addition & 2 deletions statsd/container_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ func isHostCgroupNamespace() bool {
var initContainerID = func(userProvidedID string, _, _ bool) {
initOnce.Do(func() {
if userProvidedID != "" {
containerID = userProvidedID
return
storeContainerID(userProvidedID)
}
})
}
6 changes: 3 additions & 3 deletions statsd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ func TestReadCIDOrInode(t *testing.T) {

for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
prevContainerID := containerID
prevContainerID := getContainerID()
defer func() {
containerID = prevContainerID
patchContainerID(prevContainerID)
}()

sysFsCgroupPath := path.Join(os.TempDir(), "sysfscgroup")
Expand Down Expand Up @@ -483,7 +483,7 @@ func TestReadCIDOrInode(t *testing.T) {
require.NoError(t, err)

readCIDOrInode("", procSelfCgroup.Name(), mountInfo.Name(), sysFsCgroupPath, true, tc.isHostCgroupNs)
require.Equal(t, expectedResult, containerID)
require.Equal(t, expectedResult, getContainerID())
})
}
}
Loading
Loading