AWS X-Ray ID generator for OpenTelemetry that generates trace IDs following AWS X-Ray's format requirements for distributed tracing compatibility
npx @tessl/cli install tessl/npm-opentelemetry--id-generator-aws-xray@2.0.0The OpenTelemetry IdGenerator for AWS X-Ray generates trace IDs with its first four bytes set to the start time of the trace followed by a unique identifier consisting of 12 bytes of randomly generated numbers. This enables compatibility with AWS X-Ray service for distributed tracing in cloud environments.
npm install --save @opentelemetry/id-generator-aws-xray@opentelemetry/api ^1.0.0@opentelemetry/sdk-trace-base ^2.0.0import { AWSXRayIdGenerator } from "@opentelemetry/id-generator-aws-xray";For CommonJS:
const { AWSXRayIdGenerator } = require("@opentelemetry/id-generator-aws-xray");Additional imports for advanced usage:
import { IdGenerator } from "@opentelemetry/sdk-trace-base";
import { INVALID_SPANID, INVALID_TRACEID } from "@opentelemetry/api";import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { AWSXRayIdGenerator } from "@opentelemetry/id-generator-aws-xray";
// Configure tracer with AWS X-Ray ID generator
const tracerConfig = {
idGenerator: new AWSXRayIdGenerator(),
// other configuration options...
};
const tracerProvider = new NodeTracerProvider(tracerConfig);
// The ID generator will now create AWS X-Ray compatible trace and span IDsThe AWS X-Ray IdGenerator implements the OpenTelemetry IdGenerator interface and provides platform-specific implementations for both Node.js and browser environments:
IdGenerator interfaceMain class that implements AWS X-Ray compatible ID generation for OpenTelemetry tracing systems.
/**
* IdGenerator that generates trace IDs conforming to AWS X-Ray format.
* Implements the OpenTelemetry IdGenerator interface.
*/
class AWSXRayIdGenerator implements IdGenerator {
constructor();
generateTraceId(): string;
generateSpanId(): string;
}Usage Example:
import { AWSXRayIdGenerator } from "@opentelemetry/id-generator-aws-xray";
const idGenerator = new AWSXRayIdGenerator();
// Generate AWS X-Ray compatible trace ID
const traceId = idGenerator.generateTraceId();
console.log(traceId); // e.g., "58406520a006649127e371903a2de979"
// Generate span ID
const spanId = idGenerator.generateSpanId();
console.log(spanId); // e.g., "6e3b4749a8b7e2f3"Generates a random 16-byte trace ID formatted as a 32-character lowercase hexadecimal string. The first 4 bytes correspond to the current time in Unix epoch seconds (AWS X-Ray format requirement).
/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits. The first 4 bytes correspond to the current
* time, in seconds, as per X-Ray trace ID format.
* @returns 32-character hexadecimal trace ID string
*/
generateTraceId(): string;Trace ID Format Details:
58406520a006649127e371903a2de979
58406520 = timestamp (1480615200 seconds = Dec 1, 2016 10:00 AM PST)a006649127e371903a2de979 = random 24-character identifierGenerates a random 8-byte span ID formatted as a 16-character lowercase hexadecimal string. Includes validation to ensure the ID is never all zeros.
/**
* Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex
* characters corresponding to 64 bits.
* @returns 16-character hexadecimal span ID string
*/
generateSpanId(): string;Span ID Details:
0000000000000001 if random generation produces all zeros6e3b4749a8b7e2f3/**
* OpenTelemetry IdGenerator interface implemented by AWSXRayIdGenerator
* Import from: @opentelemetry/sdk-trace-base
*/
interface IdGenerator {
generateTraceId(): string;
generateSpanId(): string;
}Relevant OpenTelemetry constants used with ID validation:
/**
* Constants from @opentelemetry/api for ID validation
*/
const INVALID_SPANID: string; // "0000000000000000" - invalid span ID
const INVALID_TRACEID: string; // "00000000000000000000000000000000" - invalid trace ID