Skip to content

Commit

Permalink
Add instrumentation to List request for Vercel Blob
Browse files Browse the repository at this point in the history
  • Loading branch information
Rindrics committed Dec 18, 2024
1 parent 38d6eb6 commit 9e97b24
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
32 changes: 22 additions & 10 deletions app/(playground)/p/[agentId]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { anthropic } from "@ai-sdk/anthropic";
import { google } from "@ai-sdk/google";
import { openai } from "@ai-sdk/openai";
import { toJsonSchema } from "@valibot/to-json-schema";
import { del, list, put } from "@vercel/blob";
import { type ListBlobResult, del, list, put } from "@vercel/blob";
import { type LanguageModelV1, jsonSchema, streamObject } from "ai";
import { createStreamableValue } from "ai/rsc";
import { MockLanguageModelV1, simulateReadableStream } from "ai/test";
Expand Down Expand Up @@ -468,26 +468,38 @@ export async function putGraph(graph: Graph) {

export async function remove(fileData: FileData) {
const startTime = Date.now();
const blobList = await list({
prefix: buildFileFolderPath(fileData.id),
});
function calcTotalSize(blobList: ListBlobResult): number {
return blobList.blobs.reduce((sum, blob) => sum + blob.size, 0);
}
const { blobList } = await withCountMeasurement(
createLogger("remove"),
async () => {
const result = await list({
prefix: buildFileFolderPath(fileData.id),
});
return {
blobList: result,
size: calcTotalSize(result),
};
},
ExternalServiceName.VercelBlob,
startTime,
VercelBlobOperation.List,
);

const startTimeDel = Date.now();
if (blobList.blobs.length > 0) {
await withCountMeasurement(
createLogger("remove"),
async () => {
await del(blobList.blobs.map((blob) => blob.url));

const totalSize = blobList.blobs.reduce(
(sum, blob) => sum + blob.size,
0,
);
return {
size: totalSize,
size: calcTotalSize(blobList),
};
},
ExternalServiceName.VercelBlob,
startTime,
startTimeDel,
VercelBlobOperation.Del,
);
waitForTelemetryExport();
Expand Down
22 changes: 18 additions & 4 deletions app/(playground)/p/[agentId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,30 @@ export default async function Page({
async function persistGraph(graph: Graph) {
"use server";
const startTime = Date.now();
const logger = createLogger("persistGraph");
const { url } = await putGraph(graph);
await db
.update(agents)
.set({
graphUrl: url,
})
.where(eq(agents.id, agentId));
const blobList = await list({
prefix: buildGraphFolderPath(graph.id),
});
const { blobList } = await withCountMeasurement(
logger,
async () => {
const result = await list({
prefix: buildGraphFolderPath(graph.id),
});
const size = result.blobs.reduce((sum, blob) => sum + blob.size, 0);
return {
blobList: result,
size,
};
},
ExternalServiceName.VercelBlob,
startTime,
VercelBlobOperation.List,
);

const oldBlobs = blobList.blobs
.filter((blob) => blob.url !== url)
Expand All @@ -91,7 +105,7 @@ export default async function Page({
}));
if (oldBlobs.length > 0) {
await withCountMeasurement(
createLogger("persistGraph"),
logger,
async () => {
await del(oldBlobs.map((blob) => blob.url));
const totalSize = oldBlobs.reduce((sum, blob) => sum + blob.size, 0);
Expand Down
7 changes: 7 additions & 0 deletions lib/opentelemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ const VercelBlobDelSchema = RequestCount.extend({
blobSizeStored: z.number(), // minus the size of the blob deleted
});

const VercelBlobListSchema = RequestCount.extend({
externalServiceName: z.literal(ExternalServiceName.VercelBlob),
operationType: z.literal("list"),
blobSizeTransfered: z.number(),
});

const VercelBlobRequestCountSchema = z.discriminatedUnion("operationType", [
VercelBlobPutSchema,
VercelBlobFetchSchema,
VercelBlobDelSchema,
VercelBlobListSchema,
]);

const RequestCountSchema = z.union([
Expand Down
6 changes: 6 additions & 0 deletions lib/opentelemetry/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ export const VercelBlobOperation = {
blobSizeStored: -result.size,
}),
},
List: {
type: "list" as const,
measure: (result: { size: number }) => ({
blobSizeTransfered: result.size,
}),
},
} as const;

type VercelBlobOperationType =
Expand Down

0 comments on commit 9e97b24

Please sign in to comment.