Skip to content
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
44 changes: 1 addition & 43 deletions src/cloudflare/internal/d1-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,10 @@
import { withSpan } from 'cloudflare-internal:tracing-helpers';
import type { Span } from './tracing';

interface D1Meta {
duration: number;
size_after: number;
rows_read: number;
rows_written: number;
last_row_id: number;
changed_db: boolean;
changes: number;

/**
* The region of the database instance that executed the query.
*/
served_by_region?: string;

/**
* True if-and-only-if the database instance that executed the query was the primary.
*/
served_by_primary?: boolean;

timings?: {
/**
* The duration of the SQL query execution by the database instance. It doesn't include any network time.
*/
sql_duration_ms: number;
};

/**
* Number of total attempts to execute the query, due to automatic retries.
* Note: All other fields in the response like `timings` only apply to the last attempt.
*/
total_attempts?: number;
}

interface Fetcher {
fetch: typeof fetch;
}

type D1Response = {
success: true;
meta: D1Meta & Record<string, unknown>;
error?: never;
};

type D1Result<T = unknown> = D1Response & {
results: T[];
};

type D1RawOptions = {
columnNames?: boolean;
};
Expand Down Expand Up @@ -734,6 +691,7 @@ async function toJson<T = unknown>(response: Response): Promise<T> {
function addAggregatedD1MetaToSpan(span: Span, metas: D1Meta[]): void {
const aggregatedMeta = aggregateD1Meta(metas);
addD1MetaToSpan(span, aggregatedMeta);
span.setAttribute('cloudflare.d1.response.meta', JSON.stringify(metas));
}

function addD1MetaToSpan(span: Span, meta: D1Meta): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const test = {
async test() {
await runInstrumentationTest(state, expectedSpans, {
testName: 'D1 instrumentation',
logReceived: true,
mapFn: (span) => {
// varies test-by-test because random duration in d1-mock.js
if (span['http.response.body.size']) {
Expand Down Expand Up @@ -676,6 +677,8 @@ const expectedSpans = [
'cloudflare.d1.response.last_row_id': 2,
'cloudflare.d1.response.changed_db': false,
'cloudflare.d1.response.changes': 0,
'cloudflare.d1.response.meta':
'[{"duration":0.0026086921579674773,"served_by":"d1-mock","changes":0,"last_row_id":2,"changed_db":false,"size_after":8192,"rows_read":1,"rows_written":0},{"duration":0.009433951424302995,"served_by":"d1-mock","changes":0,"last_row_id":2,"changed_db":false,"size_after":8192,"rows_read":1,"rows_written":0}]',
closed: true,
},
{
Expand Down Expand Up @@ -837,6 +840,8 @@ const expectedSpans = [
'cloudflare.d1.response.last_row_id': 3,
'cloudflare.d1.response.changed_db': true,
'cloudflare.d1.response.changes': 5,
'cloudflare.d1.response.meta':
'[{"duration":0.005720710256240147,"served_by":"d1-mock","changes":0,"last_row_id":2,"changed_db":true,"size_after":12288,"rows_read":1,"rows_written":2},{"duration":0.004320826749400744,"served_by":"d1-mock","changes":0,"last_row_id":2,"changed_db":true,"size_after":16384,"rows_read":1,"rows_written":2},{"duration":0.00036466236099620187,"served_by":"d1-mock","changes":2,"last_row_id":2,"changed_db":true,"size_after":16384,"rows_read":0,"rows_written":2},{"duration":0.00026737735172635004,"served_by":"d1-mock","changes":3,"last_row_id":3,"changed_db":true,"size_after":16384,"rows_read":0,"rows_written":3}]',
closed: true,
},
{
Expand Down Expand Up @@ -994,6 +999,8 @@ const expectedSpans = [
'cloudflare.d1.response.last_row_id': 3,
'cloudflare.d1.response.changed_db': true,
'cloudflare.d1.response.changes': 0,
'cloudflare.d1.response.meta':
'[{"duration":0.00528014130524755,"served_by":"d1-mock","changes":0,"last_row_id":3,"changed_db":true,"size_after":12288,"rows_read":4,"rows_written":0},{"duration":0.004510939746142337,"served_by":"d1-mock","changes":0,"last_row_id":3,"changed_db":true,"size_after":8192,"rows_read":3,"rows_written":0},{"duration":0.009795894690743141,"served_by":"d1-mock","changes":0,"last_row_id":3,"changed_db":true,"size_after":4096,"rows_read":2,"rows_written":0}]',
closed: true,
},
];
3 changes: 2 additions & 1 deletion src/cloudflare/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"cloudflare:*": ["./*"],
"cloudflare-internal:*": ["./internal/*"]
}
}
},
"include": ["**/*", "../../types/defines/d1.d.ts"]
}
Loading