reduce labels for container info

This commit is contained in:
Andy Xie 2018-01-11 23:08:38 +08:00
parent 5a2e3d06d6
commit 1ccbe6fdd0
22 changed files with 93 additions and 81 deletions

View File

@ -70,16 +70,16 @@ type InMemoryCache struct {
backend storage.StorageDriver
}
func (self *InMemoryCache) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (self *InMemoryCache) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
var cstore *containerCache
var ok bool
func() {
self.lock.Lock()
defer self.lock.Unlock()
if cstore, ok = self.containerCacheMap[ref.Name]; !ok {
cstore = newContainerStore(ref, self.maxAge)
self.containerCacheMap[ref.Name] = cstore
if cstore, ok = self.containerCacheMap[cInfo.ContainerReference.Name]; !ok {
cstore = newContainerStore(cInfo.ContainerReference, self.maxAge)
self.containerCacheMap[cInfo.ContainerReference.Name] = cstore
}
}()
@ -87,7 +87,7 @@ func (self *InMemoryCache) AddStats(ref info.ContainerReference, stats *info.Con
// TODO(monnand): To deal with long delay write operations, we
// may want to start a pool of goroutines to do write
// operations.
if err := self.backend.AddStats(ref, stats); err != nil {
if err := self.backend.AddStats(cInfo, stats); err != nil {
glog.Error(err)
}
}

View File

@ -27,8 +27,10 @@ import (
const containerName = "/container"
var (
containerRef = info.ContainerReference{Name: containerName}
zero time.Time
cInfo = info.ContainerInfo{
ContainerReference: info.ContainerReference{Name: containerName},
}
zero time.Time
)
// Make stats with the specified identifier.
@ -51,15 +53,17 @@ func TestAddStats(t *testing.T) {
memoryCache := New(60*time.Second, nil)
assert := assert.New(t)
assert.Nil(memoryCache.AddStats(containerRef, makeStat(0)))
assert.Nil(memoryCache.AddStats(containerRef, makeStat(1)))
assert.Nil(memoryCache.AddStats(containerRef, makeStat(2)))
assert.Nil(memoryCache.AddStats(containerRef, makeStat(0)))
containerRef2 := info.ContainerReference{
Name: "/container2",
assert.Nil(memoryCache.AddStats(&cInfo, makeStat(0)))
assert.Nil(memoryCache.AddStats(&cInfo, makeStat(1)))
assert.Nil(memoryCache.AddStats(&cInfo, makeStat(2)))
assert.Nil(memoryCache.AddStats(&cInfo, makeStat(0)))
cInfo2 := info.ContainerInfo{
ContainerReference: info.ContainerReference{
Name: "/container2",
},
}
assert.Nil(memoryCache.AddStats(containerRef2, makeStat(0)))
assert.Nil(memoryCache.AddStats(containerRef2, makeStat(1)))
assert.Nil(memoryCache.AddStats(&cInfo2, makeStat(0)))
assert.Nil(memoryCache.AddStats(&cInfo2, makeStat(1)))
}
func TestRecentStatsNoRecentStats(t *testing.T) {
@ -74,7 +78,7 @@ func makeWithStats(n int) *InMemoryCache {
memoryCache := New(60*time.Second, nil)
for i := 0; i < n; i++ {
memoryCache.AddStats(containerRef, makeStat(i))
memoryCache.AddStats(&cInfo, makeStat(i))
}
return memoryCache
}

View File

@ -148,7 +148,6 @@ func (self *containerdContainerHandler) ContainerReference() (info.ContainerRefe
Id: self.id,
Name: self.name,
Namespace: k8sContainerdNamespace,
Labels: self.labels,
Aliases: self.aliases,
}, nil
}

View File

@ -89,7 +89,6 @@ func TestHandler(t *testing.T) {
Name: "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
Aliases: []string{"40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"},
Namespace: k8sContainerdNamespace,
Labels: map[string]string{"io.cri-containerd.kind": "sandbox"},
},
},
} {

View File

@ -209,7 +209,6 @@ func (self *crioContainerHandler) ContainerReference() (info.ContainerReference,
Name: self.name,
Aliases: self.aliases,
Namespace: CrioNamespace,
Labels: self.labels,
}, nil
}

View File

@ -99,7 +99,6 @@ func TestHandler(t *testing.T) {
Name: "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/crio-81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
Aliases: []string{"test", "81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f"},
Namespace: CrioNamespace,
Labels: map[string]string{"io.kubernetes.container.name": "POD"},
},
},
} {

View File

@ -369,7 +369,6 @@ func (self *dockerContainerHandler) ContainerReference() (info.ContainerReferenc
Name: self.name,
Aliases: self.aliases,
Namespace: DockerNamespace,
Labels: self.labels,
}, nil
}

View File

@ -178,7 +178,6 @@ func (handler *rktContainerHandler) ContainerReference() (info.ContainerReferenc
Name: handler.name,
Aliases: handler.aliases,
Namespace: RktNamespace,
Labels: handler.labels,
}, nil
}

View File

@ -85,8 +85,6 @@ type ContainerReference struct {
// Namespace under which the aliases of a container are unique.
// An example of a namespace is "docker" for Docker containers.
Namespace string `json:"namespace,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
// Sorts by container name.

View File

@ -615,7 +615,12 @@ func (c *containerData) updateStats() error {
}
return err
}
err = c.memoryCache.AddStats(ref, stats)
cInfo := info.ContainerInfo{
ContainerReference: ref,
}
err = c.memoryCache.AddStats(&cInfo, stats)
if err != nil {
return err
}

View File

@ -102,8 +102,12 @@ func expectManagerWithContainers(containers []string, query *info.ContainerInfoR
if err != nil {
t.Error(err)
}
cInfo := info.ContainerInfo{
ContainerReference: ref,
}
for _, stat := range cinfo.Stats {
err = memoryCache.AddStats(ref, stat)
err = memoryCache.AddStats(&cInfo, stat)
if err != nil {
t.Error(err)
}
@ -148,8 +152,13 @@ func expectManagerWithContainersV2(containers []string, query *info.ContainerInf
if err != nil {
t.Error(err)
}
cInfo := info.ContainerInfo{
ContainerReference: ref,
}
for _, stat := range cinfo.Stats {
err = memoryCache.AddStats(ref, stat)
err = memoryCache.AddStats(&cInfo, stat)
if err != nil {
t.Error(err)
}

View File

@ -193,7 +193,7 @@ func (self *bigqueryStorage) GetSchema() *bigquery.TableSchema {
}
func (self *bigqueryStorage) containerStatsToRows(
ref info.ContainerReference,
cInfo *info.ContainerInfo,
stats *info.ContainerStats,
) (row map[string]interface{}) {
row = make(map[string]interface{})
@ -205,9 +205,9 @@ func (self *bigqueryStorage) containerStatsToRows(
row[colMachineName] = self.machineName
// Container name
name := ref.Name
if len(ref.Aliases) > 0 {
name = ref.Aliases[0]
name := cInfo.ContainerReference.Name
if len(cInfo.ContainerReference.Aliases) > 0 {
name = cInfo.ContainerReference.Aliases[0]
}
row[colContainerName] = name
@ -250,7 +250,7 @@ func (self *bigqueryStorage) containerStatsToRows(
}
func (self *bigqueryStorage) containerFilesystemStatsToRows(
ref info.ContainerReference,
cInfo *info.ContainerInfo,
stats *info.ContainerStats,
) (rows []map[string]interface{}) {
for _, fsStat := range stats.Filesystem {
@ -263,13 +263,13 @@ func (self *bigqueryStorage) containerFilesystemStatsToRows(
return rows
}
func (self *bigqueryStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (self *bigqueryStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
if stats == nil {
return nil
}
rows := make([]map[string]interface{}, 0)
rows = append(rows, self.containerStatsToRows(ref, stats))
rows = append(rows, self.containerFilesystemStatsToRows(ref, stats)...)
rows = append(rows, self.containerStatsToRows(cInfo, stats))
rows = append(rows, self.containerFilesystemStatsToRows(cInfo, stats)...)
for _, row := range rows {
err := self.client.InsertRow(row)
if err != nil {

View File

@ -68,13 +68,13 @@ func new() (storage.StorageDriver, error) {
}
func (self *elasticStorage) containerStatsAndDefaultValues(
ref info.ContainerReference, stats *info.ContainerStats) *detailSpec {
cInfo *info.ContainerInfo, stats *info.ContainerStats) *detailSpec {
timestamp := stats.Timestamp.UnixNano() / 1E3
var containerName string
if len(ref.Aliases) > 0 {
containerName = ref.Aliases[0]
if len(cInfo.ContainerReference.Aliases) > 0 {
containerName = cInfo.ContainerReference.Aliases[0]
} else {
containerName = ref.Name
containerName = cInfo.ContainerReference.Name
}
detail := &detailSpec{
Timestamp: timestamp,
@ -85,7 +85,7 @@ func (self *elasticStorage) containerStatsAndDefaultValues(
return detail
}
func (self *elasticStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (self *elasticStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
if stats == nil {
return nil
}
@ -94,7 +94,7 @@ func (self *elasticStorage) AddStats(ref info.ContainerReference, stats *info.Co
self.lock.Lock()
defer self.lock.Unlock()
// Add some default params based on ContainerStats
detail := self.containerStatsAndDefaultValues(ref, stats)
detail := self.containerStatsAndDefaultValues(cInfo, stats)
// Index a cadvisor (using JSON serialization)
_, err := self.client.Index().
Index(self.indexName).

View File

@ -108,7 +108,7 @@ const (
)
func (self *influxdbStorage) containerFilesystemStatsToPoints(
ref info.ContainerReference,
cInfo *info.ContainerInfo,
stats *info.ContainerStats) (points []*influxdb.Point) {
if len(stats.Filesystem) == 0 {
return points
@ -143,20 +143,20 @@ func (self *influxdbStorage) containerFilesystemStatsToPoints(
points = append(points, pointFsUsage, pointFsLimit)
}
self.tagPoints(ref, stats, points)
self.tagPoints(cInfo, stats, points)
return points
}
// Set tags and timestamp for all points of the batch.
// Points should inherit the tags that are set for BatchPoints, but that does not seem to work.
func (self *influxdbStorage) tagPoints(ref info.ContainerReference, stats *info.ContainerStats, points []*influxdb.Point) {
func (self *influxdbStorage) tagPoints(cInfo *info.ContainerInfo, stats *info.ContainerStats, points []*influxdb.Point) {
// Use container alias if possible
var containerName string
if len(ref.Aliases) > 0 {
containerName = ref.Aliases[0]
if len(cInfo.ContainerReference.Aliases) > 0 {
containerName = cInfo.ContainerReference.Aliases[0]
} else {
containerName = ref.Name
containerName = cInfo.ContainerReference.Name
}
commonTags := map[string]string{
@ -166,13 +166,13 @@ func (self *influxdbStorage) tagPoints(ref info.ContainerReference, stats *info.
for i := 0; i < len(points); i++ {
// merge with existing tags if any
addTagsToPoint(points[i], commonTags)
addTagsToPoint(points[i], ref.Labels)
addTagsToPoint(points[i], cInfo.Spec.Labels)
points[i].Time = stats.Timestamp
}
}
func (self *influxdbStorage) containerStatsToPoints(
ref info.ContainerReference,
cInfo *info.ContainerInfo,
stats *info.ContainerStats,
) (points []*influxdb.Point) {
// CPU usage: Total usage in nanoseconds
@ -208,7 +208,7 @@ func (self *influxdbStorage) containerStatsToPoints(
points = append(points, makePoint(serTxBytes, stats.Network.TxBytes))
points = append(points, makePoint(serTxErrors, stats.Network.TxErrors))
self.tagPoints(ref, stats, points)
self.tagPoints(cInfo, stats, points)
return points
}
@ -221,7 +221,7 @@ func (self *influxdbStorage) defaultReadyToFlush() bool {
return time.Since(self.lastWrite) >= self.bufferDuration
}
func (self *influxdbStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (self *influxdbStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
if stats == nil {
return nil
}
@ -231,8 +231,8 @@ func (self *influxdbStorage) AddStats(ref info.ContainerReference, stats *info.C
self.lock.Lock()
defer self.lock.Unlock()
self.points = append(self.points, self.containerStatsToPoints(ref, stats)...)
self.points = append(self.points, self.containerFilesystemStatsToPoints(ref, stats)...)
self.points = append(self.points, self.containerStatsToPoints(cInfo, stats)...)
self.points = append(self.points, self.containerFilesystemStatsToPoints(cInfo, stats)...)
if self.readyToFlush() {
pointsToFlush = self.points
self.points = make([]*influxdb.Point, 0)

View File

@ -47,9 +47,9 @@ func (self *influxDbTestStorageDriver) readyToFlush() bool {
return self.count >= self.buffer
}
func (self *influxDbTestStorageDriver) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (self *influxDbTestStorageDriver) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
self.count++
return self.base.AddStats(ref, stats)
return self.base.AddStats(cInfo, stats)
}
func (self *influxDbTestStorageDriver) Close() error {

View File

@ -60,11 +60,11 @@ type detailSpec struct {
ContainerStats *info.ContainerStats `json:"container_stats,omitempty"`
}
func (driver *kafkaStorage) infoToDetailSpec(ref info.ContainerReference, stats *info.ContainerStats) *detailSpec {
func (driver *kafkaStorage) infoToDetailSpec(cInfo *info.ContainerInfo, stats *info.ContainerStats) *detailSpec {
timestamp := time.Now()
containerID := ref.Id
containerLabels := ref.Labels
containerName := container.GetPreferredName(ref)
containerID := cInfo.ContainerReference.Id
containerLabels := cInfo.Spec.Labels
containerName := container.GetPreferredName(cInfo.ContainerReference)
detail := &detailSpec{
Timestamp: timestamp,
@ -77,8 +77,8 @@ func (driver *kafkaStorage) infoToDetailSpec(ref info.ContainerReference, stats
return detail
}
func (driver *kafkaStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
detail := driver.infoToDetailSpec(ref, stats)
func (driver *kafkaStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
detail := driver.infoToDetailSpec(cInfo, stats)
b, err := json.Marshal(detail)
driver.producer.Input() <- &kafka.ProducerMessage{

View File

@ -65,13 +65,13 @@ func (self *redisStorage) defaultReadyToFlush() bool {
}
// We must add some default params (for example: MachineName,ContainerName...)because containerStats do not include them
func (self *redisStorage) containerStatsAndDefaultValues(ref info.ContainerReference, stats *info.ContainerStats) *detailSpec {
func (self *redisStorage) containerStatsAndDefaultValues(cInfo *info.ContainerInfo, stats *info.ContainerStats) *detailSpec {
timestamp := stats.Timestamp.UnixNano() / 1E3
var containerName string
if len(ref.Aliases) > 0 {
containerName = ref.Aliases[0]
if len(cInfo.ContainerReference.Aliases) > 0 {
containerName = cInfo.ContainerReference.Aliases[0]
} else {
containerName = ref.Name
containerName = cInfo.ContainerReference.Name
}
detail := &detailSpec{
Timestamp: timestamp,
@ -83,7 +83,7 @@ func (self *redisStorage) containerStatsAndDefaultValues(ref info.ContainerRefer
}
// Push the data into redis
func (self *redisStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (self *redisStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
if stats == nil {
return nil
}
@ -93,7 +93,7 @@ func (self *redisStorage) AddStats(ref info.ContainerReference, stats *info.Cont
self.lock.Lock()
defer self.lock.Unlock()
// Add some default params based on containerStats
detail := self.containerStatsAndDefaultValues(ref, stats)
detail := self.containerStatsAndDefaultValues(cInfo, stats)
// To json
b, _ := json.Marshal(detail)
if self.readyToFlush() {

View File

@ -105,16 +105,16 @@ func (self *statsdStorage) containerFsStatsToValues(
}
// Push the data into redis
func (self *statsdStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (self *statsdStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
if stats == nil {
return nil
}
var containerName string
if len(ref.Aliases) > 0 {
containerName = ref.Aliases[0]
if len(cInfo.ContainerReference.Aliases) > 0 {
containerName = cInfo.ContainerReference.Aliases[0]
} else {
containerName = ref.Name
containerName = cInfo.ContainerReference.Name
}
series := self.containerStatsToValues(stats)

View File

@ -89,14 +89,14 @@ func (driver *stdoutStorage) containerFsStatsToValues(series *map[string]uint64,
}
}
func (driver *stdoutStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
func (driver *stdoutStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
if stats == nil {
return nil
}
containerName := ref.Name
if len(ref.Aliases) > 0 {
containerName = ref.Aliases[0]
containerName := cInfo.ContainerReference.Name
if len(cInfo.ContainerReference.Aliases) > 0 {
containerName = cInfo.ContainerReference.Aliases[0]
}
var buffer bytes.Buffer

View File

@ -22,7 +22,7 @@ import (
)
type StorageDriver interface {
AddStats(ref info.ContainerReference, stats *info.ContainerStats) error
AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error
// Close will clear the state of the storage driver. The elements
// stored in the underlying storage may or may not be deleted depending

View File

@ -25,8 +25,8 @@ type MockStorageDriver struct {
MockCloseMethod bool
}
func (self *MockStorageDriver) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
args := self.Called(ref, stats)
func (self *MockStorageDriver) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
args := self.Called(cInfo.ContainerReference, stats)
return args.Error(0)
}

View File

@ -123,14 +123,16 @@ func StorageDriverFillRandomStatsFunc(
samplePeriod := 1 * time.Second
ref := info.ContainerReference{
Name: containerName,
cInfo := info.ContainerInfo{
ContainerReference: info.ContainerReference{
Name: containerName,
},
}
trace := buildTrace(cpuTrace, memTrace, samplePeriod)
for _, stats := range trace {
err := driver.AddStats(ref, stats)
err := driver.AddStats(&cInfo, stats)
if err != nil {
t.Fatalf("unable to add stats: %v", err)
}