Add analysis duration for telemetry

main
Elie 2021-04-28 12:42:06 +02:00 committed by sundowndev
parent a45b2e4bf1
commit 7d903fd6e9
5 changed files with 31 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"sort"
"strings"
"time"
"github.com/r3labs/diff/v2"
@ -40,6 +41,7 @@ type Analysis struct {
differences []Difference
summary Summary
alerts alerter.Alerts
Duration time.Duration
}
type serializableDifference struct {

View File

@ -2,6 +2,7 @@ package pkg
import (
"fmt"
"time"
globaloutput "github.com/cloudskiff/driftctl/pkg/output"
"github.com/jmespath/go-jmespath"
@ -59,6 +60,7 @@ func NewDriftCTL(remoteSupplier resource.Supplier, iacSupplier resource.Supplier
}
func (d DriftCTL) Run() (*analyser.Analysis, error) {
start := time.Now()
remoteResources, resourcesFromState, err := d.scan()
if err != nil {
return nil, err
@ -114,6 +116,7 @@ func (d DriftCTL) Run() (*analyser.Analysis, error) {
driftIgnore := filter.NewDriftIgnore()
analysis, err := d.analyzer.Analyze(remoteResources, resourcesFromState, driftIgnore)
analysis.Duration = time.Since(start)
if err != nil {
return nil, err

View File

@ -103,6 +103,14 @@ func matchByAttributes(input, attrs map[string]interface{}) bool {
func TestDriftctlRun_BasicBehavior(t *testing.T) {
cases := TestCases{
{
name: "analysis duration is set",
stateResources: []resource.Resource{},
remoteResources: []resource.Resource{},
assert: func(result *test.ScanResult, err error) {
result.NotZero(result.Duration)
},
},
{
name: "infrastructure should be in sync",
stateResources: []resource.Resource{

View File

@ -17,6 +17,7 @@ type telemetry struct {
Arch string `json:"arch"`
TotalResources int `json:"total_resources"`
TotalManaged int `json:"total_managed"`
Duration uint `json:"duration"`
}
func SendTelemetry(analysis *analyser.Analysis) {
@ -31,6 +32,7 @@ func SendTelemetry(analysis *analyser.Analysis) {
Arch: runtime.GOARCH,
TotalResources: analysis.Summary().TotalResources,
TotalManaged: analysis.Summary().TotalManaged,
Duration: uint(analysis.Duration.Seconds() + 0.5),
}
body, err := json.Marshal(t)

View File

@ -31,6 +31,7 @@ func TestSendTelemetry(t *testing.T) {
a := &analyser.Analysis{}
a.AddManaged(&resource.FakeResource{})
a.AddUnmanaged(&resource.FakeResource{})
a.Duration = 123.4 * 1e9 // 123.4 seconds
return a
}(),
expectedBody: &telemetry{
@ -39,6 +40,21 @@ func TestSendTelemetry(t *testing.T) {
Arch: runtime.GOARCH,
TotalResources: 2,
TotalManaged: 1,
Duration: 123,
},
},
{
name: "valid analysis with round up",
analysis: func() *analyser.Analysis {
a := &analyser.Analysis{}
a.Duration = 123.5 * 1e9 // 123.5 seconds
return a
}(),
expectedBody: &telemetry{
Version: version.Current(),
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Duration: 124,
},
},
{