Skip to content

chore(tracer): apply UA to AWS SDK clients used by Tracer #1575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/tracer/src/provider/ProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
setLogger,
Logger,
} from 'aws-xray-sdk-core';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

class ProviderService implements ProviderServiceInterface {
public captureAWS<T>(awssdk: T): T {
Expand All @@ -29,6 +30,8 @@ class ProviderService implements ProviderServiceInterface {
}

public captureAWSv3Client<T>(service: T): T {
addUserAgentMiddleware(service, 'tracer');

// Type must be aliased as any because of this https://github.com/aws/aws-xray-sdk-node/issues/439#issuecomment-859715660
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return captureAWSv3Client(service as any);
Expand Down
24 changes: 21 additions & 3 deletions packages/tracer/tests/unit/ProviderService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@

import { ProviderService } from '../../src/provider';
import {
captureAsyncFunc,
captureAWS,
captureAWSClient,
captureAWSv3Client,
captureAsyncFunc,
captureHTTPsGlobal,
captureFunc,
captureHTTPsGlobal,
getNamespace,
getSegment,
Segment,
setContextMissingStrategy,
setDaemonAddress,
setLogger,
setSegment,
Subsegment,
Segment,
} from 'aws-xray-sdk-core';
import http from 'http';
import https from 'https';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import * as UserAgentMiddleware from '@aws-lambda-powertools/commons/lib/userAgentMiddleware';

jest.mock('aws-xray-sdk-core', () => ({
...jest.requireActual('aws-xray-sdk-core'),
Expand Down Expand Up @@ -85,6 +87,22 @@ describe('Class: ProviderService', () => {
expect(captureAWSv3Client).toHaveBeenCalledTimes(1);
expect(captureAWSv3Client).toHaveBeenCalledWith({});
});

test('when called, it adds the correct user agent middleware', () => {
// Prepare
const provider: ProviderService = new ProviderService();

// spy on addUserAgentMiddleware
const spy = jest.spyOn(UserAgentMiddleware, 'addUserAgentMiddleware');

// Act
const dynamoDBClient = new DynamoDBClient({});
provider.captureAWSv3Client(dynamoDBClient);

// Assess
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(dynamoDBClient, 'tracer');
});
});

describe('Method: captureAsyncFunc', () => {
Expand Down