-
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.
Add memory extractors for pod and node level (amazon-contributing#147)
* Add memory extractor at pod and node level 1. Added memory extractor at pod and node level 2. Add unit tests for memory extractor 3. use cpu and memory extractor for k8s windows * Fix adding tags to collected metrics from extractors
- Loading branch information
1 parent
1ad51be
commit e44056d
Showing
5 changed files
with
173 additions
and
23 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
66 changes: 66 additions & 0 deletions
66
receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/mem_extractor.go
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,66 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package extractors // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors" | ||
|
||
import ( | ||
"time" | ||
|
||
ci "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight" | ||
awsmetrics "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/metrics" | ||
cExtractor "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
type MemMetricExtractor struct { | ||
logger *zap.Logger | ||
rateCalculator awsmetrics.MetricCalculator | ||
} | ||
|
||
func (m *MemMetricExtractor) HasValue(rawMetric *RawMetric) bool { | ||
if rawMetric.MemoryStats != nil { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (m *MemMetricExtractor) GetValue(rawMetric *RawMetric, mInfo cExtractor.CPUMemInfoProvider, containerType string) []*cExtractor.CAdvisorMetric { | ||
var metrics []*cExtractor.CAdvisorMetric | ||
|
||
metric := cExtractor.NewCadvisorMetric(containerType, m.logger) | ||
identifier := rawMetric.Id | ||
|
||
metric.AddField(ci.MetricName(containerType, ci.MemUsage), *rawMetric.MemoryStats.UsageBytes) | ||
metric.AddField(ci.MetricName(containerType, ci.MemRss), *rawMetric.MemoryStats.RSSBytes) | ||
metric.AddField(ci.MetricName(containerType, ci.MemWorkingset), *rawMetric.MemoryStats.WorkingSetBytes) | ||
|
||
multiplier := float64(time.Second) | ||
cExtractor.AssignRateValueToField(&m.rateCalculator, metric.GetFields(), ci.MetricName(containerType, ci.MemPgfault), identifier, | ||
float64(*rawMetric.MemoryStats.PageFaults), rawMetric.Time, multiplier) | ||
cExtractor.AssignRateValueToField(&m.rateCalculator, metric.GetFields(), ci.MetricName(containerType, ci.MemPgmajfault), identifier, | ||
float64(*rawMetric.MemoryStats.MajorPageFaults), rawMetric.Time, multiplier) | ||
|
||
memoryCapacity := mInfo.GetMemoryCapacity() | ||
if metric.GetField(ci.MetricName(containerType, ci.MemWorkingset)) != nil && memoryCapacity != 0 { | ||
metric.AddField(ci.MetricName(containerType, ci.MemUtilization), float64(metric.GetField(ci.MetricName(containerType, ci.MemWorkingset)).(uint64))/float64(memoryCapacity)*100) | ||
} | ||
|
||
if containerType == ci.TypeNode { | ||
metric.AddField(ci.MetricName(containerType, ci.MemLimit), memoryCapacity) | ||
} | ||
|
||
metrics = append(metrics, metric) | ||
return metrics | ||
} | ||
|
||
func (m *MemMetricExtractor) Shutdown() error { | ||
return m.rateCalculator.Shutdown() | ||
} | ||
|
||
func NewMemMetricExtractor(logger *zap.Logger) *MemMetricExtractor { | ||
return &MemMetricExtractor{ | ||
logger: logger, | ||
rateCalculator: cExtractor.NewFloat64RateCalculator(), | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/mem_extractor_test.go
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,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package extractors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight" | ||
cExtractor "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/cadvisor/extractors" | ||
cTestUtils "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/cadvisor/testutils" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/k8swindows/testutils" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMemStats(t *testing.T) { | ||
MockCPUMemInfo := cTestUtils.MockCPUMemInfo{} | ||
result := testutils.LoadKubeletSummary(t, "./testdata/PreSingleKubeletSummary.json") | ||
result2 := testutils.LoadKubeletSummary(t, "./testdata/CurSingleKubeletSummary.json") | ||
|
||
podRawMetric := ConvertPodToRaw(&result.Pods[0]) | ||
podRawMetric2 := ConvertPodToRaw(&result2.Pods[0]) | ||
|
||
containerType := containerinsight.TypePod | ||
extractor := NewMemMetricExtractor(nil) | ||
|
||
var cMetrics []*cExtractor.CAdvisorMetric | ||
if extractor.HasValue(podRawMetric) { | ||
cMetrics = extractor.GetValue(podRawMetric, MockCPUMemInfo, containerType) | ||
} | ||
if extractor.HasValue(podRawMetric2) { | ||
cMetrics = extractor.GetValue(podRawMetric2, MockCPUMemInfo, containerType) | ||
} | ||
|
||
cExtractor.AssertContainsTaggedUint(t, cMetrics[0], "pod_memory_rss", 0) | ||
cExtractor.AssertContainsTaggedUint(t, cMetrics[0], "pod_memory_usage", 0) | ||
cExtractor.AssertContainsTaggedUint(t, cMetrics[0], "pod_memory_working_set", 209088512) | ||
|
||
cExtractor.AssertContainsTaggedFloat(t, cMetrics[0], "pod_memory_pgfault", 0, 0) | ||
cExtractor.AssertContainsTaggedFloat(t, cMetrics[0], "pod_memory_pgmajfault", 0, 0) | ||
require.NoError(t, extractor.Shutdown()) | ||
|
||
// for node type | ||
containerType = containerinsight.TypeNode | ||
extractor = NewMemMetricExtractor(nil) | ||
|
||
nodeRawMetric := ConvertNodeToRaw(&result.Node) | ||
nodeRawMetric2 := ConvertNodeToRaw(&result2.Node) | ||
|
||
if extractor.HasValue(nodeRawMetric) { | ||
cMetrics = extractor.GetValue(nodeRawMetric, MockCPUMemInfo, containerType) | ||
} | ||
|
||
if extractor.HasValue(nodeRawMetric2) { | ||
cMetrics = extractor.GetValue(nodeRawMetric2, MockCPUMemInfo, containerType) | ||
} | ||
|
||
cExtractor.AssertContainsTaggedUint(t, cMetrics[0], "node_memory_rss", 0) | ||
cExtractor.AssertContainsTaggedUint(t, cMetrics[0], "node_memory_usage", 3572293632) | ||
cExtractor.AssertContainsTaggedUint(t, cMetrics[0], "node_memory_working_set", 1026678784) | ||
cExtractor.AssertContainsTaggedInt(t, cMetrics[0], "node_memory_limit", 1073741824) | ||
|
||
cExtractor.AssertContainsTaggedFloat(t, cMetrics[0], "node_memory_pgfault", 0, 0) | ||
cExtractor.AssertContainsTaggedFloat(t, cMetrics[0], "node_memory_pgmajfault", 0, 0) | ||
cExtractor.AssertContainsTaggedFloat(t, cMetrics[0], "node_memory_utilization", 95, 0.7) | ||
|
||
require.NoError(t, extractor.Shutdown()) | ||
} |
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