import { AwsClient } from "aws4fetch"; /** * Request interface (e.g. `fetch`). */ export declare type Fetch = (request: Request) => Promise; /** * AWS X-Ray HTTP header for passing around trace information. */ export declare const AWS_TRACE_HEADER = "X-Amzn-Trace-Id"; /** * AWS error response wrapper. */ export declare class AwsError extends Error { status: number; constructor(status: number); } /** * AWS list of unprocessed segments. */ export interface UnprocessedSegments { code?: string; id?: string; message?: string; } /** * AWS segment definition. */ export interface SegmentJson extends SubsegmentJson { /** * A unique identifier that connects all segments and subsegments originating from a single client request. */ trace_id: string; /** * An object with information about your application. */ service?: { /** * A string that identifies the version of your application that served the request. */ version: string; }; /** * A string that identifies the user who sent the request. */ user?: string; /** * A subsegment ID you specify if the request originated from an instrumented application. The X-Ray SDK adds the parent subsegment ID to the tracing header for downstream HTTP calls. In the case of nested subsegments, a subsegment can have a segment or a subsegment as its parent. */ parent_id?: string; } /** * AWS segment HTTP definition. */ export interface SegmentHttpJson { request?: { url?: string; method?: string; user_agent?: string; client_ip?: string; x_forwarded_for?: boolean; traced?: boolean; }; response?: { status?: number; content_length?: number; }; } /** * AWS sub-segment definition. */ export interface SubsegmentJson { /** * The logical name of the service that handled the request, up to 200 characters. For example, your application's name or domain name. Names can contain Unicode letters, numbers, and whitespace, and the following symbols: _, ., :, /, %, &, #, =, +, \, -, @. */ name: string; /** * A 64-bit identifier for the segment, unique among segments in the same trace, in 16 hexadecimal digits. */ id: string; /** * That is the time the segment was created, in floating point seconds in epoch time. For example, `1480615200.010` or `1.480615200010E9`. Use as many decimal places as you need. Microsecond resolution is recommended when available. */ start_time: number; /** * That is the time the segment was closed. For example, `1480615200.090` or `1.480615200090E9`. Specify either an `end_time` or `in_progress`. */ end_time?: number; /** * Set to `true` instead of specifying an `end_time` to record that a segment is started, but is not complete. Send an in-progress segment when your application receives a request that will take a long time to serve, to trace the request receipt. When the response is sent, send the complete segment to overwrite the in-progress segment. Only send one complete segment, and one or zero in-progress segments, per request. */ in_progress?: boolean; /** * Object with key-value pairs that you want X-Ray to index for search. */ annotations?: Record; /** * Object with any additional data that you want to store in the segment. */ metadata?: Record; /** * Array of subsegment objects. */ subsegments?: SubsegmentJson[]; /** * HTTP objects with information about the original HTTP request. */ http?: SegmentHttpJson; /** * Indicates that a client error occurred (response status code was 4XX Client Error). */ error?: boolean; /** * Indicates that a server error occurred (response status code was 5XX Server Error). */ fault?: boolean; /** * Indicates that a request was throttled (response status code was 429 Too Many Requests). */ throttle?: boolean; /** * A cause can be either a 16 character exception ID or an object. */ cause?: string | SegmentCauseJson; } /** * HTTP segment request. */ export interface SegmentHttpRequest { /** * The full URL of the request, compiled from the protocol, hostname, and path of the request. */ url?: string; /** * The request method. For example, `GET`. */ method?: string; /** * The user agent string from the requester's client. */ userAgent?: string; /** * The IP address of the requester. Can be retrieved from the IP packet's Source Address or, for forwarded requests, from an `X-Forwarded-For` header. */ clientIp?: string; /** * Boolean indicating that the client_ip was read from an X-Forwarded-For header and is not reliable as it could have been forged (segments only). */ xForwardedFor?: boolean; /** * Boolean indicating that the downstream call is to another traced service. If this field is set to true, X-Ray considers the trace to be broken until the downstream service uploads a segment with a parent_id that matches the id of the subsegment that contains this block (subsegments only). */ traced?: boolean; } /** * HTTP segment response. */ export interface SegmentHttpResponse { /** * Number indicating the HTTP status of the response. */ status?: number; /** * Number indicating the length of the response body in bytes. */ contentLength?: number; } export interface StackFrameJson { /** * The relative path to the file. */ path?: string; /** * The line in the file. */ line?: number; /** * The function or method name. */ label?: string; } export interface SegmentExceptionJson { /** * A 64-bit identifier for the exception, unique among segments in the same trace, in 16 hexadecimal digits. */ id: string; /** * The exception message. */ message?: string; /** * The exception type. */ type?: string; /** * Indicates that the exception was caused by an error returned by a downstream service. */ remote?: boolean; /** * Indicates the number of stack frames that are omitted from the stack. */ truncated?: number; /** * Indicates the number of exceptions that were skipped between this exception and its child, that is, the exception that it caused. */ skipped?: number; /** * Exception ID of the exception's parent, that is, the exception that caused this exception. */ cause?: string; /** * Array of `StackFrame` objects. */ stack?: StackFrameJson[]; } /** * Segment cause JSON definition. */ export interface SegmentCauseJson { /** * The full path of the working directory when the exception occurred. */ working_directory?: string; /** * The array of paths to libraries or modules in use when the exception occurred. */ paths?: string[]; /** * The array of exception objects. */ exceptions?: SegmentExceptionJson[]; } /** * AWS unprocessed segments error wrapper. */ export declare class UnprocessedError extends Error { unprocessedSegments: UnprocessedSegments[]; constructor(unprocessedSegments: UnprocessedSegments[]); } /** * Subsegment not ended error. */ export declare class SubsegmentNotEndedError extends TypeError { segment: Subsegment; constructor(segment: Subsegment); } /** * Capture stack frames from an error instance. */ export declare function captureStackFrames(error: Error): StackFrameJson[]; /** * Create a segment exception object from an error instance. */ export declare function captureException(error: Error, cause?: string): SegmentExceptionJson; /** * Segment `cause` wrapper. */ export declare class SegmentCause { directory?: string | undefined; paths?: string[] | undefined; exceptions: SegmentExceptionJson[]; constructor(directory?: string | undefined, paths?: string[] | undefined); /** * Capture an error and any associated `cause`. */ captureException(error: Error): void; toJSON(): SegmentCauseJson; } /** * HTTP segment instance. */ export declare class SegmentHttp { request?: SegmentHttpRequest | undefined; response?: SegmentHttpResponse | undefined; constructor(request?: SegmentHttpRequest | undefined, response?: SegmentHttpResponse | undefined); toJSON(): SegmentHttpJson; } /** * Subsegment wrapper. * * Docs: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html */ export declare class Subsegment { name: string; traceId: string; id: string; startTime: number; endTime?: number; metadata: Record; annotations: Record; subsegments: Subsegment[]; http?: SegmentHttp; error?: boolean; throttle?: boolean; fault?: boolean; cause?: string | SegmentCause; constructor(name: string, traceId: string); /** * Start a new subsegment. */ startSegment(name: string): Subsegment; /** * End the current segment by setting `endTime` to now. */ end(): this; /** * Capture the error instance as the cause, enables `error`. */ captureException(error: Error, cwd?: string): void; toJSON(): SubsegmentJson; } /** * Segment instance, wraps `Subsegment` with automatic `traceId` and additional context allowed. */ export declare class Segment extends Subsegment { traceId: string; service?: { version: string; }; user?: string; constructor(name: string, traceId?: string); toJSON(): SegmentJson; } /** * AWS X-Ray constructor options. */ export interface AwsXrayOptions { region: string; accessKeyId: string; secretAccessKey: string; fetch?: Fetch; } /** * AWS X-Ray helper instance. */ export declare class AwsXray { region: string; client: AwsClient; fetch: Fetch; constructor(options: AwsXrayOptions); /** * Submit AWS X-Ray segment to AWS. */ traceSegment(segment: Segment): Promise; } /** * Captured `fetch` type. */ export declare type CapturedFetch = (input: RequestInfo, init: RequestInit & { segment: Subsegment; }) => Promise; /** * Wrap an instance of `fetch` with AWS X-Ray tracing and optional trace header forwarding. */ export declare function captureFetch(fetch: (input: Request) => Promise, forwardTrace?: boolean): CapturedFetch;