Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ucan stream consumer provider add count #182

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions stacks/ucan-invocation-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ export function UcanInvocationStack({ stack, app }) {
handler: 'functions/metrics-upload-remove-total.consumer',
deadLetterQueue: metricsUploadRemoveTotalDLQ.cdk.queue,
})

// metrics provider/add count
const metricsProviderAddTotalDLQ = new Queue(stack, 'metrics-provider-add-total-dlq')
const metricsProviderAddTotalConsumer = new Function(stack, 'metrics-provider-add-total-consumer', {
environment: {
TABLE_NAME: adminMetricsTable.tableName
},
permissions: [adminMetricsTable],
handler: 'functions/metrics-provider-add-total.consumer',
deadLetterQueue: metricsProviderAddTotalDLQ.cdk.queue,
})

// metrics per space
const spaceMetricsDLQ = new Queue(stack, 'space-metrics-dlq')
Expand Down Expand Up @@ -258,6 +269,14 @@ export function UcanInvocationStack({ stack, app }) {
}
}
},
metricsConsumerAddTotalConsumer: {
function: metricsProviderAddTotalConsumer,
cdk: {
eventSource: {
...(getKinesisEventSourceConfig(stack))
}
}
},
spaceMetricsUploadAddTotalConsumer: {
function: spaceMetricsUploadAddTotalConsumer,
cdk: {
Expand Down
5 changes: 5 additions & 0 deletions ucan-invocation/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {
add as providerAdd,
} from '@web3-storage/capabilities/provider'
import {
add as storeAdd,
remove as storeRemove
Expand All @@ -17,6 +20,7 @@ export const STORE_ADD = storeAdd.can
export const STORE_REMOVE = storeRemove.can
export const UPLOAD_ADD = uploadAdd.can
export const UPLOAD_REMOVE = uploadRemove.can
export const PROVIDER_ADD = providerAdd.can

// Admin Metrics
export const METRICS_NAMES = {
Expand All @@ -26,6 +30,7 @@ export const METRICS_NAMES = {
STORE_ADD_SIZE_TOTAL: `${STORE_ADD}-size-total`,
STORE_REMOVE_TOTAL: `${STORE_REMOVE}-total`,
STORE_REMOVE_SIZE_TOTAL: `${STORE_REMOVE}-size-total`,
PROVIDER_ADD_TOTAL: `${PROVIDER_ADD}-total`,
}

// Spade Metrics
Expand Down
47 changes: 47 additions & 0 deletions ucan-invocation/functions/metrics-provider-add-total.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as Sentry from '@sentry/serverless'

import { hasOkReceipt } from '../utils/receipt.js'
import { createMetricsTable } from '../tables/metrics.js'
import { parseKinesisEvent } from '../utils/parse-kinesis-event.js'
import { PROVIDER_ADD } 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 updateProviderAddTotal(ucanInvocations, {
metricsTable: createMetricsTable(AWS_REGION, tableName, {
endpoint: dbEndpoint
})
})
}

/**
* @param {import('../types').UcanInvocation[]} ucanInvocations
* @param {import('../types').TotalSizeCtx} ctx
*/
export async function updateProviderAddTotal (ucanInvocations, ctx) {
const invocationsWithConsumerAdd = ucanInvocations.filter(
inv => inv.value.att.find(a => a.can === PROVIDER_ADD) && hasOkReceipt(inv)
).flatMap(inv => inv.value.att)

await ctx.metricsTable.incrementProviderAddTotal(invocationsWithConsumerAdd)
}

export const consumer = Sentry.AWSLambda.wrapHandler(handler)
2 changes: 1 addition & 1 deletion ucan-invocation/functions/metrics-upload-add-total.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ export async function updateUploadAddTotal (ucanInvocations, ctx) {
await ctx.metricsTable.incrementUploadAddTotal(invocationsWithUploadAdd)
}

export const consumer = Sentry.AWSLambda.wrapHandler(handler)
export const consumer = Sentry.AWSLambda.wrapHandler(handler)
5 changes: 3 additions & 2 deletions ucan-invocation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
"@aws-sdk/client-dynamodb": "^3.226.0",
"@aws-sdk/client-eventbridge": "^3.218.0",
"@sentry/serverless": "^7.22.0",
"@web3-storage/capabilities": "^3.2.0",
"@web3-storage/capabilities": "^4.0.1",
"uint8arrays": "^4.0.2"
},
"devDependencies": {
"@aws-sdk/client-dynamodb": "^3.226.0",
"@aws-sdk/client-s3": "^3.226.0",
"@serverless-stack/resources": "*",
"@ucanto/interface": "^4.2.3",
"@ucanto/interface": "^5.0.0",
"@ucanto/principal": "^5.0.0",
"ava": "^4.3.3",
"multiformats": "^11.0.1",
"nanoid": "4.0.0",
Expand Down
22 changes: 22 additions & 0 deletions ucan-invocation/tables/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ export function createMetricsTable (region, tableName, options = {}) {
})
})

await dynamoDb.send(updateCmd)
},
/**
* Increment total count from provider/add operations.
*
* @param {Capabilities} operationsInv
*/
incrementProviderAddTotal: async (operationsInv) => {
const invTotalSize = operationsInv.length

const updateCmd = new UpdateItemCommand({
TableName: tableName,
UpdateExpression: `ADD #value :value`,
ExpressionAttributeNames: {'#value': 'value'},
ExpressionAttributeValues: {
':value': { N: String(invTotalSize) },
},
Key: marshall({
name: METRICS_NAMES.PROVIDER_ADD_TOTAL
})
})

await dynamoDb.send(updateCmd)
}
}
Expand Down
Loading