Scaffold monorepo dev setup

This commit is contained in:
dirtydishes 2025-12-27 18:45:26 -05:00
commit d2a09e095a
47 changed files with 1033 additions and 0 deletions

View file

@ -0,0 +1,8 @@
{
"name": "@islandflow/observability",
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
}
}

View file

@ -0,0 +1,2 @@
export * from "./logger";
export * from "./metrics";

View file

@ -0,0 +1,54 @@
export type LogLevel = "debug" | "info" | "warn" | "error";
export type LogContext = Record<string, unknown>;
export type LogRecord = LogContext & {
level: LogLevel;
service: string;
msg: string;
ts: string;
};
export type LoggerFn = (msg: string, context?: LogContext) => void;
export type Logger = {
debug: LoggerFn;
info: LoggerFn;
warn: LoggerFn;
error: LoggerFn;
};
export type LoggerOptions = {
service: string;
now?: () => string;
sink?: (record: LogRecord) => void;
};
const defaultSink = (record: LogRecord) => {
console.log(JSON.stringify(record));
};
export const createLogger = ({
service,
now = () => new Date().toISOString(),
sink = defaultSink
}: LoggerOptions): Logger => {
const write = (level: LogLevel, msg: string, context?: LogContext) => {
const record: LogRecord = {
level,
service,
msg,
ts: now(),
...(context ?? {})
};
sink(record);
};
return {
debug: (msg, context) => write("debug", msg, context),
info: (msg, context) => write("info", msg, context),
warn: (msg, context) => write("warn", msg, context),
error: (msg, context) => write("error", msg, context)
};
};

View file

@ -0,0 +1,51 @@
export type MetricType = "counter" | "gauge" | "timing";
export type MetricTags = Record<string, string>;
export type MetricRecord = {
name: string;
type: MetricType;
value: number;
ts: number;
service?: string;
tags?: MetricTags;
};
export type MetricsEmitter = (record: MetricRecord) => void;
export type MetricsOptions = {
service?: string;
emit?: MetricsEmitter;
now?: () => number;
};
export type Metrics = {
count: (name: string, value?: number, tags?: MetricTags) => void;
gauge: (name: string, value: number, tags?: MetricTags) => void;
timing: (name: string, value: number, tags?: MetricTags) => void;
};
const noopEmit: MetricsEmitter = () => {};
export const createMetrics = ({
service,
emit = noopEmit,
now = () => Date.now()
}: MetricsOptions = {}): Metrics => {
const write = (type: MetricType, name: string, value: number, tags?: MetricTags) => {
emit({
name,
type,
value,
tags,
service,
ts: now()
});
};
return {
count: (name, value = 1, tags) => write("counter", name, value, tags),
gauge: (name, value, tags) => write("gauge", name, value, tags),
timing: (name, value, tags) => write("timing", name, value, tags)
};
};

View file

@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"types": []
},
"include": ["src/**/*.ts"]
}