Skip to content

Commit

Permalink
fix: improve logs (#73)
Browse files Browse the repository at this point in the history
Example output before my change:

```
Retrieval failed with status code 502: no candidates found

{
  timeout: false,
  startAt: 2024-05-31T07:30:43.389Z,
  // etc.
  protocol: "graphsync",
  providerAddress: "/ip4/118.212.68.22/tcp/24501/p2p/12D3KooWJJEL214g7yMFMFaE6aPRaQTLVVCNFjWeiBLUCTzP98tp"
}
Submitting measurement...
{
  sparkVersion: "1.11.4",
  zinniaVersion: "0.19.1",
  cid: "bafykbzacechhbxnsn5dw6lx7phcfvkjc6kqsuuwl4bnynexewb5y3vtcw2fvw",
  minerId: "f02894875",
  timeout: false,
  startAt: 2024-05-31T07:30:43.389Z,
  // etc.
  providerAddress: "/ip4/118.212.68.22/tcp/24501/p2p/12D3KooWJJEL214g7yMFMFaE6aPRaQTLVVCNFjWeiBLUCTzP98tp",
  participantAddress: "0x000000000000000000000000000000000000dEaD",
  stationId: "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
```

This commit removes the first log which was printed when `fetch()`
was able to get some HTTP response frome the server.

It is also removing the trailing newline from HTTP error response bodies
before we print them to the console.

Finally, it adds an empty line between tasks, to visually delimit
logs belonging to different tasks.
  • Loading branch information
bajtos authored Jun 3, 2024
1 parent c7fde0a commit 2e47236
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/ipni-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function queryTheIndex (cid, providerId) {
try {
const res = await fetch(url)
if (!res.ok) {
console.error('IPNI query failed, HTTP response: %s %s', res.status, await res.text())
console.error('IPNI query failed, HTTP response: %s %s', res.status, (await res.text()).trimEnd())
return { indexerResult: `ERROR_${res.status}` }
}

Expand Down
2 changes: 1 addition & 1 deletion lib/miner-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function rpc (method, ...params) {
const res = await fetch(req)

if (!res.ok) {
throw new Error(`JSON RPC failed with ${res.code}: ${await res.text()}`)
throw new Error(`JSON RPC failed with ${res.code}: ${(await res.text()).trimEnd()}`)
}

const body = await res.json()
Expand Down
6 changes: 3 additions & 3 deletions lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,13 @@ export default class Spark {
}
} else {
console.error('Retrieval failed with status code %s: %s',
res.status, await res.text())
res.status, (await res.text()).trimEnd())
}
} finally {
clearTimeout(timeout)
}

stats.endAt = new Date()
console.log(stats)
}

async submitMeasurement (task, stats) {
Expand Down Expand Up @@ -226,6 +225,7 @@ export default class Spark {
if (delay > 0) {
console.log('Sleeping for %s seconds before starting the next task...', Math.round(delay / 1000))
await sleep(delay)
console.log() // add an empty line to visually delimit logs from different tasks
}
}
}
Expand All @@ -247,7 +247,7 @@ async function assertOkResponse (res, errorMsg) {
try {
body = await res.text()
} catch {}
const err = new Error(`${errorMsg ?? 'Fetch failed'} (${res.status}): ${body}`)
const err = new Error(`${errorMsg ?? 'Fetch failed'} (${res.status}): ${body.trimEnd()}`)
err.statusCode = res.status
err.serverMessage = body
throw err
Expand Down
1 change: 1 addition & 0 deletions manual-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ const minerId = 'f010479'
const spark = new Spark()
const stats = { cid, minerId, indexerResult: null, statusCode: null, byteLength: 0 }
await spark.executeRetrievalCheck({ cid, minerId }, stats)
console.log('Measurement: %o', stats)

0 comments on commit 2e47236

Please sign in to comment.