Skip to content

Commit

Permalink
feat(aws-sdk): add http status code attribute to aws sdk span (open-t…
Browse files Browse the repository at this point in the history
…elemetry#844)

* feat(aws-sdk): add http status code attribute to aws sdk span

* chore: pr comment to improve coding style
  • Loading branch information
Yaniv Davidi authored Jan 24, 2022
1 parent 7032a33 commit 09b8555
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
removeSuffixFromStringIfExists,
} from './utils';
import { RequestMetadata } from './services/ServiceExtension';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

const V3_CLIENT_CONFIG_KEY = Symbol(
'opentelemetry.instrumentation.aws-sdk.client.config'
Expand Down Expand Up @@ -328,6 +329,14 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
}

span.setAttribute(AttributeNames.AWS_REQUEST_ID, response.requestId);

const httpStatusCode = response.httpResponse?.statusCode;
if (httpStatusCode) {
span.setAttribute(
SemanticAttributes.HTTP_STATUS_CODE,
httpStatusCode
);
}
span.end();
});
});
Expand Down Expand Up @@ -472,6 +481,16 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
if (requestId) {
span.setAttribute(AttributeNames.AWS_REQUEST_ID, requestId);
}

const httpStatusCode =
response.output?.$metadata?.httpStatusCode;
if (httpStatusCode) {
span.setAttribute(
SemanticAttributes.HTTP_STATUS_CODE,
httpStatusCode
);
}

const extendedRequestId =
response.output?.$metadata?.extendedRequestId;
if (extendedRequestId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ import { SpanStatusCode, Span } from '@opentelemetry/api';
import { AttributeNames } from '../src/enums';
import { mockV2AwsSend } from './testing-utils';
import * as expect from 'expect';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

describe('instrumentation-aws-sdk-v2', () => {
const responseMockSuccess = {
requestId: '0000000000000',
error: null,
httpResponse: {
statusCode: 200,
},
};

const responseMockWithError = {
requestId: '0000000000000',
error: 'something went wrong',
httpResponse: {
statusCode: 400,
},
};

const getAwsSpans = (): ReadableSpan[] => {
Expand Down Expand Up @@ -111,6 +118,9 @@ describe('instrumentation-aws-sdk-v2', () => {
expect(spanCreateBucket.attributes[AttributeNames.AWS_REGION]).toBe(
'us-east-1'
);
expect(
spanCreateBucket.attributes[SemanticAttributes.HTTP_STATUS_CODE]
).toBe(200);

expect(spanCreateBucket.name).toBe('S3.CreateBucket');

Expand All @@ -136,6 +146,9 @@ describe('instrumentation-aws-sdk-v2', () => {
'us-east-1'
);
expect(spanPutObject.name).toBe('S3.PutObject');
expect(
spanPutObject.attributes[SemanticAttributes.HTTP_STATUS_CODE]
).toBe(200);
});

it('adds proper number of spans with correct attributes if both, promise and callback were used', async () => {
Expand Down Expand Up @@ -184,6 +197,9 @@ describe('instrumentation-aws-sdk-v2', () => {
expect(spanPutObjectCb.attributes[AttributeNames.AWS_REGION]).toBe(
'us-east-1'
);
expect(
spanPutObjectCb.attributes[SemanticAttributes.HTTP_STATUS_CODE]
).toBe(200);
});

it('adds proper number of spans with correct attributes if only promise was used', async () => {
Expand Down Expand Up @@ -219,6 +235,9 @@ describe('instrumentation-aws-sdk-v2', () => {
expect(spanPutObjectCb.attributes[AttributeNames.AWS_REGION]).toBe(
'us-east-1'
);
expect(
spanPutObjectCb.attributes[SemanticAttributes.HTTP_STATUS_CODE]
).toBe(200);
});

it('should create span if no callback is supplied', done => {
Expand Down Expand Up @@ -259,6 +278,9 @@ describe('instrumentation-aws-sdk-v2', () => {
expect(spanCreateBucket.attributes[AttributeNames.AWS_ERROR]).toBe(
responseMockWithError.error
);
expect(
spanCreateBucket.attributes[SemanticAttributes.HTTP_STATUS_CODE]
).toBe(400);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('instrumentation-aws-sdk-v3', () => {
expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('S3');
expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region);
expect(span.name).toEqual('S3.PutObject');
expect(span.attributes[SemanticAttributes.HTTP_STATUS_CODE]).toEqual(200);
});

it('callback interface', done => {
Expand All @@ -104,6 +105,9 @@ describe('instrumentation-aws-sdk-v3', () => {
expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('S3');
expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region);
expect(span.name).toEqual('S3.PutObject');
expect(span.attributes[SemanticAttributes.HTTP_STATUS_CODE]).toEqual(
200
);
done();
});
});
Expand Down Expand Up @@ -131,6 +135,7 @@ describe('instrumentation-aws-sdk-v3', () => {
expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('S3');
expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region);
expect(span.name).toEqual('S3.PutObject');
expect(span.attributes[SemanticAttributes.HTTP_STATUS_CODE]).toEqual(200);
});

it('aws error', async () => {
Expand Down Expand Up @@ -315,6 +320,9 @@ describe('instrumentation-aws-sdk-v3', () => {
expect(span.attributes[SemanticAttributes.MESSAGING_URL]).toEqual(
params.QueueUrl
);
expect(span.attributes[SemanticAttributes.HTTP_STATUS_CODE]).toEqual(
200
);
});

it('sqs receive add messaging attributes and context', done => {
Expand Down Expand Up @@ -353,6 +361,9 @@ describe('instrumentation-aws-sdk-v3', () => {
expect(attributes[SemanticAttributes.MESSAGING_OPERATION]).toMatch(
MessagingOperationValues.RECEIVE
);
expect(span.attributes[SemanticAttributes.HTTP_STATUS_CODE]).toEqual(
200
);
done();
});
});
Expand Down

0 comments on commit 09b8555

Please sign in to comment.