Skip to content
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
2 changes: 1 addition & 1 deletion .agents/skills/create-adapter/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Read [references/adapter-template.md](references/adapter-template.md) for the fu
Key architecture rules:

1. **Config interface** -- service-specific fields (API key, endpoint, etc.) plus optional `timeout?: number`
2. **`getRuntimeConfig()` helper** -- dynamic `require('nitropack/runtime')` wrapped in try/catch
2. **`getRuntimeConfig()`** -- import from `./_utils` (shared helper, do NOT redefine locally)
3. **Config priority** (highest to lowest):
- Overrides passed to `create{Name}Drain()`
- `runtimeConfig.evlog.{name}`
Expand Down
19 changes: 2 additions & 17 deletions .agents/skills/create-adapter/references/adapter-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Replace `{Name}`, `{name}`, and `{NAME}` with the actual service name.

```typescript
import type { DrainContext, WideEvent } from '../types'
import { getRuntimeConfig } from './_utils'

// --- 1. Config Interface ---
// Define all service-specific configuration fields.
Expand Down Expand Up @@ -45,23 +46,7 @@ export function to{Name}Event(event: WideEvent): {Name}Event {
}
}

// --- 3. Runtime Config Helper ---
// Dynamic require to avoid bundling issues outside Nitro.
// Returns undefined when not in a Nitro context.
function getRuntimeConfig(): {
evlog?: { {name}?: Partial<{Name}Config> }
{name}?: Partial<{Name}Config>
} | undefined {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { useRuntimeConfig } = require('nitropack/runtime')
return useRuntimeConfig()
} catch {
return undefined
}
}

// --- 4. Factory Function ---
// --- 3. Factory Function ---
// Returns a drain function that resolves config at call time.
// Config priority: overrides > runtimeConfig.evlog.{name} > runtimeConfig.{name} > env vars

Expand Down
3 changes: 0 additions & 3 deletions bun.lock

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

3 changes: 0 additions & 3 deletions packages/evlog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@
"test:coverage": "vitest run --coverage",
"typecheck": "echo 'Typecheck handled by build'"
},
"dependencies": {
"defu": "^6.1.4"
},
"devDependencies": {
"@nuxt/devtools": "^3.1.1",
"@nuxt/schema": "^4.3.1",
Expand Down
13 changes: 13 additions & 0 deletions packages/evlog/src/adapters/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Try to get runtime config from Nitro/Nuxt environment.
* Returns undefined if not in a Nitro context.
*/
export function getRuntimeConfig(): Record<string, unknown> | undefined {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { useRuntimeConfig } = require('nitropack/runtime')
return useRuntimeConfig()
} catch {
return undefined
}
}
16 changes: 1 addition & 15 deletions packages/evlog/src/adapters/axiom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DrainContext, WideEvent } from '../types'
import { getRuntimeConfig } from './_utils'

export interface AxiomConfig {
/** Axiom dataset name */
Expand All @@ -13,21 +14,6 @@ export interface AxiomConfig {
timeout?: number
}

/**
* Try to get runtime config from Nitro/Nuxt environment.
* Returns undefined if not in a Nitro context.
*/
function getRuntimeConfig(): { evlog?: { axiom?: Partial<AxiomConfig> }, axiom?: Partial<AxiomConfig> } | undefined {
try {
// Dynamic import to avoid bundling issues when not in Nitro
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { useRuntimeConfig } = require('nitropack/runtime')
return useRuntimeConfig()
} catch {
return undefined
}
}

/**
* Create a drain function for sending logs to Axiom.
*
Expand Down
16 changes: 1 addition & 15 deletions packages/evlog/src/adapters/otlp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DrainContext, LogLevel, WideEvent } from '../types'
import { getRuntimeConfig } from './_utils'

export interface OTLPConfig {
/** OTLP HTTP endpoint (e.g., http://localhost:4318) */
Expand Down Expand Up @@ -70,21 +71,6 @@ const SEVERITY_TEXT_MAP: Record<LogLevel, string> = {
error: 'ERROR',
}

/**
* Try to get runtime config from Nitro/Nuxt environment.
* Returns undefined if not in a Nitro context.
*/
function getRuntimeConfig(): { evlog?: { otlp?: Partial<OTLPConfig> }, otlp?: Partial<OTLPConfig> } | undefined {
try {
// Dynamic import to avoid bundling issues when not in Nitro
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { useRuntimeConfig } = require('nitropack/runtime')
return useRuntimeConfig()
} catch {
return undefined
}
}

/**
* Convert a value to OTLP attribute value format.
*/
Expand Down
16 changes: 1 addition & 15 deletions packages/evlog/src/adapters/posthog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DrainContext, WideEvent } from '../types'
import { getRuntimeConfig } from './_utils'

export interface PostHogConfig {
/** PostHog project API key */
Expand All @@ -21,21 +22,6 @@ export interface PostHogEvent {
properties: Record<string, unknown>
}

/**
* Try to get runtime config from Nitro/Nuxt environment.
* Returns undefined if not in a Nitro context.
*/
function getRuntimeConfig(): { evlog?: { posthog?: Partial<PostHogConfig> }, posthog?: Partial<PostHogConfig> } | undefined {
try {
// Dynamic import to avoid bundling issues when not in Nitro
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { useRuntimeConfig } = require('nitropack/runtime')
return useRuntimeConfig()
} catch {
return undefined
}
}

/**
* Convert a WideEvent to a PostHog event format.
*/
Expand Down
16 changes: 1 addition & 15 deletions packages/evlog/src/adapters/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DrainContext, LogLevel, WideEvent } from '../types'
import { getRuntimeConfig } from './_utils'

export interface SentryConfig {
/** Sentry DSN */
Expand Down Expand Up @@ -45,21 +46,6 @@ const SEVERITY_MAP: Record<LogLevel, number> = {
error: 17,
}

/**
* Try to get runtime config from Nitro/Nuxt environment.
* Returns undefined if not in a Nitro context.
*/
function getRuntimeConfig(): { evlog?: { sentry?: Partial<SentryConfig> }, sentry?: Partial<SentryConfig> } | undefined {
try {
// Dynamic import to avoid bundling issues when not in Nitro
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { useRuntimeConfig } = require('nitropack/runtime')
return useRuntimeConfig()
} catch {
return undefined
}
}

function parseSentryDsn(dsn: string): SentryDsnParts {
const url = new URL(dsn)
const publicKey = url.username
Expand Down
23 changes: 20 additions & 3 deletions packages/evlog/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { defu } from 'defu'
import type { EnvironmentContext, Log, LogLevel, LoggerConfig, RequestLogger, RequestLoggerOptions, SamplingConfig, TailSamplingContext, WideEvent } from './types'
import { colors, detectEnvironment, formatDuration, getConsoleMethod, getLevelColor, isDev, matchesPattern } from './utils'

function isPlainObject(val: unknown): val is Record<string, unknown> {
return val !== null && typeof val === 'object' && !Array.isArray(val)
}

function deepDefaults(base: Record<string, unknown>, defaults: Record<string, unknown>): Record<string, unknown> {
const result = { ...base }
for (const key in defaults) {
const baseVal = result[key]
const defaultVal = defaults[key]
if (baseVal === undefined || baseVal === null) {
result[key] = defaultVal
} else if (isPlainObject(baseVal) && isPlainObject(defaultVal)) {
result[key] = deepDefaults(baseVal, defaultVal)
}
}
return result
}

let globalEnv: EnvironmentContext = {
service: 'app',
environment: 'development',
Expand Down Expand Up @@ -221,7 +238,7 @@ export function createRequestLogger(options: RequestLoggerOptions = {}): Request

return {
set<T extends Record<string, unknown>>(data: T): void {
context = defu(data, context) as Record<string, unknown>
context = deepDefaults(data, context) as Record<string, unknown>
},

error(error: Error | string, errorContext?: Record<string, unknown>): void {
Expand All @@ -236,7 +253,7 @@ export function createRequestLogger(options: RequestLoggerOptions = {}): Request
stack: err.stack,
},
}
context = defu(errorData, context) as Record<string, unknown>
context = deepDefaults(errorData, context) as Record<string, unknown>
},

emit(overrides?: Record<string, unknown> & { _forceKeep?: boolean }): WideEvent | null {
Expand Down
Loading