Configuration

Observability

Debug logging, custom logger, and request lifecycle hooks in the Kelp SDK.

The SDK provides three layers of observability: a debug flag for verbose logging, a custom logger to route output, and onRequest/onResponse/onError lifecycle hooks for integrating with your own tracing or monitoring tooling.

Debug mode

Set debug: true to emit a log line for every request attempt (including retries). Output goes to the configured logger (defaults to console).

const kelp = createKelpClient({
  searchUrl: 'https://{TENANT_ID}-search.kelp.click',
  analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
  apiKey: 'kelp_...',
  debug: true,
});

The SDK never logs the API key or full response bodies, even in debug mode.

Custom logger

By default debug output goes to console. Supply a logger object to route it elsewhere - a structured logger, a remote sink, or a no-op:

import pino from 'pino';

const logger = pino({ level: 'debug' });

const kelp = createKelpClient({
  searchUrl: 'https://{TENANT_ID}-search.kelp.click',
  analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
  apiKey: 'kelp_...',
  debug: true,
  logger: {
    debug: (...a) => logger.debug(a),
    info:  (...a) => logger.info(a),
    warn:  (...a) => logger.warn(a),
    error: (...a) => logger.error(a),
  },
});

The logger interface:

interface Logger {
  debug(...args: unknown[]): void;
  info(...args: unknown[]): void;
  warn(...args: unknown[]): void;
  error(...args: unknown[]): void;
}

SDK log output is emitted only when debug: true. Setting a custom logger without debug: true has no effect.

Lifecycle hooks

Three optional hooks let you observe every request without coupling to the logger:

HookCalledPayload
onRequestBefore each attempt (including retries){ url, method, attempt }
onResponseAfter a response arrives, before error-checking{ url, method, status, attempt }
onErrorAfter all retries are exhaustedThe thrown Error
const kelp = createKelpClient({
  searchUrl: 'https://{TENANT_ID}-search.kelp.click',
  analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
  apiKey: 'kelp_...',
  maxRetries: 2,

  onRequest({ url, method, attempt }) {
    mySpan.setAttribute('kelp.url', url);
    mySpan.setAttribute('kelp.attempt', attempt);
  },

  onResponse({ url, status, attempt }) {
    metrics.record('kelp.response', { url, status, attempt });
  },

  onError(err) {
    Sentry.captureException(err);
  },
});

Hook call order with retries

For a request that succeeds on the second attempt:

  1. onRequest (attempt 0)
  2. onResponse (attempt 0, status 503)
  3. (backoff delay)
  4. onRequest (attempt 1)
  5. onResponse (attempt 1, status 200)

onError is only called once, after all retries are exhausted and the final error is about to be thrown.

What is never logged

Regardless of the debug setting, the SDK does not log:

  • The API key or any authorization header value
  • Full request or response bodies
  • User identity values (anonymous id, session id, user id)

On this page