Skip to content

Commit

Permalink
fix: Reorder logging arguments based on Bunyan interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslbw committed May 13, 2021
1 parent 723e763 commit 46b26a0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 76 deletions.
18 changes: 8 additions & 10 deletions packages/api-cardano-db-hasura/src/CardanoNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export class CardanoNodeClient {
public async getTipSlotNo () {
const tip = await this.stateQueryClient.ledgerTip()
const slotNo = tip === 'origin' ? 0 : tip.slot
this.logger.debug('getTipSlotNo', { module: 'CardanoNodeClient', value: slotNo })
this.logger.debug({ module: 'CardanoNodeClient', slotNo }, 'getTipSlotNo')
return slotNo
}

public async getProtocolParams () {
const protocolParams = await this.stateQueryClient.currentProtocolParameters()
this.logger.debug('getProtocolParams', { module: 'CardanoNodeClient', value: protocolParams })
this.logger.debug({ module: 'CardanoNodeClient', protocolParams }, 'getProtocolParams')
return protocolParams
}

Expand All @@ -45,7 +45,7 @@ export class CardanoNodeClient {
this.stateQueryClient = await createStateQueryClient(options)
this.txSubmissionClient = await createTxSubmissionClient(options)
if (!(await this.isInCurrentEra())) {
this.logger.warn('cardano-node is still synchronizing', { module: 'CardanoNodeClient' })
this.logger.warn({ module: 'CardanoNodeClient' }, 'cardano-node is still synchronizing')
throw new Error()
}
}, {
Expand All @@ -60,13 +60,11 @@ export class CardanoNodeClient {

private async isInCurrentEra () {
const { protocolVersion } = await this.getProtocolParams()
this.logger.debug('Comparing current protocol params with last known major version from cardano-node config', {
this.logger.debug({
module: 'CardanoNodeClient',
value: {
currentProtocolVersion: protocolVersion,
lastConfiguredMajorVersion: this.lastConfiguredMajorVersion
}
})
currentProtocolVersion: protocolVersion,
lastConfiguredMajorVersion: this.lastConfiguredMajorVersion
}, 'Comparing current protocol params with last known major version from cardano-node config')
return protocolVersion.major >= this.lastConfiguredMajorVersion
}

Expand All @@ -81,7 +79,7 @@ export class CardanoNodeClient {
try {
await this.txSubmissionClient.submitTx(transaction)
const hash = getHashOfSignedTransaction(transaction)
this.logger.info('submitTransaction', { module: 'CardanoNodeClient', hash })
this.logger.info({ module: 'CardanoNodeClient', hash }, 'submitTransaction')
return hash
} catch (error) {
if (!isEraMismatch(error.message)) {
Expand Down
51 changes: 29 additions & 22 deletions packages/api-cardano-db-hasura/src/DataSyncController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class DataSyncController {
const batchSize = 500
const assetsWithoutMetadataCount = await this.hasuraClient.assetsWithoutMetadataCount({ _lte: 2 })
this.logger.debug(
'Newly discovered assets missing metadata',
{ module: 'DataSyncController', value: assetsWithoutMetadataCount }
{ module: 'DataSyncController', qty: assetsWithoutMetadataCount },
'Newly discovered assets missing metadata'
)
const batchQty = Math.ceil(assetsWithoutMetadataCount / batchSize)
let totalCount = 0
Expand All @@ -93,7 +93,7 @@ export class DataSyncController {
}
return totalCount
},
pollingInterval.initial,
pollingInterval.initial * 10,
this.logger
),
ongoing: new DataFetcher<number>(
Expand All @@ -103,8 +103,8 @@ export class DataSyncController {
const assetsEligibleForMetadataRefreshCount =
await this.hasuraClient.assetsEligibleForMetadataRefreshCount({ _gt: 2 })
this.logger.debug(
'Assets eligible for metadata refresh',
{ module: 'DataSyncController', value: assetsEligibleForMetadataRefreshCount }
{ module: 'DataSyncController', qty: assetsEligibleForMetadataRefreshCount },
'Assets eligible for metadata refresh'
)
const batchQty = Math.ceil(assetsEligibleForMetadataRefreshCount / batchSize)
let totalCount = 0
Expand All @@ -119,7 +119,7 @@ export class DataSyncController {
}
return totalCount
},
pollingInterval.ongoing,
pollingInterval.ongoing * 10,
this.logger
)
}
Expand All @@ -130,8 +130,8 @@ export class DataSyncController {
const batchSize = 500
const distinctAssetsInTokensCount = await this.hasuraClient.distinctAssetsInTokensCount()
this.logger.debug(
'Distinct assets in tokens count',
{ module: 'DataSyncController', value: distinctAssetsInTokensCount }
{ module: 'DataSyncController', qty: distinctAssetsInTokensCount },
'Distinct assets in tokens count'
)
const batchQty = Math.ceil(distinctAssetsInTokensCount / batchSize)
let totalCount = 0
Expand All @@ -142,11 +142,13 @@ export class DataSyncController {
const assetsAlreadyInDb =
await this.hasuraClient.getAssetsById(assetsInBatch.map(asset => asset.assetId))
this.logger.debug(
'Assets from tokens',
{
module: 'DataSyncController',
value: { batch: i, qty: assetsInBatch.length, existing: assetsAlreadyInDb.length }
}
batch: i,
qty: assetsInBatch.length,
existing: assetsAlreadyInDb.length
},
'Assets from tokens'
)
const newAssets = assetsInBatch
.filter(asset => assetsAlreadyInDb.find(existingAsset =>
Expand All @@ -158,19 +160,22 @@ export class DataSyncController {
policyId: asset.policyId,
metadataFetchAttempts: 0
}))
this.logger.debug('asset IDs diff', { module: 'DataSyncController', value: newAssets.length })
this.logger.debug(
{ module: 'DataSyncController', qty: newAssets.length },
'asset IDs diff'
)
if (newAssets.length > 0) {
totalCount = totalCount + newAssets.length
await this.hasuraClient.insertAssets(newAssets)
this.logger.debug(
'synchronised assets table from tokens',
{ module: 'DataSyncController', value: { batch: i, qty: newAssets.length } }
{ module: 'DataSyncController', batch: i, qty: newAssets.length },
'synchronised assets table from tokens'
)
}
}
return totalCount
},
120 * 1000,
1200 * 1000,
this.logger
)
}
Expand All @@ -196,8 +201,8 @@ export class DataSyncController {
...{ metadataHash: hash(obj.metadata) }
}))
this.logger.debug(
'Metadata with updates to apply',
{ module: 'DataSyncController', value: assetsWithMetadata.length }
{ module: 'DataSyncController', qty: assetsWithMetadata.length },
'Metadata with updates to apply'
)
if (assetsWithMetadata.length > 0) {
await this.hasuraClient.addMetadata(assetsWithMetadata)
Expand All @@ -220,7 +225,7 @@ export class DataSyncController {
return response.data.subjects
} catch (error) {
if (error.code === 'ENOTFOUND') {
this.logger.error(error.message)
this.logger.error({ err: error })
} else {
throw error
}
Expand Down Expand Up @@ -251,24 +256,26 @@ export class DataSyncController {
}

public async initialize () {
this.logger.info('Initializing', { module: 'DataSyncController' })
this.logger.info({ module: 'DataSyncController' }, 'Initializing')
await this.ensureAssetFingerprints()
await this.assetSynchronizer.initialize()
if (this.metadataServerUri) {
await this.ensureMetadataServerIsAvailable()
await this.metadataSynchronizer.initial.initialize()
await this.metadataSynchronizer.ongoing.initialize()
}
this.logger.info('Initialized', { module: 'DataSyncController' })
this.logger.info({ module: 'DataSyncController' }, 'Initialized')
}

public async shutdown () {
this.logger.info('Shutting down', { module: 'DataSyncController' })
this.logger.info({ module: 'DataSyncController' }, 'Shutting down')
if (this.metadataServerUri) {
await this.metadataSynchronizer.initial.shutdown()
await this.metadataSynchronizer.ongoing.shutdown()
}
await this.assetSynchronizer.shutdown()
this.logger.info('Shutdown complete', { module: 'DataSyncController' })
this.logger.info(
{ module: 'DataSyncController' },
'Shutdown complete')
}
}
12 changes: 6 additions & 6 deletions packages/api-cardano-db-hasura/src/Db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ export class Db {

public async init ({ onDbSetup }: { onDbSetup: Function }): Promise<void> {
this.pgSubscriber.events.on('connected', async () => {
this.logger.debug('DbClient.pgSubscriber: Connected')
this.logger.debug({ module: 'Db' }, 'pgSubscriber: Connected')
await onDbSetup()
})
this.pgSubscriber.events.on('reconnect', (attempt) => {
this.logger.warn(`DbClient.pgSubscriber: Reconnecting attempt ${attempt}`)
this.logger.warn({ module: 'Db' }, `pgSubscriber: Reconnecting attempt ${attempt}`)
})
this.pgSubscriber.events.on('error', (error) => {
this.logger.error('DbClient.pgSubscriber: Fatal database connection error:', error)
this.logger.error({ module: 'Db', err: error }, 'pgSubscriber')
process.exit(1)
})
this.pgSubscriber.notifications.on('cardano_db_sync_startup', async payload => {
switch (payload) {
case 'init' :
this.logger.warn('DbClient.pgSubscriber: cardano-db-sync-extended starting, schema will be reset')
this.logger.warn({ module: 'Db' }, 'pgSubscriber: cardano-db-sync-extended starting, schema will be reset')
break
case 'db-setup' :
await onDbSetup()
break
default :
this.logger.error(`DbClient.pgSubscriber: Unknown message payload ${payload}`)
this.logger.error({ module: 'Db' }, `DbClient.pgSubscriber: Unknown message payload ${payload}`)
}
})
try {
await this.pgSubscriber.connect()
await this.pgSubscriber.listenTo('cardano_db_sync_startup')
} catch (error) {
this.logger.error(error)
this.logger.error({ err: error })
}
}

Expand Down
Loading

0 comments on commit 46b26a0

Please sign in to comment.