Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor telemetry reporting logic. #683

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ func GetAllResources(c context.Context, query *Query, configObj config.Config) (
identifiers, err := (*resource).GetAndSetIdentifiers(c, configObj)
if err != nil {
logging.Errorf("Unable to retrieve %v, %v", (*resource).ResourceName(), err)

// Reporting resource-level failures encountered during the GetIdentifiers phase
// Note: This is useful to understand which resources are failing more often than others.
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: fmt.Sprintf("error:GetIdentifiers:%s", (*resource).ResourceName()),
}, map[string]interface{}{
"region": region,
})

ge := report.GeneralError{
Error: err,
Description: fmt.Sprintf("Unable to retrieve %s", (*resource).ResourceName()),
Expand Down Expand Up @@ -155,6 +164,14 @@ func nukeAllResourcesInRegion(account *AwsAccountResources, region string, bar *
// of the current job.Since we handle each individual resource deletion error within its own resource-specific code,
// we can safely discard this error
_ = err

// Report to telemetry - aggregated metrics of failures per resources.
// Note: This is useful to understand which resources are failing more often than others.
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: fmt.Sprintf("error:Nuke:%s", (*awsResource).ResourceName()),
}, map[string]interface{}{
"region": region,
})
}

if i != len(batches)-1 {
Expand Down
7 changes: 0 additions & 7 deletions aws/resources/access_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"github.com/aws/aws-sdk-go/service/accessanalyzer"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/gruntwork-io/go-commons/errors"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
"github.com/hashicorp/go-multierror"
"sync"
)
Expand Down Expand Up @@ -64,11 +62,6 @@ func (analyzer *AccessAnalyzer) nukeAll(names []*string) error {
if err := <-errChan; err != nil {
allErrs = multierror.Append(allErrs, err)
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking Access Analyzer",
}, map[string]interface{}{
"region": analyzer.Region,
})
}
}
finalErr := allErrs.ErrorOrNil()
Expand Down
5 changes: 2 additions & 3 deletions aws/resources/access_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/aws/aws-sdk-go/service/accessanalyzer"
"github.com/aws/aws-sdk-go/service/accessanalyzer/accessanalyzeriface"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/stretchr/testify/require"
"regexp"
"testing"
Expand All @@ -30,7 +29,7 @@ func (m mockedAccessAnalyzer) DeleteAnalyzer(input *accessanalyzer.DeleteAnalyze
}

func TestAccessAnalyzer_GetAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

now := time.Now()
Expand Down Expand Up @@ -91,7 +90,7 @@ func TestAccessAnalyzer_GetAll(t *testing.T) {
}

func TestAccessAnalyzer_NukeAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

aa := AccessAnalyzer{
Expand Down
7 changes: 0 additions & 7 deletions aws/resources/acm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/gruntwork-io/go-commons/errors"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
)

// Returns a list of strings of ACM ARNs
Expand Down Expand Up @@ -80,11 +78,6 @@ func (a *ACM) nukeAll(arns []*string) error {
_, err := a.Client.DeleteCertificate(params)
if err != nil {
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking ACM",
}, map[string]interface{}{
"region": a.Region,
})
} else {
deletedCount++
logging.Debugf("Deleted ACM: %s", *acmArn)
Expand Down
7 changes: 3 additions & 4 deletions aws/resources/acm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/aws/aws-sdk-go/service/acm"
"github.com/aws/aws-sdk-go/service/acm/acmiface"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/stretchr/testify/require"
)

Expand All @@ -32,7 +31,7 @@ func (m mockedACM) DeleteCertificate(input *acm.DeleteCertificateInput) (*acm.De
}

func TestACMGetAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testDomainName := "test-domain-name"
Expand Down Expand Up @@ -80,7 +79,7 @@ func TestACMGetAll(t *testing.T) {
}

func TestACMGetAll_FilterInUse(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testDomainName := "test-domain-name"
Expand All @@ -105,7 +104,7 @@ func TestACMGetAll_FilterInUse(t *testing.T) {
}

func TestACMNukeAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testDomainName := "test-domain-name"
Expand Down
8 changes: 0 additions & 8 deletions aws/resources/acmpca.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"sync"
"time"

"github.com/gruntwork-io/cloud-nuke/telemetry"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/acmpca"
"github.com/gruntwork-io/cloud-nuke/config"
Expand Down Expand Up @@ -84,11 +81,6 @@ func (ap *ACMPCA) nukeAll(arns []*string) error {
if err := <-errChan; err != nil {
allErrs = multierror.Append(allErrs, err)
logging.Errorf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking ACMPCA",
}, map[string]interface{}{
"region": ap.Region,
})
}
}

Expand Down
7 changes: 3 additions & 4 deletions aws/resources/acmpca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/aws/aws-sdk-go/service/acmpca"
"github.com/aws/aws-sdk-go/service/acmpca/acmpcaiface"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -45,7 +44,7 @@ func (m mockedACMPCA) DeleteCertificateAuthority(
}

func TestAcmPcaGetAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testArn := "test-arn"
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestAcmPcaGetAll(t *testing.T) {
}

func TestAcmPcaNukeAll_DisabledCA(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testArn := "test-arn"
Expand All @@ -102,7 +101,7 @@ func TestAcmPcaNukeAll_DisabledCA(t *testing.T) {
}

func TestAcmPcaNukeAll_EnabledCA(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testArn := "test-arn"
Expand Down
7 changes: 0 additions & 7 deletions aws/resources/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"context"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/gruntwork-io/cloud-nuke/util"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/gruntwork-io/go-commons/errors"
)

Expand Down Expand Up @@ -86,11 +84,6 @@ func (ami *AMIs) nukeAll(imageIds []*string) error {

if err != nil {
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking AMI",
}, map[string]interface{}{
"region": ami.Region,
})
} else {
deletedCount++
logging.Debugf("Deleted AMI: %s", *imageID)
Expand Down
7 changes: 3 additions & 4 deletions aws/resources/ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/stretchr/testify/assert"
"regexp"
"testing"
Expand All @@ -30,7 +29,7 @@ func (m mockedAMI) DeregisterImage(input *ec2.DeregisterImageInput) (*ec2.Deregi
}

func TestAMIGetAll_SkipAWSManaged(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testName := "test-ami"
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestAMIGetAll_SkipAWSManaged(t *testing.T) {
}

func TestAMIGetAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testName := "test-ami"
Expand Down Expand Up @@ -118,7 +117,7 @@ func TestAMIGetAll(t *testing.T) {
}

func TestAMINukeAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testName := "test-ami"
Expand Down
9 changes: 0 additions & 9 deletions aws/resources/apigateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"context"
"sync"

commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/gruntwork-io/go-commons/errors"
"github.com/hashicorp/go-multierror"
)
Expand Down Expand Up @@ -62,12 +59,6 @@ func (gateway *ApiGateway) nukeAll(identifiers []*string) error {
if err := <-errChan; err != nil {
allErrs = multierror.Append(allErrs, err)
logging.Debugf("[Failed] %s", err)

telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking API Gateway",
}, map[string]interface{}{
"region": gateway.Region,
})
}
}
finalErr := allErrs.ErrorOrNil()
Expand Down
9 changes: 4 additions & 5 deletions aws/resources/apigateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/apigateway/apigatewayiface"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -42,7 +41,7 @@ func (m mockedApiGateway) DeleteClientCertificate(*apigateway.DeleteClientCertif
}

func TestAPIGatewayGetAllAndNukeAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testApiID := "aws-nuke-test-" + util.UniqueID()
Expand All @@ -66,7 +65,7 @@ func TestAPIGatewayGetAllAndNukeAll(t *testing.T) {
}

func TestAPIGatewayGetAllTimeFilter(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testApiID := "aws-nuke-test-" + util.UniqueID()
Expand Down Expand Up @@ -106,7 +105,7 @@ func TestAPIGatewayGetAllTimeFilter(t *testing.T) {
}

func TestNukeAPIGatewayMoreThanOne(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testApiID1 := "aws-nuke-test-" + util.UniqueID()
Expand All @@ -133,7 +132,7 @@ func TestNukeAPIGatewayMoreThanOne(t *testing.T) {
}

func TestNukeAPIGatewayWithCertificates(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testApiID1 := "aws-nuke-test-" + util.UniqueID()
Expand Down
15 changes: 3 additions & 12 deletions aws/resources/apigatewayv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package resources
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/service/apigatewayv2/apigatewayv2iface"
"github.com/gruntwork-io/cloud-nuke/logging"
"sync"

commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/aws/aws-sdk-go/service/apigatewayv2/apigatewayv2iface"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/gruntwork-io/go-commons/errors"
"github.com/hashicorp/go-multierror"
"sync"
)

func (gw *ApiGatewayV2) getAll(c context.Context, configObj config.Config) ([]*string, error) {
Expand Down Expand Up @@ -68,11 +64,6 @@ func (gw *ApiGatewayV2) nukeAll(identifiers []*string) error {
for _, errChan := range errChans {
if err := <-errChan; err != nil {
allErrs = multierror.Append(allErrs, err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking API Gateway V2",
}, map[string]interface{}{
"region": gw.Region,
})
}
}
finalErr := allErrs.ErrorOrNil()
Expand Down
5 changes: 2 additions & 3 deletions aws/resources/apigatewayv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/aws/aws-sdk-go/service/apigatewayv2/apigatewayv2iface"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func (m mockedApiGatewayV2) DeleteApiMapping(*apigatewayv2.DeleteApiMappingInput
}

func TestApiGatewayV2GetAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

testApiID := "test-api-id"
Expand Down Expand Up @@ -93,7 +92,7 @@ func TestApiGatewayV2GetAll(t *testing.T) {
}

func TestApiGatewayV2NukeAll(t *testing.T) {
telemetry.InitTelemetry("cloud-nuke", "")

t.Parallel()

gw := ApiGatewayV2{
Expand Down
Loading