| 
 | 1 | +/* eslint-disable max-len */  | 
 | 2 | +'use strict'  | 
 | 3 | + | 
 | 4 | +const semver = require('semver')  | 
 | 5 | +const agent = require('../../dd-trace/test/plugins/agent')  | 
 | 6 | +const { setup } = require('./spec_helpers')  | 
 | 7 | + | 
 | 8 | +const helloWorldSMD = {  | 
 | 9 | +  Comment: 'A Hello World example of the Amazon States Language using a Pass state',  | 
 | 10 | +  StartAt: 'HelloWorld',  | 
 | 11 | +  States: {  | 
 | 12 | +    HelloWorld: {  | 
 | 13 | +      Type: 'Pass',  | 
 | 14 | +      Result: 'Hello World!',  | 
 | 15 | +      End: true  | 
 | 16 | +    }  | 
 | 17 | +  }  | 
 | 18 | +}  | 
 | 19 | + | 
 | 20 | +describe('Sfn', () => {  | 
 | 21 | +  let tracer  | 
 | 22 | + | 
 | 23 | +  withVersions('aws-sdk', ['aws-sdk', '@aws-sdk/smithy-client'], (version, moduleName) => {  | 
 | 24 | +    let stateMachineArn  | 
 | 25 | +    let client  | 
 | 26 | + | 
 | 27 | +    setup()  | 
 | 28 | + | 
 | 29 | +    before(() => {  | 
 | 30 | +      client = getClient()  | 
 | 31 | +    })  | 
 | 32 | + | 
 | 33 | +    function getClient () {  | 
 | 34 | +      const params = { endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }  | 
 | 35 | +      if (moduleName === '@aws-sdk/smithy-client') {  | 
 | 36 | +        const lib = require(`../../../versions/@aws-sdk/client-sfn@${version}`).get()  | 
 | 37 | +        const client = new lib.SFNClient(params)  | 
 | 38 | +        return {  | 
 | 39 | +          client,  | 
 | 40 | +          createStateMachine: function () {  | 
 | 41 | +            const req = new lib.CreateStateMachineCommand(...arguments)  | 
 | 42 | +            return client.send(req)  | 
 | 43 | +          },  | 
 | 44 | +          deleteStateMachine: function () {  | 
 | 45 | +            const req = new lib.DeleteStateMachineCommand(...arguments)  | 
 | 46 | +            return client.send(req)  | 
 | 47 | +          },  | 
 | 48 | +          startExecution: function () {  | 
 | 49 | +            const req = new lib.StartExecutionCommand(...arguments)  | 
 | 50 | +            return client.send(req)  | 
 | 51 | +          },  | 
 | 52 | +          describeExecution: function () {  | 
 | 53 | +            const req = new lib.DescribeExecutionCommand(...arguments)  | 
 | 54 | +            return client.send(req)  | 
 | 55 | +          }  | 
 | 56 | +        }  | 
 | 57 | +      } else {  | 
 | 58 | +        const { StepFunctions } = require(`../../../versions/aws-sdk@${version}`).get()  | 
 | 59 | +        const client = new StepFunctions(params)  | 
 | 60 | +        return {  | 
 | 61 | +          client,  | 
 | 62 | +          createStateMachine: function () { return client.createStateMachine(...arguments).promise() },  | 
 | 63 | +          deleteStateMachine: function () {  | 
 | 64 | +            return client.deleteStateMachine(...arguments).promise()  | 
 | 65 | +          },  | 
 | 66 | +          startExecution: function () { return client.startExecution(...arguments).promise() },  | 
 | 67 | +          describeExecution: function () { return client.describeExecution(...arguments).promise() }  | 
 | 68 | +        }  | 
 | 69 | +      }  | 
 | 70 | +    }  | 
 | 71 | + | 
 | 72 | +    async function createStateMachine (name, definition, xargs) {  | 
 | 73 | +      return client.createStateMachine({  | 
 | 74 | +        definition: JSON.stringify(definition),  | 
 | 75 | +        name: name,  | 
 | 76 | +        roleArn: 'arn:aws:iam::123456:role/test',  | 
 | 77 | +        ...xargs  | 
 | 78 | +      })  | 
 | 79 | +    }  | 
 | 80 | + | 
 | 81 | +    async function deleteStateMachine (arn) {  | 
 | 82 | +      return client.deleteStateMachine({ stateMachineArn: arn })  | 
 | 83 | +    }  | 
 | 84 | + | 
 | 85 | +    describe('Traces', () => {  | 
 | 86 | +      before(() => {  | 
 | 87 | +        tracer = require('../../dd-trace')  | 
 | 88 | +        tracer.use('aws-sdk')  | 
 | 89 | +      })  | 
 | 90 | +      // aws-sdk v2 doesn't support StepFunctions below 2.7.10  | 
 | 91 | +      // https://github.com/aws/aws-sdk-js/blob/5dba638fd/CHANGELOG.md?plain=1#L18  | 
 | 92 | +      if (moduleName !== 'aws-sdk' || semver.intersects(version, '>=2.7.10')) {  | 
 | 93 | +        beforeEach(() => { return agent.load('aws-sdk') })  | 
 | 94 | +        beforeEach(async () => {  | 
 | 95 | +          const data = await createStateMachine('helloWorld', helloWorldSMD, {})  | 
 | 96 | +          stateMachineArn = data.stateMachineArn  | 
 | 97 | +        })  | 
 | 98 | + | 
 | 99 | +        afterEach(() => { return agent.close({ ritmReset: false }) })  | 
 | 100 | + | 
 | 101 | +        afterEach(async () => {  | 
 | 102 | +          await deleteStateMachine(stateMachineArn)  | 
 | 103 | +        })  | 
 | 104 | + | 
 | 105 | +        it('is instrumented', async function () {  | 
 | 106 | +          const startExecInput = {  | 
 | 107 | +            stateMachineArn,  | 
 | 108 | +            input: JSON.stringify({ moduleName })  | 
 | 109 | +          }  | 
 | 110 | +          const expectSpanPromise = agent.use(traces => {  | 
 | 111 | +            const span = traces[0][0]  | 
 | 112 | +            expect(span).to.have.property('resource', 'startExecution')  | 
 | 113 | +            expect(span.meta).to.have.property('statemachinearn', stateMachineArn)  | 
 | 114 | +          })  | 
 | 115 | + | 
 | 116 | +          const resp = await client.startExecution(startExecInput)  | 
 | 117 | + | 
 | 118 | +          const result = await client.describeExecution({ executionArn: resp.executionArn })  | 
 | 119 | +          const sfInput = JSON.parse(result.input)  | 
 | 120 | +          expect(sfInput).to.have.property('_datadog')  | 
 | 121 | +          expect(sfInput._datadog).to.have.property('x-datadog-trace-id')  | 
 | 122 | +          expect(sfInput._datadog).to.have.property('x-datadog-parent-id')  | 
 | 123 | +          return expectSpanPromise.then(() => {})  | 
 | 124 | +        })  | 
 | 125 | +      }  | 
 | 126 | +    })  | 
 | 127 | +  })  | 
 | 128 | +})  | 
0 commit comments