High-precision timestamp support using Fluentd's EventTime format for microsecond-precision event timing.
High-precision timestamp class for event-time logging with nanosecond precision.
/**
* EventTime class for high-precision timestamps
* @param epoch - Unix epoch seconds
* @param nano - Nanosecond precision component
*/
class EventTime {
constructor(epoch, nano);
// Instance properties
epoch: number; // Unix epoch seconds
nano: number; // Nanosecond precision (0-999999999)
}Usage Examples:
const { EventTime } = require('fluent-logger');
// Create EventTime with specific epoch and nanoseconds
const eventTime = new EventTime(1489547207, 745003500);
console.log(eventTime.epoch); // 1489547207
console.log(eventTime.nano); // 745003500
// Use with logger
const logger = require('fluent-logger');
logger.configure('app');
logger.emit('event', {
message: 'High-precision timestamp event'
}, eventTime);Create EventTime for the current time with high precision.
/**
* Create EventTime for current time with high precision
* @returns EventTime instance representing current time
*/
static EventTime.now(): EventTime;Usage Examples:
const { EventTime } = require('fluent-logger');
// Get current time with high precision
const now = EventTime.now();
console.log(`Current time: ${now.epoch}.${now.nano}`);
// Use for real-time logging
const logger = require('fluent-logger');
logger.configure('realtime');
// Log with precise current timestamp
logger.emit('user_action', {
action: 'click',
element: 'submit_button',
page: '/checkout'
}, EventTime.now());
// Compare timing precision
const start = EventTime.now();
// ... some operation ...
const end = EventTime.now();
logger.emit('performance', {
operation: 'data_processing',
start_epoch: start.epoch,
start_nano: start.nano,
end_epoch: end.epoch,
end_nano: end.nano,
duration_ns: (end.epoch - start.epoch) * 1e9 + (end.nano - start.nano)
});Create EventTime from a JavaScript Date object.
/**
* Create EventTime from JavaScript Date object
* @param date - JavaScript Date object
* @returns EventTime instance with equivalent timestamp
*/
static EventTime.fromDate(date): EventTime;Usage Examples:
const { EventTime } = require('fluent-logger');
// Convert Date to EventTime
const date = new Date('2023-01-15T10:30:45.123Z');
const eventTime = EventTime.fromDate(date);
console.log(eventTime.epoch); // 1673777445
console.log(eventTime.nano); // 123000000 (123ms converted to nanoseconds)
// Use with scheduled events
const logger = require('fluent-logger');
logger.configure('scheduler');
const scheduledTime = new Date('2023-12-31T23:59:59.999Z');
logger.emit('scheduled_event', {
type: 'year_end_summary',
status: 'pending'
}, EventTime.fromDate(scheduledTime));
// Log past events with specific timestamps
const events = [
{ time: new Date('2023-01-01T00:00:00.000Z'), event: 'new_year' },
{ time: new Date('2023-07-04T12:00:00.000Z'), event: 'independence_day' },
{ time: new Date('2023-12-25T08:00:00.000Z'), event: 'christmas' }
];
events.forEach(({ time, event }) => {
logger.emit('historical_event', {
name: event,
category: 'holiday'
}, EventTime.fromDate(time));
});Create EventTime from a numeric timestamp (milliseconds since Unix epoch).
/**
* Create EventTime from numeric timestamp
* @param timestamp - Timestamp in milliseconds since Unix epoch
* @returns EventTime instance with equivalent timestamp
*/
static EventTime.fromTimestamp(timestamp): EventTime;Usage Examples:
const { EventTime } = require('fluent-logger');
// Convert millisecond timestamp to EventTime
const timestamp = 1673777445123; // 2023-01-15T10:30:45.123Z
const eventTime = EventTime.fromTimestamp(timestamp);
console.log(eventTime.epoch); // 1673777445
console.log(eventTime.nano); // 123000000
// Use with stored timestamps
const logger = require('fluent-logger');
logger.configure('analytics');
// Process stored event data with original timestamps
const storedEvents = [
{ timestamp: 1673777445123, user_id: 12345, action: 'login' },
{ timestamp: 1673777456789, user_id: 12345, action: 'view_product' },
{ timestamp: 1673777467890, user_id: 12345, action: 'add_to_cart' }
];
storedEvents.forEach(event => {
logger.emit('user_journey', {
user_id: event.user_id,
action: event.action,
original_timestamp: event.timestamp
}, EventTime.fromTimestamp(event.timestamp));
});
// Current timestamp
const now = Date.now();
logger.emit('system_metric', {
metric: 'cpu_usage',
value: 75.5
}, EventTime.fromTimestamp(now));EventTime objects are automatically serialized by msgpack for transmission to Fluentd.
/**
* Pack EventTime to Buffer for msgpack serialization
* @param eventTime - EventTime instance to pack
* @returns Buffer containing packed EventTime data
*/
static EventTime.pack(eventTime): Buffer;
/**
* Unpack Buffer to EventTime for msgpack deserialization
* @param buffer - Buffer containing packed EventTime data
* @returns EventTime instance
*/
static EventTime.unpack(buffer): EventTime;Usage Examples:
const { EventTime } = require('fluent-logger');
// These methods are used internally by fluent-logger
// You typically don't need to call them directly
// But you can use them for custom serialization if needed
const eventTime = EventTime.now();
const packed = EventTime.pack(eventTime);
console.log('Packed size:', packed.length); // 8 bytes
const unpacked = EventTime.unpack(packed);
console.log('Unpacked epoch:', unpacked.epoch);
console.log('Unpacked nano:', unpacked.nano);
// Verify round-trip serialization
console.log('Original:', eventTime.epoch, eventTime.nano);
console.log('Round-trip:', unpacked.epoch, unpacked.nano);
console.log('Equal:', eventTime.epoch === unpacked.epoch && eventTime.nano === unpacked.nano);Understanding the precision differences between regular timestamps and EventTime.
Usage Examples:
const { EventTime } = require('fluent-logger');
// Regular JavaScript Date precision (milliseconds)
const regularDate = new Date();
console.log('Regular Date:', regularDate.toISOString());
console.log('Milliseconds:', regularDate.getTime());
// EventTime precision (nanoseconds)
const eventTime = EventTime.now();
console.log('EventTime epoch:', eventTime.epoch);
console.log('EventTime nano:', eventTime.nano);
console.log('Total nanoseconds:', eventTime.epoch * 1e9 + eventTime.nano);
// Demonstrate precision difference
const logger = require('fluent-logger');
logger.configure('precision-test');
// Low precision (1-second granularity without EventTime)
logger.emit('low_precision', { value: 1 });
// Medium precision (millisecond granularity with Date)
logger.emit('medium_precision', { value: 2 }, new Date());
// High precision (nanosecond granularity with EventTime)
logger.emit('high_precision', { value: 3 }, EventTime.now());
// Timing measurements with high precision
const start = EventTime.now();
// Simulate some work
setTimeout(() => {
const end = EventTime.now();
const durationNs = (end.epoch - start.epoch) * 1e9 + (end.nano - start.nano);
logger.emit('timing_measurement', {
operation: 'async_operation',
duration_nanoseconds: durationNs,
duration_milliseconds: durationNs / 1e6,
start_time: start,
end_time: end
}, end);
}, 100);EventTime can be mixed with regular timestamps in the same application.
const logger = require('fluent-logger');
const { EventTime } = require('fluent-logger');
logger.configure('mixed-precision');
// Use EventTime for high-precision events
logger.emit('precise_event', {
type: 'performance_measurement',
metric: 'api_response_time'
}, EventTime.now());
// Use regular Date for normal events
logger.emit('normal_event', {
type: 'user_action',
action: 'page_view'
}, new Date());
// Use default timestamp (current time) for simple events
logger.emit('simple_event', {
type: 'system_info',
message: 'Application healthy'
});class EventTime {
epoch: number; // Unix epoch seconds (integer)
nano: number; // Nanosecond precision (0-999999999)
constructor(epoch: number, nano: number);
static now(): EventTime;
static fromDate(date: Date): EventTime;
static fromTimestamp(timestamp: number): EventTime;
static pack(eventTime: EventTime): Buffer;
static unpack(buffer: Buffer): EventTime;
}