@carecard/telemetry

中文文档
3.45.0发布于 2026/11/03ISCTypeScriptESM + CJS近 30 天下载 2576

Privacy-safe OpenTelemetry tracing for CareCard Node.js services

原始使用文档
文档目录9 节
  1. Release 1.1.1 and production data
  2. Install
  3. Bootstrap
  4. Configuration
  5. Propagation
  6. Privacy boundary
  7. Public API
  8. Validation
  9. Caller-owned identity and exclusions

@carecard/telemetry

Release 1.1.1 and production data

CareCard Release 1.1.1 is the production baseline. Seeding with --seed-release-data is complete. Production cluster data, database rows, and stored user files will be preserved going forward. There will be no complete user-data wipeout and re-creation.

Future production changes must use data-preserving migrations. Do not replay the completed initialization or run reset, rollback, or seed workflows as part of ordinary production deployment.

The completed seed inventory and verification record documents the accepted release baseline and verification limitations.

Privacy-safe OpenTelemetry tracing for CareCard Node.js server processes.

Mandatory repository policy: load $pkg-telemetry-coding-standards-and-best-practices before changing, reviewing, testing, documenting, or releasing this package.

The package owns one explicit tracing bootstrap for CareCard services. It uses W3C Trace Context, exports OTLP/HTTP spans to Grafana Alloy, and instruments HTTP, PostgreSQL, and Kafka without sending application payloads to the tracing backend.

Non-negotiable TypeScript type rule: Never use the TypeScript type any; always use specific domain types, generics, existing project types, or unknown with explicit narrowing in all TypeScript-family files (.ts, .tsx, .mts, .cts, and .d.ts).

Install

npm install @carecard/[email protected] --save-exact

Bootstrap

Initialize tracing after environment configuration and before loading http, express, pg, kafkajs, or undici. Node instrumentation patches modules when they load, so importing those libraries first can leave requests uninstrumented.

require('./src/config');

const packageJson = require('./package.json');
const { createTelemetryShutdown, initializeServiceTelemetry } = require('@carecard/telemetry');

const telemetry = initializeServiceTelemetry({
  serviceName: 'worker',
  serviceVersion: packageJson.version,
});

const serverRuntime = require('./src/serverRuntime');

const shutdown = createTelemetryShutdown({
  telemetryRuntime: telemetry,
  shutdownResources: serverRuntime.shutdown,
});

installTelemetryShutdownHandlers(telemetry) is available for small processes that do not already own SIGINT and SIGTERM handling. Services with an existing graceful-shutdown path should use createTelemetryShutdown() so application resources stop first and telemetry still flushes if cleanup fails.

The Next.js dashboard calls the same initializer only from its Node.js instrumentation.ts registration hook. Browser and Edge runtimes must not load this package.

Configuration

VariableDefaultMeaning
OTEL_SDK_DISABLEDfalseSet to true to disable the SDK. It is disabled by default when NODE_ENV=test unless explicitly set to false.
OTEL_EXPORTER_OTLP_TRACES_ENDPOINThttp://127.0.0.1:4318/v1/tracesAlloy OTLP/HTTP trace receiver. Credentials, query parameters, and fragments are rejected.
OTEL_TRACES_SAMPLERparentbased_traceidratioparentbased_traceidratio, always_on, or always_off.
OTEL_TRACES_SAMPLER_ARG1 development; 0.1 productionRoot sampling ratio from 0 through 1.

Use a ratio of 1 for local development and Docker Desktop Kubernetes. Production defaults to 0.1; the self-hosted production profile also sets it explicitly. Parent-based sampling preserves an upstream decision across service boundaries.

Propagation

Automatic instrumentation creates and propagates child spans for:

  • inbound and outbound Node HTTP requests;
  • Express request handling;
  • PostgreSQL operations without query parameters or enhanced database reporting;
  • KafkaJS producers and consumers using the existing traceparent message header;
  • server-side fetch requests through Undici.

Routine health, readiness, metrics, and Next.js static requests are not traced. Only W3C traceparent is propagated. Caller-provided tracestate is ignored, and baggage is intentionally not configured.

For durable queues, capture the active context when creating the record and restore it when the record is later processed:

const { captureActiveTraceparent, runWithTraceparent } = require('@carecard/telemetry');

const eventMetadata = {
  traceparent: captureActiveTraceparent(),
};

await runWithTraceparent(
  {
    spanName: 'outbox publish',
    traceparent: eventMetadata.traceparent,
  },
  () => publishOutboxEvent(),
);

Span names passed to runWithTraceparent must be fixed, lower-case operational names. Never build them from user, route, record, email, token, or payload data.

Privacy boundary

The exporter applies a source-side privacy boundary before a span leaves the process. It keeps bounded operational fields such as route templates, HTTP methods and status codes, database operation names, database namespaces, and Kafka topic names.

It removes SQL text, query parameters, message payloads, message keys, URL values, headers, cookies, authorization data, request and response bodies, client addresses, user agents, exception messages, exception stacks, status descriptions, dynamic span-event names, and link attributes. Grafana Alloy repeats defensive attribute deletion before forwarding spans to Tempo.

Do not add hooks that attach domain identifiers, email addresses, JWTs, file names, form data, or other application values to spans.

Public API

  • initializeServiceTelemetry({ serviceName, serviceVersion }) accepts CareCard app-* and ms-* runtime identities, including app-auth and app-dashboard.
  • createTelemetryShutdown({ telemetryRuntime, shutdownResources })
  • captureActiveTraceparent()
  • runWithTraceparent({ spanName, traceparent }, operation)
  • installTelemetryShutdownHandlers(runtime)

The package exposes CommonJS, ESM, and TypeScript declarations.

Validation

npm run test:All
npm run lint
npm run format:check
npm audit --audit-level=high

Caller-owned identity and exclusions

Service names are generic bounded operational identifiers. Set serviceNamespace explicitly when needed. There is no built-in application naming rule, namespace, health route list, or framework route exclusion. Supply ignoreIncomingRequest with a boolean decision for the owning service or application. Outgoing exports remain excluded to avoid telemetry recursion. Privacy sanitization and W3C propagation remain technical package responsibilities.

版本与发布记录

@carecard/telemetry 共发布 28 个版本,首次发布于 2026/07/30;过去 12 个月发布了 28 个版本。

版本发布日期距今
3.45.0latest2026/11/036 天前
3.44.02026/10/1327 天前
3.41.02026/10/1228 天前
3.40.02026/10/1228 天前
3.39.02026/10/1228 天前
3.38.02026/10/1228 天前

关于 @carecard/telemetry 的常见问题

@carecard/telemetry 是什么?

Privacy-safe OpenTelemetry tracing for CareCard Node.js services

如何安装 @carecard/telemetry?

在项目目录运行 npm install @carecard/telemetry,或使用 pnpm add / yarn add / bun add。

@carecard/telemetry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@carecard/telemetry 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@carecard/telemetry 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.21.0 <25。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@carecard/telemetry 可以商用吗?

@carecard/telemetry 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@carecard/telemetry 的下载量有多少?

根据 npm 官方下载统计,@carecard/telemetry 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,576 次,日均约 86 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carecard/telemetry 还在维护吗?

最新版本 3.45.0 发布于 2026/11/03(6 天前);至今共发布 28 个版本,其中过去 12 个月发布了 28 个;首次发布于 2026/07/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。