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

src: add sentry breadcrumbs, tail worker #164

Merged
merged 2 commits into from
Nov 11, 2024
Merged
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
7 changes: 7 additions & 0 deletions src/middleware/cacheMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export function cached(middleware: Middleware): Middleware {

const wrapper: Middleware = {
async handle(request, ctx, next) {
ctx.sentry.addBreadcrumb({
category: 'CacheMiddleware',
data: {
underlyingMiddleware: middleware.constructor.name,
},
});

if (!isCacheEnabled(ctx.env)) {
return middleware.handle(request, ctx, next);
}
Expand Down
8 changes: 7 additions & 1 deletion src/middleware/notFoundMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { Context } from '../context';
import responses from '../responses';
import type { Request } from '../routes/request';
import type { Middleware } from './middleware';

export class NotFoundMiddleware implements Middleware {
handle(request: Request): Promise<Response> {
handle(request: Request, ctx: Context): Promise<Response> {
ctx.sentry.addBreadcrumb({
category: 'NotFoundMiddleware',
message: 'hit',
});

return Promise.resolve(responses.fileNotFound(request.method));
}
}
8 changes: 7 additions & 1 deletion src/middleware/optionsMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { CACHE_HEADERS } from '../constants/cache';
import type { Context } from '../context';
import type { Middleware } from './middleware';

/**
* Handles OPTION requests, just returns what HTTP methods we support
*/
export class OptionsMiddleware implements Middleware {
handle(): Promise<Response> {
handle(_: Request, ctx: Context): Promise<Response> {
ctx.sentry.addBreadcrumb({
category: 'OptionsMiddleware',
message: 'hit',
});

return Promise.resolve(
new Response(undefined, {
headers: {
Expand Down
5 changes: 5 additions & 0 deletions src/middleware/originMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import type { Middleware } from './middleware';
*/
export class OriginMiddleware implements Middleware {
async handle(request: Request, ctx: Context): Promise<Response> {
ctx.sentry.addBreadcrumb({
category: 'OriginMiddleware',
message: 'hit',
});

const res = await fetch(
`${ctx.env.ORIGIN_HOST}${request.urlObj.pathname}`,
{
Expand Down
8 changes: 8 additions & 0 deletions src/middleware/r2Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export class R2Middleware implements Middleware {
const path = getR2Path(request);
const isPathADirectory = isDirectoryPath(path);

ctx.sentry.addBreadcrumb({
category: 'R2Middleware',
data: {
r2Path: path,
isPathADirectory,
},
});

return isPathADirectory
? handleDirectory(request, path, ctx)
: handleFile(request, path, ctx);
Expand Down
19 changes: 13 additions & 6 deletions src/middleware/subtituteMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ export class SubtitutionMiddleware implements Middleware {
this.replaceValue = replaceValue;
}

handle(request: Request, _: Context): Promise<Response> {
handle(request: Request, ctx: Context): Promise<Response> {
const newUrl = request.url.replaceAll(this.searchValue, this.replaceValue);

ctx.sentry.addBreadcrumb({
type: 'navigation',
category: 'SubstitutionMiddleware',
data: {
from: request.url,
to: newUrl,
},
});

// todo fix this
return Promise.resolve(
Response.redirect(
request.url.replaceAll(this.searchValue, this.replaceValue)
)
);
return Promise.resolve(Response.redirect(newUrl));
}
}
6 changes: 5 additions & 1 deletion tests/unit/middleware/substituteMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ it('correctly substitutes url `/dist/latest` to `/dist/v1.0.0`', async () => {
const middleware = new SubtitutionMiddleware(router, 'latest', 'v1.0.0');

// @ts-expect-error full request & ctx not needed
middleware.handle(originalRequest, undefined);
middleware.handle(originalRequest, {
sentry: {
addBreadcrumb: () => {},
},
});
});
3 changes: 3 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ S3_ENDPOINT = 'https://07be8d2fbc940503ca1be344714cb0d1.r2.cloudflarestorage.com
BUCKET_NAME='dist-prod'
ORIGIN_HOST = 'https://origin.nodejs.org'

[env.prod.tail_consumers]
service = "dist-worker-prod-tail"

[[env.prod.r2_buckets]]
binding = 'R2_BUCKET'
preview_bucket_name = 'dist-prod'
Expand Down
Loading