diff --git a/containermetrics/cached_container_metric.go b/containermetrics/cached_container_metric.go index 4e7aac13..e1e6308e 100644 --- a/containermetrics/cached_container_metric.go +++ b/containermetrics/cached_container_metric.go @@ -7,6 +7,6 @@ type CachedContainerMetrics struct { DiskQuotaBytes uint64 `json:"disk_quota_bytes"` MemoryUsageBytes uint64 `json:"memory_usage_bytes"` MemoryQuotaBytes uint64 `json:"memory_quota_bytes"` - RxBytes uint64 `json:"rx_bytes"` - TxBytes uint64 `json:"tx_bytes"` + RxBytes *uint64 `json:"rx_bytes,omitempty"` + TxBytes *uint64 `json:"tx_bytes,omitempty"` } diff --git a/containermetrics/reporters_runner_test.go b/containermetrics/reporters_runner_test.go index 90e92f64..7ac3c341 100644 --- a/containermetrics/reporters_runner_test.go +++ b/containermetrics/reporters_runner_test.go @@ -80,6 +80,10 @@ var _ = Describe("ReportersRunner", func() { ginkgomon.Interrupt(process) }) + one := uint64(1) + two := uint64(2) + three := uint64(3) + sentCPUUsage := func() []cpuUsage { usage := []cpuUsage{} @@ -150,8 +154,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(1024), ContainerAgeInNanoseconds: 1000, AbsoluteCPUEntitlementInNanoseconds: 2000, - RxInBytes: 1, - TxInBytes: 1, + RxInBytes: &one, + TxInBytes: &one, }, }, "container-guid-without-index": executor.Metrics{ @@ -164,8 +168,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(1024), ContainerAgeInNanoseconds: 1001, AbsoluteCPUEntitlementInNanoseconds: 2001, - RxInBytes: 1, - TxInBytes: 1, + RxInBytes: &one, + TxInBytes: &one, }, }, "container-guid-with-index": executor.Metrics{ @@ -178,8 +182,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(2048), ContainerAgeInNanoseconds: 1002, AbsoluteCPUEntitlementInNanoseconds: 2002, - RxInBytes: 1, - TxInBytes: 1, + RxInBytes: &one, + TxInBytes: &one, }, }, "container-guid-without-preloaded-rootfs": executor.Metrics{ @@ -192,8 +196,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(2048), ContainerAgeInNanoseconds: 1003, AbsoluteCPUEntitlementInNanoseconds: 2003, - RxInBytes: 1, - TxInBytes: 1, + RxInBytes: &one, + TxInBytes: &one, }, }, "container-guid-without-age": executor.Metrics{ @@ -206,8 +210,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(1024), ContainerAgeInNanoseconds: 0, AbsoluteCPUEntitlementInNanoseconds: 2004, - RxInBytes: 1, - TxInBytes: 1, + RxInBytes: &one, + TxInBytes: &one, }, }, } @@ -223,8 +227,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(1024), ContainerAgeInNanoseconds: 1000 + uint64(10*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2000, - RxInBytes: 2, - TxInBytes: 2, + RxInBytes: &two, + TxInBytes: &two, }, }, @@ -238,8 +242,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 4096, ContainerAgeInNanoseconds: 1001 + uint64(10*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2001, - RxInBytes: 2, - TxInBytes: 2, + RxInBytes: &two, + TxInBytes: &two, }, }, "container-guid-with-index": executor.Metrics{ @@ -252,8 +256,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 512, ContainerAgeInNanoseconds: 1002 + uint64(10*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2002, - RxInBytes: 2, - TxInBytes: 2, + RxInBytes: &two, + TxInBytes: &two, }}, "container-guid-without-preloaded-rootfs": executor.Metrics{ MetricsConfig: executor.MetricsConfig{Tags: map[string]string{"source_id": "source-id-without-preloaded-rootfs"}}, @@ -265,8 +269,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 2048, ContainerAgeInNanoseconds: 1003 + uint64(10*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2003, - RxInBytes: 2, - TxInBytes: 2, + RxInBytes: &two, + TxInBytes: &two, }, }, "container-guid-without-age": executor.Metrics{ @@ -279,8 +283,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(1024), ContainerAgeInNanoseconds: 0, AbsoluteCPUEntitlementInNanoseconds: 2004, - RxInBytes: 2, - TxInBytes: 2, + RxInBytes: &two, + TxInBytes: &two, }, }, "container-guid-without-metrics-at-t0": executor.Metrics{ @@ -293,8 +297,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 512, ContainerAgeInNanoseconds: 1002 + uint64(10*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2002, - RxInBytes: 2, - TxInBytes: 2, + RxInBytes: &two, + TxInBytes: &two, }, }, } @@ -310,8 +314,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(1024), ContainerAgeInNanoseconds: 1000 + uint64(20*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2000, - RxInBytes: 3, - TxInBytes: 3, + RxInBytes: &three, + TxInBytes: &three, }, }, @@ -325,8 +329,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 234, ContainerAgeInNanoseconds: 1001 + uint64(20*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2001, - RxInBytes: 3, - TxInBytes: 3, + RxInBytes: &three, + TxInBytes: &three, }, }, "container-guid-with-index": executor.Metrics{ @@ -339,8 +343,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 43200, ContainerAgeInNanoseconds: 1002 + uint64(20*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2002, - RxInBytes: 3, - TxInBytes: 3, + RxInBytes: &three, + TxInBytes: &three, }, }, "container-guid-without-preloaded-rootfs": executor.Metrics{ @@ -353,8 +357,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 2048, ContainerAgeInNanoseconds: 1003 + uint64(20*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2003, - RxInBytes: 3, - TxInBytes: 3, + RxInBytes: &three, + TxInBytes: &three, }, }, "container-guid-without-age": executor.Metrics{ @@ -367,8 +371,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: megsToBytes(1024), ContainerAgeInNanoseconds: 0, AbsoluteCPUEntitlementInNanoseconds: 2004, - RxInBytes: 3, - TxInBytes: 3, + RxInBytes: &three, + TxInBytes: &three, }, }, "container-guid-without-metrics-at-t0": executor.Metrics{ @@ -381,8 +385,8 @@ var _ = Describe("ReportersRunner", func() { DiskLimitInBytes: 512, ContainerAgeInNanoseconds: 1002 + uint64(20*time.Second), AbsoluteCPUEntitlementInNanoseconds: 2002, - RxInBytes: 3, - TxInBytes: 3, + RxInBytes: &three, + TxInBytes: &three, }, }, } @@ -676,8 +680,8 @@ var _ = Describe("ReportersRunner", func() { DiskUsageBytes: 4560, MemoryQuotaBytes: megsToBytes(7890), DiskQuotaBytes: 4096, - RxBytes: 2, - TxBytes: 2, + RxBytes: &two, + TxBytes: &two, })) Expect(containerMetrics).To(HaveKeyWithValue("container-guid-with-index", &containermetrics.CachedContainerMetrics{ MetricGUID: "source-id-with-index", @@ -686,8 +690,8 @@ var _ = Describe("ReportersRunner", func() { DiskUsageBytes: 6540, MemoryQuotaBytes: megsToBytes(9870), DiskQuotaBytes: 512, - RxBytes: 2, - TxBytes: 2, + RxBytes: &two, + TxBytes: &two, })) Expect(containerMetrics).To(HaveKeyWithValue("container-guid-without-source-id", &containermetrics.CachedContainerMetrics{ MetricGUID: "", @@ -696,8 +700,8 @@ var _ = Describe("ReportersRunner", func() { DiskUsageBytes: megsToBytes(456), MemoryQuotaBytes: megsToBytes(789), DiskQuotaBytes: megsToBytes(1024), - RxBytes: 2, - TxBytes: 2, + RxBytes: &two, + TxBytes: &two, })) Expect(containerMetrics).To(HaveKeyWithValue("container-guid-without-preloaded-rootfs", &containermetrics.CachedContainerMetrics{ MetricGUID: "source-id-without-preloaded-rootfs", @@ -706,8 +710,8 @@ var _ = Describe("ReportersRunner", func() { DiskUsageBytes: 4560, MemoryQuotaBytes: megsToBytes(6780), DiskQuotaBytes: 2048, - RxBytes: 2, - TxBytes: 2, + RxBytes: &two, + TxBytes: &two, })) Expect(containerMetrics).To(HaveKeyWithValue("container-guid-without-age", &containermetrics.CachedContainerMetrics{ MetricGUID: "source-id-without-age", @@ -716,8 +720,8 @@ var _ = Describe("ReportersRunner", func() { DiskUsageBytes: megsToBytes(456), MemoryQuotaBytes: megsToBytes(789), DiskQuotaBytes: megsToBytes(1024), - RxBytes: 2, - TxBytes: 2, + RxBytes: &two, + TxBytes: &two, })) Expect(containerMetrics).To(HaveKeyWithValue("container-guid-without-metrics-at-t0", &containermetrics.CachedContainerMetrics{ MetricGUID: "source-id-without-metrics-at-t0", @@ -726,8 +730,8 @@ var _ = Describe("ReportersRunner", func() { DiskUsageBytes: 6540, MemoryQuotaBytes: megsToBytes(9870), DiskQuotaBytes: 512, - RxBytes: 2, - TxBytes: 2, + RxBytes: &two, + TxBytes: &two, })) }) }) diff --git a/depot/containerstore/containerstore.go b/depot/containerstore/containerstore.go index 95a4f784..92b6a8c9 100644 --- a/depot/containerstore/containerstore.go +++ b/depot/containerstore/containerstore.go @@ -359,6 +359,13 @@ func (cs *containerStore) Metrics(logger lager.Logger) (map[string]executor.Cont rootFSSize := cs.rootFSSizer.RootFSSizeFromPath(nodeInfo.RootFSPath) diskUsage := gardenMetric.DiskStat.TotalBytesUsed - rootFSSize + + var rxInBytes, txInBytes *uint64 + if gardenMetric.NetworkStat != nil { + rxInBytes = &gardenMetric.NetworkStat.RxBytes + txInBytes = &gardenMetric.NetworkStat.TxBytes + } + containerMetrics[guid] = executor.ContainerMetrics{ MemoryUsageInBytes: gardenMetric.MemoryStat.TotalUsageTowardLimit, DiskUsageInBytes: diskUsage, @@ -367,8 +374,8 @@ func (cs *containerStore) Metrics(logger lager.Logger) (map[string]executor.Cont TimeSpentInCPU: time.Duration(gardenMetric.CPUStat.Usage), ContainerAgeInNanoseconds: uint64(gardenMetric.Age), AbsoluteCPUEntitlementInNanoseconds: gardenMetric.CPUEntitlement, - RxInBytes: gardenMetric.NetworkStat.RxBytes, - TxInBytes: gardenMetric.NetworkStat.TxBytes, + RxInBytes: rxInBytes, + TxInBytes: txInBytes, } } diff --git a/depot/containerstore/containerstore_test.go b/depot/containerstore/containerstore_test.go index 30ea903f..010cf5e6 100644 --- a/depot/containerstore/containerstore_test.go +++ b/depot/containerstore/containerstore_test.go @@ -3036,6 +3036,10 @@ var _ = Describe("Container Store", func() { _, err = containerStore.Create(logger, "some-trace-id", containerGuid4) Expect(err).ToNot(HaveOccurred()) + networkStat1 := garden.ContainerNetworkStat{ + RxBytes: uint64(42), + TxBytes: uint64(43), + } bulkMetrics := map[string]garden.ContainerMetricsEntry{ containerGuid1: garden.ContainerMetricsEntry{ Metrics: garden.Metrics{ @@ -3050,10 +3054,7 @@ var _ = Describe("Container Store", func() { }, Age: 1000000000, CPUEntitlement: 100, - NetworkStat: garden.ContainerNetworkStat{ - RxBytes: 42, - TxBytes: 43, - }, + NetworkStat: &networkStat1, }, }, containerGuid2: garden.ContainerMetricsEntry{ @@ -3069,10 +3070,7 @@ var _ = Describe("Container Store", func() { }, Age: 2000000000, CPUEntitlement: 200, - NetworkStat: garden.ContainerNetworkStat{ - RxBytes: 44, - TxBytes: 45, - }, + NetworkStat: nil, }, }, containerGuid4: garden.ContainerMetricsEntry{ @@ -3118,8 +3116,8 @@ var _ = Describe("Container Store", func() { Expect(container1Metrics.TimeSpentInCPU).To(Equal(5 * time.Second)) Expect(container1Metrics.ContainerAgeInNanoseconds).To(Equal(uint64(1000000000))) Expect(container1Metrics.AbsoluteCPUEntitlementInNanoseconds).To(Equal(uint64(100))) - Expect(container1Metrics.RxInBytes).To(Equal(uint64(42))) - Expect(container1Metrics.TxInBytes).To(Equal(uint64(43))) + Expect(*container1Metrics.RxInBytes).To(Equal(uint64(42))) + Expect(*container1Metrics.TxInBytes).To(Equal(uint64(43))) container2Metrics, ok := metrics[containerGuid2] Expect(ok).To(BeTrue()) @@ -3130,8 +3128,8 @@ var _ = Describe("Container Store", func() { Expect(container2Metrics.TimeSpentInCPU).To(Equal(1 * time.Millisecond)) Expect(container2Metrics.ContainerAgeInNanoseconds).To(Equal(uint64(2000000000))) Expect(container2Metrics.AbsoluteCPUEntitlementInNanoseconds).To(Equal(uint64(200))) - Expect(container2Metrics.RxInBytes).To(Equal(uint64(44))) - Expect(container2Metrics.TxInBytes).To(Equal(uint64(45))) + Expect(container2Metrics.RxInBytes).To(BeNil()) + Expect(container2Metrics.TxInBytes).To(BeNil()) }) Context("when fetching bulk metrics fails", func() { diff --git a/resources.go b/resources.go index 58545718..042e42df 100644 --- a/resources.go +++ b/resources.go @@ -234,8 +234,8 @@ type ContainerMetrics struct { TimeSpentInCPU time.Duration `json:"time_spent_in_cpu"` AbsoluteCPUEntitlementInNanoseconds uint64 `json:"absolute_cpu_entitlement_in_ns"` ContainerAgeInNanoseconds uint64 `json:"container_age_in_ns"` - RxInBytes uint64 `json:"rx_in_bytes"` - TxInBytes uint64 `json:"tx_in_bytes"` + RxInBytes *uint64 `json:"rx_in_bytes"` + TxInBytes *uint64 `json:"tx_in_bytes"` } type MetricsConfig struct {