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

Add sentry error tracking #22

Merged
merged 6 commits into from
Jul 31, 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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
run: deno lint

- name: Run tests
env:
DOPPLER_ENVIRONMENT: gitlab_ci
KEYS_VERSION: gitlab_ci
run: |
deno test --coverage=coverage
deno coverage ./coverage --lcov --output=coverage.lcov
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ ADD . .
# Compile the main app so that it doesn't need to be compiled each startup/entry.
RUN deno cache main.ts

CMD ["run", "--allow-net", "--allow-env", "main.ts"]
CMD ["run", "--allow-net", "--allow-env", "--allow-read=./src", "main.ts"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ curl "https://keys.demery.net/api?allOf=demery&allOf=thunderbird&noneOf=disabled
### Running locally

```sh
deno run --allow-net --allow-env main.ts
doppler run -- deno run --allow-net --allow-env --allow-read=./src main.ts
```

### Run tests
Expand Down
38 changes: 38 additions & 0 deletions deno.lock

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

1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { z } from "https://deno.land/x/[email protected]/mod.ts";
export { ZodError } from "https://deno.land/x/[email protected]/ZodError.ts";
export * as Sentry from "https://deno.land/x/[email protected]/index.mjs";
6 changes: 3 additions & 3 deletions flake.lock

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

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
nativeBuildInputs = [ pkgs.bashInteractive ];
buildInputs = with pkgs; [
deno
doppler
];
};
});
Expand Down
2 changes: 1 addition & 1 deletion helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
- name: http-web
containerPort: 8000
env:
- name: VERSION
- name: KEYS_VERSION
value: {{ .Values.version | quote }}
envFrom:
- secretRef:
Expand Down
24 changes: 19 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@ import start from "./src/server.ts";
import keys from "./src/public_keys.ts";
import { filterIncludesKey, parseParameters } from "./src/filter.ts";
import { parseEnvironmentVariables } from "./src/environment.ts";
import { Sentry } from "./deps.ts";

const environment = parseEnvironmentVariables(Deno.env.toObject());

start(environment.PORT, {
filterIncludesKey,
parseParameters,
keys,
});
if (environment.SENTRY_DSN) {
Sentry.init({
dsn: environment.SENTRY_DSN,
environment: environment.DOPPLER_ENVIRONMENT,
tracesSampleRate: 1.0,
release: environment.KEYS_VERSION,
});
}

start(
environment.PORT,
{
filterIncludesKey,
parseParameters,
keys,
},
environment.KEYS_VERSION,
);
21 changes: 18 additions & 3 deletions src/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,43 @@ import {
import { parseEnvironmentVariables } from "./environment.ts";
import { ZodError } from "../deps.ts";

const baseVariables = {
DOPPLER_ENVIRONMENT: "unit_tests",
KEYS_VERSION: "unit_tests",
};

Deno.test(
"parseEnvironmentVariables: must convert string variables to correct types",
() => {
const variables = {
...baseVariables,
PORT: "1234",
};

assertEquals(parseEnvironmentVariables(variables), { PORT: 1234 });
assertEquals(parseEnvironmentVariables(variables), {
...baseVariables,
PORT: 1234,
});
},
);
Deno.test(
"parseEnvironmentVariables: must use defaults if variables are not supplied",
() => {
const variables = {};
const variables = {
...baseVariables,
};

assertEquals(parseEnvironmentVariables(variables), { PORT: 8000 });
assertEquals(parseEnvironmentVariables(variables), {
...baseVariables,
PORT: 8000,
});
},
);
Deno.test(
"parseEnvironmentVariables: must throw ZodError if input is invalid",
() => {
const variables = {
...baseVariables,
PORT: "not-a-number",
};

Expand Down
3 changes: 3 additions & 0 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { z } from "../deps.ts";

const environmentSchema = z.object({
PORT: z.string().regex(/^\d+$/).transform(Number).default("8000"),
DOPPLER_ENVIRONMENT: z.string(),
SENTRY_DSN: z.string().optional(),
KEYS_VERSION: z.string(),
});

export function parseEnvironmentVariables(variableObject: unknown) {
Expand Down
7 changes: 5 additions & 2 deletions src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Deno.test(
const response = await handleRequest(
new Request(`${TEST_URL}/not_found`),
emptyDependencies,
"unit_tests",
);

assertEquals(response.status, 404);
Expand All @@ -51,6 +52,7 @@ Deno.test("handleRequest: must return pgp key for /pgp", async () => {
const response = await handleRequest(
new Request(`${TEST_URL}/pgp`),
emptyDependencies,
"unit_tests",
);

assertEquals(response.status, 200);
Expand All @@ -67,6 +69,7 @@ Deno.test(
const response = await handleRequest(
new Request(`${TEST_URL}/pgp/daniel_emery.pub.asc`),
emptyDependencies,
"unit_tests",
);

assertEquals(response.status, 200);
Expand Down Expand Up @@ -94,7 +97,7 @@ Deno.test(
parseParameters: parseParametersSpy,
filterIncludesKey: filterIncludesKeySpy,
keys: fakeKeys,
});
}, "unit_tests");

assertSpyCalls(parseParametersSpy, 1);
assertSpyCall(parseParametersSpy, 0, {
Expand Down Expand Up @@ -127,7 +130,7 @@ Deno.test(
parseParameters: parseParametersStub,
filterIncludesKey: filterIncludesKeyStub,
keys: fakeKeys,
});
}, "unit_tests");

assertSpyCalls(parseParametersStub, 1);
assertSpyCall(parseParametersStub, 0, {
Expand Down
17 changes: 14 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
* parameters.
* @param port The port to listen on.
*/
export default function start(port: number, dependencies: ServerDependencies) {
export default function start(
port: number,
dependencies: ServerDependencies,
version: string,

Check warning on line 25 in src/server.ts

View check run for this annotation

Codecov / codecov/patch

src/server.ts#L22-L25

Added lines #L22 - L25 were not covered by tests
) {
console.log(`Server listening at :${port}/api`);
Deno.serve({
port,
handler: (req) => handleRequest(req, dependencies),
handler: (req) => handleRequest(req, dependencies, version),

Check warning on line 30 in src/server.ts

View check run for this annotation

Codecov / codecov/patch

src/server.ts#L30

Added line #L30 was not covered by tests
});
}

export function handleRequest(req: Request, dependencies: ServerDependencies) {
export function handleRequest(
req: Request,
dependencies: ServerDependencies,
version: string,
) {
const { filterIncludesKey, parseParameters, keys } = dependencies;
try {
const url = new URL(req.url);
Expand Down Expand Up @@ -72,6 +80,9 @@
return new Response(responseData, {
status: STATUS_CODE.OK,
statusText: STATUS_TEXT[STATUS_CODE.OK],
headers: {
"X-Keys-Version": version,
},
});
} catch (err) {
console.error(err);
Expand Down
Loading