atrace
Application development and telemetry toolkit.
Features
- Errors
- Standardizes error codes
- Extensible custom errors
- Causal chain utilities
- Logging
- Inferred contextual metadata, including tracing information
- Structured metadata
- Compatible with
pino
- Settings
- Convenient with Node and browser environments (WebPack, ...)
- Validation for missing and invalid settings with informative errors
- Tight types inferred with minimal boilerplate
- Easy to test
Quickstart
Errors
Good error handling is a prerequisite for good logging. To help with this,
atrace provides two simple but powerful building blocks.
Libraries
The libraryErrorClass function generates a base error class which provides:
- Namespaced error codes;
- Causal chains;
- Optional structured data.
import {libraryErrorClass} from 'atrace';
const GenericError = libraryErrorClass('my-lib');
const err1 = new GenericError('SOMETHING_FAILED');
type ErrorID = 'BAR_MISSING' | 'BAZ_BROKEN';
const SpecificError = libraryErrorClass<ErrorID>('my-lib');
const err2 = new SpecificError('BAR_MISSING');
const err3 = new SpecificError('SOMETHING_FAILED');
class CustomError extends libraryErrorClass<ErrorID>('my-lib') {
constructor(id: ErrorID, someData: number) {
super(id, {values: {someData}});
}
}
const err4 = new CustomError('BAZ_BROKEN', 123);
Applications
Beyond the capabilities offered by library errors, applications typically also
need the ability to surface statuses back to the client. As a convenience, the
standardError function is also available to create application errors from a
status.
import {applicationErrorFactory, standardError} from 'atrace';
const newError = applicationErrorFactory('bar', {
'BAD_TOGGLE': 'INVALID_ARGUMENT',
'BOOM': 'INTERNAL',
});
const err1 = newError('BAD_TOGGLE');
const err2 = standardError('NOT_FOUND');
Settings
import {
intSetting,
invalidSource,
settingsProvider,
stringSetting,
} from 'atrace';
const settings = settingsProvider((env) => ({
port: intSetting(env.PORT ?? 8080),
auth: {
clientID: stringSetting(env.CLIENT_ID ?? 'my-client'),
clientSecret: stringSetting({
source: env.CLIENT_SECRET ?? invalidSource,
sensitive: true,
}),
},
tag: stringSetting(env.TAG),
}));
const val = settings();
Importantly, val's type will automatically be inferred as:
{
readonly port: number;
readonly auth: {
readonly clientID: string;
readonly clientSecret: string;
}
readonly tag: string | undefined;
}
If CLIENT_SECRET wasn't defined in the environment, the config provider will
throw an informative exception. To test different setting values without needing
mocks, pass in a custom environment when calling the config provider.
const val = settings({
CLIENT_ID: 'test-id',
CLIENT_SECRET: 'test-secret',
});
Defining a custom setting
We provide a convenience factory builder for types which do not need any
non-standard creation arguments.
import {simpleSettingFactory} from 'atrace';
const dateSetting = simpleSettingFactory((s): Date => new Date(s));
If additional arguments are desired, it's always possible--just more verbose--to
use the underlying setting factory directly. For example, here's one way to
implement an enum setting:
import {newSetting, Setting, SettingParams, SettingSource} from 'atrace';
export interface EnumSettingParams<E extends string, S>
extends SettingParams<S> {
readonly symbols: ReadonlyArray<E>;
}
export function enumSetting<E extends string, S extends SettingSource>(
params: EnumSettingParams<E, S>
): Setting<S, E> {
return newSetting(params, (s): any => {
if (!~params.symbols.indexOf(s as any)) {
throw new Error('Invalid enum value');
}
return s;
});
}
Recommended project structure
In production code, we recommend using modular configuration types. Performing
post-processing on setting values is easy with a settingsManager:
import {settingsManager} from 'atrace';
const withSettings = settingsManager((env) => ({}));
export interface ModuleAConfig {}
export interface ModuleBConfig {}
export interface Config {
readonly moduleA: ModuleAConfig;
readonly moduleB: ModuleBConfig;
}
export const config = withSettings((val) => {
return {};
});
import * as sut from '../src/config';
const env = {};
expect(config(env)).toEqual();