惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 叶小钗
AWS News Blog
AWS News Blog
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
V
V2EX
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Latest news
Latest news
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
T
Threatpost
L
Lohrmann on Cybersecurity
小众软件
小众软件
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
Google Online Security Blog
Google Online Security Blog
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
The Register - Security
The Register - Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
Security Latest
Security Latest
Recent Announcements
Recent Announcements
C
Check Point Blog
B
Blog
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
I
InfoQ

Deno

Deno 2.8 | Deno Claw Patrol: an open-source security firewall for agents | Deno Fresh 2.3: Zero JS by default, View Transitions, and Temporal support | Deno Deno 2.7: Temporal API, Windows ARM, and npm overrides | Deno Build a dinosaur runner game with Deno, pt. 6 | Deno Build a dinosaur runner game with Deno, pt. 5 | Deno Deno Deploy is Generally Available | Deno Introducing Deno Sandbox | Deno Build a dinosaur runner game with Deno, pt. 4 | Deno Build a dinosaur runner game with Deno, pt. 3 | Deno Build a dinosaur runner game with Deno, pt. 2 | Deno React / Next.js Denial-of-Service Vulnerability: Deno Deploy users protected | Deno Deno 2.6: dx is the new npx | Deno Build a dinosaur runner game with Deno, pt. 1 | Deno React Server Functions / Next.js Vulnerability: Deno Deploy users protected | Deno My highlights from the new Deno Deploy | Deno Deno's Other Open Source Projects | Deno How Deno protects against npm exploits | Deno Help Us Raise $200k to Free JavaScript from Oracle | Deno Deno 2.5: Permissions in the config file | Deno Fresh 2.0 Graduates to Beta, Adds Vite Support | Deno Deno 2.4: deno bundle is back | Deno JavaScript™ Trademark Update | Deno What's coming to JavaScript | Deno A brief history of JavaScript | Deno Reports of Deno's Demise Have Been Greatly Exaggerated | Deno An Update on Fresh | Deno How Plaid migrated 100 services to a new database platform 5x faster with Deno | Deno Deno 2.3: Improved deno compile, local npm packages, and more | Deno Add JSR packages with pnpm and Yarn | Deno Zero-config Debugging with Deno and OpenTelemetry | Deno Exploring Art with TypeScript, Jupyter, Polars, and Observable Plot | Deno Deno v Oracle Update 3: Fighting the JavaScript Trademark | Deno Build a custom RAG AI agent in TypeScript and Jupyter | Deno toranoana.deno #20 登録受付中(2025年3月14日) | Deno Node just added TypeScript support. What does that mean for Deno? | Deno The Dino 🦕, the Llama 🦙, and the Whale 🐋 | Deno Publish a lint rule, get a prize | Deno Deno 2.2: OpenTelemetry, Lint Plugins, node:sqlite | Deno If you're not using npm specifiers, you're doing it wrong | Deno How Deno's documentation is evolving | Deno Oracle justified its JavaScript trademark with Node.js—now it wants that ignored | Deno Introducing the JSR open governance board | Deno Intro to Wasm in Deno | Deno Announcing OpenAI on JSR | Deno Deno in 2024 | Deno Goodbye WinterCG, welcome WinterTC | Deno Build a SolidJS app with Deno | Deno Run your Next.js SSR app on Deno Deploy | Deno Solve Advent of Code 2024 with Deno and Win Prizes! | Deno Deno v. Oracle: Canceling the JavaScript Trademark | Deno Deno 2.1: Wasm Imports and other enhancements | Deno Build a Typesafe API with tRPC and Deno | Deno Self-contained Executable Programs with Deno Compile | Deno Build a Database App with Drizzle ORM and Deno | Deno Introducing your new JavaScript package manager: Deno | Deno Announcing Growthbook on JSR | Deno Build an Astro site with Deno | Deno How to convert CommonJS to ESM | Deno Announcing Deno 2 | Deno The Final Touches: What’s New In v2.0.0-rc.10 | Deno Announcing Stable V8 Bindings for Rust | Deno Deno 2.0 Release Candidate | Deno Secure, efficient private npm registries with Cloudsmith and Deno | Deno Painting the Plane as We Fly It: Designing JSR | Deno Introducing Web Cache API support on Deno Deploy | Deno Deno 1.46: The Last 1.x Release | Deno Protect your cloud spend with new Deno Deploy spend limits | Deno What we got wrong about HTTP imports | Deno Benchmarking AWS Lambda Cold Starts Across JavaScript Runtimes | Deno Announcing Supabase on JSR | Deno Deno 1.45: Workspace and Monorepo Support | Deno Introducing KV Backup for Deno Subhosting | Deno A Gentle Intro to TypeScript | Deno Announcing Hono on JSR | Deno How We Made the Deno Language Server Ten Times Faster | Deno How the Guardian uses Deno to audit accessibility and performance across their 2.7 million articles | Deno Introducing More Flexible Domain Association for Deno Subhosting | Deno The stabilization process of the Standard Library has begun | Deno Deno 1.44: Private npm registries, improved Node.js compat, and performance boosts | Deno How we built a secure, performant, multi-tenant cloud platform to run untrusted code | Deno The Deno Standard Library is now available on JSR | Deno How to document your JavaScript package | Deno Your Low Code Solution Needs an Escape Hatch | Deno Deno 1.43: Improved Language Server performance | Deno How Slack used Deno to save months of engineering effort in launching their new platform | Deno JSR Is Not Another Package Manager | Deno Announcing the Hookdeck SDK on JSR | Deno Announcing the Neon Serverless Driver on JSR | Deno An intro to TSConfig for JavaScript Developers | Deno How we built JSR | Deno How Netlify used Deno Subhosting to build a successful edge functions product | Deno Introducing Simpler Project Creation in Deno Deploy | Deno Deno 1.42: Better dependency management with JSR | Deno Introducing deployctl, the command line interface for Deno Deploy | Deno Introducing JSR - the JavaScript Registry | Deno How to add Monaco to a Next.js app and securely run untrusted user code | Deno Survey Results and Roadmap | Deno Deno 1.41: smaller deno compile binaries | Deno Webhooks suck, but here are alternatives | Deno
How to get deep traces in your Node.js backend with OTel and Deno | Deno
Andy Jiang · 2025-03-14 · via Deno

One important facet of running production software is observability—monitoring logs, traces, and metrics so you can identify and fix issues quickly. In Node.js servers, adding observability isn’t trivial. You have to add instrumentation code, modify your logger, and configure various services. What if you could see logs and traces immediately, without any additional work?

In this post, we’ll show how you can immediately see what a Node.js backend is doing—without making any code changes—by running it in Deno. Now that Deno can run Node programs and has built-in OpenTelemetry support, this is easier than ever.

  • Deep tracing in Node
  • Deno’s built-in OTel support
  • What’s next?

Deep tracing in Node

Before we set up OTel with Deno, let’s go over how to set it up in Node.

Here’s a simple app (GitHub source) that displays a chat box, which takes your input and passes it to ChatGPT.

Screenshot of our very simple chat app.

To follow along, clone this repo and run the app in the node-express subdirectory.

Currently, we console.log at key points in the single-file app:

node server.js
Server is running on http://localhost:8000
Serving the chat interface...
Prompt: Tell me about the last time you felt sad.
Sending request to OpenAI...
Received response from OpenAI

Let’s add OpenTelemetry to the program and use Grafana’s open-source LGTM stack to introspect it. Since this post isn’t about setting up an OTel stack from scratch, we’ll describe it at a high level:

  • Add instrumentation code at the application level to send logging and trace data to the proper endpoints.
  • Run Grafana’s OpenTelemetry LGTM stack, which can be done with a single docker run command.
  • Set key environment variables to configure the routing of telemetry data.

Let’s start our server with new environment variables:

OTEL_SERVICE_NAME=chat-app OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 node server.js

Then, we’ll start Grafana’s OTel LGTM stack:

docker run --name lgtm -p 3000:3000 -p 4317:4317 -p 4318:4318 --rm -ti \
    -v "$PWD"/lgtm/grafana:/data/grafana \
    -v "$PWD"/lgtm/prometheus:/data/prometheus \
    -v "$PWD"/lgtm/loki:/data/loki \
    -e GF_PATHS_DATA=/data/grafana \
    docker.io/grafana/otel-lgtm:0.8.1

Now, when you open Grafana at localhost:3000, click “Explore,” and select “Tempo,” you’ll see traces.

Seeing Node traces in Tempo and Grafana

Clicking on the trace shows a POST request, which is a single span within that trace. Clicking “Logs for this span” displays logs associated with this request:

Seeing Node logs from traces in Tempo and Grafana

In Node, setting up OTel required importing 8 new dependencies, an additional 84 lines of instrumentation code, and replacing console.logs with a custom logger configured to send log output to the OTel endpoint.

To see exactly what changes were required to integrate OpenTelemetry into our Node app, here’s the diff:

Adding dependencies in package.json

package.json

   "main": "server.js",
   "type": "module",
   "scripts": {
-    "start": "node server.js"
+    "start": "OTEL_SERVICE_NAME=chat-app OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 OTEL_LOG_LEVEL=error node server.js"
   },
   "dependencies": {
+    "@opentelemetry/api": "^1.9.0",
+    "@opentelemetry/auto-instrumentations-node": "^0.56.1",
+    "@opentelemetry/exporter-logs-otlp-http": "^0.57.2",
+    "@opentelemetry/exporter-metrics-otlp-http": "^0.57.2",
+    "@opentelemetry/exporter-trace-otlp-http": "^0.57.2",
+    "@opentelemetry/resources": "^1.30.1",
+    "@opentelemetry/sdk-node": "^0.57.2",
+    "@opentelemetry/semantic-conventions": "^1.30.0",
     "dotenv": "^16.4.7",
     "express": "^4.18.3"
   }
Adding OpenTelemetry required importing eight new dependencies.

Replacing console.log calls in server.js

server.js

 import dotenv from 'dotenv';
+import { logger } from './telemetry.js';
 import express from 'express';
 
 dotenv.config();

 // Code...

 // Middleware to parse JSON bodies
+app.use((req, _res, next) => {
+  logger.info("Incoming request", {
+    attributes: {
+      path: req.path,
+      method: req.method,
+    },
+  });
+  next();
+});
 app.use(express.json());
 
 // Serve static HTML
 app.get("/", (_req, res) => {
-  console.log("Serving the chat interface");
+  logger.info("Serving the chat interface");
   res.send(`<!DOCTYPE html>
 <html>
 <head>

 // Code...

 app.post("/api/chat", async (req, res) => {
   try {
     const { prompt } = req.body;
-    console.log("Prompt:", prompt);
+    logger.info(`Prompt: ${prompt}`);
 
     // Add your hardcoded system prompt here
     const systemPrompt =
       "You are a helpful AI assistant. Please provide clear and concise responses.";
 
-    console.log("Sending request to OpenAI...");
+    logger.info("Sending request to OpenAI...");
     const response = await fetch("https://api.openai.com/v1/chat/completions", {
       method: "POST",
       headers: {

       }),
     });
 
-    console.log("Received response from OpenAI");
+    logger.info("Received response from OpenAI");
     const data = await response.json();
 
     res.json({
       response: data.choices[0].message.content,
     });
   } catch (error) {
-    console.error("Error:", error);
+    logger.error(`Error: ${error}`);
     res.status(500).json({
       error: error.message,
     });

// Code...

 // Start the server
 app.listen(port, () => {
-  console.log(`Server is running on http://localhost:${port}`);
+  logger.info(`Server is running on http://localhost:${port}`);
 });
We’ve replaced `console.log` calls with a custom `logger` that writes logs to the OTel collector.

telemetry.js

+import process from 'process';
+import { NodeSDK } from '@opentelemetry/sdk-node';
+import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
+import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
+import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
+import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
+import {
+  LoggerProvider,
+  SimpleLogRecordProcessor,
+} from '@opentelemetry/sdk-logs';
+import { Resource } from '@opentelemetry/resources';
+import { trace, context } from '@opentelemetry/api';
+
+const resource = new Resource({
+  "service.name": process.env.OTEL_SERVICE_NAME || "chat-app",
+});
+
+const logExporter = new OTLPLogExporter({
+  url: "http://localhost:4318/v1/logs",
+});
+
+const loggerProvider = new LoggerProvider({
+  resource, // ✅ Attach the resource with the updated attribute
+});
+
+loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(logExporter));
+
+const logger = loggerProvider.getLogger("chat-app-logger");
+
+const customLogger = {
+  info: (message, attributes = {}) => {
+    const activeSpan = trace.getSpan(context.active());
+    const traceId = activeSpan
+      ? activeSpan.spanContext().traceId
+      : "no-trace-id";
+    logger.emit({
+      traceId,
+      body: message,
+      severityText: "INFO",
+      attributes,
+    });
+  },
+  error: (message, attributes = {}) => {
+    const activeSpan = trace.getSpan(context.active());
+    const traceId = activeSpan
+      ? activeSpan.spanContext().traceId
+      : "no-trace-id";
+    logger.emit({
+      traceId,
+      body: message,
+      severityText: "ERROR",
+      attributes,
+    });
+  },
+};
+
+const sdk = new NodeSDK({
+  traceExporter: new OTLPTraceExporter({
+    url: "http://localhost:4318/v1/traces",
+  }),
+  metricExporter: new OTLPMetricExporter({
+    url: "http://localhost:4318/v1/metrics",
+  }),
+  instrumentations: [
+    new HttpInstrumentation(),
+  ],
+  logExporter,
+  resource,
+});
+
+// Start the SDK and handle the promise properly
+sdk.start();
+
+process.on("SIGTERM", () => {
+  sdk.shutdown()
+    .then(() => logger.info("Telemetry shutdown complete"))
+    .finally(() => process.exit(0));
+});
+
+export { customLogger as logger };
The entirety of telemetry.js was created to configure auto-generating traces, metrics, and logging, as well as exporting telemetry data to the OTel collector endpoint.

Note: if you want to explore these files directly, check out the “original” Node app (without OTel) and the identical Node app but with OTel setup.

But what if there was an easier way to get logs and tracing without the instrumentation and configuration steps?

Deno’s built-in OTel support

Deno added built-in OTel in 2.2, allowing us to spin up the OTel stack in a single command. It automatically collects and exports traces, metrics, and logs from console.log, fetch, and Deno.serve().

With Deno 2.2+, you can immediately see traces and logs in Grafana by just passing a few additional environment variables and the --unstable-otel flag:

OTEL_DENO=true OTEL_SERVICE_NAME=chat-app deno \
  --unstable-otel -NRE --env-file server.js

Note: The --unstable-otel flag enables Deno’s built-in OpenTelemetry, which is still experimental and subject to change.

We can spin up the same OTel-LGTM stack:

docker run --name lgtm -p 3000:3000 -p 4317:4317 -p 4318:4318 --rm -ti \
    -v "$PWD"/lgtm/grafana:/data/grafana \
    -v "$PWD"/lgtm/prometheus:/data/prometheus \
    -v "$PWD"/lgtm/loki:/data/loki \
    -e GF_PATHS_DATA=/data/grafana \
    docker.io/grafana/otel-lgtm:0.8.1

After playing with the app a bit to generate some logs, when we visit localhost:3000, click on “Explore,” then “Tempo,” we can see traces.

Seeing Deno traces in Tempo and Grafana

Clicking “Logs for this span” shows the logs associated with the same HTTP request:

Seeing Deno traces in Tempo and Grafana

We’ve expanded the last console.log, and we can see that Deno and OTel have automatically attached metadata to help contextualize this log. Logs from JavaScript code are exported with the relevant span context if they occur inside an active span.

All of this works without adding any additional instrumentation code or configuration. If there’s a Node.js server where you want immediate logging and tracing, try running it with OTel-enabled Deno.

Learn more about advanced use cases with Deno’s built-in OTel support.

What’s next?

OpenTelemetry has drastically simplified ingesting and exporting telemetry data, and now that it’s built into Deno, adding observability is easier than ever. Without extra config or instrumentation, you can immediately get logs and tracing from HTTP requests and console logs.

We’ll continue improving our built-in OTel integration, with many updates on the way. We’ll also publish more resources on setting up OTel and sending telemetry data to your preferred observability stack.

🚨️ Deno 2.2 released! 🚨️