UNPKG

@loglayer/transport-new-relic

Version:

New Relic transport for the LogLayer logging library.

105 lines (102 loc) 3.96 kB
import { LoggerlessTransportConfig, LoggerlessTransport, LogLayerTransportParams } from '@loglayer/transport'; /** * Configuration options for the New Relic transport. */ interface NewRelicTransportConfig extends LoggerlessTransportConfig { /** * The New Relic API key */ apiKey: string; /** * The New Relic Log API endpoint * @default https://log-api.newrelic.com/log/v1 */ endpoint?: string; /** * Optional callback for error handling */ onError?: (err: Error) => void; /** * Optional callback for debugging log entries before they are sent */ onDebug?: (entry: Record<string, any>) => void; /** * Whether to use gzip compression * @default true */ useCompression?: boolean; /** * Number of retry attempts before giving up * @default 3 */ maxRetries?: number; /** * Base delay between retries in milliseconds * @default 1000 */ retryDelay?: number; /** * Whether to respect rate limiting by waiting when a 429 response is received * @default true */ respectRateLimit?: boolean; } /** * NewRelicTransport is responsible for sending logs to New Relic's Log API. * It handles validation, compression, retries, and rate limiting according to New Relic's specifications. * * Features: * - Validates payload size (max 1MB) * - Validates number of attributes (max 255) * - Validates attribute name length (max 255 characters) * - Truncates attribute values longer than 4094 characters * - Supports gzip compression * - Handles rate limiting with configurable behavior * - Implements retry logic with exponential backoff */ declare class NewRelicTransport extends LoggerlessTransport { private apiKey; private endpoint; private onError?; private onDebug?; private useCompression; private maxRetries; private retryDelay; private respectRateLimit; /** * Creates a new instance of NewRelicTransport. * * @param config - Configuration options for the transport * @param config.apiKey - New Relic API key for authentication * @param config.endpoint - Optional custom endpoint URL (defaults to New Relic's Log API endpoint) * @param config.onError - Optional error callback for handling errors * @param config.onDebug - Optional callback for debugging log entries before they are sent * @param config.useCompression - Whether to use gzip compression (defaults to true) * @param config.maxRetries - Maximum number of retry attempts (defaults to 3) * @param config.retryDelay - Base delay between retries in milliseconds (defaults to 1000) * @param config.respectRateLimit - Whether to honor rate limiting headers (defaults to true) */ constructor(config: NewRelicTransportConfig); /** * Processes and ships log entries to New Relic. * * This method: * 1. Validates the message size * 2. Creates and validates the log entry * 3. Validates the final payload size * 4. Asynchronously sends the log entry to New Relic * * The actual sending is done asynchronously in a fire-and-forget manner to maintain * compatibility with the base transport class while still providing retry and error handling. * * @param params - Log parameters including level, messages, and metadata * @param params.logLevel - The severity level of the log * @param params.messages - Array of message strings to be joined * @param params.data - Optional metadata to include with the log * @param params.hasData - Whether metadata is present * @returns The original messages array * @throws {ValidationError} If the payload exceeds size limits or validation fails */ shipToLogger({ logLevel, messages, data, hasData }: LogLayerTransportParams): any[]; } export { NewRelicTransport, type NewRelicTransportConfig };