-
Notifications
You must be signed in to change notification settings - Fork 13
Add Patching Mechanism with AWS SDK telemetry improvements #13
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
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2d45ca1
Add Patching Mechanism with AWS SDK telemetry improvements
jj22ee 01af484
Merge branch 'main' into adot-js-patching-mechanism-pr
jj22ee 6f1396b
Merge branch 'main' into adot-js-patching-mechanism-pr
jj22ee 06abcf0
merge fix for constructor update
jj22ee fbe0cc8
address comments
jj22ee 4dee0b2
remove duplicate test in register.test.ts
jj22ee 15dba8c
undo sample app package.json update, regenerate package-lock.json
jj22ee 5264ce5
update instrumentation-patch.test unit test
jj22ee f373792
address comments, merge aws-sdk test apps into one
jj22ee 61dce47
use GetQueueAttributes command for SQS in test app
jj22ee 84f9f4e
add missing expectations for queue name
jj22ee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...istro-opentelemetry-node-autoinstrumentation/src/patches/aws/services/ServiceExtension.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import { DiagLogger, Span, SpanAttributes, SpanKind, Tracer } from '@opentelemetry/api'; | ||
import { | ||
AwsSdkInstrumentationConfig, | ||
NormalizedRequest, | ||
NormalizedResponse, | ||
} from '@opentelemetry/instrumentation-aws-sdk'; | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// The OpenTelemetry Authors code | ||
// We need to copy these interfaces that are not exported by Opentelemetry | ||
export interface RequestMetadata { | ||
// isIncoming - if true, then the operation callback / promise should be bind with the operation's span | ||
isIncoming: boolean; | ||
spanAttributes?: SpanAttributes; | ||
spanKind?: SpanKind; | ||
spanName?: string; | ||
} | ||
|
||
export interface ServiceExtension { | ||
// called before request is sent, and before span is started | ||
requestPreSpanHook: ( | ||
request: NormalizedRequest, | ||
config: AwsSdkInstrumentationConfig, | ||
diag: DiagLogger | ||
) => RequestMetadata; | ||
|
||
// called before request is sent, and after span is started | ||
requestPostSpanHook?: (request: NormalizedRequest) => void; | ||
|
||
responseHook?: ( | ||
response: NormalizedResponse, | ||
span: Span, | ||
tracer: Tracer, | ||
config: AwsSdkInstrumentationConfig | ||
) => void; | ||
} |
5 changes: 5 additions & 0 deletions
5
aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services/index.ts
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { KinesisServiceExtension } from './kinesis'; | ||
export { S3ServiceExtension } from './s3'; |
44 changes: 44 additions & 0 deletions
44
aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services/kinesis.ts
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Attributes, Span, SpanKind, Tracer } from '@opentelemetry/api'; | ||
import { | ||
AwsSdkInstrumentationConfig, | ||
NormalizedRequest, | ||
NormalizedResponse, | ||
} from '@opentelemetry/instrumentation-aws-sdk'; | ||
import { RequestMetadata, ServiceExtension } from './ServiceExtension'; | ||
|
||
// The OpenTelemetry Authors code | ||
// This file's contents are being contributed to upstream | ||
// - https://github.com/open-telemetry/opentelemetry-js-contrib/pull/2361 | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const _AWS_KINESIS_STREAM_NAME = 'aws.kinesis.stream.name'; | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export class KinesisServiceExtension implements ServiceExtension { | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
requestPreSpanHook(request: NormalizedRequest, _config: AwsSdkInstrumentationConfig): RequestMetadata { | ||
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; | ||
} | ||
|
||
const isIncoming = false; | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
isIncoming, | ||
spanAttributes, | ||
spanKind, | ||
spanName, | ||
}; | ||
} | ||
|
||
requestPostSpanHook = (request: NormalizedRequest) => {}; | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
responseHook = (response: NormalizedResponse, span: Span, tracer: Tracer, config: AwsSdkInstrumentationConfig) => {}; | ||
} |
44 changes: 44 additions & 0 deletions
44
aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services/s3.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Attributes, Span, SpanKind, Tracer } from '@opentelemetry/api'; | ||
import { | ||
AwsSdkInstrumentationConfig, | ||
NormalizedRequest, | ||
NormalizedResponse, | ||
} from '@opentelemetry/instrumentation-aws-sdk'; | ||
import { RequestMetadata, ServiceExtension } from './ServiceExtension'; | ||
|
||
// The OpenTelemetry Authors code | ||
// This file's contents are being contributed to upstream | ||
// - https://github.com/open-telemetry/opentelemetry-js-contrib/pull/2361 | ||
|
||
const _AWS_S3_BUCKET = 'aws.s3.bucket'; | ||
|
||
export class S3ServiceExtension implements ServiceExtension { | ||
requestPreSpanHook(request: NormalizedRequest, _config: AwsSdkInstrumentationConfig): RequestMetadata { | ||
const bucketName = request.commandInput.Bucket; | ||
|
||
const spanKind: SpanKind = SpanKind.CLIENT; | ||
let spanName: string | undefined; | ||
|
||
const spanAttributes: Attributes = {}; | ||
|
||
if (bucketName) { | ||
spanAttributes[_AWS_S3_BUCKET] = bucketName; | ||
} | ||
|
||
const isIncoming = false; | ||
|
||
return { | ||
isIncoming, | ||
spanAttributes, | ||
spanKind, | ||
spanName, | ||
}; | ||
} | ||
|
||
requestPostSpanHook = (request: NormalizedRequest) => {}; | ||
|
||
responseHook = (response: NormalizedResponse, span: Span, tracer: Tracer, config: AwsSdkInstrumentationConfig) => {}; | ||
} |
30 changes: 30 additions & 0 deletions
30
aws-distro-opentelemetry-node-autoinstrumentation/src/patches/instrumentation-patch.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Instrumentation } from '@opentelemetry/instrumentation'; | ||
import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk'; | ||
import { KinesisServiceExtension, S3ServiceExtension } from './aws/services'; | ||
|
||
export function applyInstrumentationPatches(instrumentations: Instrumentation[]): void { | ||
/* | ||
Apply patches to upstream instrumentation libraries. | ||
|
||
This method is invoked to apply changes to upstream instrumentation libraries, typically when changes to upstream | ||
are required on a timeline that cannot wait for upstream release. Generally speaking, patches should be short-term | ||
local solutions that are comparable to long-term upstream solutions. | ||
|
||
Where possible, automated testing should be run to catch upstream changes resulting in broken patches | ||
*/ | ||
instrumentations.forEach(instrumentation => { | ||
if (instrumentation.instrumentationName === '@opentelemetry/instrumentation-aws-sdk') { | ||
// Access private property servicesExtensions of AwsInstrumentation | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const services = (instrumentation as AwsInstrumentation).servicesExtensions?.services; | ||
if (services) { | ||
services.set('S3', new S3ServiceExtension()); | ||
services.set('Kinesis', new KinesisServiceExtension()); | ||
} | ||
} | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
aws-distro-opentelemetry-node-autoinstrumentation/test/patches/instrumentation-patch.test.ts
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; | ||
import { Instrumentation } from '@opentelemetry/instrumentation'; | ||
import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk'; | ||
import { expect } from 'expect'; | ||
import { applyInstrumentationPatches } from './../../src/patches/instrumentation-patch'; | ||
|
||
describe('InstrumentationPatchTest', () => { | ||
it('PatchAwsSdkInstrumentation', () => { | ||
const instrumentations: Instrumentation[] = getNodeAutoInstrumentations(); | ||
applyInstrumentationPatches(instrumentations); | ||
|
||
const filteredInstrumentations: Instrumentation[] = instrumentations.filter( | ||
instrumentation => instrumentation.instrumentationName === '@opentelemetry/instrumentation-aws-sdk' | ||
); | ||
expect(filteredInstrumentations.length).toEqual(1); | ||
|
||
const awsSdkInstrumentation = filteredInstrumentations[0]; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const services: Map<string, ServiceExtension> = (awsSdkInstrumentation as AwsInstrumentation).servicesExtensions | ||
?.services; | ||
// Not from patching | ||
expect(services.has('SQS')).toBeTruthy(); | ||
expect(services.has('SNS')).toBeTruthy(); | ||
expect(services.has('DynamoDB')).toBeTruthy(); | ||
expect(services.has('Lambda')).toBeTruthy(); | ||
// From patching | ||
expect(services.has('S3')).toBeTruthy(); | ||
expect(services.has('Kinesis')).toBeTruthy(); | ||
// Sanity check | ||
expect(services.has('InvalidService')).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.