Skip to content

Commit

Permalink
fix aws-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed Dec 16, 2024
1 parent 9ca90b2 commit 88f4f36
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 24 deletions.
14 changes: 10 additions & 4 deletions packages/datadog-plugin-aws-sdk/test/aws-sdk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const agent = require('../../dd-trace/test/plugins/agent')
const { setup, sort } = require('./spec_helpers')
const semver = require('semver')
const { ERROR_MESSAGE, ERROR_STACK, ERROR_TYPE } = require('../../dd-trace/src/constants')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

describe('Plugin', () => {
// TODO: use the Request class directly for generic tests
Expand Down Expand Up @@ -75,14 +76,16 @@ describe('Plugin', () => {

const s3ClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-s3' : 'aws-sdk'
const sqsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sqs' : 'aws-sdk'
const versionCtx = withVersions.context

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

before(() => {
AWS = require(`../../../versions/${s3ClientName}@${version}`).get()
const clientVersion = getEquivVersion(s3ClientName, versionCtx)
AWS = require(`../../../versions/${s3ClientName}@${clientVersion}`).get()
s3 = new AWS.S3({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1', s3ForcePathStyle: true })
tracer = require('../../dd-trace')
})
Expand Down Expand Up @@ -225,7 +228,8 @@ describe('Plugin', () => {
})

before(() => {
AWS = require(`../../../versions/${s3ClientName}@${version}`).get()
const clientVersion = getEquivVersion(s3ClientName, versionCtx)
AWS = require(`../../../versions/${s3ClientName}@${clientVersion}`).get()
s3 = new AWS.S3({ endpoint: 'http://127.0.0.1:5000', region: 'us-east-1', s3ForcePathStyle: true })
tracer = require('../../dd-trace')
})
Expand Down Expand Up @@ -262,8 +266,10 @@ describe('Plugin', () => {
})

before(() => {
const { S3 } = require(`../../../versions/${s3ClientName}@${version}`).get()
const { SQS } = require(`../../../versions/${sqsClientName}@${version}`).get()
const s3Version = getEquivVersion(s3ClientName, versionCtx)
const { S3 } = require(`../../../versions/${s3ClientName}@${s3Version}`).get()
const sqsVersion = getEquivVersion(s3ClientName, versionCtx)
const { SQS } = require(`../../../versions/${sqsClientName}@${sqsVersion}`).get()

s3 = new S3({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1', s3ForcePathStyle: true })
sqs = new SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
Expand Down
8 changes: 6 additions & 2 deletions packages/datadog-plugin-aws-sdk/test/kinesis.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const agent = require('../../dd-trace/test/plugins/agent')
const { setup } = require('./spec_helpers')
const helpers = require('./kinesis_helpers')
const { rawExpectedSchema } = require('./kinesis-naming')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

describe('Kinesis', function () {
this.timeout(10000)
Expand All @@ -19,17 +20,20 @@ describe('Kinesis', function () {
const streamName = 'MyStream'
const streamNameDSM = 'MyStreamDSM'
const kinesisClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-kinesis' : 'aws-sdk'
const versionCtx = withVersions.context

function createResources (streamName, cb) {
AWS = require(`../../../versions/${kinesisClientName}@${version}`).get()
const clientVersion = getEquivVersion(kinesisClientName, versionCtx)
AWS = require(`../../../versions/${kinesisClientName}@${clientVersion}`).get()

const params = {
endpoint: 'http://127.0.0.1:4566',
region: 'us-east-1'
}

if (moduleName === '@aws-sdk/smithy-client') {
const { NodeHttpHandler } = require(`../../../versions/@aws-sdk/node-http-handler@${version}`).get()
const handlerVersion = getEquivVersion('@aws-sdk/node-http-handler', versionCtx)
const { NodeHttpHandler } = require(`../../../versions/@aws-sdk/node-http-handler@${handlerVersion}`).get()

params.requestHandler = new NodeHttpHandler()
}
Expand Down
5 changes: 4 additions & 1 deletion packages/datadog-plugin-aws-sdk/test/lambda.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const JSZip = require('jszip')
const agent = require('../../dd-trace/test/plugins/agent')
const { setup } = require('./spec_helpers')
const { rawExpectedSchema } = require('./lambda-naming')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

const zip = new JSZip()

Expand All @@ -19,6 +20,7 @@ describe('Plugin', () => {
let tracer

const lambdaClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-lambda' : 'aws-sdk'
const versionCtx = withVersions.context

const parsePayload = payload => {
if (typeof payload !== 'string') {
Expand All @@ -38,7 +40,8 @@ describe('Plugin', () => {
})

before(done => {
AWS = require(`../../../versions/${lambdaClientName}@${version}`).get()
const clientVersion = getEquivVersion(lambdaClientName, versionCtx)
AWS = require(`../../../versions/${lambdaClientName}@${clientVersion}`).get()

lambda = new AWS.Lambda({ endpoint: 'http://127.0.0.1:4567', region: 'us-east-1' })
lambda.createFunction({
Expand Down
6 changes: 5 additions & 1 deletion packages/datadog-plugin-aws-sdk/test/s3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { setup } = require('./spec_helpers')
const axios = require('axios')
const { rawExpectedSchema } = require('./s3-naming')
const { S3_PTR_KIND, SPAN_POINTER_DIRECTION } = require('../../dd-trace/src/constants')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

const bucketName = 's3-bucket-name-test'

Expand All @@ -27,6 +28,8 @@ describe('Plugin', () => {
let s3
let tracer

const versionCtx = withVersions.context

const s3ClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-s3' : 'aws-sdk'
describe('with configuration', () => {
before(() => {
Expand All @@ -36,7 +39,8 @@ describe('Plugin', () => {
})

before(done => {
AWS = require(`../../../versions/${s3ClientName}@${version}`).get()
const clientVersion = getEquivVersion(s3ClientName, versionCtx)
AWS = require(`../../../versions/${s3ClientName}@${clientVersion}`).get()
s3 = new AWS.S3({ endpoint: 'http://127.0.0.1:4566', s3ForcePathStyle: true, region: 'us-east-1' })

// Fix for LocationConstraint issue - only for SDK v2
Expand Down
8 changes: 6 additions & 2 deletions packages/datadog-plugin-aws-sdk/test/sns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { setup } = require('./spec_helpers')
const { rawExpectedSchema } = require('./sns-naming')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

describe('Sns', function () {
setup()
Expand All @@ -24,6 +25,7 @@ describe('Sns', function () {

const snsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sns' : 'aws-sdk'
const sqsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sqs' : 'aws-sdk'
const versionCtx = withVersions.context

let childSpansFound = 0
const assertPropagation = (done, childSpans = 1) => {
Expand All @@ -45,8 +47,10 @@ describe('Sns', function () {
}

function createResources (queueName, topicName, cb) {
const { SNS } = require(`../../../versions/${snsClientName}@${version}`).get()
const { SQS } = require(`../../../versions/${sqsClientName}@${version}`).get()
const snsVersion = getEquivVersion(snsClientName, versionCtx)
const { SNS } = require(`../../../versions/${snsClientName}@${snsVersion}`).get()
const sqsVersion = getEquivVersion(sqsClientName, versionCtx)
const { SQS } = require(`../../../versions/${sqsClientName}@${sqsVersion}`).get()

sns = new SNS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
sqs = new SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
Expand Down
14 changes: 10 additions & 4 deletions packages/datadog-plugin-aws-sdk/test/sqs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const agent = require('../../dd-trace/test/plugins/agent')
const { setup } = require('./spec_helpers')
const semver = require('semver')
const { rawExpectedSchema } = require('./sqs-naming')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

const queueName = 'SQS_QUEUE_NAME'
const queueNameDSM = 'SQS_QUEUE_NAME_DSM'
Expand Down Expand Up @@ -36,6 +37,7 @@ describe('Plugin', () => {
let tracer

const sqsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sqs' : 'aws-sdk'
const versionCtx = withVersions.context

describe('without configuration', () => {
before(() => {
Expand All @@ -49,7 +51,8 @@ describe('Plugin', () => {
})

before(done => {
AWS = require(`../../../versions/${sqsClientName}@${version}`).get()
const clientVersion = getEquivVersion(sqsClientName, versionCtx)
AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get()

sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
sqs.createQueue(queueOptions, (err, res) => {
Expand Down Expand Up @@ -314,7 +317,8 @@ describe('Plugin', () => {
})

before(done => {
AWS = require(`../../../versions/${sqsClientName}@${version}`).get()
const clientVersion = getEquivVersion(sqsClientName, versionCtx)
AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get()

sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
sqs.createQueue(queueOptions, (err, res) => {
Expand Down Expand Up @@ -405,7 +409,8 @@ describe('Plugin', () => {
})

before(done => {
AWS = require(`../../../versions/${sqsClientName}@${version}`).get()
const clientVersion = getEquivVersion(sqsClientName, versionCtx)
AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get()

sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
sqs.createQueue(queueOptionsDsm, (err, res) => {
Expand All @@ -416,7 +421,8 @@ describe('Plugin', () => {
})

before(done => {
AWS = require(`../../../versions/${sqsClientName}@${version}`).get()
const clientVersion = getEquivVersion(sqsClientName, versionCtx)
AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get()

sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
sqs.createQueue(queueOptionsDsmConsumerOnly, (err, res) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/datadog-plugin-aws-sdk/test/stepfunctions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { setup } = require('./spec_helpers')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

const helloWorldSMD = {
Comment: 'A Hello World example of the Amazon States Language using a Pass state',
Expand All @@ -23,6 +24,7 @@ describe('Sfn', () => {
withVersions('aws-sdk', ['aws-sdk', '@aws-sdk/smithy-client'], (version, moduleName) => {
let stateMachineArn
let client
const versionCtx = withVersions.context

setup()

Expand All @@ -33,7 +35,8 @@ describe('Sfn', () => {
function getClient () {
const params = { endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }
if (moduleName === '@aws-sdk/smithy-client') {
const lib = require(`../../../versions/@aws-sdk/client-sfn@${version}`).get()
const sfnVersion = getEquivVersion('@aws-sdk/client-sfn', versionCtx)
const lib = require(`../../../versions/@aws-sdk/client-sfn@${sfnVersion}`).get()
const client = new lib.SFNClient(params)
return {
client,
Expand Down
10 changes: 3 additions & 7 deletions packages/datadog-plugin-langchain/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { useEnv } = require('../../../integration-tests/helpers')
const agent = require('../../dd-trace/test/plugins/agent')
const { getIdeallyTestedVersions } = require('../../dd-trace/test/setup/helpers/version-utils')
const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils')

const nock = require('nock')

Expand Down Expand Up @@ -34,12 +34,8 @@ describe('Plugin', () => {

describe('langchain', () => {
withVersions('langchain', ['@langchain/core'], version => {
const coreVersions = getIdeallyTestedVersions('@langchain/core', [withVersions.range])
const isFirst = coreVersions[0].version === version
const openAiVersions = getIdeallyTestedVersions('@langchain/openai', [withVersions.range])
const openAiVersion = (openAiVersions[isFirst ? 0 : openAiVersions.length - 1]).version
const anthropicVersions = getIdeallyTestedVersions('@langchain/anthropic', [withVersions.range])
const anthropicVersion = (anthropicVersions[isFirst ? 0 : anthropicVersions.length - 1]).version
const openAiVersion = getEquivVersion('@langchain/openai', withVersions.context)
const anthropicVersion = getEquivVersion('@langchain/anthropic', withVersions.context)

beforeEach(() => {
return agent.load('langchain')
Expand Down
20 changes: 19 additions & 1 deletion packages/dd-trace/test/setup/helpers/version-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,27 @@ function getIdeallyTestedVersions (name, ranges) {
}, [])
}

// For a given module name, get the version of it corresponding to the same
// range as the currently tested module from `withVersions`. Note that this
// should only be used when a module and a support module have the same version
// range, but not necessarily the same versions within that range.
// You should always pass `withVersions.context` into this funciton as `orig`,
// and always capture it in the first scope inside the `withVersions` callback.
function getEquivVersion (name, orig) {
if (!orig) {
throw new Error('No context found to get equiv version')
}

const origVersions = getIdeallyTestedVersions(orig.moduleName, [orig.range])
const isFirst = origVersions[0].version === orig.version
const otherModuleVersions = getIdeallyTestedVersions(name, [orig.range])
return otherModuleVersions[isFirst ? 0 : otherModuleVersions.length - 1].version
}

module.exports = {
getAllVersions,
getAllVersionsMatching,
getIdeallyTestedVersionRanges,
getIdeallyTestedVersions
getIdeallyTestedVersions,
getEquivVersion
}
4 changes: 3 additions & 1 deletion packages/dd-trace/test/setup/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ function withVersions (plugin, modules, range, cb) {
require('module').Module._initPaths()
})

withVersions.range = v.range
const context = { moduleName, ...v }
withVersions.context = context
cb(v.version, moduleName, v.version) // TODO get rid of 3rd param here
delete withVersions.context

after(() => {
process.env.NODE_PATH = nodePath
Expand Down

0 comments on commit 88f4f36

Please sign in to comment.