@loglayer/transport-new-relic
Version:
New Relic transport for the LogLayer logging library.
95 lines (93 loc) • 3.58 kB
JavaScript
import { HttpTransport, HttpTransportError, LogSizeError, RateLimitError } from "@loglayer/transport-http";
//#region src/NewRelicTransport.ts
const MAX_ATTRIBUTES = 255;
const MAX_ATTRIBUTE_NAME_LENGTH = 255;
const MAX_ATTRIBUTE_VALUE_LENGTH = 4094;
/**
* Error thrown when log entry validation fails.
* This includes attribute count, attribute name length, and other New Relic API validations.
*/
var ValidationError = class extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
}
};
/**
* Validates attributes against New Relic's constraints.
* - Checks number of attributes (max 255)
* - Validates attribute name length (max 255 characters)
* - Truncates attribute values longer than 4094 characters
*
* @param attributes - The attributes to validate
* @returns The validated (and potentially modified) attributes
* @throws {ValidationError} If validation fails
*/
function validateAttributes(attributes) {
const validated = {};
const attributeCount = Object.keys(attributes).length;
if (attributeCount > MAX_ATTRIBUTES) throw new ValidationError(`Log entry exceeds maximum number of attributes (${MAX_ATTRIBUTES}). Found: ${attributeCount}`);
for (const [key, value] of Object.entries(attributes)) {
if (key.length > MAX_ATTRIBUTE_NAME_LENGTH) throw new ValidationError(`Attribute name exceeds maximum length (${MAX_ATTRIBUTE_NAME_LENGTH}). Found: ${key.length}`);
if (typeof value === "string" && value.length > MAX_ATTRIBUTE_VALUE_LENGTH) validated[key] = value.slice(0, MAX_ATTRIBUTE_VALUE_LENGTH);
else validated[key] = value;
}
return validated;
}
/**
* Default payload template that formats log data for New Relic's Log API.
* Each entry includes timestamp, level, the log message, and optional attributes.
*/
function defaultNewRelicPayload(params) {
const logEntry = {
timestamp: Date.now(),
level: params.logLevel,
log: params.message
};
if (params.data) logEntry.attributes = validateAttributes(params.data);
return JSON.stringify(logEntry);
}
/**
* NewRelicTransport is responsible for sending logs to New Relic's Log API.
* It extends HttpTransport with New Relic-specific configuration, validation,
* and formatting.
*
* Features:
* - Validates attribute count (max 255)
* - Validates attribute name length (max 255 characters)
* - Truncates attribute values longer than 4094 characters
* - Gzip compression by default (via HttpTransport)
* - Retry logic with exponential backoff (via HttpTransport)
* - Rate limiting support (via HttpTransport)
* - Batch sending support (via HttpTransport)
* - Error and debug callbacks
*/
var NewRelicTransport = class extends HttpTransport {
/**
* Creates a new instance of NewRelicTransport.
*
* @param config - Configuration options for the transport
*/
constructor(config) {
const { apiKey, endpoint, payloadTemplate, ...httpConfig } = config;
super({
...httpConfig,
url: endpoint ?? "https://log-api.newrelic.com/log/v1",
method: "POST",
headers: {
"Content-Type": "application/json",
"Api-Key": apiKey
},
payloadTemplate: payloadTemplate ?? defaultNewRelicPayload,
compression: httpConfig.compression ?? true,
maxRetries: httpConfig.maxRetries ?? 3,
retryDelay: httpConfig.retryDelay ?? 1e3,
respectRateLimit: httpConfig.respectRateLimit ?? true,
maxLogSize: httpConfig.maxLogSize ?? 1048576,
maxPayloadSize: httpConfig.maxPayloadSize ?? 1048576
});
}
};
//#endregion
export { HttpTransportError, LogSizeError, NewRelicTransport, RateLimitError, ValidationError };
//# sourceMappingURL=index.mjs.map