Skip to content

Commit

Permalink
fix(aws-sdk): correct setting error in attributes (open-telemetry#1495)
Browse files Browse the repository at this point in the history
* fix(aws-sdk): correct setting error in attributes

* fix(aws-sdk): record the exception

* fix(aws-sdk): repair tests

* fix(aws-sdk): improve tests

---------

Co-authored-by: Amir Blum <[email protected]>
  • Loading branch information
thib3113 and blumamir authored May 16, 2023
1 parent 3e5a3b1 commit 5f87026
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
5 changes: 2 additions & 3 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ In addition to the above attributes, the instrumentation also collect the follow
| `aws.service.identifier` | string | Identifier for the service in the sdk | "sqs" |
| `aws.service.name` | string | Abbreviation name for the service | "Amazon SQS" |
| `aws.request.id` | uuid | Request unique id, as returned from aws on response | "01234567-89ab-cdef-0123-456789abcdef" |
| `aws.error` | string | information about a service or networking error, as returned from AWS | "UriParameterError: Expected uri parameter to have length >= 1, but found "" for params.Bucket" |

### Custom User Attributes

The instrumentation user can configure a `preRequestHook` function which will be called before each request, with a normalized request object (across v2 and v3) and the corresponding span.
This hook can be used to add custom attributes to the span with any logic.
The instrumentation user can configure a `preRequestHook` function which will be called before each request, with a normalized request object (across v2 and v3) and the corresponding span.
This hook can be used to add custom attributes to the span with any logic.
For example, user can add interesting attributes from the `request.params`, and write custom logic based on the service and operation.
Usage example:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class AwsInstrumentation extends InstrumentationBase<any> {

self._callUserResponseHook(span, normalizedResponse);
if (response.error) {
span.setAttribute(AttributeNames.AWS_ERROR, response.error);
span.recordException(response.error);
} else {
this.servicesExtensions.responseHook(
normalizedResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
export enum AttributeNames {
AWS_ERROR = 'aws.error',
AWS_OPERATION = 'aws.operation',
AWS_REGION = 'aws.region',
AWS_SERVICE_API = 'aws.service.api',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { AttributeNames } from '../src/enums';
import { mockV2AwsSend } from './testing-utils';
import { expect } from 'expect';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { AWSError } from 'aws-sdk';
import { HttpResponse } from 'aws-sdk/lib/http_response';

describe('instrumentation-aws-sdk-v2', () => {
const responseMockSuccess = {
Expand All @@ -43,9 +45,20 @@ describe('instrumentation-aws-sdk-v2', () => {
},
};

const responseMockWithError = {
const error: AWSError = {
name: 'error',
message: 'something went wrong',
stack: 'fakeStack',
code: 'errorCode',
time: new Date(),
};

const responseMockWithError: Pick<
AWS.Response<any, AWSError>,
'requestId' | 'error'
> & { httpResponse: Partial<HttpResponse> } = {
requestId: '0000000000000',
error: 'something went wrong',
error,
httpResponse: {
statusCode: 400,
},
Expand Down Expand Up @@ -275,9 +288,22 @@ describe('instrumentation-aws-sdk-v2', () => {
const awsSpans = getAwsSpans();
expect(awsSpans.length).toBe(1);
const [spanCreateBucket] = awsSpans;
expect(spanCreateBucket.attributes[AttributeNames.AWS_ERROR]).toBe(
responseMockWithError.error
const exceptionEvent = spanCreateBucket.events.filter(
event => event.name === 'exception'
);
expect(exceptionEvent.length).toBe(1);

expect(exceptionEvent[0]).toStrictEqual(
expect.objectContaining({
name: 'exception',
attributes: {
'exception.message': 'something went wrong',
'exception.stacktrace': 'fakeStack',
'exception.type': 'errorCode',
},
})
);

expect(
spanCreateBucket.attributes[SemanticAttributes.HTTP_STATUS_CODE]
).toBe(400);
Expand Down

0 comments on commit 5f87026

Please sign in to comment.