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

template: Make all configuration values explicit #1560

Merged
merged 2 commits into from
May 20, 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
9 changes: 9 additions & 0 deletions .changeset/tasty-schools-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'skuba': patch
---

template: Make all configuration values explicit

Previously, `src/config.ts` included optional configuration values and inheritance between environments in the spirit of [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). While the templated file was wired up in a "safe" way—the production environment never inherited from other environments and explicitly specified all its configuration values—its pattern was misappropriated elsewhere and led to local configuration values affecting production environments.

Instead, we now list all configuration values explicitly against each environment.
4 changes: 2 additions & 2 deletions docs/cli/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ For example, your `src/app.ts` may look like:
import 'skuba-dive/register';

// You can use the `src` module alias after registration.
import { rootLogger } 'src/framework/logging';
import { logger } 'src/framework/logging';
```

---
Expand Down Expand Up @@ -73,7 +73,7 @@ For example, your `src/app.ts` may look like:
import 'skuba-dive/register';

// You can use the `src` module alias after registration.
import { rootLogger } 'src/framework/logging';
import { logger } 'src/framework/logging';
```

### Start an executable script
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
"optional": true
}
},
"packageManager": "pnpm@9.0.5",
"packageManager": "pnpm@9.1.0",
"engines": {
"node": ">=18.18.0"
},
Expand Down
2 changes: 2 additions & 0 deletions template/express-rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"dependencies": {
"@seek/logger": "^6.0.0",
"express": "^4.17.1",
"hot-shots": "^10.0.0",
"seek-datadog-custom-metrics": "^4.6.3",
"skuba-dive": "^2.0.0"
},
"devDependencies": {
Expand Down
20 changes: 14 additions & 6 deletions template/express-rest-api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ interface Config {
name: string;
version: string;

metricsServer?: string;
port?: number;
metricsServer: string | null;
port: number | null;
}

type Environment = (typeof environments)[number];
Expand All @@ -26,19 +26,27 @@ const configs: Record<Environment, () => Omit<Config, 'environment'>> = {
logLevel: 'debug',
name: '<%- serviceName %>',
version: 'local',

metricsServer: null,
port: null,
}),

test: () => ({
...configs.local(),

logLevel: Env.string('LOG_LEVEL', { default: 'silent' }),
name: '<%- serviceName %>',
version: 'test',

metricsServer: null,
port: null,
}),

[dev]: () => ({
...configs[prod](),

logLevel: 'debug',
name: Env.string('SERVICE'),
version: Env.string('VERSION'),

metricsServer: 'localhost',
port: Env.nonNegativeInteger('PORT'),
}),

[prod]: () => ({
Expand Down
2 changes: 1 addition & 1 deletion template/express-rest-api/src/framework/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import createLogger from '@seek/logger';

import { config } from 'src/config';

export const rootLogger = createLogger({
export const logger = createLogger({
base: {
environment: config.environment,
version: config.version,
Expand Down
11 changes: 11 additions & 0 deletions template/express-rest-api/src/framework/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { StatsD } from 'hot-shots';
import { createStatsDClient } from 'seek-datadog-custom-metrics';

import { config } from 'src/config';

import { logger } from './logging';

/* istanbul ignore next: StatsD client is not our responsibility */
export const metricsClient = createStatsDClient(StatsD, config, (err) =>
logger.error({ err }, 'StatsD error'),
);
4 changes: 2 additions & 2 deletions template/express-rest-api/src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './register';

import app from './app';
import { config } from './config';
import { rootLogger } from './framework/logging';
import { logger } from './framework/logging';

// If your application is deployed with more than 1 vCPU you can delete this
// file and use a clustering utility to run `lib/app`.
Expand All @@ -11,7 +11,7 @@ const listener = app.listen(config.port, () => {
const address = listener.address();

if (typeof address === 'object' && address) {
rootLogger.debug(`listening on port ${address.port}`);
logger.debug(`listening on port ${address.port}`);
}
});

Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa-compose": "^4.2.0",
"seek-datadog-custom-metrics": "^4.2.1",
"seek-datadog-custom-metrics": "^4.6.3",
"seek-koala": "^7.0.0",
"skuba-dive": "^2.0.0",
"zod": "^3.19.1"
Expand Down
18 changes: 14 additions & 4 deletions template/koa-rest-api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ interface Config {
name: string;
version: string;

metricsServer?: string;
port?: number;
metricsServer: string | null;
port: number | null;
}

type Environment = (typeof environments)[number];
Expand All @@ -26,19 +26,29 @@ const configs: Record<Environment, () => Omit<Config, 'environment'>> = {
logLevel: 'debug',
name: '<%- serviceName %>',
version: 'local',

metricsServer: null,
port: null,
}),

test: () => ({
...configs.local(),

logLevel: Env.string('LOG_LEVEL', { default: 'silent' }),
name: '<%- serviceName %>',
version: 'test',

metricsServer: null,
port: null,
}),

[dev]: () => ({
...configs[prod](),

logLevel: 'debug',
name: Env.string('SERVICE'),
version: Env.string('VERSION'),

metricsServer: 'localhost',
port: Env.nonNegativeInteger('PORT'),
}),

[prod]: () => ({
Expand Down
16 changes: 6 additions & 10 deletions template/lambda-sqs-worker-cdk/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ interface Config {

type Environment = (typeof environments)[number];

const dev = 'dev';
const prod = 'prod';

const environments = ['local', 'test', dev, prod] as const;
const environments = ['local', 'test', 'dev', 'prod'] as const;

const environment = Env.oneOf(environments)('ENVIRONMENT');

Expand All @@ -26,19 +23,18 @@ const configs: Record<Environment, () => Omit<Config, 'environment'>> = {
}),

test: () => ({
...configs.local(),

logLevel: Env.string('LOG_LEVEL', { default: 'silent' }),
name: '<%- serviceName %>',
version: 'test',
}),

[dev]: () => ({
...configs[prod](),

dev: () => ({
logLevel: 'debug',
name: Env.string('SERVICE'),
version: Env.string('VERSION'),
}),

[prod]: () => ({
prod: () => ({
logLevel: 'info',
name: Env.string('SERVICE'),
version: Env.string('VERSION'),
Expand Down
13 changes: 9 additions & 4 deletions template/lambda-sqs-worker/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ const configs: Record<Environment, () => Omit<Config, 'environment'>> = {
}),

test: () => ({
...configs.local(),

logLevel: Env.string('LOG_LEVEL', { default: 'silent' }),
metrics: false,
name: '<%- serviceName %>',
version: 'test',

destinationSnsTopicArn: 'arn:aws:sns:us-east-2:123456789012:destination',
}),

dev: () => ({
...configs.prod(),

logLevel: 'debug',
metrics: true,
name: Env.string('SERVICE'),
version: Env.string('VERSION'),

destinationSnsTopicArn: Env.string('DESTINATION_SNS_TOPIC_ARN'),
}),

prod: () => ({
Expand Down
Loading