Skip to content

Commit

Permalink
feat: adapt validation in OTelBin next.js app to changed lambda contract
Browse files Browse the repository at this point in the history
  • Loading branch information
bripkens committed Jan 11, 2024
1 parent bd78cba commit 8e45fe1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
11 changes: 10 additions & 1 deletion packages/otelbin/package-lock.json

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

3 changes: 2 additions & 1 deletion packages/otelbin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"tailwindcss-animate": "^1.0.6",
"ts-jest": "^29.1.1",
"yaml": "^2.3.4",
"yaml-language-server": "^1.14.0"
"yaml-language-server": "^1.14.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@svgr/webpack": "^8.1.0",
Expand Down
27 changes: 23 additions & 4 deletions packages/otelbin/src/app/validation/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2023 Dash0 Inc.
// SPDX-License-Identifier: Apache-2.0

import z from "zod";
import retryFetch from "fetch-retry";
import { Redis } from "@upstash/redis";
import { Ratelimit } from "@upstash/ratelimit";
Expand All @@ -19,6 +20,12 @@ const rateLimit = new Ratelimit({
prefix: "rate-limit-validate",
});

const validationPayloadSchema = z.object({
config: z.string(),
env: z.record(z.string()).optional(),
});
type ValidationPayload = z.infer<typeof validationPayloadSchema>;

export async function POST(request: NextRequest): Promise<NextResponse> {
const distro = request.nextUrl.searchParams.get("distro");
const version = request.nextUrl.searchParams.get("version");
Expand All @@ -36,7 +43,21 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
);
}

const config = await request.text();
const reqBody = await request.json();
let validationPayload: ValidationPayload;
try {
validationPayload = validationPayloadSchema.parse(reqBody);
} catch (e: unknown) {
console.error('Failed to parse request body as JSON: "%s"', reqBody, e);
return NextResponse.json(
{
error: `Could not parse request body.`,
},
{
status: 400,
}
);
}

const userIdentifier = getUserIdentifier(request);
const { success } = await rateLimit.blockUntilReady(userIdentifier, 1000 * 60);
Expand Down Expand Up @@ -66,9 +87,7 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
"COLLECTOR_CONFIGURATION_VALIDATION_API_KEY env var is not configured"
),
},
body: JSON.stringify({
config,
}),
body: JSON.stringify(validationPayload),
retries: 3,
retryDelay: 1000,
retryOn: [500, 503],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export function useServerSideValidation(): ValidationState {
"Content-Type": "application/json",
Accept: "application/json",
},
body: config,
body: JSON.stringify({
config,
env: {},
}),
}
);

Expand Down

0 comments on commit 8e45fe1

Please sign in to comment.