Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhulston committed Nov 22, 2024
1 parent ec6ca0b commit 0ab6403
Showing 1 changed file with 119 additions and 77 deletions.
196 changes: 119 additions & 77 deletions packages/datadog-plugin-aws-sdk/test/dynamodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,49 +128,68 @@ describe('Plugin', () => {
describe('span pointers', () => {
beforeEach(() => {
DynamoDb.dynamoPrimaryKeyConfig = null
process.env.DD_AWS_SDK_DYNAMODB_TABLE_PRIMARY_KEYS = null
delete process.env.DD_AWS_SDK_DYNAMODB_TABLE_PRIMARY_KEYS
})

function testSpanPointers ({ expectedLength = 1, expectedHashes, operation }) {
return (done) => {
agent.use(traces => {
try {
const span = traces[0][0]
const links = JSON.parse(span.meta?.['_dd.span_links'] || '[]')
expect(links).to.have.lengthOf(expectedLength)

if (expectedHashes) {
if (Array.isArray(expectedHashes)) {
expectedHashes.forEach((hash, i) => {
expect(links[i].attributes['ptr.hash']).to.equal(hash)
})
} else {
expect(links[0].attributes).to.deep.equal({
'ptr.kind': DYNAMODB_PTR_KIND,
'ptr.dir': SPAN_POINTER_DIRECTION.DOWNSTREAM,
'ptr.hash': expectedHashes,
'link.kind': 'span-pointer'
})
try {
let hasCompleted = false
operation((err) => {
if (err) {
console.log('[testSpanPointers] Error while completing operation:', err)
if (!hasCompleted) {
hasCompleted = true
return done(err)
}
}
done()
} catch (error) {
console.log('Test error:', error)
return done(error)
}
}).catch(done)

operation((err) => {
if (err) {
console.log('Test error:', err)
return done(err)
}
})
agent.use(traces => {
if (hasCompleted) return
try {
const span = traces[0][0]
const links = JSON.parse(span.meta?.['_dd.span_links'] || '[]')
expect(links).to.have.lengthOf(expectedLength)

if (expectedHashes) {
if (Array.isArray(expectedHashes)) {
expectedHashes.forEach((hash, i) => {
expect(links[i].attributes['ptr.hash']).to.equal(hash)
})
} else {
expect(links[0].attributes).to.deep.equal({
'ptr.kind': DYNAMODB_PTR_KIND,
'ptr.dir': SPAN_POINTER_DIRECTION.DOWNSTREAM,
'ptr.hash': expectedHashes,
'link.kind': 'span-pointer'
})
}
}
hasCompleted = true
return done()
} catch (error) {
console.log('[testSpanPointers] Test error:', error)
if (!hasCompleted) {
hasCompleted = true
return done(error)
}
}
}).catch(error => {
console.log('[testSpanPointers] Caught error during agent.use:', error)
if (!hasCompleted) {
hasCompleted = true
done(error)
}
})
})
} catch (error) {
console.log('[testSpanPointers] Caught error:', error)
}
}
}

describe('1-key table', () => {
it('should add span pointer for putItem when config is valid',
it('should add span pointer for putItem when config is valid', () => {
testSpanPointers({
expectedHashes: '27f424c8202ab35efbf8b0b444b1928f',
operation: (callback) => {
Expand All @@ -185,9 +204,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is invalid',
it('should not add links or error for putItem when config is invalid', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -201,9 +220,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is missing',
it('should not add links or error for putItem when config is missing', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -217,9 +236,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointer for updateItem',
it('should add span pointer for updateItem', () => {
testSpanPointers({
expectedHashes: '27f424c8202ab35efbf8b0b444b1928f',
operation: (callback) => {
Expand All @@ -235,9 +254,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointer for deleteItem',
it('should add span pointer for deleteItem', () => {
testSpanPointers({
expectedHashes: '27f424c8202ab35efbf8b0b444b1928f',
operation: (callback) => {
Expand All @@ -247,7 +266,7 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointers for transactWriteItems', () => {
// Skip for older versions that don't support transactWriteItems
Expand Down Expand Up @@ -336,7 +355,7 @@ describe('Plugin', () => {
})

describe('2-key table', () => {
it('should add span pointer for putItem when config is valid',
it('should add span pointer for putItem when config is valid', () => {
testSpanPointers({
expectedHashes: 'cc32f0e49ee05d3f2820ccc999bfe306',
operation: (callback) => {
Expand All @@ -350,9 +369,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is invalid',
it('should not add links or error for putItem when config is invalid', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -366,9 +385,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is missing',
it('should not add links or error for putItem when config is missing', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -382,43 +401,66 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointer for updateItem',
testSpanPointers({
expectedHashes: '8a6f801cc4e7d1d5e0dd37e0904e6316',
operation: (callback) => {
dynamo.updateItem({
TableName: twoKeyTableName,
Key: {
id: { N: '3' },
binary: { B: Buffer.from('Hello world 3') }
},
AttributeUpdates: {
someOtherField: {
Action: 'PUT',
Value: { S: 'new value' }
}
}
}, callback)
it('should add span pointer for updateItem', function (done) {
dynamo.putItem({
TableName: twoKeyTableName,
Item: {
id: { N: '100' },
binary: { B: Buffer.from('abc') }
}
}, async function (err) {
if (err) {
console.log('MYERR:', err)
return done(err)
}
await new Promise(resolve => setTimeout(resolve, 100))
testSpanPointers({
expectedHashes: '5dac7d25254d596482a3c2c187e51046',
operation: (callback) => {
dynamo.updateItem({
TableName: twoKeyTableName,
Key: {
id: { N: '100' },
binary: { B: Buffer.from('abc') }
},
AttributeUpdates: {
someOtherField: {
Action: 'PUT',
Value: { S: 'new value' }
}
}
}, callback)
}
})(done)
})
)
})

it('should add span pointer for deleteItem',
testSpanPointers({
expectedHashes: '8a6f801cc4e7d1d5e0dd37e0904e6316',
operation: (callback) => {
dynamo.deleteItem({
TableName: twoKeyTableName,
Key: {
id: { N: '3' },
binary: { B: Buffer.from('Hello world 3') }
}
}, callback)
it('should add span pointer for deleteItem', function (done) {
dynamo.putItem({
TableName: twoKeyTableName,
Item: {
id: { N: '200' },
binary: { B: Buffer.from('Hello world') }
}
}, async function (err) {
if (err) return done(err)
await new Promise(resolve => setTimeout(resolve, 100))
testSpanPointers({
expectedHashes: 'c356b0dd48c734d889e95122750c2679',
operation: (callback) => {
dynamo.deleteItem({
TableName: twoKeyTableName,
Key: {
id: { N: '200' },
binary: { B: Buffer.from('Hello world') }
}
}, callback)
}
})(done)
})
)
})

it('should add span pointers for transactWriteItems', () => {
// Skip for older versions that don't support transactWriteItems
Expand Down

0 comments on commit 0ab6403

Please sign in to comment.