-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #845 from forta-network/kisel/forta-1545-docker-st…
…ats-metrics Poll docker resources and publish as metrics
- Loading branch information
Showing
15 changed files
with
337 additions
and
103 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
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,26 @@ | ||
package docker | ||
|
||
type ContainerResources struct { | ||
CPUStats CPUStats `json:"cpu_stats"` | ||
MemoryStats MemoryStats `json:"memory_stats"` | ||
NetworkStats map[string]NetworkStats `json:"networks"` | ||
} | ||
|
||
type CPUStats struct { | ||
CPUUsage CPUUsage `json:"cpu_usage"` | ||
} | ||
|
||
type CPUUsage struct { | ||
TotalUsage uint64 `json:"total_usage"` | ||
} | ||
|
||
type MemoryStats struct { | ||
Usage uint64 `json:"usage"` | ||
} | ||
|
||
type NetworkStats struct { | ||
// Bytes received | ||
RxBytes uint64 `json:"rx_bytes"` | ||
// Bytes sent | ||
TxBytes uint64 `json:"tx_bytes"` | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,74 @@ | ||
package containers | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/forta-network/forta-core-go/domain" | ||
"github.com/forta-network/forta-core-go/protocol" | ||
"github.com/forta-network/forta-node/clients" | ||
"github.com/forta-network/forta-node/clients/docker" | ||
"github.com/forta-network/forta-node/services/components/metrics" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
defaultPollingInterval = time.Second * 10 | ||
) | ||
|
||
// PollDockerResources gets CPU and MEM usage for all agent containers with docker stats. | ||
func PollDockerResources( | ||
ctx context.Context, dockerClient clients.DockerClient, | ||
msgClient clients.MessageClient, | ||
) { | ||
pollingTicker := time.NewTicker(defaultPollingInterval) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
logrus.Info("stopping docker resources poller") | ||
return | ||
case <-pollingTicker.C: | ||
logrus.Info("polling docker resources") | ||
containers, err := dockerClient.GetContainers(ctx) | ||
if err != nil { | ||
logrus.WithError(err).Error("error while getting docker containers") | ||
continue | ||
} | ||
|
||
for _, container := range containers { | ||
resources, err := dockerClient.ContainerStats(ctx, container.ID) | ||
if err != nil { | ||
logrus.WithError(err).Error("error while getting container stats", container.ID) | ||
continue | ||
} | ||
|
||
botID, ok := container.Labels[docker.LabelFortaBotID] | ||
if !ok { | ||
continue | ||
} | ||
|
||
var ( | ||
bytesSent uint64 | ||
bytesRecv uint64 | ||
) | ||
|
||
for _, network := range resources.NetworkStats { | ||
bytesSent += network.TxBytes | ||
bytesRecv += network.RxBytes | ||
} | ||
|
||
metrics.SendAgentMetrics(msgClient, []*protocol.AgentMetric{ | ||
metrics.CreateResourcesMetric( | ||
botID, domain.MetricDockerResourcesCPU, float64(resources.CPUStats.CPUUsage.TotalUsage)), | ||
metrics.CreateResourcesMetric( | ||
botID, domain.MetricDockerResourcesMemory, float64(resources.MemoryStats.Usage)), | ||
metrics.CreateResourcesMetric( | ||
botID, domain.MetricDockerResourcesNetworkSent, float64(bytesSent)), | ||
metrics.CreateResourcesMetric( | ||
botID, domain.MetricDockerResourcesNetworkReceive, float64(bytesRecv)), | ||
}) | ||
} | ||
} | ||
} | ||
} |
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,86 @@ | ||
package containers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/forta-network/forta-core-go/domain" | ||
"github.com/forta-network/forta-core-go/protocol" | ||
"github.com/forta-network/forta-node/clients/docker" | ||
"github.com/forta-network/forta-node/clients/messaging" | ||
mock_clients "github.com/forta-network/forta-node/clients/mocks" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPollDockerResources(t *testing.T) { | ||
defaultPollingInterval = 100 * time.Millisecond | ||
|
||
ctrl := gomock.NewController(t) | ||
dockerClient := mock_clients.NewMockDockerClient(ctrl) | ||
msgClient := mock_clients.NewMockMessageClient(ctrl) | ||
|
||
ctx := context.Background() | ||
|
||
dockerContainerID := "test-container-id" | ||
agentID := "test-agent-id" | ||
|
||
dockerClient.EXPECT().GetContainers(ctx).Return(docker.ContainerList{ | ||
{ | ||
ID: dockerContainerID, | ||
Labels: map[string]string{ | ||
docker.LabelFortaBotID: agentID, | ||
}, | ||
}, | ||
}, nil) | ||
|
||
dockerClient.EXPECT().ContainerStats(ctx, dockerContainerID).Return(&docker.ContainerResources{ | ||
CPUStats: docker.CPUStats{ | ||
CPUUsage: docker.CPUUsage{ | ||
TotalUsage: 33, | ||
}, | ||
}, | ||
MemoryStats: docker.MemoryStats{ | ||
Usage: 100, | ||
}, | ||
NetworkStats: map[string]docker.NetworkStats{ | ||
"eth0": { | ||
RxBytes: 123, | ||
TxBytes: 456, | ||
}, | ||
}, | ||
}, nil) | ||
|
||
doneCh := make(chan struct{}) | ||
|
||
msgClient.EXPECT().PublishProto(messaging.SubjectMetricAgent, gomock.Any()).Do(func(v1, v2 interface{}) { | ||
metrics := v2.(*protocol.AgentMetricList) | ||
assert.Len(t, metrics.Metrics, 4) | ||
|
||
// CPU metric | ||
assert.Equal(t, agentID, metrics.Metrics[0].AgentId) | ||
assert.Equal(t, domain.MetricDockerResourcesCPU, metrics.Metrics[0].Name) | ||
assert.Equal(t, float64(33), metrics.Metrics[0].Value) | ||
|
||
// Memory metric | ||
assert.Equal(t, agentID, metrics.Metrics[1].AgentId) | ||
assert.Equal(t, domain.MetricDockerResourcesMemory, metrics.Metrics[1].Name) | ||
assert.Equal(t, float64(100), metrics.Metrics[1].Value) | ||
|
||
// Network bytes sent metric | ||
assert.Equal(t, agentID, metrics.Metrics[2].AgentId) | ||
assert.Equal(t, domain.MetricDockerResourcesNetworkSent, metrics.Metrics[2].Name) | ||
assert.Equal(t, float64(456), metrics.Metrics[2].Value) | ||
|
||
// Network bytes received metric | ||
assert.Equal(t, agentID, metrics.Metrics[3].AgentId) | ||
assert.Equal(t, domain.MetricDockerResourcesNetworkReceive, metrics.Metrics[3].Name) | ||
assert.Equal(t, float64(123), metrics.Metrics[3].Value) | ||
|
||
close(doneCh) | ||
}) | ||
|
||
go PollDockerResources(ctx, dockerClient, msgClient) | ||
<-doneCh | ||
} |
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
Oops, something went wrong.