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

contrib/aws/aws-sdk-go: add WithErrorCheck option #1682

Merged
merged 7 commits into from
May 12, 2023
Merged

contrib/aws/aws-sdk-go: add WithErrorCheck option #1682

merged 7 commits into from
May 12, 2023

Conversation

delca85
Copy link
Contributor

@delca85 delca85 commented Jan 22, 2023

What does this PR do?

It adds an option to aws/aws-sdk-go tracer. This option is called WithErrorCheck and it specifies a function which determines whether the passed error should be marked as an error. The fn is called whenever an aws operation finishes with an error.

Motivation

This applies the same logic introduced by #1315 on sql tracer.
We need this otherwise DD raises an issue for every kind of error.

Describe how to test/QA your changes

Reviewer's Checklist

  • If known, an appropriate milestone has been selected; otherwise the Triage milestone is set.
  • Changed code has unit tests for its functionality.
  • If this interacts with the agent in a new way, a system test has been added.

WithErrorCheck specifies a function which determines whether the passed
error should be marked as an error. The fn is called whenever an aws operation
finishes with an error.
@delca85 delca85 requested a review from a team January 22, 2023 21:12
@johanneswuerbach
Copy link
Contributor

@knusbaum sorry for the direct ping, but any chance to get this or something similar merge? This is currently polluting our error tracking :-/

@johanneswuerbach
Copy link
Contributor

@katiehockman any chance this could also be added to next milestone?

@dianashevchenko dianashevchenko added this to the v1.49.0 milestone Mar 15, 2023
@dianashevchenko dianashevchenko added the apm:ecosystem contrib/* related feature requests or bugs label Mar 15, 2023
Comment on lines 41 to 42
cfg := aws.NewConfig().WithRegion("us-west-2")
sess := session.Must(session.NewSession(cfg))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cfg := aws.NewConfig().WithRegion("us-west-2")
sess := session.Must(session.NewSession(cfg))
sess := session.Must(session.NewSession( aws.NewConfig().WithRegion("us-west-2")))

Comment on lines 50 to 52
spans := mt.FinishedSpans()
assert.True(t, len(spans) > 0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is failing in the tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written below, I ran the contrib test with ./test.sh --contrib and every test under contrib/aws/aws-sdk-go/aws succeeded.

Comment on lines 53 to 54
s := spans[len(spans)-1]
assert.Equal(t, errExist, s.Tag(ext.Error) != nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s := spans[len(spans)-1]
assert.Equal(t, errExist, s.Tag(ext.Error) != nil)
assert.Equal(t, errExist, spans[0].Tag(ext.Error) != nil)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.


// WithErrorCheck specifies a function fn which determines whether the passed
// error should be marked as an error. The fn is called whenever an aws operation
// finishes with an error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// finishes with an error
// finishes with an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

Comment on lines 34 to 40

func TestWithErrorCheck(t *testing.T) {
testOpts := func(errExist bool, opts ...awstrace.Option) func(t *testing.T) {
return func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this test belongs in aws_test.go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved.

Comment on lines 59 to 60
t.Run("errcheck", testOpts(false, awstrace.WithErrorCheck(func(err error) bool {
return !strings.Contains(err.Error(), `InvalidAccessKeyId: The AWS Access Key Id you provided does not exist in our records`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error that is actually received doesn't contain this text, so it will fail. For the test purposes, it's best to check for different content. NoCredentialProviders: no valid providers in chain. Deprecated.

Suggested change
t.Run("errcheck", testOpts(false, awstrace.WithErrorCheck(func(err error) bool {
return !strings.Contains(err.Error(), `InvalidAccessKeyId: The AWS Access Key Id you provided does not exist in our records`)
t.Run("errcheck", testOpts(false, awstrace.WithErrorCheck(func(err error) bool {
return !strings.Contains(err.Error(), `NoCredentialProviders: no valid providers in chain`)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the contrib test with ./test.sh --contrib and every test under contrib/aws/aws-sdk-go/aws succeeded.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the workflow is still failing - link. Since the error I received signified the lack of credentials, it is possible that you have some environment variables set on your machine. I would recommend to rerun this test specifically and include a different error check.
I have changed the assertion to log the error:

assert.Equal(t, errExist, spans[0].Tag(ext.Error) != nil, fmt.Sprintf("error.message isn't nil: %s", spans[0].Tag(ext.Error)))

Here is the output of running a test on my machine.

 Error Trace:    /Users/diana.shevchenko/go/src/github.com/dd-trace-go/contrib/aws/aws-sdk-go/aws/aws_test.go:273
                Error:          Not equal: 
                                expected: false
                                actual  : true
                Test:           TestWithErrorCheck/errcheck
                Messages:       error.message isn't nil: NoCredentialProviders: no valid providers in chain. Deprecated.
                                        For verbose messaging see aws.Config.CredentialsChainVerboseErrors

Like I have mentioned previously, the error contains NoCredentialProviders

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I didn't think about some issues related to my local environment.
I pushed new code according to your suggestion.

Comment on lines 38 to 51
mt := mocktracer.Start()
defer mt.Stop()

cfg := aws.NewConfig().WithRegion("us-west-2")
sess := session.Must(session.NewSession(cfg))
sess = awstrace.WrapSession(sess, opts...)

s3api := s3.New(sess)
s3api.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String("some-bucket-name"),
})

spans := mt.FinishedSpans()
assert.True(t, len(spans) > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One has to create a span before hand, an example of that can be found in other tests of aws_test.go.

root, ctx := tracer.StartSpanFromContext(context.Background(), "test")
sess := session.Must(session.NewSession(aws.NewConfig().WithRegion("us-west-2")))
sess = WrapSession(sess, opts...)
s3api := s3.New(sess)
s3api.CreateBucketWithContext(ctx, &s3.CreateBucketInput{
	Bucket: aws.String("some-bucket-name"),
})
root.Finish()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the creation of the span.

@delca85
Copy link
Contributor Author

delca85 commented Apr 18, 2023

@dianashevchenko is there still any problem with this PR? I saw it had been tagged as related to v1.49.0 release but then never merged.

@ajgajg1134 ajgajg1134 modified the milestones: v1.49.0, v1.51.0 May 12, 2023
@zarirhamza
Copy link
Contributor

LGTM

Just a minor change to only check the return boolean value in the tests. As we update AWS libraries, the exact string of the error will most likely change (which it did) and will break the test every time, so we future proofed it by only checking the boolean return value.

@ajgajg1134 ajgajg1134 merged commit aff2b5a into DataDog:main May 12, 2023
9 checks passed
katiehockman pushed a commit that referenced this pull request Jun 6, 2023
Co-authored-by: Diana Shevchenko <40775148+dianashevchenko@users.noreply.github.com>
Co-authored-by: Zarir Hamza <zarir.hamza@datadoghq.com>
katiehockman pushed a commit that referenced this pull request Jun 28, 2023
Adds an WithErrorCheck function similar to #1682
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apm:ecosystem contrib/* related feature requests or bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants