@coralogix/opentelemetry

中文文档
0.5.1发布于 2026/11/02Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 10.9万

Coralogix extensions for the OpenTelemetry Node SDK, including a transaction sampler for Coralogix APM.

原始使用文档
文档目录12 节
  1. Requirements
  2. CoralogixTransactionSampler
  3. Supported instrumentation
  4. Emitted attributes
  5. TransactionSpanProcessor
  6. How naming works
  7. Options
  8. Transactions with Express
  9. Prefixed and nested routers
  10. Express version support
  11. Changelog
  12. License

Coralogix

OpenTelemetry Node SDK Extensions

CI npm version License: Apache-2.0


Coralogix extensions for the OpenTelemetry Node SDK. This package adds Coralogix-specific behavior on top of a standard OpenTelemetry tracing setup — transaction tagging via a sampler and/or a SpanProcessor, including exclusive self duration on the processor path.

npm install --save @coralogix/opentelemetry

Requirements

This package relies on your application's existing OpenTelemetry setup. The following are peerDependencies and must be installed alongside it:

PackageVersion
@opentelemetry/api^1.7.0
@opentelemetry/sdk-trace-base^2.8.0

The examples below also use @opentelemetry/resources and @opentelemetry/semantic-conventions, which are part of a typical OpenTelemetry Node setup.

Note: This package targets OpenTelemetry JS SDK 2.x. The resource/provider APIs shown below (resourceFromAttributes, ATTR_SERVICE_NAME) are the 2.x APIs; if you are still on SDK 1.x, adapt the setup accordingly (new Resource(...), SemanticResourceAttributes).

CoralogixTransactionSampler

CoralogixTransactionSampler wraps an existing OpenTelemetry sampler to define, report, and monitor Coralogix transactions. It sets Coralogix transaction attributes on sampled spans and propagates the transaction identity across the trace.

import { CoralogixTransactionSampler } from "@coralogix/opentelemetry";
import { AlwaysOnSampler } from "@opentelemetry/sdk-trace-base";

// Wrap your existing sampler...
const sampler = new CoralogixTransactionSampler(new AlwaysOnSampler());

// ...or omit the argument to default to a ParentBased(AlwaysOn) sampler.
const defaultSampler = new CoralogixTransactionSampler();

Pass the sampler to your tracer provider like any other OpenTelemetry sampler:

import { BasicTracerProvider } from "@opentelemetry/sdk-trace-base";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";

const tracerProvider = new BasicTracerProvider({
    resource: resourceFromAttributes({
        [ATTR_SERVICE_NAME]: "<your-service-name>",
    }),
    sampler: new CoralogixTransactionSampler(),
});

Supported instrumentation

It works with individual auto-instrumentation and manual instrumentation. The bundled auto-instrumentation method is not supported.

Emitted attributes

When a span starts a transaction, the sampler adds the following attributes:

AttributeDescription
cgx.transactionThe transaction name (e.g. GET /users/:id).
cgx.transaction.distributedThe distributed transaction name propagated across services.
cgx.transaction.rootWhether this span is the root of the transaction.

TransactionSpanProcessor

TransactionSpanProcessor wraps a SpanExporter to tag Coralogix transactions, stamp exclusive self duration (cgx.transaction.self_duration, seconds) on completed local traces, and record the matching histogram (unit s). It works with any sampler.

How naming works

Transaction membership (new vs inherit, and cgx.transaction.root) is decided on span start. The display name cgx.transaction is stamped only when a completed local trace is finalized for export, using overrideName ?? rootSpan.name. That matters for Express: the HTTP span often starts as GET and is later renamed to GET /myroute by middleware — the exported transaction name is the final root span name.

Every completed local trace is exported immediately. The processor buffers up to CORALOGIX_MAX_SPANS_PER_TRACE completed spans for transaction enrichment (default: 256), and up to CORALOGIX_MAX_TRANSACTION_TRACES transactions retained in memory at once (default: unlimited). When a positive span limit is exceeded, it immediately exports that trace's buffer and streams later spans unchanged. CORALOGIX_MAX_SPANS_PER_TRACE=0 is unlimited. When the trace limit is reached, later traces stream unchanged. The trace limit applies only while transactions are retained in memory; 0 is unlimited, and once a buffered transaction finishes, capacity is available again. Those traces receive no processor transaction tags, self-duration, or metrics.

import { BasicTracerProvider, ConsoleSpanExporter } from "@opentelemetry/sdk-trace-base";
import { TransactionSpanProcessor } from "@coralogix/opentelemetry";

const tracerProvider = new BasicTracerProvider({
    spanProcessors: [
        new TransactionSpanProcessor(new ConsoleSpanExporter(), {
            // Optional MeterProvider for the self-duration histogram; defaults to the global one.
        }),
    ],
});

Options

Constructor options win over environment variables. Invalid env values fall back to the default.

OptionTypeDefaultEnv varMeaning
completionHoldbackMillisnumber100OTEL_CX_TRANSACTION_COMPLETION_HOLDBACK_MILLISAfter the last live span ends, wait this long before finalize so late siblings can join. 0 = finalize immediately.
shutdownIdleWaitMillisnumber30000—How long shutdown waits for in-flight spans.
meterProviderMeterProviderglobal—MeterProvider for the self-duration histogram.

Transactions with Express

To resolve stable, low-cardinality transaction names for Express routes (e.g. GET /users/:id instead of a per-request path), call setExpressApp so the sampler can learn your app's routes and endpoints.

Important: Call setExpressApp after all routes and routers have been registered. The sampler reads the Express router stack at the moment you call it, so any routes added afterwards will not be resolved.

import { BasicTracerProvider } from "@opentelemetry/sdk-trace-base";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { CoralogixTransactionSampler } from "@coralogix/opentelemetry";
import express from "express";
import router from "./router";

const sampler = new CoralogixTransactionSampler();

const tracerProvider = new BasicTracerProvider({
    resource: resourceFromAttributes({
        [ATTR_SERVICE_NAME]: "<your-service-name>",
    }),
    sampler,
});

const app = express();
app.use("/", router);

// Register routes first, then hand the app to the sampler.
sampler.setExpressApp(app);

app.listen(3000, () => {
    console.log("Server is running");
});

Prefixed and nested routers

setExpressApp resolves the full route template, including mount prefixes and arbitrarily nested routers. For example, given:

const orders = express.Router();
orders.get("/orders/:oid", handler);

const api = express.Router();
api.use("/deep", orders);

app.use("/api", api);

a request to /api/deep/orders/9 resolves to the transaction GET /api/deep/orders/:oid.

Express version support

Both Express 4 and Express 5 are supported.

Parameters are templatized correctly on both versions, including a parameter used in a mount prefix (app.use("/v/:ver", router)): a request to /v/2/things/abc resolves to GET /v/:ver/things/:t.

Changelog

See CHANGELOG.md for release notes. Note that route-template resolution for prefixed and nested routers is a breaking output change: transaction names for affected apps change, so update any dashboards, alerts, or saved views keyed on the previous names.

License

Licensed under the Apache License 2.0.

版本与发布记录

@coralogix/opentelemetry 共发布 11 个版本,首次发布于 2023/11/22;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.1latest2026/11/028 天前
0.5.02026/08/262 个月前
0.4.02026/07/084 个月前
0.3.02026/07/074 个月前
0.2.12026/07/014 个月前
0.2.02026/06/304 个月前

关于 @coralogix/opentelemetry 的常见问题

@coralogix/opentelemetry 是什么?

Coralogix extensions for the OpenTelemetry Node SDK, including a transaction sampler for Coralogix APM.

如何安装 @coralogix/opentelemetry?

在项目目录运行 npm install @coralogix/opentelemetry,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@opentelemetry/api(^1.7.0)、@opentelemetry/core(^2.8.0)、@opentelemetry/sdk-trace-base(^2.8.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@coralogix/opentelemetry 支持 TypeScript 吗?

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

@coralogix/opentelemetry 支持 ESM 还是 CommonJS?

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

@coralogix/opentelemetry 可以商用吗?

@coralogix/opentelemetry 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@coralogix/opentelemetry 的下载量有多少?

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

@coralogix/opentelemetry 还在维护吗?

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