1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617import {
1718 AwsInstrumentation ,
1819 AwsSdkRequestHookInformation ,
@@ -31,39 +32,28 @@ import {
3132 S3 ,
3233 S3Client ,
3334} from '@aws-sdk/client-s3' ;
34- import { SQS } from '@aws-sdk/client-sqs' ;
35- import { propagation , SpanKind } from '@opentelemetry/api' ;
35+ import { SpanKind } from '@opentelemetry/api' ;
3636
3737// set aws environment variables, so tests in non aws environment are able to run
3838process . env . AWS_ACCESS_KEY_ID = 'testing' ;
3939process . env . AWS_SECRET_ACCESS_KEY = 'testing' ;
4040
4141import 'mocha' ;
42- import { ReadableSpan } from '@opentelemetry/sdk-trace-base' ;
43- import { context , SpanStatusCode , trace , Span } from '@opentelemetry/api' ;
42+ import { SpanStatusCode , Span } from '@opentelemetry/api' ;
4443import {
45- ATTR_URL_FULL ,
46- MESSAGINGOPERATIONVALUES_RECEIVE ,
4744 SEMATTRS_HTTP_STATUS_CODE ,
48- SEMATTRS_MESSAGING_OPERATION ,
49- SEMATTRS_MESSAGING_SYSTEM ,
5045 SEMATTRS_RPC_METHOD ,
5146 SEMATTRS_RPC_SERVICE ,
5247 SEMATTRS_RPC_SYSTEM ,
5348} from '@opentelemetry/semantic-conventions' ;
54- import {
55- ATTR_MESSAGING_BATCH_MESSAGE_COUNT ,
56- ATTR_MESSAGING_DESTINATION_NAME ,
57- ATTR_MESSAGING_MESSAGE_ID ,
58- } from '../src/semconv' ;
5949import { AttributeNames } from '../src/enums' ;
6050import { expect } from 'expect' ;
6151import * as fs from 'fs' ;
6252import * as nock from 'nock' ;
6353
6454const region = 'us-east-1' ;
6555
66- describe ( 'instrumentation-aws-sdk-v3' , ( ) => {
56+ describe ( 'instrumentation-aws-sdk-v3 (client-s3) ' , ( ) => {
6757 const s3Client = new S3 ( { region } ) ;
6858
6959 describe ( 'functional' , ( ) => {
@@ -286,202 +276,4 @@ describe('instrumentation-aws-sdk-v3', () => {
286276 } ) ;
287277 } ) ;
288278 } ) ;
289-
290- describe ( 'custom service behavior' , ( ) => {
291- describe ( 'SQS' , ( ) => {
292- const sqsClient = new SQS ( { region } ) ;
293-
294- it ( 'sqs send add messaging attributes' , async ( ) => {
295- nock ( `https://sqs.${ region } .amazonaws.com/` )
296- . matchHeader ( 'content-type' , 'application/x-www-form-urlencoded' )
297- . post ( '/' )
298- . reply (
299- 200 ,
300- fs . readFileSync ( './test/mock-responses/sqs-send.xml' , 'utf8' )
301- ) ;
302- // @aws -sdk/client-sqs >=3.446.0 uses a new JSON protocol.
303- nock ( `https://sqs.${ region } .amazonaws.com/` )
304- . matchHeader ( 'content-type' , 'application/x-amz-json-1.0' )
305- . post ( '/' )
306- . reply (
307- 200 ,
308- fs . readFileSync ( './test/mock-responses/sqs-send.json' , 'utf8' )
309- ) ;
310-
311- const params = {
312- QueueUrl :
313- 'https://sqs.us-east-1.amazonaws.com/731241200085/otel-demo-aws-sdk' ,
314- MessageBody : 'payload example from v3 without batch' ,
315- } ;
316- const response = await sqsClient . sendMessage ( params ) ;
317- expect ( getTestSpans ( ) . length ) . toBe ( 1 ) ;
318- const [ span ] = getTestSpans ( ) ;
319-
320- // make sure we have the general aws attributes:
321- expect ( span . attributes [ SEMATTRS_RPC_SYSTEM ] ) . toEqual ( 'aws-api' ) ;
322- expect ( span . attributes [ SEMATTRS_RPC_METHOD ] ) . toEqual ( 'SendMessage' ) ;
323- expect ( span . attributes [ SEMATTRS_RPC_SERVICE ] ) . toEqual ( 'SQS' ) ;
324- expect ( span . attributes [ AttributeNames . AWS_REGION ] ) . toEqual ( region ) ;
325-
326- // custom messaging attributes
327- expect ( span . attributes [ SEMATTRS_MESSAGING_SYSTEM ] ) . toEqual ( 'aws_sqs' ) ;
328- expect ( span . attributes [ ATTR_MESSAGING_DESTINATION_NAME ] ) . toEqual (
329- 'otel-demo-aws-sdk'
330- ) ;
331- expect ( span . attributes [ ATTR_URL_FULL ] ) . toEqual ( params . QueueUrl ) ;
332- expect ( span . attributes [ ATTR_MESSAGING_MESSAGE_ID ] ) . toEqual (
333- response . MessageId
334- ) ;
335- expect ( span . attributes [ SEMATTRS_HTTP_STATUS_CODE ] ) . toEqual ( 200 ) ;
336- } ) ;
337-
338- it ( 'sqs send message batch attributes' , async ( ) => {
339- nock ( `https://sqs.${ region } .amazonaws.com/` )
340- . matchHeader ( 'content-type' , 'application/x-www-form-urlencoded' )
341- . post ( '/' )
342- . reply (
343- 200 ,
344- fs . readFileSync ( './test/mock-responses/sqs-send-batch.xml' , 'utf8' )
345- ) ;
346- nock ( `https://sqs.${ region } .amazonaws.com/` )
347- . matchHeader ( 'content-type' , 'application/x-amz-json-1.0' )
348- . post ( '/' )
349- . reply (
350- 200 ,
351- fs . readFileSync ( './test/mock-responses/sqs-send-batch.json' , 'utf8' )
352- ) ;
353-
354- const params = {
355- QueueUrl :
356- 'https://sqs.us-east-1.amazonaws.com/731241200085/otel-demo-aws-sdk' ,
357- MessageBody : 'payload example from v3 without batch' ,
358- Entries : [
359- {
360- Id : '1000' ,
361- MessageBody : 'msg body for 1000' ,
362- } ,
363- {
364- Id : '1001' ,
365- MessageBody : 'msg body for 1001' ,
366- } ,
367- ] ,
368- } ;
369- await sqsClient . sendMessageBatch ( params ) ;
370- expect ( getTestSpans ( ) . length ) . toBe ( 1 ) ;
371- const [ span ] = getTestSpans ( ) ;
372-
373- // make sure we have the general aws attributes:
374- expect ( span . attributes [ SEMATTRS_RPC_SYSTEM ] ) . toEqual ( 'aws-api' ) ;
375- expect ( span . attributes [ SEMATTRS_RPC_METHOD ] ) . toEqual (
376- 'SendMessageBatch'
377- ) ;
378- expect ( span . attributes [ SEMATTRS_RPC_SERVICE ] ) . toEqual ( 'SQS' ) ;
379- expect ( span . attributes [ AttributeNames . AWS_REGION ] ) . toEqual ( region ) ;
380-
381- // messaging semantic attributes
382- expect ( span . attributes [ SEMATTRS_MESSAGING_SYSTEM ] ) . toEqual ( 'aws_sqs' ) ;
383- expect ( span . attributes [ ATTR_MESSAGING_DESTINATION_NAME ] ) . toEqual (
384- 'otel-demo-aws-sdk'
385- ) ;
386- expect ( span . attributes [ ATTR_URL_FULL ] ) . toEqual ( params . QueueUrl ) ;
387- expect ( span . attributes [ SEMATTRS_HTTP_STATUS_CODE ] ) . toEqual ( 200 ) ;
388- } ) ;
389-
390- it ( 'sqs receive add messaging attributes' , done => {
391- nock ( `https://sqs.${ region } .amazonaws.com/` )
392- . matchHeader ( 'content-type' , 'application/x-www-form-urlencoded' )
393- . post ( '/' )
394- . reply (
395- 200 ,
396- fs . readFileSync ( './test/mock-responses/sqs-receive.xml' , 'utf8' )
397- ) ;
398- nock ( `https://sqs.${ region } .amazonaws.com/` )
399- . matchHeader ( 'content-type' , 'application/x-amz-json-1.0' )
400- . post ( '/' )
401- . reply (
402- 200 ,
403- fs . readFileSync ( './test/mock-responses/sqs-receive.json' , 'utf8' )
404- ) ;
405-
406- const params = {
407- QueueUrl :
408- 'https://sqs.us-east-1.amazonaws.com/731241200085/otel-demo-aws-sdk' ,
409- MaxNumberOfMessages : 3 ,
410- } ;
411- sqsClient . receiveMessage ( params ) . then ( res => {
412- expect ( getTestSpans ( ) . length ) . toBe ( 1 ) ;
413- const [ span ] = getTestSpans ( ) ;
414-
415- // make sure we have the general aws attributes:
416- expect ( span . attributes [ SEMATTRS_RPC_SYSTEM ] ) . toEqual ( 'aws-api' ) ;
417- expect ( span . attributes [ SEMATTRS_RPC_METHOD ] ) . toEqual (
418- 'ReceiveMessage'
419- ) ;
420- expect ( span . attributes [ SEMATTRS_RPC_SERVICE ] ) . toEqual ( 'SQS' ) ;
421- expect ( span . attributes [ AttributeNames . AWS_REGION ] ) . toEqual ( region ) ;
422- expect ( span . attributes [ SEMATTRS_HTTP_STATUS_CODE ] ) . toEqual ( 200 ) ;
423- expect ( span . attributes [ ATTR_MESSAGING_BATCH_MESSAGE_COUNT ] ) . toEqual (
424- 2
425- ) ;
426- expect ( span . links . length ) . toBe ( 2 ) ;
427-
428- const messages = res . Messages || [ ] ;
429- expect ( messages . length ) . toEqual ( span . links . length ) ;
430-
431- for ( let i = 0 ; i < span . links . length ; i ++ ) {
432- const link = span . links [ i ] ;
433- const messageId = messages [ i ] . MessageId ;
434- const traceparent =
435- messages [ i ] . MessageAttributes ?. traceparent . StringValue ?. split (
436- '-'
437- ) || [ ] ;
438- const traceId = traceparent [ 1 ] ;
439- const spanId = traceparent [ 2 ] ;
440- expect ( link . attributes ?. [ ATTR_MESSAGING_MESSAGE_ID ] ) . toEqual (
441- messageId
442- ) ;
443- expect ( link . context . traceId ) . toEqual ( traceId ) ;
444- expect ( link . context . spanId ) . toEqual ( spanId ) ;
445- }
446- done ( ) ;
447- } ) ;
448- } ) ;
449-
450- // Propagating span context to SQS ReceiveMessage promise handler is
451- // broken with `@aws-sdk/client-sqs` v3.316.0 and later.
452- // https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1477
453- it . skip ( 'sqs receive context' , done => {
454- nock ( `https://sqs.${ region } .amazonaws.com/` )
455- . matchHeader ( 'content-type' , 'application/x-www-form-urlencoded' )
456- . post ( '/' )
457- . reply (
458- 200 ,
459- fs . readFileSync ( './test/mock-responses/sqs-receive.xml' , 'utf8' )
460- ) ;
461- nock ( `https://sqs.${ region } .amazonaws.com/` )
462- . matchHeader ( 'content-type' , 'application/x-amz-json-1.0' )
463- . post ( '/' )
464- . reply (
465- 200 ,
466- fs . readFileSync ( './test/mock-responses/sqs-receive.json' , 'utf8' )
467- ) ;
468-
469- const params = {
470- QueueUrl :
471- 'https://sqs.us-east-1.amazonaws.com/731241200085/otel-demo-aws-sdk' ,
472- MaxNumberOfMessages : 3 ,
473- } ;
474- sqsClient . receiveMessage ( params ) . then ( res => {
475- const receiveCallbackSpan = trace . getSpan ( context . active ( ) ) ;
476- expect ( receiveCallbackSpan ) . toBeDefined ( ) ;
477- const attributes = ( receiveCallbackSpan as unknown as ReadableSpan )
478- . attributes ;
479- expect ( attributes [ SEMATTRS_MESSAGING_OPERATION ] ) . toMatch (
480- MESSAGINGOPERATIONVALUES_RECEIVE
481- ) ;
482- done ( ) ;
483- } ) ;
484- } ) ;
485- } ) ;
486- } ) ;
487279} ) ;
0 commit comments