Opinionated Omni Observability for Gunshi
README.md

gunsho11y #

Opinionated omni observability (o11y) plugin for Gunshi

Table of Contents #

Features #

  • Hierarchical logger names ala debug module (foo:bar:baz)
  • DEBUG= env var support for debug-style filtering by default
  • Default ndjson output
  • Redaction support with contributions from other plugins and the app
  • Shared root logger across commands
  • Optional pretty printing for development
  • Optional gunshi-c12 plugin integration
  • OpenTelemetry instrumentation support
  • Optional OpenTelemetry-compatible timestamp field (nanoseconds since Unix epoch)

Install #

pnpm install gunshi-pino

Usage #

Basic Usage #

import { cli } from "gunshi"
import pino from "gunshi-pino"

const command = {
	name: "app",
	run: (ctx) => {
		const log = ctx.extensions["gunshi-pino"].getLogger("app:db")
		log.info("connected to database")

		const http = ctx.extensions["gunshi-pino"].getLogger("app:http")
		http.debug({ port: 3000 }, "server started")
	},
}

await cli(process.argv.slice(2), command, {
	plugins: [pino()],
})

DEBUG Filtering #

Filter logging by setting the DEBUG environment variable:

# Enable all loggers
DEBUG=* node src/cli.ts

# Enable specific logger
DEBUG=app:db node src/cli.ts

# Enable namespace with wildcard
DEBUG=app:* node src/cli.ts

# Enable multiple patterns
DEBUG=app:db,http:* node src/cli.ts

# Exclude specific patterns
DEBUG=*,-app:internal node src/cli.ts

More specific patterns take precedence:

# Disable app:*, but enable app:db specifically
DEBUG=-app:*,app:db node src/cli.ts

Specify log levels per pattern with pipe syntax:

# Set app:db to debug level, http:* to warn level
DEBUG=app:db|debug,http:*|warn node src/cli.ts

# Mix patterns with and without levels (no level = trace)
DEBUG=app:*|debug,telemetry node src/cli.ts

# Combine with wildcards and specificity
DEBUG=*|info,app:*|debug,app:db|trace node src/cli.ts

Redaction #

Redact sensitive fields from log output:

import pino from "gunshi-pino"

const plugin = pino({
	redactions: ["password", "secret", "user.ssn", "headers.authorization"],
})

Add redactions dynamically:

const command = {
	run: (ctx) => {
		const ext = ctx.extensions["gunshi-pino"]
		ext.addRedactions(["apiKey", "token"])

		const log = ext.getLogger("app")
		log.info({ password: "secret123" }, "user login")
		// {"level":30,"name":"app","password":"[Redacted]","msg":"user login"}
	},
}

Pretty Printing #

Enable pretty output for development:

import pino from "gunshi-pino"

const plugin = pino({
	pretty: true,
})

Requires pino-pretty to be installed:

pnpm install pino-pretty

OpenTelemetry Integration #

Enable OpenTelemetry instrumentation for distributed tracing:

import pino from "gunshi-pino"
import { NodeSDK } from "@opentelemetry/sdk-node"

const sdk = new NodeSDK({
	// ... other OTEL configuration
})

await sdk.start()

const plugin = pino({
	opentelemetry: {
		sdk,
	},
})

Requires @opentelemetry/sdk-node and other OpenTelemetry packages to be installed:

pnpm add @opentelemetry/sdk-node

Timestamp Field #

Add an OpenTelemetry-compatible timestamp field (nanoseconds since Unix epoch) to all log entries:

import pino from "gunshi-pino"

// Use default hrtime.bigint implementation
const plugin = pino({
	timestamper: true,
})

Use the performance.now() implementation instead:

import pino, { performanceTimestamp } from "gunshi-pino"

const plugin = pino({
	timestamper: performanceTimestamp,
})

Provide a custom timestamper function:

import pino from "gunshi-pino"

const plugin = pino({
	timestamper: () => Date.now() * 1_000_000, // milliseconds to nanoseconds
})

The timestamp format follows the OpenTelemetry logs specification: https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/logs/data-model.md

API #

createPinoPlugin(options?) #

Creates the gunshi-pino plugin.

interface PinoPluginOptions {
	/** Redaction paths to apply */
	redactions?: string[]

	/** Custom log levels */
	customLevels?: Record<string, number>

	/** Base pino options */
	pinoOptions?: pino.LoggerOptions

	/** Enable pretty printing */
	pretty?: boolean

	/** Override DEBUG env var */
	debug?: string

	/** OpenTelemetry instrumentation */
	opentelemetry?: {
		sdk: NodeSDK
	}

	/** Add OpenTelemetry-compatible timestamp field (nanoseconds since epoch) */
	timestamper?: (() => number) | boolean
}

PinoExtension #

The extension exposed via ctx.extensions["gunshi-pino"].

interface PinoExtension {
	/** Get a child logger with hierarchical name */
	getLogger(name: string): pino.Logger

	/** The root logger instance */
	logger: pino.Logger

	/** Add redaction paths dynamically */
	addRedactions(paths: string[]): void
}

Contributing #

PRs welcome. Please open an issue first to discuss changes.

License #

MIT © rektide de la faye