Skip to content

Commit

Permalink
fix: avoid type imports of the aws-sdk package in the built assets (#…
Browse files Browse the repository at this point in the history
…1066)

* fix: remove aws-sdk types from exports

* style: disable lint rules for reasons

* fix: keep `typeof AWS` in non-public API

* chore: add AWS copyright notice
  • Loading branch information
rauno56 authored Sep 13, 2022
1 parent 954f29c commit 457be50
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type V2PluginRequest = AWS.Request<any, any> & {
[REQUEST_SPAN_KEY]?: Span;
};

export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
export class AwsInstrumentation extends InstrumentationBase<any> {
static readonly component = 'aws-sdk';
protected override _config!: AwsSdkInstrumentationConfig;
private servicesExtensions: ServicesExtensions = new ServicesExtensions();
Expand Down Expand Up @@ -177,7 +177,7 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
return moduleExports;
}

protected patchV2(moduleExports: typeof AWS, moduleVersion?: string) {
protected patchV2(moduleExports: any, moduleVersion?: string) {
diag.debug(
`aws-sdk instrumentation: applying patch to ${AwsInstrumentation.component}`
);
Expand All @@ -196,7 +196,7 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
return moduleExports;
}

protected unpatchV2(moduleExports?: typeof AWS) {
protected unpatchV2(moduleExports?: any) {
if (isWrapped(moduleExports?.Request.prototype.send)) {
this._unwrap(moduleExports!.Request.prototype, 'send');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* AWS SDK for JavaScript
* Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This product includes software developed at
* Amazon Web Services, Inc. (http://aws.amazon.com/).
*/

/*
These are slightly modified and simplified versions of the actual SQS types included
in the official distribution:
https://github.com/aws/aws-sdk-js/blob/master/clients/sqs.d.ts
These are brought here to avoid having users install the `aws-sdk` whenever they
require this instrumentation.
*/

// eslint-disable-next-line @typescript-eslint/no-empty-interface -- Our lint doesn't like it, but we prefer to keep it the way original source code has it
interface Blob {}
type Binary = Buffer | Uint8Array | Blob | string;

// eslint-disable-next-line @typescript-eslint/no-namespace -- Prefer to contain the types copied over in one location
export namespace SNS {
interface MessageAttributeValue {
/**
* Amazon SNS supports the following logical data types: String, String.Array, Number, and Binary. For more information, see Message Attribute Data Types.
*/
DataType: string;
/**
* Strings are Unicode with UTF8 binary encoding. For a list of code values, see ASCII Printable Characters.
*/
StringValue?: string;
/**
* Binary type attributes can store any binary data, for example, compressed data, encrypted data, or images.
*/
BinaryValue?: Binary;
}

export type MessageAttributeMap = { [key: string]: MessageAttributeValue };
}

// eslint-disable-next-line @typescript-eslint/no-namespace -- Prefer to contain the types copied over in one location
export namespace SQS {
type StringList = string[];
type BinaryList = Binary[];
interface MessageAttributeValue {
/**
* Strings are Unicode with UTF-8 binary encoding. For a list of code values, see ASCII Printable Characters.
*/
StringValue?: string;
/**
* Binary type attributes can store any binary data, such as compressed data, encrypted data, or images.
*/
BinaryValue?: Binary;
/**
* Not implemented. Reserved for future use.
*/
StringListValues?: StringList;
/**
* Not implemented. Reserved for future use.
*/
BinaryListValues?: BinaryList;
/**
* Amazon SQS supports the following logical data types: String, Number, and Binary. For the Number data type, you must use StringValue. You can also append custom labels. For more information, see Amazon SQS Message Attributes in the Amazon SQS Developer Guide.
*/
DataType: string;
}

export type MessageBodyAttributeMap = {
[key: string]: MessageAttributeValue;
};

type MessageSystemAttributeMap = { [key: string]: string };

export interface Message {
/**
* A unique identifier for the message. A MessageIdis considered unique across all accounts for an extended period of time.
*/
MessageId?: string;
/**
* An identifier associated with the act of receiving the message. A new receipt handle is returned every time you receive a message. When deleting a message, you provide the last received receipt handle to delete the message.
*/
ReceiptHandle?: string;
/**
* An MD5 digest of the non-URL-encoded message body string.
*/
MD5OfBody?: string;
/**
* The message's contents (not URL-encoded).
*/
Body?: string;
/**
* A map of the attributes requested in ReceiveMessage to their respective values. Supported attributes: ApproximateReceiveCount ApproximateFirstReceiveTimestamp MessageDeduplicationId MessageGroupId SenderId SentTimestamp SequenceNumber ApproximateFirstReceiveTimestamp and SentTimestamp are each returned as an integer representing the epoch time in milliseconds.
*/
Attributes?: MessageSystemAttributeMap;
/**
* An MD5 digest of the non-URL-encoded message attribute string. You can use this attribute to verify that Amazon SQS received the message correctly. Amazon SQS URL-decodes the message before creating the MD5 digest. For information about MD5, see RFC1321.
*/
MD5OfMessageAttributes?: string;
/**
* Each message attribute consists of a Name, Type, and Value. For more information, see Amazon SQS message attributes in the Amazon SQS Developer Guide.
*/
MessageAttributes?: MessageBodyAttributeMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
propagation,
diag,
} from '@opentelemetry/api';
import type { SQS, SNS } from 'aws-sdk';
import type { SQS, SNS } from '../aws-sdk.types';

// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html
export const MAX_MESSAGE_ATTRIBUTES = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { Span } from '@opentelemetry/api';
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
import type * as AWS from 'aws-sdk';
import { SQS } from './aws-sdk.types';

/**
* These are normalized request and response, which are used by both sdk v2 and v3.
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface AwsSdkResponseCustomAttributeFunction {
}

export interface AwsSdkSqsProcessHookInformation {
message: AWS.SQS.Message;
message: SQS.Message;
}
export interface AwsSdkSqsProcessCustomAttributeFunction {
(span: Span, sqsProcessInfo: AwsSdkSqsProcessHookInformation): void;
Expand Down
11 changes: 4 additions & 7 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
import { NormalizedRequest } from './types';
import type { Request } from 'aws-sdk';
import { Context, SpanAttributes, context } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { AttributeNames } from './enums';
Expand All @@ -32,14 +31,12 @@ export const removeSuffixFromStringIfExists = (
: str;
};

export const normalizeV2Request = (
awsV2Request: Request<any, any>
): NormalizedRequest => {
const service = (awsV2Request as any)?.service;
export const normalizeV2Request = (awsV2Request: any): NormalizedRequest => {
const service = awsV2Request?.service;
return {
serviceName: service?.api?.serviceId?.replace(/\s+/g, ''),
commandName: toPascalCase((awsV2Request as any)?.operation),
commandInput: (awsV2Request as any).params,
commandName: toPascalCase(awsV2Request?.operation),
commandInput: awsV2Request.params,
region: service?.config?.region,
};
};
Expand Down

0 comments on commit 457be50

Please sign in to comment.