Improve redis logging

This commit is contained in:
Chocobozzz 2024-10-23 10:54:23 +02:00
parent be42ccb3f5
commit 8745c915d5
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 23 additions and 11 deletions

View File

@ -1,7 +1,7 @@
import { Redis as IoRedis, RedisOptions } from 'ioredis'
import { exists } from '@server/helpers/custom-validators/misc.js'
import { sha256 } from '@peertube/peertube-node-utils'
import { logger } from '../helpers/logger.js'
import { exists } from '@server/helpers/custom-validators/misc.js'
import { Redis as IoRedis, RedisOptions } from 'ioredis'
import { logger, loggerTagsFactory } from '../helpers/logger.js'
import { generateRandomString } from '../helpers/utils.js'
import { CONFIG } from '../initializers/config.js'
import {
@ -16,6 +16,8 @@ import {
WEBSERVER
} from '../initializers/constants.js'
const lTags = loggerTagsFactory('redis')
class Redis {
private static instance: Redis
@ -33,25 +35,25 @@ class Redis {
this.initialized = true
const redisMode = CONFIG.REDIS.SENTINEL.ENABLED ? 'sentinel' : 'standalone'
logger.info('Connecting to redis ' + redisMode + '...')
logger.info(`Connecting to Redis in "${redisMode}" mode...`, lTags())
this.client = new IoRedis(Redis.getRedisClientOptions('', { enableAutoPipelining: true }))
this.client.on('error', err => logger.error('Redis failed to connect', { err }))
this.client.on('error', err => logger.error('Redis failed to connect', { err, ...lTags() }))
this.client.on('connect', () => {
logger.info('Connected to redis.')
logger.info('Connected to redis.', lTags())
this.connected = true
})
this.client.on('reconnecting', (ms) => {
logger.error(`Reconnecting to redis in ${ms}.`)
logger.error(`Reconnecting to redis in ${ms}.`, lTags())
})
this.client.on('close', () => {
logger.error('Connection to redis has closed.')
logger.error('Connection to redis has closed.', lTags())
this.connected = false
})
this.client.on('end', () => {
logger.error('Connection to redis has closed and no more reconnects will be done.')
logger.error('Connection to redis has closed and no more reconnects will be done.', lTags())
})
this.prefix = 'redis-' + WEBSERVER.HOST + '-'
@ -62,6 +64,11 @@ class Redis {
const connectTimeout = 20000 // Could be slow since node use sync call to compile PeerTube
if (CONFIG.REDIS.SENTINEL.ENABLED) {
logger.info(
`Using sentinel redis options`,
{ sentinels: CONFIG.REDIS.SENTINEL.SENTINELS, name: CONFIG.REDIS.SENTINEL.MASTER_NAME, ...lTags() }
)
return {
connectionName,
connectTimeout,
@ -73,6 +80,11 @@ class Redis {
}
}
logger.info(
`Using standalone redis options`,
{ db: CONFIG.REDIS.DB, host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT, path: CONFIG.REDIS.SOCKET, ...lTags() }
)
return {
connectionName,
connectTimeout,
@ -202,7 +214,7 @@ class Redis {
const valueInt = parseInt(valueString, 10)
if (isNaN(valueInt)) {
logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
logger.error(`Cannot get videos views stats of video ${videoId} in hour ${hour}: views number is NaN (${valueString}).`, lTags())
return undefined
}
@ -243,7 +255,7 @@ class Redis {
const valueInt = parseInt(valueString, 10)
if (isNaN(valueInt)) {
logger.error('Cannot get videos views of video %d: views number is NaN (%s).', videoId, valueString)
logger.error(`Cannot get videos views of video ${videoId}: views number is NaN (${valueString}).`, lTags())
return undefined
}