Add analysis duration for telemetry
parent
a45b2e4bf1
commit
7d903fd6e9
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue