Skip to content

Commit

Permalink
some enhancement and unit tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
danehlim committed Jun 6, 2024
1 parent 7172f3b commit f375a3c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
17 changes: 6 additions & 11 deletions agent/stats/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,16 @@ func (container *StatsContainer) processStatsStream() error {
return err
}

isFirstStatAfterContainerRestart := container.syncContainerRestartAggregationData(rawStat)
isFirstStatAfterContainerRestart := apiContainer != nil && apiContainer.RestartPolicyEnabled() &&
container.syncContainerRestartAggregationData(rawStat)
err = container.statsQueue.AddContainerStat(rawStat, getNonDockerContainerStats(apiContainer),
&container.restartAggregationData.LastStatBeforeLastRestart, container.hasRestartedBefore())
if err != nil {
seelog.Warnf("Container [%s]: error converting stats for container: %v", dockerID, err)
continue
}
if isFirstStatAfterContainerRestart {
err = container.saveRestartAggregationData()
err = container.saveRestartAggregationData(apiContainer)
if err != nil {
logger.Error("Failed to update container's stats restart aggregation data in database",
logger.Fields{
Expand Down Expand Up @@ -227,15 +228,9 @@ func (container *StatsContainer) syncContainerRestartAggregationData(stat *types
}

// saveRestartAggregationData is used to save a container's restart aggregation data in Agent's local state database.
func (container *StatsContainer) saveRestartAggregationData() error {
dockerID := container.containerMetadata.DockerID
agentContainer, err := container.resolver.ResolveContainer(dockerID)
if err != nil {
return errors.Wrap(err, "failed to resolve container via container metadata resolver")
}

agentContainer.Container.SetRestartAggregationDataForStats(container.restartAggregationData)
err = container.dataClient.SaveContainer(agentContainer.Container)
func (container *StatsContainer) saveRestartAggregationData(apiContainer *apicontainer.Container) error {
apiContainer.SetRestartAggregationDataForStats(container.restartAggregationData)
err := container.dataClient.SaveContainer(apiContainer)
if err != nil {
return errors.Wrap(err, "failed to save data for container")
}
Expand Down
1 change: 0 additions & 1 deletion agent/stats/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func TestContainerStatsCollection(t *testing.T) {
errC := make(chan error)
numStats := 8
mockDockerClient.EXPECT().Stats(ctx, dockerID, dockerclient.StatsInactivityTimeout).Return(statChan, errC)
mockDockerClient.EXPECT().DescribeContainer(ctx, dockerID).Times(numStats)
go metricSenderFunc(statChan, numStats, nil)()

container := &StatsContainer{
Expand Down

0 comments on commit f375a3c

Please sign in to comment.