@lntrn/otel
v0.2.1
Published
Lantern's add-on for the OpenTelemetry Node SDK: node --import @lntrn/otel/register sends traces, metrics and logs to Lantern's edge or client ingest through the stock NodeSDK and OTLP exporters.
Readme
@lntrn/otel
Lantern's add-on for the OpenTelemetry Node SDK.
Already on OpenTelemetry? Add this
npm install @lntrn/otel @opentelemetry/api @opentelemetry/auto-instrumentations-nodeexport OTEL_SERVICE_NAME=checkout-api
export LNTRN_EDGE_USER=<org>.<id> LNTRN_EDGE_PASSWORD=… # an edge credential
export LNTRN_NAMESPACE=checkout LNTRN_ENVIRONMENT=prod
node --import @lntrn/otel/register server.jsThat replaces your instrumentation.mjs. @lntrn/otel/register starts a
stock NodeSDK before the app loads:
- the stock OTLP/HTTP protobuf exporters for traces, metrics and logs;
@opentelemetry/auto-instrumentations-nodewhen it is installed, withfsoff and query strings kept off spans: a server span hasurl.pathand nourl.query, and a client span'surl.fullis origin + path (an OAuth callback'scodeis never recorded);- OpenTelemetry's ES module hook, so ESM libraries (pino, express, …) are instrumented too;
- a flush before the process exits, when the event loop empties and on
SIGTERM/SIGINT.
Your code keeps using @opentelemetry/api as before.
In a container: CMD ["node", "--import", "@lntrn/otel/register", "server.js"],
or NODE_OPTIONS="--import @lntrn/otel/register".
Already starting NodeSDK yourself? Call setup() instead, with the same
options plus your own:
// instrumentation.mjs, loaded with --import
import { register } from "node:module";
import { setup, noQueryInstrumentationConfig } from "@lntrn/otel";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
register("@opentelemetry/instrumentation/hook.mjs", import.meta.url);
const lantern = setup({
serviceName: "checkout-api",
// noQueryInstrumentationConfig keeps query strings off spans, as register does.
instrumentations: [getNodeAutoInstrumentations({ ...noQueryInstrumentationConfig() })],
sdk: { /* sampler, views, … anything else NodeSDK takes */ },
});
process.once("SIGTERM", () => lantern.shutdown().finally(() => process.exit(0)));Requires Node 22.15 or newer, as @opentelemetry/sdk-node 0.300 does.
Tested with sdk-node 0.300.0 and @opentelemetry/api 1.10.0.
Targets
setup() picks the first one configured, unless target (or LNTRN_TARGET)
names one:
| Target | When | Sends to | Auth |
|---|---|---|---|
| edge | LNTRN_EDGE_USER + LNTRN_EDGE_PASSWORD | https://otlp.lntrn.dev | HTTP Basic, the edge credential |
| device | token or LNTRN_DEVICE_TOKEN | https://in.lntrn.dev | Bearer <device token> |
| gateway | LNTRN_ENDPOINT with no credential | that endpoint, e.g. an in-cluster collector | none |
| otel | OTEL_EXPORTER_OTLP_ENDPOINT (or a signal's own) | wherever OTEL_EXPORTER_OTLP_* say: plain NodeSDK | OTEL_EXPORTER_OTLP_HEADERS |
| none | nothing configured, or OTEL_SDK_DISABLED=true | nowhere. Spans still get ids, so log lines still carry them | |
Details:
LNTRN_ENDPOINT(endpoint) replaces the edge's or the ingest's address, e.g.https://forwarder.otlp.lntrn.dev.- The paths
/v1/traces,/v1/metricsand/v1/logsare appended. - Lantern's endpoints are OTLP/HTTP only. There is no gRPC.
Node processes on devices
A process running on hardware you don't control can't hold an edge credential. Examples: an emulation node, a kiosk, a desktop app's main process. It sends to the client ingest with a device token instead:
import { setup } from "@lntrn/otel";
setup({
serviceName: "emulation-node",
// Called only when the cached token is due for renewal: a minute before exp.
token: () => fetchTokenFromYourBackend(), // or mintToken({...}) from @lntrn/device/server, where the key lives
});The token cache is @lntrn/device/otel's TokenCache, the same one the JS
device add-on uses. Concurrent exports share one fetch, and a failed fetch
keeps the old token while it is still valid. The ingest files the records
under the token's project.
Resource
| Attribute | From |
|---|---|
| service.name | serviceName; OTEL_SERVICE_NAME wins |
| service.version | serviceVersion |
| service.namespace | namespace or LNTRN_NAMESPACE |
| deployment.environment.name | environment or LNTRN_ENVIRONMENT |
| vcs.repository.url.full | repositoryUrl, then LNTRN_VCS_REPOSITORY_URL, then GitLab CI's CI_PROJECT_URL |
| vcs.ref.head.revision | revision, then LNTRN_VCS_REVISION, then GitLab CI's CI_COMMIT_SHA |
| anything | attributes; OTEL_RESOURCE_ATTRIBUTES wins |
lanternDetector() is the namespace, environment and vcs part as a
ResourceDetector, for a NodeSDK you configure entirely yourself.
Which commit is running
vcs.repository.url.full and vcs.ref.head.revision are the OpenTelemetry
semantic-convention attributes for the repository and commit a service was
built from. Lantern uses them to say what changed before an alert. The URL
loses any credentials, a trailing / and .git. An attribute with no source
is left out.
A container runs where GitLab's variables don't exist, so carry them from the
image build into its environment. The k8s pipeline passes them as build args
when the project sets
DOCKER_BUILD_ARGS: "CI_PROJECT_URL=$CI_PROJECT_URL CI_COMMIT_SHA=$CI_COMMIT_SHA":
ARG CI_PROJECT_URL CI_COMMIT_SHA
ENV LNTRN_VCS_REPOSITORY_URL=${CI_PROJECT_URL} LNTRN_VCS_REVISION=${CI_COMMIT_SHA}Prefer the LNTRN_VCS_* names in the image. A process running inside a
GitLab job sees that job's CI_*, which may be another project's.
Turning a signal off
OTEL_TRACES_EXPORTER, OTEL_METRICS_EXPORTER or OTEL_LOGS_EXPORTER set to
none (or disableTraces/disableMetrics/disableLogs) leaves that signal
without an exporter. In Kubernetes, with Lantern's agent collecting container
output, set OTEL_LOGS_EXPORTER=none. pino's OpenTelemetry instrumentation
still writes trace_id and span_id into every stdout line.
LNTRN_INSTRUMENTATIONS=none stops register from loading the
auto-instrumentations.
Releasing
Bump "version" in package.json, add the CHANGELOG entry, and merge to
main. CI (.xeonr-ci.yaml, app lntrn-otel-node) tests, builds and publishes
any version the registry hasn't got, and tags it node-vX.Y.Z.
