generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 15
SigV4 Authentication support for http/protobuf exporter #156
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 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8600230
SigV4 Support
liustve f9cca5b
added escaped '.'
liustve d84b956
added unit testing, made dependencies optional
liustve f89d088
returning early to stop appsignals metrics
liustve fa97573
added proxyquire
liustve c68866a
lint fix
liustve 165078c
fix async issue
liustve 695182a
added node version detection
liustve 37e3d77
adding test coverage ignore if node version less than 16
liustve 4465b86
linting fix
liustve 0490762
removed it.skip() test
liustve caa75d9
removed it.skip() for nodejs environments < 16
liustve 18d0b54
excluding otlp-aws-span-exporter.ts from test coverage
liustve 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
82 changes: 82 additions & 0 deletions
82
aws-distro-opentelemetry-node-autoinstrumentation/src/otlp-aws-span-exporter.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,82 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; | ||
| import { diag } from '@opentelemetry/api'; | ||
| import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base'; | ||
| import { ExportResult } from '@opentelemetry/core'; | ||
| import { defaultProvider } from '@aws-sdk/credential-provider-node'; | ||
| import { Sha256 } from '@aws-crypto/sha256-js'; | ||
| import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; | ||
| import { SignatureV4 } from '@smithy/signature-v4'; | ||
| import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer'; | ||
| import { HttpRequest } from '@smithy/protocol-http'; | ||
|
|
||
| const SERVICE_NAME = 'xray'; | ||
majanjua-amzn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * This exporter extends the functionality of the OTLPProtoTraceExporter to allow spans to be exported | ||
| * to the XRay OTLP endpoint https://xray.[AWSRegion].amazonaws.com/v1/traces. Utilizes the aws-sdk | ||
| * library to sign and directly inject SigV4 Authentication to the exported request's headers. <a | ||
| * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html">...</a> | ||
| */ | ||
| export class OTLPAwsSpanExporter extends OTLPProtoTraceExporter { | ||
| private endpoint: string; | ||
| private region: string; | ||
|
|
||
| constructor(endpoint: string, config?: OTLPExporterNodeConfigBase) { | ||
| super(config); | ||
| this.region = endpoint.split('.')[1]; | ||
| this.endpoint = endpoint; | ||
| } | ||
|
|
||
| /** | ||
| * Overrides the upstream implementation of export. All behaviors are the same except if the | ||
| * endpoint is an XRay OTLP endpoint, we will sign the request with SigV4 in headers before | ||
| * sending it to the endpoint. Otherwise, we will skip signing. | ||
| */ | ||
| public override async export(items: ReadableSpan[], resultCallback: (result: ExportResult) => void): Promise<void> { | ||
| const url = new URL(this.endpoint); | ||
| const serializedSpans: Uint8Array | undefined = ProtobufTraceSerializer.serializeRequest(items); | ||
|
|
||
| if (serializedSpans === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| /* | ||
| This is bad practice but there is no other way to access and inject SigV4 headers | ||
| into the request headers before the traces get exported. | ||
| */ | ||
| const oldHeaders = (this as any)._transport._transport._parameters.headers; | ||
majanjua-amzn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const request = new HttpRequest({ | ||
| method: 'POST', | ||
| protocol: 'https', | ||
| hostname: url.hostname, | ||
| path: url.pathname, | ||
| body: serializedSpans, | ||
| headers: { | ||
| ...oldHeaders, | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| host: url.hostname, | ||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| const signer = new SignatureV4({ | ||
| credentials: defaultProvider(), | ||
| region: this.region, | ||
| service: SERVICE_NAME, | ||
| sha256: Sha256, | ||
| }); | ||
|
|
||
| const signedRequest = await signer.sign(request); | ||
|
|
||
| (this as any)._transport._transport._parameters.headers = signedRequest.headers; | ||
liustve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (exception) { | ||
| diag.debug( | ||
| `Failed to sign/authenticate the given exported Span request to OTLP XRay endpoint with error: ${exception}` | ||
| ); | ||
| } | ||
|
|
||
| await super.export(items, resultCallback); | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.