Skip to content

Monitoring

Track lock performance and debug issues.

Available Metrics

When the MetricsPlugin is registered alongside LocksPlugin, the following Prometheus metrics are emitted automatically:

MetricTypeLabelsDescription
redisx_lock_acquisitions_totalCounterstatus (acquired | failed)Lock acquisition attempts
redisx_lock_wait_duration_secondsHistogramTime waiting to acquire a lock
redisx_lock_hold_duration_secondsHistogramHow long a lock was held
redisx_locks_activeGaugeCurrently held locks

Prometheus Queries

yaml
# Acquisition success rate
rate(redisx_lock_acquisitions_total{status="acquired"}[5m])
/ rate(redisx_lock_acquisitions_total[5m])

# Average wait time
rate(redisx_lock_wait_duration_seconds_sum[5m])
/ rate(redisx_lock_wait_duration_seconds_count[5m])

# Currently held locks
redisx_locks_active

Custom Logging

Lock lifecycle events are tracked automatically via MetricsPlugin and TracingPlugin. For custom logging, wrap the LockService:

typescript
import { Injectable, Inject, Logger } from '@nestjs/common';
import { LOCK_SERVICE, ILockService, ILock, ILockOptions } from '@nestjs-redisx/locks';

@Injectable()
export class LoggingLockService {
  private readonly logger = new Logger('Locks');

  constructor(
    @Inject(LOCK_SERVICE) private readonly lockService: ILockService,
  ) {}

  async acquire(key: string, options?: ILockOptions): Promise<ILock> {
    this.logger.debug(`Acquiring lock: ${key}`);
    const lock = await this.lockService.acquire(key, options);
    this.logger.debug(`Lock acquired: ${key}`);
    return lock;
  }
}

Next Steps

Released under the MIT License.