-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(server-utils): Add orchestrion aws-sdk channel integration core #22142
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
Draft
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9747e2e
feat(server-utils): Add orchestrion aws-sdk channel integration core
andreiborza 19006ed
Set sentry.op explicitly on aws-sdk channel spans
andreiborza 8220e0e
Make responseHook contract explicit about in-place response mutation
andreiborza a8a601f
Document why in-place response mutation is safe through the channel
andreiborza 6e0ed9c
Hold span end for pending region resolution
andreiborza 70c9d09
Read extendedRequestId off the error object with metadata fallback
andreiborza a36b11d
Use CLOUD_REGION from @sentry/conventions
andreiborza 4eced1d
Use underscore in span origin: auto.aws.orchestrion.aws_sdk
andreiborza 1227294
Guard region provider call against synchronous throws
andreiborza 92b04fc
Trace commands constructed without an input object
andreiborza 9510b15
Document and narrow any usage in core aws-sdk files
andreiborza 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
Some comments aren't visible on the classic Files Changed page.
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
17 changes: 17 additions & 0 deletions
17
packages/server-utils/src/integrations/tracing-channel/aws-sdk/constants.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,17 @@ | ||
| /** | ||
| * AWS-specific span attribute names used by the aws-sdk channel integration. | ||
| * | ||
| * These mirror the constants the OTel `@opentelemetry/instrumentation-aws-sdk` emits (some are | ||
| * unstable/obsolete OTel semantic conventions with no `ATTR_*` export in | ||
| * `@opentelemetry/semantic-conventions`), inlined here so the integration stays free of OTel deps. | ||
| * Attributes that exist in `@sentry/conventions/attributes` are imported from there instead; | ||
| * TODO(aws-sdk): the active attributes below are being added to sentry-conventions and should move | ||
| * to `@sentry/conventions/attributes` imports once a release containing them ships. | ||
| */ | ||
|
|
||
| /** The span origin every aws-sdk channel span carries, mirroring the uniform OTel `auto.otel.aws`. */ | ||
| export const AWS_SDK_ORIGIN = 'auto.aws.orchestrion.aws_sdk'; | ||
|
|
||
| export const ATTR_RPC_SYSTEM = 'rpc.system'; | ||
| export const AWS_REQUEST_ID = 'aws.request.id'; | ||
| export const AWS_REQUEST_EXTENDED_ID = 'aws.request.extended_id'; |
255 changes: 255 additions & 0 deletions
255
packages/server-utils/src/integrations/tracing-channel/aws-sdk/index.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,255 @@ | ||
| import * as diagnosticsChannel from 'node:diagnostics_channel'; | ||
| import type { IntegrationFn, Span } from '@sentry/core'; | ||
| import { | ||
| debug, | ||
| defineIntegration, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
| SPAN_KIND, | ||
| startInactiveSpan, | ||
| waitForTracingChannelBinding, | ||
| } from '@sentry/core'; | ||
| import { CLOUD_REGION, HTTP_STATUS_CODE } from '@sentry/conventions/attributes'; | ||
| import { DEBUG_BUILD } from '../../../debug-build'; | ||
| import { CHANNELS } from '../../../orchestrion/channels'; | ||
| import type { TracingChannelLifeCycleOptions } from '../../../tracing-channel'; | ||
| import { bindTracingChannelToSpan } from '../../../tracing-channel'; | ||
| import { AWS_REQUEST_EXTENDED_ID, AWS_REQUEST_ID, AWS_SDK_ORIGIN } from './constants'; | ||
| import { ServicesExtensions } from './services'; | ||
| import type { NormalizedRequest, NormalizedResponse, RequestMetadata } from './types'; | ||
| import { extractAttributesFromNormalizedRequest, normalizeV3Request, removeSuffixFromStringIfExists } from './utils'; | ||
|
|
||
| // Same name as the OTel `Aws` integration by design, so enabling injection swaps this in for it. | ||
| const INTEGRATION_NAME = 'Aws' as const; | ||
|
|
||
| // The context orchestrion's transform attaches to the channel: `arguments` is the live args of the | ||
| // wrapped `Client.prototype.send` call (`[command, ...]`), `self` the client, `result`/`error` the | ||
| // settled value. The `_sentry*` fields are stashed by us across the call's lifecycle. | ||
| interface AwsSendChannelContext { | ||
| arguments: unknown[]; | ||
| self?: { config?: AwsClientConfig; constructor?: { name?: string } }; | ||
| result?: unknown; | ||
| error?: unknown; | ||
| _sentryNormalizedRequest?: NormalizedRequest; | ||
| _sentryRequestMetadata?: RequestMetadata; | ||
| _sentryRegion?: { settled: boolean; promise: Promise<void> }; | ||
| } | ||
|
|
||
| interface AwsClientConfig { | ||
| serviceId?: string; | ||
| region?: () => string | Promise<string> | undefined; | ||
| } | ||
|
|
||
| interface AwsV3Command { | ||
| input?: Record<string, unknown>; | ||
| constructor?: { name?: string }; | ||
| } | ||
|
|
||
| /** Runs a span-building callback so a throw inside it can never break the user's aws-sdk call. */ | ||
| function safe<T>(fn: () => T): T | undefined { | ||
| try { | ||
| return fn(); | ||
| } catch (error) { | ||
| DEBUG_BUILD && debug.warn('[orchestrion:aws-sdk] error building span', error); | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| // `metadata` is smithy's `ResponseMetadata`, read off the untyped channel result/error (`any` for the | ||
| // same reason as `CommandInput`, see types.ts). | ||
| function setMetadataAttributes(span: Span, metadata: Record<string, any> | undefined): void { | ||
| if (!metadata) { | ||
| return; | ||
| } | ||
| if (metadata.requestId) { | ||
| span.setAttribute(AWS_REQUEST_ID, metadata.requestId); | ||
| } | ||
| if (metadata.httpStatusCode) { | ||
| // oxlint-disable-next-line typescript/no-deprecated | ||
| span.setAttribute(HTTP_STATUS_CODE, metadata.httpStatusCode); | ||
| } | ||
| if (metadata.extendedRequestId) { | ||
| span.setAttribute(AWS_REQUEST_EXTENDED_ID, metadata.extendedRequestId); | ||
| } | ||
| } | ||
|
|
||
| const _awsChannelIntegration = (() => { | ||
| const servicesExtensions = new ServicesExtensions(); | ||
|
|
||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setupOnce() { | ||
| // `tracingChannel` is unavailable before Node 18.19 so do nothing in that case. | ||
| if (!diagnosticsChannel.tracingChannel) { | ||
| return; | ||
| } | ||
|
|
||
| const getSpan = (data: AwsSendChannelContext): Span | undefined => | ||
| safe(() => { | ||
| const command = data.arguments[0] as AwsV3Command | undefined; | ||
| const commandName = command?.constructor?.name; | ||
| if (!command || !commandName) { | ||
| // Not a recognizable v3 command call; leave the active context untouched. | ||
| return undefined; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const clientConfig = data.self?.config; | ||
| const serviceName = | ||
| clientConfig?.serviceId ?? | ||
| // `clientName` isn't available at the `send` boundary; fall back to the client's | ||
| // constructor name (e.g. `S3Client` -> `S3`). `serviceId` is set for all AWS clients. | ||
| removeSuffixFromStringIfExists(data.self?.constructor?.name || 'AWS', 'Client'); | ||
|
|
||
| // Commands with all-optional members can be constructed without an input (`new | ||
| // ListBucketsCommand()`); the OTel path traces those too, so default rather than bail. | ||
| const normalizedRequest = normalizeV3Request(serviceName, commandName, command.input ?? {}, undefined); | ||
| const requestMetadata = servicesExtensions.requestPreSpanHook(normalizedRequest); | ||
|
|
||
| const span = startInactiveSpan({ | ||
| name: requestMetadata.spanName ?? `${normalizedRequest.serviceName}.${normalizedRequest.commandName}`, | ||
| kind: requestMetadata.spanKind ?? SPAN_KIND.CLIENT, | ||
| // `rpc` matches what the exporter infers from `rpc.service` for the OTel aws-sdk spans; | ||
| // service extensions override it where inference yields a different op (DynamoDB: `db`). | ||
| op: requestMetadata.spanOp ?? 'rpc', | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: AWS_SDK_ORIGIN, | ||
| ...extractAttributesFromNormalizedRequest(normalizedRequest), | ||
| ...requestMetadata.spanAttributes, | ||
| }, | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| data._sentryNormalizedRequest = normalizedRequest; | ||
| data._sentryRequestMetadata = requestMetadata; | ||
|
|
||
| // `region` resolves asynchronously while `send` proceeds (a channel subscriber cannot delay | ||
| // the traced call the way the OTel middleware does). Backfill it onto the span and the | ||
| // normalized request once available; `deferSpanEnd` holds the span open until this settles | ||
| // so `cloud.region` cannot be lost when `send` settles first (e.g. an early failure). | ||
| // | ||
| // The provider call is guarded separately: the span is already started, so a synchronous | ||
| // throw bubbling into the enclosing `safe` would discard it without ending it (a leaked | ||
| // open span). | ||
| let regionResult: string | Promise<string> | undefined; | ||
| try { | ||
| regionResult = clientConfig?.region?.(); | ||
| } catch { | ||
| // Nothing to do; continue without a region. | ||
| } | ||
| const regionHolder = { settled: false, promise: Promise.resolve() }; | ||
| regionHolder.promise = Promise.resolve(regionResult) | ||
| .then(region => { | ||
| if (region) { | ||
| normalizedRequest.region = region; | ||
| span.setAttribute(CLOUD_REGION, region); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| // Nothing to do; continue without a region. | ||
| }) | ||
| .finally(() => { | ||
| regionHolder.settled = true; | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| data._sentryRegion = regionHolder; | ||
|
|
||
| // Inject trace-propagation headers into outgoing messages (SQS/SNS/Lambda). Runs before | ||
| // `send` proceeds, so the mutated `commandInput` is used to build the request. | ||
| safe(() => servicesExtensions.requestPostSpanHook(normalizedRequest, span)); | ||
|
|
||
| return span; | ||
| }); | ||
|
|
||
| const opts: TracingChannelLifeCycleOptions<AwsSendChannelContext> = { | ||
| deferSpanEnd({ span, data, end }) { | ||
| const normalizedRequest = data._sentryNormalizedRequest; | ||
| const requestMetadata = data._sentryRequestMetadata; | ||
| if (!normalizedRequest) { | ||
| return false; | ||
| } | ||
|
|
||
| const failed = 'error' in data; | ||
|
|
||
| // The channel `result`/`error` are untyped; the `$metadata` casts below name smithy's | ||
| // `ResponseMetadata` shape (`any`-valued, see `setMetadataAttributes`). | ||
| safe(() => { | ||
| if (failed) { | ||
| const err = data.error as | ||
| | { $metadata?: Record<string, any>; RequestId?: string; extendedRequestId?: string } | ||
| | undefined; | ||
| const errMetadata = err?.$metadata; | ||
| // Like the OTel path, read RequestId/extendedRequestId off the error itself, with | ||
| // `$metadata` (which smithy service errors also carry) as the fallback. A spread won't | ||
| // do: `$metadata` includes these keys with `undefined` values, clobbering the fallback. | ||
| setMetadataAttributes(span, { | ||
| requestId: err?.RequestId ?? errMetadata?.requestId, | ||
| httpStatusCode: errMetadata?.httpStatusCode, | ||
| extendedRequestId: err?.extendedRequestId ?? errMetadata?.extendedRequestId, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const output = data.result as { $metadata?: Record<string, any> } | undefined; | ||
| setMetadataAttributes(span, output?.$metadata); | ||
|
|
||
| const normalizedResponse: NormalizedResponse = { | ||
| data: output, | ||
| request: normalizedRequest, | ||
| requestId: output?.$metadata?.requestId, | ||
| }; | ||
| servicesExtensions.responseHook(normalizedResponse, span); | ||
| }); | ||
|
|
||
| // Streaming responses end the span when their wrapped stream is consumed (see | ||
| // bedrock-runtime); the helper must not end it on `send` settling. Errors always end here. | ||
| if (requestMetadata?.isStream && !failed) { | ||
| return true; | ||
| } | ||
|
|
||
| // Normally the region settles long before `send` does (the SDK awaits it internally to | ||
| // build the endpoint), but when `send` settles first (e.g. an early failure) hold the span | ||
| // open until the region backfill lands. The error status was already applied by the | ||
| // helper's `error` subscriber, so a plain `end()` suffices. | ||
| const region = data._sentryRegion; | ||
| if (region && !region.settled) { | ||
| void region.promise.then(() => end()); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| }, | ||
| }; | ||
|
|
||
| // The AWS SDK's `Client.prototype.send` lives in different smithy packages across versions; the | ||
| // transform injects one channel per package. Only the package hosting the app's client fires, so | ||
| // subscribing to all of them is safe and never double-instruments a single call. | ||
| const awsSendChannels = [ | ||
| CHANNELS.AWS_SMITHY_CORE_SEND, | ||
| CHANNELS.AWS_SMITHY_CLIENT_SEND, | ||
| CHANNELS.AWS_SDK_SMITHY_CLIENT_SEND, | ||
| ] as const; | ||
|
|
||
| DEBUG_BUILD && debug.log(`[orchestrion:aws-sdk] subscribing to channels "${awsSendChannels.join('", "')}"`); | ||
|
|
||
| waitForTracingChannelBinding(() => { | ||
| for (const channelName of awsSendChannels) { | ||
| bindTracingChannelToSpan( | ||
| diagnosticsChannel.tracingChannel<AwsSendChannelContext>(channelName), | ||
| getSpan, | ||
| opts, | ||
| ); | ||
| } | ||
| }); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * EXPERIMENTAL — orchestrion-driven aws-sdk (v3) integration. | ||
| * | ||
| * Subscribes to the `orchestrion:@smithy/smithy-client:send` (and equivalent) diagnostics_channel | ||
| * the orchestrion code transform injects into the AWS SDK's smithy `Client.prototype.send`, emitting | ||
| * spans identical to the OTel `@opentelemetry/instrumentation-aws-sdk` integration (with a distinct | ||
| * `auto.aws.orchestrion.aws_sdk` origin). Requires the orchestrion runtime hook or bundler plugin — | ||
| * wire it up via `experimentalUseDiagnosticsChannelInjection()`. | ||
| * | ||
| * @experimental | ||
| */ | ||
| export const awsChannelIntegration = defineIntegration(_awsChannelIntegration); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
22 changes: 22 additions & 0 deletions
22
packages/server-utils/src/integrations/tracing-channel/aws-sdk/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,22 @@ | ||
| import type { Span } from '@sentry/core'; | ||
| import type { NormalizedRequest, NormalizedResponse, RequestMetadata } from '../types'; | ||
|
|
||
| export type { RequestMetadata }; | ||
|
|
||
| export interface ServiceExtension { | ||
| // called before the request is sent, and before the span is started | ||
| requestPreSpanHook: (request: NormalizedRequest) => RequestMetadata; | ||
|
|
||
| // called before the request is sent, and after the span is started. `span` is the started span, | ||
| // used to derive trace-propagation headers injected into outgoing messages. | ||
| requestPostSpanHook?: (request: NormalizedRequest, span: Span) => void; | ||
|
|
||
| // Called after the response is received. Unlike the OTel middleware patch, a tracing-channel | ||
| // subscriber cannot replace the value the caller's promise resolves with: the injected settle | ||
| // handler returns the captured result, not `ctx.result`. It does however publish `asyncEnd` | ||
| // synchronously BEFORE the caller's continuations run, and `response.data` is the same object the | ||
| // caller receives, so extensions that need to alter the response (e.g. wrap a stream) must mutate | ||
| // `response.data` in place; the mutation is guaranteed to be visible to the caller. Same idiom as | ||
| // the vercel-ai subscribers' `result.stream` tap. | ||
| responseHook?: (response: NormalizedResponse, span: Span) => void; | ||
| } |
31 changes: 31 additions & 0 deletions
31
...ages/server-utils/src/integrations/tracing-channel/aws-sdk/services/ServicesExtensions.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,31 @@ | ||
| import type { Span } from '@sentry/core'; | ||
| import type { NormalizedRequest, NormalizedResponse, RequestMetadata } from '../types'; | ||
| import type { ServiceExtension } from './ServiceExtension'; | ||
|
|
||
| export class ServicesExtensions implements ServiceExtension { | ||
| private _services: Map<string, ServiceExtension>; | ||
|
|
||
| public constructor() { | ||
| // Per-service extensions, keyed by the client's `serviceId` (e.g. `'S3'`). Services without a | ||
| // registered extension still get the base rpc span from the subscriber. | ||
| this._services = new Map(); | ||
| } | ||
|
andreiborza marked this conversation as resolved.
|
||
|
|
||
| public requestPreSpanHook(request: NormalizedRequest): RequestMetadata { | ||
| const serviceExtension = this._services.get(request.serviceName); | ||
| if (!serviceExtension) { | ||
| return {}; | ||
| } | ||
| return serviceExtension.requestPreSpanHook(request); | ||
| } | ||
|
|
||
| public requestPostSpanHook(request: NormalizedRequest, span: Span): void { | ||
| const serviceExtension = this._services.get(request.serviceName); | ||
| serviceExtension?.requestPostSpanHook?.(request, span); | ||
| } | ||
|
|
||
| public responseHook(response: NormalizedResponse, span: Span): void { | ||
| const serviceExtension = this._services.get(response.request.serviceName); | ||
| serviceExtension?.responseHook?.(response, span); | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
packages/server-utils/src/integrations/tracing-channel/aws-sdk/services/index.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 @@ | ||
| export { ServicesExtensions } from './ServicesExtensions'; |
36 changes: 36 additions & 0 deletions
36
packages/server-utils/src/integrations/tracing-channel/aws-sdk/types.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,36 @@ | ||
| import type { SpanKindValue } from '@sentry/core'; | ||
|
|
||
| // Command inputs are service-specific shapes from hundreds of AWS APIs; typing them would require | ||
| // depending on the `@aws-sdk/*` client types. The per-service hooks read fields defensively instead. | ||
| export type CommandInput = Record<string, any>; | ||
|
|
||
| /** | ||
| * These are normalized request and response. They organize the relevant data in one interface which | ||
| * can be processed in a uniform manner in the per-service hooks. | ||
| */ | ||
| export interface NormalizedRequest { | ||
| serviceName: string; | ||
| commandName: string; | ||
| commandInput: CommandInput; | ||
| region?: string; | ||
| } | ||
|
|
||
| export interface NormalizedResponse { | ||
| // The command output, shaped per service/command (see `CommandInput` on why this isn't typed). | ||
| data: any; | ||
| request: NormalizedRequest; | ||
| requestId?: string; | ||
| } | ||
|
|
||
| /** Span metadata a per-service extension returns for the subscriber to build the span from. */ | ||
| export interface RequestMetadata { | ||
| // If true, then the response is a stream so the subscriber must not end the span when `send` settles. | ||
| // The service extension ends the span itself, generally by wrapping the stream and ending after it is | ||
| // consumed. | ||
| isStream?: boolean; | ||
| spanAttributes?: Record<string, unknown>; | ||
| spanKind?: SpanKindValue; | ||
| spanName?: string; | ||
| // Overrides the default `rpc` span op (e.g. `db` for DynamoDB). | ||
| spanOp?: string; | ||
| } |
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.