Skip to content

Commit

Permalink
fix aws-sdk complete event running in wrong async context (#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Jun 8, 2022
1 parent a18f4bc commit 6b6507b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
3 changes: 0 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ services:
- FORCE_NONINTERACTIVE=true
- START_WEB=0
- LAMBDA_EXECUTOR=local
- DEBUG=true
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
kafka:
image: debezium/kafka:1.7
ports:
Expand Down
9 changes: 5 additions & 4 deletions packages/datadog-instrumentations/src/aws-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ function wrapRequest (send) {
const channelSuffix = getChannelSuffix(serviceIdentifier)
const startCh = channel(`apm:aws:request:start:${channelSuffix}`)
if (!startCh.hasSubscribers) return send.apply(this, arguments)
const innerAr = new AsyncResource('apm:aws:request:inner')
const outerAr = new AsyncResource('apm:aws:request:outer')

this.on('complete', response => {
channel(`apm:aws:request:complete:${channelSuffix}`).publish({ response })
})
return innerAr.runInAsyncScope(() => {
this.on('complete', innerAr.bind(response => {
channel(`apm:aws:request:complete:${channelSuffix}`).publish({ response })
}))

return new AsyncResource('apm:aws:request:inner').runInAsyncScope(() => {
startCh.publish({
serviceIdentifier,
operation: this.operation,
Expand Down
52 changes: 28 additions & 24 deletions packages/datadog-plugin-aws-sdk/test/aws-sdk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ describe('Plugin', () => {
let tracer

describe('without configuration', () => {
before(() => {
return agent.load(['aws-sdk', 'http'], [{}, { server: false }])
})

before(() => {
AWS = require(`../../../versions/aws-sdk@${version}`).get()

const endpoint = new AWS.Endpoint('http://localhost:4572')
const endpoint = new AWS.Endpoint('http://127.0.0.1:4572')

s3 = new AWS.S3({ endpoint, s3ForcePathStyle: true })
tracer = require('../../dd-trace')

return agent.load('aws-sdk')
})

after(() => {
Expand Down Expand Up @@ -127,27 +129,27 @@ describe('Plugin', () => {

describe('with configuration', () => {
before(() => {
AWS = require(`../../../versions/aws-sdk@${version}`).get()

const endpoint = new AWS.Endpoint('http://localhost:5000')

s3 = new AWS.S3({ endpoint, s3ForcePathStyle: true })
tracer = require('../../dd-trace')

return agent.load('aws-sdk', {
return agent.load(['aws-sdk', 'http'], [{
service: 'test',
splitByAwsService: false,
hooks: {
request (span, response) {
span.setTag('hook.operation', response.request.operation)
if (response.error.code === 'NetworkingError' || response.error.code === 'UnknownEndpoint') {
span.addTags({
'error': 0
})
}
span.addTags({
'error': 0
})
}
}
})
}, { server: false }])
})

before(() => {
AWS = require(`../../../versions/aws-sdk@${version}`).get()

const endpoint = new AWS.Endpoint('http://127.0.0.1:5000')

s3 = new AWS.S3({ endpoint, s3ForcePathStyle: true })
tracer = require('../../dd-trace')
})

after(() => {
Expand All @@ -173,17 +175,19 @@ describe('Plugin', () => {
})

describe('with service configuration', () => {
before(() => {
return agent.load(['aws-sdk', 'http'], [{
service: 'test',
s3: false
}, { server: false }])
})

before(() => {
AWS = require(`../../../versions/aws-sdk@${version}`).get()

s3 = new AWS.S3({ endpoint: new AWS.Endpoint('http://localhost:4572'), s3ForcePathStyle: true })
sqs = new AWS.SQS({ endpoint: new AWS.Endpoint('http://localhost:4576') })
s3 = new AWS.S3({ endpoint: new AWS.Endpoint('http://127.0.0.1:4572'), s3ForcePathStyle: true })
sqs = new AWS.SQS({ endpoint: new AWS.Endpoint('http://127.0.0.1:4576') })
tracer = require('../../dd-trace')

return agent.load('aws-sdk', {
service: 'test',
s3: false
})
})

after(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-aws-sdk/test/lambda.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Plugin', () => {
before(done => {
AWS = require(`../../../versions/aws-sdk@${version}`).get()

const lambdaEndpoint = new AWS.Endpoint('http://localhost:4566')
const lambdaEndpoint = new AWS.Endpoint('http://127.0.0.1:4566')
lambda = new AWS.Lambda({ endpoint: lambdaEndpoint, region: 'us-east-1' })

lambda.createFunction({
Expand Down
4 changes: 2 additions & 2 deletions packages/datadog-plugin-aws-sdk/test/sqs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Plugin', () => {
before(done => {
AWS = require(`../../../versions/aws-sdk@${version}`).get()

const endpoint = new AWS.Endpoint('http://localhost:4576')
const endpoint = new AWS.Endpoint('http://127.0.0.1:4576')

sqs = new AWS.SQS({ endpoint, region: 'us-east-1' })
sqs.createQueue(queueOptions, (err, res) => {
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Plugin', () => {
before(done => {
AWS = require(`../../../versions/aws-sdk@${version}`).get()

const endpoint = new AWS.Endpoint('http://localhost:4576')
const endpoint = new AWS.Endpoint('http://127.0.0.1:4576')

sqs = new AWS.SQS({ endpoint, region: 'us-east-1' })
sqs.createQueue(queueOptions, (err, res) => {
Expand Down
16 changes: 8 additions & 8 deletions packages/dd-trace/test/setup/services/localstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ function waitForAWS () {
// Set the region
AWS.config.update({ region: 'us-east-1' })

const ddbEndpoint = new AWS.Endpoint('http://localhost:4569')
const kinesisEndpoint = new AWS.Endpoint('http://localhost:4568')
const s3Endpoint = new AWS.Endpoint('http://localhost:4572')
const sqsEndpoint = new AWS.Endpoint('http://localhost:4576')
const snsEndpoint = new AWS.Endpoint('http://localhost:4575')
const route53Endpoint = new AWS.Endpoint('http://localhost:4580')
const redshiftEndpoint = new AWS.Endpoint('http://localhost:4577')
const lambdaEndpoint = new AWS.Endpoint('http://localhost:4566')
const ddbEndpoint = new AWS.Endpoint('http://127.0.0.1:4569')
const kinesisEndpoint = new AWS.Endpoint('http://127.0.0.1:4568')
const s3Endpoint = new AWS.Endpoint('http://127.0.0.1:4572')
const sqsEndpoint = new AWS.Endpoint('http://127.0.0.1:4576')
const snsEndpoint = new AWS.Endpoint('http://127.0.0.1:4575')
const route53Endpoint = new AWS.Endpoint('http://127.0.0.1:4580')
const redshiftEndpoint = new AWS.Endpoint('http://127.0.0.1:4577')
const lambdaEndpoint = new AWS.Endpoint('http://127.0.0.1:4566')

const ddb = new AWS.DynamoDB({ endpoint: ddbEndpoint })
const kinesis = new AWS.Kinesis({ endpoint: kinesisEndpoint })
Expand Down

0 comments on commit 6b6507b

Please sign in to comment.