Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jj22ee committed Aug 15, 2024
1 parent f3b9422 commit ce592e5
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export enum AttributeNames {
AWS_REQUEST_ID = 'aws.request.id',
AWS_REQUEST_EXTENDED_ID = 'aws.request.extended_id',
AWS_SIGNATURE_VERSION = 'aws.signature.version',

// TODO: Add these semantic attributes to:
// - https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-semantic-conventions/src/trace/SemanticAttributes.ts
// For S3, see specification: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/object-stores/s3.md
AWS_S3_BUCKET = 'aws.s3.bucket',
AWS_KINESIS_STREAM_NAME = 'aws.kinesis.stream.name',
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Attributes, Span, SpanKind, Tracer } from '@opentelemetry/api';
import {
AwsSdkInstrumentationConfig,
NormalizedRequest,
NormalizedResponse,
} from '../types';
import { _AWS_KINESIS_STREAM_NAME } from '../utils';
import { Attributes, SpanKind } from '@opentelemetry/api';
import { AttributeNames } from '../enums';
import { AwsSdkInstrumentationConfig, NormalizedRequest } from '../types';
import { RequestMetadata, ServiceExtension } from './ServiceExtension';

export class KinesisServiceExtension implements ServiceExtension {
requestPreSpanHook(
request: NormalizedRequest,
_config: AwsSdkInstrumentationConfig
): RequestMetadata {
const streamName = request.commandInput.StreamName;

const streamName = request.commandInput?.StreamName;
const spanKind: SpanKind = SpanKind.CLIENT;
let spanName: string | undefined;

const spanAttributes: Attributes = {};

if (streamName) {
spanAttributes[_AWS_KINESIS_STREAM_NAME] = streamName;
spanAttributes[AttributeNames.AWS_KINESIS_STREAM_NAME] = streamName;
}

const isIncoming = false;
Expand All @@ -44,16 +37,6 @@ export class KinesisServiceExtension implements ServiceExtension {
isIncoming,
spanAttributes,
spanKind,
spanName,
};
}

requestPostSpanHook = (request: NormalizedRequest) => {};

responseHook = (
response: NormalizedResponse,
span: Span,
tracer: Tracer,
config: AwsSdkInstrumentationConfig
) => {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Attributes, Span, SpanKind, Tracer } from '@opentelemetry/api';
import {
AwsSdkInstrumentationConfig,
NormalizedRequest,
NormalizedResponse,
} from '../types';
import { _AWS_S3_BUCKET } from '../utils';
import { Attributes, SpanKind } from '@opentelemetry/api';
import { AttributeNames } from '../enums';
import { AwsSdkInstrumentationConfig, NormalizedRequest } from '../types';
import { RequestMetadata, ServiceExtension } from './ServiceExtension';

export class S3ServiceExtension implements ServiceExtension {
requestPreSpanHook(
request: NormalizedRequest,
_config: AwsSdkInstrumentationConfig
): RequestMetadata {
const bucketName = request.commandInput.Bucket;

const bucketName = request.commandInput?.Bucket;
const spanKind: SpanKind = SpanKind.CLIENT;
let spanName: string | undefined;

const spanAttributes: Attributes = {};

if (bucketName) {
spanAttributes[_AWS_S3_BUCKET] = bucketName;
spanAttributes[AttributeNames.AWS_S3_BUCKET] = bucketName;
}

const isIncoming = false;
Expand All @@ -44,16 +37,6 @@ export class S3ServiceExtension implements ServiceExtension {
isIncoming,
spanAttributes,
spanKind,
spanName,
};
}

requestPostSpanHook = (request: NormalizedRequest) => {};

responseHook = (
response: NormalizedResponse,
span: Span,
tracer: Tracer,
config: AwsSdkInstrumentationConfig
) => {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NormalizedRequest } from './types';
import { Attributes, Context, context } from '@opentelemetry/api';
import {
SEMATTRS_RPC_METHOD,
SEMATTRS_RPC_SERVICE,
SEMATTRS_RPC_SYSTEM,
} from '@opentelemetry/semantic-conventions';
import { AttributeNames } from './enums';

// TODO: Add these semantic attributes to:
// - https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-semantic-conventions/src/trace/SemanticAttributes.ts
// For S3, see specification: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/object-stores/s3.md
export const _AWS_S3_BUCKET = 'aws.s3.bucket';
export const _AWS_KINESIS_STREAM_NAME = 'aws.kinesis.stream.name';
import { NormalizedRequest } from './types';

const toPascalCase = (str: string): string =>
typeof str === 'string' ? str.charAt(0).toUpperCase() + str.slice(1) : str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ import {
registerInstrumentationTesting,
} from '@opentelemetry/contrib-test-utils';
import { AwsInstrumentation } from '../src';
import { AttributeNames } from '../src/enums';
registerInstrumentationTesting(new AwsInstrumentation());

import { Kinesis } from '@aws-sdk/client-kinesis';
import * as AWS from 'aws-sdk';
import { AWSError } from 'aws-sdk';
import * as nock from 'nock';

import { SpanKind } from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import { expect } from 'expect';
import { _AWS_KINESIS_STREAM_NAME } from '../src/utils';

const region = 'us-east-1';

describe('Kinesis', () => {
describe('Kinesis - v2', () => {
let kinesis: AWS.Kinesis;
beforeEach(() => {
AWS.config.credentials = {
Expand All @@ -44,7 +45,7 @@ describe('Kinesis', () => {
};
});

describe('CreateStream', () => {
describe('DescribeStream', () => {
it('adds Stream Name', async () => {
kinesis = new AWS.Kinesis({ region: region });
const dummyStreamName = 'dummy-stream-name';
Expand All @@ -54,10 +55,9 @@ describe('Kinesis', () => {
.reply(200, 'null');

await kinesis
.createStream(
.describeStream(
{
StreamName: dummyStreamName,
ShardCount: 3,
},
(err: AWSError) => {
expect(err).toBeFalsy();
Expand All @@ -66,15 +66,55 @@ describe('Kinesis', () => {
.promise();

const testSpans = getTestSpans();
const creationSpans = testSpans.filter((s: ReadableSpan) => {
return s.name === 'Kinesis.CreateStream';
const describeSpans = testSpans.filter((s: ReadableSpan) => {
return s.name === 'Kinesis.DescribeStream';
});
expect(creationSpans.length).toBe(1);
const creationSpan = creationSpans[0];
expect(creationSpan.attributes[_AWS_KINESIS_STREAM_NAME]).toBe(
dummyStreamName
expect(describeSpans.length).toBe(1);
const describeSpan = describeSpans[0];
expect(
describeSpan.attributes[AttributeNames.AWS_KINESIS_STREAM_NAME]
).toBe(dummyStreamName);
expect(describeSpan.kind).toBe(SpanKind.CLIENT);
});
});
});

describe('Kinesis - v3', () => {
let kinesis: Kinesis;
beforeEach(() => {
kinesis = new Kinesis({
region: region,
credentials: {
accessKeyId: 'abcde',
secretAccessKey: 'abcde',
},
});
});

describe('DescribeStream', () => {
it('adds Stream Name', async () => {
const dummyStreamName = 'dummy-stream-name';

nock(`https://kinesis.${region}.amazonaws.com/`).post('/').reply(200, {});

await kinesis
.describeStream({
StreamName: dummyStreamName,
})
.catch((err: any) => {});

const testSpans: ReadableSpan[] = getTestSpans();
const describeSpans: ReadableSpan[] = testSpans.filter(
(s: ReadableSpan) => {
return s.name === 'Kinesis.DescribeStream';
}
);
expect(creationSpan.kind).toBe(SpanKind.CLIENT);
expect(describeSpans.length).toBe(1);
const describeSpan = describeSpans[0];
expect(
describeSpan.attributes[AttributeNames.AWS_KINESIS_STREAM_NAME]
).toBe(dummyStreamName);
expect(describeSpan.kind).toBe(SpanKind.CLIENT);
});
});
});
49 changes: 46 additions & 3 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/test/s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ import {
registerInstrumentationTesting,
} from '@opentelemetry/contrib-test-utils';
import { AwsInstrumentation } from '../src';
import { AttributeNames } from '../src/enums';
registerInstrumentationTesting(new AwsInstrumentation());

import { S3 } from '@aws-sdk/client-s3';
import * as AWS from 'aws-sdk';
import { AWSError } from 'aws-sdk';
import * as nock from 'nock';

import { SpanKind } from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import { expect } from 'expect';
import { _AWS_S3_BUCKET } from '../src/utils';

const region = 'us-east-1';

describe('S3', () => {
describe('S3 - v2', () => {
let s3: AWS.S3;
beforeEach(() => {
AWS.config.credentials = {
Expand Down Expand Up @@ -68,7 +69,49 @@ describe('S3', () => {
});
expect(listObjectsSpans.length).toBe(1);
const listObjectsSpan = listObjectsSpans[0];
expect(listObjectsSpan.attributes[_AWS_S3_BUCKET]).toBe(dummyBucketName);
expect(listObjectsSpan.attributes[AttributeNames.AWS_S3_BUCKET]).toBe(
dummyBucketName
);
expect(listObjectsSpan.kind).toBe(SpanKind.CLIENT);
});
});
});

describe('S3 - v3', () => {
let s3: S3;
beforeEach(() => {
s3 = new S3({
region: region,
credentials: {
accessKeyId: 'abcde',
secretAccessKey: 'abcde',
},
});
});

describe('ListObjects', () => {
it('adds bucket Name', async () => {
const dummyBucketName = 'dummy-bucket-name';

nock(`https://s3.${region}.amazonaws.com/`).post('/').reply(200, 'null');

await s3
.listObjects({
Bucket: dummyBucketName,
})
.catch((err: any) => {});

const testSpans: ReadableSpan[] = getTestSpans();
const listObjectsSpans: ReadableSpan[] = testSpans.filter(
(s: ReadableSpan) => {
return s.name === 'S3.ListObjects';
}
);
expect(listObjectsSpans.length).toBe(1);
const listObjectsSpan = listObjectsSpans[0];
expect(listObjectsSpan.attributes[AttributeNames.AWS_S3_BUCKET]).toBe(
dummyBucketName
);
expect(listObjectsSpan.kind).toBe(SpanKind.CLIENT);
});
});
Expand Down

0 comments on commit ce592e5

Please sign in to comment.