-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ucan stream consumer upload remove (#157)
Part of metrics work #117 Adds system total metrics for `upload/remove`. With these, we can track number of uploads unlinked from space Needs: - [x] #151 --------- Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
1 parent
57842a1
commit 7d1ff48
Showing
6 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as Sentry from '@sentry/serverless' | ||
|
||
import { createMetricsTable } from '../tables/metrics.js' | ||
import { parseKinesisEvent } from '../utils/parse-kinesis-event.js' | ||
import { UPLOAD_REMOVE } from '../constants.js' | ||
|
||
Sentry.AWSLambda.init({ | ||
environment: process.env.SST_STAGE, | ||
dsn: process.env.SENTRY_DSN, | ||
tracesSampleRate: 1.0, | ||
}) | ||
|
||
const AWS_REGION = process.env.AWS_REGION || 'us-west-2' | ||
|
||
/** | ||
* @param {import('aws-lambda').KinesisStreamEvent} event | ||
*/ | ||
async function handler(event) { | ||
const ucanInvocations = parseKinesisEvent(event) | ||
|
||
const { | ||
TABLE_NAME: tableName = '', | ||
// set for testing | ||
DYNAMO_DB_ENDPOINT: dbEndpoint, | ||
} = process.env | ||
|
||
await updateUploadRemoveTotal(ucanInvocations, { | ||
metricsTable: createMetricsTable(AWS_REGION, tableName, { | ||
endpoint: dbEndpoint | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* @param {import('../types').UcanInvocation[]} ucanInvocations | ||
* @param {import('../types').TotalSizeCtx} ctx | ||
*/ | ||
export async function updateUploadRemoveTotal (ucanInvocations, ctx) { | ||
const invocationsWithUploadRemove = ucanInvocations.filter( | ||
inv => inv.value.att.find(a => a.can === UPLOAD_REMOVE) | ||
).flatMap(inv => inv.value.att) | ||
|
||
await ctx.metricsTable.incrementUploadRemoveTotal(invocationsWithUploadRemove) | ||
} | ||
|
||
export const consumer = Sentry.AWSLambda.wrapHandler(handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
ucan-invocation/test/functions/metrics-upload-remove-total.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { testConsumer as test } from '../helpers/context.js' | ||
|
||
import * as Signer from '@ucanto/principal/ed25519' | ||
import * as UploadCapabilities from '@web3-storage/capabilities/upload' | ||
|
||
import { createDynamodDb } from '../helpers/resources.js' | ||
import { createSpace } from '../helpers/ucanto.js' | ||
import { randomCAR } from '../helpers/random.js' | ||
import { createDynamoAdminMetricsTable, getItemFromTable} from '../helpers/tables.js' | ||
|
||
import { updateUploadRemoveTotal } from '../../functions/metrics-upload-remove-total.js' | ||
import { createMetricsTable } from '../../tables/metrics.js' | ||
import { METRICS_NAMES } from '../../constants.js' | ||
|
||
const REGION = 'us-west-2' | ||
|
||
test.before(async t => { | ||
// Dynamo DB | ||
const { | ||
client: dynamo, | ||
endpoint: dbEndpoint | ||
} = await createDynamodDb({ port: 8000 }) | ||
|
||
t.context.dbEndpoint = dbEndpoint | ||
t.context.dynamoClient = dynamo | ||
}) | ||
|
||
test('handles a batch of single invocation with upload/remove', async t => { | ||
const { tableName } = await prepareResources(t.context.dynamoClient) | ||
const uploadService = await Signer.generate() | ||
const alice = await Signer.generate() | ||
const { spaceDid } = await createSpace(alice) | ||
const car = await randomCAR(128) | ||
|
||
const metricsTable = createMetricsTable(REGION, tableName, { | ||
endpoint: t.context.dbEndpoint | ||
}) | ||
|
||
const invocations = [{ | ||
carCid: car.cid.toString(), | ||
value: { | ||
att: [ | ||
UploadCapabilities.remove.create({ | ||
with: spaceDid, | ||
nb: { | ||
root: car.cid | ||
} | ||
}) | ||
], | ||
aud: uploadService.did(), | ||
iss: alice.did() | ||
}, | ||
ts: Date.now() | ||
}] | ||
|
||
// @ts-expect-error | ||
await updateUploadRemoveTotal(invocations, { | ||
metricsTable | ||
}) | ||
|
||
const item = await getItemFromTable(t.context.dynamoClient, tableName, { | ||
name: METRICS_NAMES.UPLOAD_REMOVE_TOTAL | ||
}) | ||
t.truthy(item) | ||
t.is(item?.name, METRICS_NAMES.UPLOAD_REMOVE_TOTAL) | ||
t.is(item?.value, 1) | ||
}) | ||
|
||
test('handles batch of single invocations with multiple upload/remove attributes', async t => { | ||
const { tableName } = await prepareResources(t.context.dynamoClient) | ||
const uploadService = await Signer.generate() | ||
const alice = await Signer.generate() | ||
const { spaceDid } = await createSpace(alice) | ||
|
||
const cars = await Promise.all( | ||
Array.from({ length: 10 }).map(() => randomCAR(128)) | ||
) | ||
|
||
const metricsTable = createMetricsTable(REGION, tableName, { | ||
endpoint: t.context.dbEndpoint | ||
}) | ||
|
||
const invocations = [{ | ||
carCid: cars[0].cid.toString(), | ||
value: { | ||
att: cars.map((car) => UploadCapabilities.remove.create({ | ||
with: spaceDid, | ||
nb: { | ||
root: car.cid | ||
} | ||
})), | ||
aud: uploadService.did(), | ||
iss: alice.did() | ||
}, | ||
ts: Date.now() | ||
}] | ||
|
||
// @ts-expect-error | ||
await updateUploadRemoveTotal(invocations, { | ||
metricsTable | ||
}) | ||
|
||
const item = await getItemFromTable(t.context.dynamoClient, tableName, { | ||
name: METRICS_NAMES.UPLOAD_REMOVE_TOTAL | ||
}) | ||
|
||
t.truthy(item) | ||
t.is(item?.name, METRICS_NAMES.UPLOAD_REMOVE_TOTAL) | ||
t.is(item?.value, cars.length) | ||
}) | ||
|
||
test('handles a batch of single invocation without upload/remove', async t => { | ||
const { tableName } = await prepareResources(t.context.dynamoClient) | ||
const uploadService = await Signer.generate() | ||
const alice = await Signer.generate() | ||
const { spaceDid } = await createSpace(alice) | ||
const car = await randomCAR(128) | ||
|
||
const metricsTable = createMetricsTable(REGION, tableName, { | ||
endpoint: t.context.dbEndpoint | ||
}) | ||
|
||
const invocations = [{ | ||
carCid: car.cid.toString(), | ||
value: { | ||
att: [ | ||
UploadCapabilities.add.create({ | ||
with: spaceDid, | ||
nb: { | ||
root: car.cid, | ||
shards: [car.cid] | ||
} | ||
}) | ||
], | ||
aud: uploadService.did(), | ||
iss: alice.did() | ||
}, | ||
ts: Date.now() | ||
}] | ||
|
||
// @ts-expect-error | ||
await updateUploadRemoveTotal(invocations, { | ||
metricsTable | ||
}) | ||
|
||
const item = await getItemFromTable(t.context.dynamoClient, tableName, { | ||
name: METRICS_NAMES.UPLOAD_REMOVE_TOTAL | ||
}) | ||
|
||
t.truthy(item) | ||
t.is(item?.name, METRICS_NAMES.UPLOAD_REMOVE_TOTAL) | ||
t.is(item?.value, 0) | ||
}) | ||
|
||
/** | ||
* @param {import('@aws-sdk/client-dynamodb').DynamoDBClient} dynamoClient | ||
*/ | ||
async function prepareResources (dynamoClient) { | ||
const [ tableName ] = await Promise.all([ | ||
createDynamoAdminMetricsTable(dynamoClient), | ||
]) | ||
|
||
return { | ||
tableName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters