Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 35x 35x 35x 35x 35x 36x 1x 1x 1x 1x 36x 36x 36x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 37x 38x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 7x 7x 7x 7x 7x 7x 3x 3x 4x 4x 4x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 4x 6x 6x 18x 6x 6x 6x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 6x 6x 6x 6x 6x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 6x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 4x 4x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 3x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x | import { Client, ClientOptions } from '@opensearch-project/opensearch'
import { AwsSigv4Signer } from '@opensearch-project/opensearch/aws'
import { defaultProvider } from '@aws-sdk/credential-provider-node'
import {
LoggerConfig,
LogLevel,
LogTransaction,
SearchQuery,
OpenSearchHit,
OpenSearchResponse,
} from './types'
export class ElasticLogger {
private client: Client
private index: string
private service: string
private environment: string
private region: string
private awsService: 'es' | 'aoss'
private readonly timeout = 30000
constructor(config: LoggerConfig) {
this.validateConfig(config)
this.index = config.index
this.service = config.service
this.environment = config.environment
this.region = config.region
this.awsService = config.awsService || 'aoss'
const clientConfig: ClientOptions = {
node: config.node,
ssl: {
rejectUnauthorized: this.environment === 'production',
},
requestTimeout: this.timeout,
maxRetries: 3,
}
if (config.authType === 'aws' || (!config.username && !config.password)) {
console.info('Using AWS SigV4 authentication')
Object.assign(
clientConfig,
AwsSigv4Signer({
region: this.region,
service: this.awsService,
getCredentials: () => {
const credentialsProvider = defaultProvider()
return credentialsProvider()
},
})
)
} else if (config.username && config.password) {
console.info('Using username/password authentication')
clientConfig.auth = {
username: config.username,
password: config.password,
}
} else {
throw new Error(
'Authentication configuration is required. Either set authType: "aws" or provide username and password.'
)
}
this.client = new Client(clientConfig)
}
/**
* Validates the configuration to ensure that all required fields are present
*
* This method validates the configuration to ensure that all required fields are present.
*
* @param config - The configuration to validate
* @returns void
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* logger.validateConfig(config)
* ```
*/
private validateConfig(config: LoggerConfig): void {
if (
!config.node ||
!config.index ||
!config.service ||
!config.environment
) {
throw new Error('All LoggerConfig fields are required')
}
}
/**
* Validates the queries to ensure that they are not empty
*
* This method validates the queries to ensure that they are not empty.
*
* @param queries - A string or array of strings to search for in the logs
* @returns An array of valid queries
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const validQueries = logger.validateQueries(['system1', 'system2'])
* console.log(validQueries)
* ```
*/
private validateQueries(queries: string | string[]): string[] {
const queryArray = Array.isArray(queries) ? queries : [queries]
const validQueries = queryArray.filter(
(query) => query && query.trim().length > 0
)
if (validQueries.length === 0) {
throw new Error('At least one non-empty query is required')
}
return validQueries
}
/**
* Builds a search query for the Elasticsearch/OpenSearch cluster
*
* This method builds a search query based on the provided queries and search configuration.
* It returns a search query object that can be used to search for logs in the Elasticsearch/OpenSearch cluster.
*
* @param queries - An array of strings to search for in the logs
* @param searchConfig - The search configuration to use
* @returns A search query object that can be used to search for logs in the Elasticsearch/OpenSearch cluster
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const searchQuery = logger.buildSearchQuery(['system1', 'system2'], { index: 'system-logs', fields: ['message', 'service', 'level'] })
* ```
*/
private buildSearchQuery(queries: string[], searchConfig: SearchQuery) {
const shouldClauses = queries.map((query) => ({
bool: {
should: searchConfig.fields.map((field) => ({
match: { [field]: query },
})),
minimum_should_match: 1,
},
}))
return {
index: searchConfig.index,
body: {
query: {
bool: {
should: shouldClauses,
minimum_should_match: 1,
},
},
},
}
}
/**
* Logs a message to the Elasticsearch/OpenSearch cluster
*
* This method logs a message to the main index.
*
* @param level - The log level (info, warn, error, debug)
* @param service - The service associated with the log
* @param message - The message to log
* @param meta - Optional metadata to include with the log
* @returns Promise<void> - Returns a promise that resolves when the message is logged
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* await logger.log('info', 'This is an info message')
* ```
*/
async log(
level: LogLevel,
message: string,
meta?: Record<string, unknown>
): Promise<void> {
if (!message) {
throw new Error('Message and service are required')
}
const logEntry = {
timestamp: new Date().toISOString(),
level,
service: this.service,
environment: this.environment,
message,
...meta,
}
try {
await this.client.index({
index: this.index,
body: logEntry,
})
} catch (error) {
await this.processError(error as Error, logEntry)
}
}
/**
* Logs a transaction to the Elasticsearch/OpenSearch cluster
*
* This method logs a transaction to the transactions index.
*
* @param transaction - The transaction to log
* @returns Promise<void> - Returns a promise that resolves when the transaction is logged
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const transaction = { name: 'transaction1', service: 'service1', status: 'success' }
* await logger.logTransaction(transaction)
* console.log('Transaction logged successfully')
* ```
*/
async logTransaction(transaction: LogTransaction): Promise<void> {
if (!transaction.name) {
throw new Error('Transaction name is required')
}
const doc = {
timestamp: new Date().toISOString(),
service: this.service,
environment: this.environment,
...transaction,
}
try {
await this.client.index({
index: `${this.index}-transactions`,
body: doc,
})
} catch (error) {
await this.processError(error as Error, doc)
}
}
/**
* Retrieves system logs from the Elasticsearch/OpenSearch cluster
*
* This method searches for system logs based on the provided queries.
* It returns an array of OpenSearchHit objects, which contain the log data.
*
* @param queries - A string or array of strings to search for in the system logs
* @returns Promise<OpenSearchHit[]> - An array of OpenSearchHit objects containing the log data
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const systemLogs = await logger.getSystemLogs(['system1', 'system2'])
* console.log(systemLogs)
* ```
*/
async getSystemLogs(queries: string | string[]): Promise<OpenSearchHit[]> {
const validQueries = this.validateQueries(queries)
const searchConfig: SearchQuery = {
index: this.index,
fields: ['message', 'service', 'level'],
}
try {
const response = (await this.client.search(
this.buildSearchQuery(validQueries, searchConfig)
)) as OpenSearchResponse
return response.body.hits.hits
} catch (error) {
console.error('Failed to get system logs:', error)
throw new Error(
`Failed to get system logs: ${error instanceof Error ? error.message : 'Unknown error'}`
)
}
}
/**
* Retrieves transaction logs from the Elasticsearch/OpenSearch cluster
*
* This method searches for transaction logs based on the provided queries.
* It returns an array of OpenSearchHit objects, which contain the log data.
*
* @param queries - A string or array of strings to search for in the transaction logs
* @returns Promise<OpenSearchHit[]> - An array of OpenSearchHit objects containing the log data
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const transactions = await logger.getLogsTransactions(['transaction1', 'transaction2'])
* console.log(transactions)
* ```
*/
async getLogsTransactions(
queries: string | string[]
): Promise<OpenSearchHit[]> {
const validQueries = this.validateQueries(queries)
const searchConfig: SearchQuery = {
index: `${this.index}-transactions`,
fields: ['name', 'service', 'status'],
}
try {
const response = (await this.client.search(
this.buildSearchQuery(validQueries, searchConfig)
)) as OpenSearchResponse
return response.body.hits.hits
} catch (error) {
console.error('Failed to get transaction logs:', error)
throw new Error(
`Failed to get transaction logs: ${error instanceof Error ? error.message : 'Unknown error'}`
)
}
}
/**
* Flushes the indices to ensure that the logs are visible
*
* This method refreshes both the main index and the transactions index
* to ensure that the logs are visible in the search results.
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* await logger.flush()
* console.log('Indices flushed successfully')
* ```
*/
async flush(): Promise<void> {
try {
await Promise.all([
this.client.indices.refresh({ index: this.index }),
this.client.indices.refresh({ index: `${this.index}-transactions` }),
])
} catch (error) {
console.error('Failed to flush indices:', error)
throw new Error(
`Failed to flush indices: ${error instanceof Error ? error.message : 'Unknown error'}`
)
}
}
/**
* Performs a health check on the Elasticsearch/OpenSearch cluster
*
* This method sends a ping request to the configured cluster to verify
* connectivity and basic cluster health. It's useful for monitoring
* and ensuring the logging service is available.
*
* @returns Promise<boolean> - Returns true if the cluster is reachable and healthy,
* false if the health check fails or cluster is unreachable
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const isHealthy = await logger.healthCheck()
* if (isHealthy) {
* console.log('Cluster is healthy')
* } else {
* console.log('Cluster health check failed')
* }
* ```
*/
async healthCheck(): Promise<boolean> {
try {
await this.client.ping()
return true
} catch (error) {
console.error('Elasticsearch health check failed:', error)
return false
}
}
/**
* Processes an error and logs it to the Elasticsearch/OpenSearch cluster
*
* This method processes an error and logs it to the Elasticsearch/OpenSearch cluster.
* It checks for mapping errors and attempts to fix them.
*
* @param error - The error to process
* @param body - The body to log
* @returns Promise<void> - Returns a promise that resolves when the error is processed
*
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* await logger.processError(new Error('Mapping error'), { message: 'Test error' })
* ```
*/
async processError(
error: Error,
body: Record<string, unknown>
): Promise<void> {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error'
// Check for mapping errors
if (
errorMessage.includes('mapper_parsing_exception') ||
errorMessage.includes('mapping_exception')
) {
console.error('OpenSearch mapping error:', errorMessage)
console.log('🔄 Attempting to fix index mapping...')
const mappingResult = await this.checkAndFixIndexMapping()
if (mappingResult.success) {
// Retry the log operation
try {
await this.client.index({
index: this.index,
body,
})
console.log('✅ Log message sent successfully after fixing mapping')
return
} catch (retryError) {
console.error('❌ Retry failed after mapping fix:', retryError)
throw new Error(
`Mapping fix failed. You may need to recreate the index. Details: ${errorMessage}`
)
}
} else {
throw new Error(
`Mapping error detected but could not be fixed: ${mappingResult.error}`
)
}
}
// Check for authentication/authorization errors
if (
errorMessage.includes('security_exception') ||
errorMessage.includes('authorization_exception') ||
errorMessage.includes('authentication_exception')
) {
console.error(
'OpenSearch authentication/authorization error:',
errorMessage
)
throw new Error(
`OpenSearch authentication failed. Please check your credentials and permissions. Details: ${errorMessage}`
)
}
// Check for connection errors
if (
errorMessage.includes('ECONNREFUSED') ||
errorMessage.includes('ENOTFOUND') ||
errorMessage.includes('timeout')
) {
console.error('OpenSearch connection error:', errorMessage)
throw new Error(
`Cannot connect to OpenSearch. Please check your connection settings. Details: ${errorMessage}`
)
}
console.error('Failed to log to Elasticsearch:', error)
throw new Error(`Failed to log to Elasticsearch: ${errorMessage}`)
}
/**
* Tests the connection and authentication to the Elasticsearch/OpenSearch cluster
*
* This method tests the connection and authentication to the Elasticsearch/OpenSearch cluster.
* It returns a boolean indicating whether the connection and authentication are successful.
*
* @returns Promise<{ success: boolean; error?: string }> - Returns a promise that resolves to an object containing the success status and any error message
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const connectionTest = await logger.testConnection()
* console.log(connectionTest)
* ```
*/
async testConnection(): Promise<{ success: boolean; error?: string }> {
try {
// Test basic connection
const pingResult = await this.client.ping()
if (!pingResult.body) {
return { success: false, error: 'Ping failed - no response body' }
}
// Test write permissions by trying to create a test document
const testIndex = `${this.index}-test-${Date.now()}`
try {
await this.client.index({
index: testIndex,
id: 'test-connection',
body: {
timestamp: new Date().toISOString(),
message: 'Connection test',
level: 'info',
},
})
// Clean up test document
await this.client.delete({
index: testIndex,
id: 'test-connection',
})
return { success: true }
} catch (writeError) {
const writeErrorMessage =
writeError instanceof Error ? writeError.message : 'Unknown error'
if (
writeErrorMessage.includes('security_exception') ||
writeErrorMessage.includes('authorization_exception')
) {
return {
success: false,
error: `Write permission denied. Please check if the user has write access to index: ${this.index}. Details: ${writeErrorMessage}`,
}
}
return {
success: false,
error: `Write test failed: ${writeErrorMessage}`,
}
}
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error'
return {
success: false,
error: `Connection test failed: ${errorMessage}`,
}
}
}
/**
* Validates the logger configuration
*
* This method validates the logger configuration to ensure that all required fields are present.
*
* @returns { valid: boolean; errors: string[] } - Returns an object containing the validation status and any error messages
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const validationResult = logger.validateLoggerConfig()
* console.log(validationResult)
* ```
*/
validateLoggerConfig(): { valid: boolean; errors: string[] } {
const errors: string[] = []
if (!this.index) errors.push('Index is required')
if (!this.service) errors.push('Service is required')
if (!this.environment) errors.push('Environment is required')
return {
valid: errors.length === 0,
errors,
}
}
/**
* Checks and fixes the index mapping
*
* This method checks and fixes the index mapping to ensure that the index is properly configured.
*
* @returns Promise<{ success: boolean; error?: string }> - Returns a promise that resolves to an object containing the success status and any error message
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const mappingResult = await logger.checkAndFixIndexMapping()
* console.log(mappingResult)
* ```
*/
async checkAndFixIndexMapping(): Promise<{
success: boolean
error?: string
}> {
try {
// Check if index exists
const indexExists = await this.client.indices.exists({
index: this.index,
})
if (!indexExists.body) {
// Create index with proper mapping
await this.client.indices.create({
index: this.index,
body: {
mappings: {
properties: {
timestamp: { type: 'date' },
level: { type: 'keyword' },
service: { type: 'keyword' },
message: { type: 'text' },
// Allow dynamic mapping for additional fields
meta: { type: 'object', dynamic: true },
},
},
settings: {
number_of_shards: 1,
number_of_replicas: 0,
},
},
})
console.log(`✅ Created index ${this.index} with proper mapping`)
return { success: true }
}
// Check current mapping
const mapping = await this.client.indices.getMapping({
index: this.index,
})
const properties = mapping.body[this.index].mappings.properties
// Check if message field has correct type
if (properties.message && properties.message.type !== 'text') {
console.warn(
`⚠️ Index ${this.index} has incorrect mapping for 'message' field`
)
console.warn(`Current type: ${properties.message.type}, Expected: text`)
// Try to update mapping (this might fail if there's data)
try {
await this.client.indices.putMapping({
index: this.index,
body: {
properties: {
message: { type: 'text' },
},
},
})
console.log(`✅ Updated mapping for index ${this.index}`)
return { success: true }
} catch (mappingError) {
const errorMsg =
mappingError instanceof Error
? mappingError.message
: 'Unknown error'
return {
success: false,
error: `Cannot update mapping. You may need to recreate the index. Details: ${errorMsg}`,
}
}
}
return { success: true }
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error'
return {
success: false,
error: `Failed to check/fix mapping: ${errorMessage}`,
}
}
}
/**
* Recreates the index
*
* This method recreates the index to ensure that the index is properly configured.
*
* @returns Promise<{ success: boolean; error?: string }> - Returns a promise that resolves to an object containing the success status and any error message
* @example
* ```typescript
* const logger = new ElasticLogger(config)
* const recreateResult = await logger.recreateIndex()
* console.log(recreateResult)
* ```
*/
async recreateIndex(): Promise<{ success: boolean; error?: string }> {
try {
// Delete index if exists
const indexExists = await this.client.indices.exists({
index: this.index,
})
if (indexExists.body) {
await this.client.indices.delete({ index: this.index })
console.log(`🗑️ Deleted existing index ${this.index}`)
}
// Create new index with proper mapping
await this.client.indices.create({
index: this.index,
body: {
mappings: {
properties: {
timestamp: { type: 'date' },
level: { type: 'keyword' },
service: { type: 'keyword' },
message: { type: 'text' },
meta: { type: 'object', dynamic: true },
},
},
settings: {
number_of_shards: 1,
number_of_replicas: 0,
},
},
})
console.log(`✅ Recreated index ${this.index} with proper mapping`)
return { success: true }
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error'
return {
success: false,
error: `Failed to recreate index: ${errorMessage}`,
}
}
}
}
|