You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS对接自托管Signoz(Docker)无追踪日志显示求助

NestJS + 自托管SigNoz无追踪数据排查与修复

问题背景

NestJS应用配置OpenTelemetry对接Docker部署的自托管SigNoz,代码运行无报错,但SigNoz UI始终无法看到日志及追踪数据。

现有实现代码

tracer.ts

'use strict';

import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { Resource } from '@opentelemetry/resources';
import { formattedLogger } from './utils/formatedLogger';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { ConfigService } from '@nestjs/config';
import { NodeSDK } from '@opentelemetry/sdk-node';

const logger = formattedLogger('Signoz');
// telemetry data to SigNoz
const exporterOptions = {
  url: 'http://server-ip:4318/v1/traces',
};

const traceExporter = new OTLPTraceExporter(exporterOptions);
const sdk = new NodeSDK({
  traceExporter,
  instrumentations: [
    getNodeAutoInstrumentations({
      '@opentelemetry/instrumentation-nestjs-core': { enabled: true },
    }),
  ],
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'imcoder-backend',
  }),
});

sdk.start();

// Gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
  sdk
    .shutdown()
    .then(() => logger.log('Tracing terminated'))
    .catch((error) => logger.error('Error terminating tracing: ' + error))
    .finally(() => process.exit(0));
});
process.on('SIGINT', () => {
  sdk
    .shutdown()
    .then(() => logger.log('Tracing terminated'))
    .catch((error) => logger.error('Error terminating tracing:' + error))
    .finally(() => process.exit(0));
});
export default sdk;

main.ts调用

// main.ts
...
tracer.start()
...

排查与修复步骤

1. 网络连通性验证

  • 若NestJS应用也在Docker容器中运行,server-ip不能使用宿主机公网IP或localhost,需改用Docker内部网络别名(如容器名)或宿主机内部IP(例如host.docker.internal,依Docker环境而定)。
  • 在NestJS容器内执行curl http://server-ip:4318/v1/traces,返回405(方法不允许)即为网络连通正常;若超时或无法访问,检查Docker网络配置,确保两个容器处于同一网络,或SigNoz的4318端口已正确映射到宿主机。

2. SDK初始化时机修正

  • 确保tracer.ts初始化代码在NestJS应用启动之前执行,避免应用初始化后才启动追踪:
    // main.ts
    import tracer from './tracer';
    tracer.start(); // 必须放在NestFactory.create之前
    
    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';
    
    async function bootstrap() {
      const app = await NestFactory.create(AppModule);
      await app.listen(3000);
    }
    bootstrap();
    
  • 原代码中tracer.ts已调用sdk.start(),main.ts重复调用可能导致初始化异常,建议移除tracer.ts内的sdk.start(),仅在main.ts中启动。

3. 开启调试日志排查

  • 添加OpenTelemetry调试日志,追踪数据发送状态:
    在tracer.ts开头插入:
    import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
    diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
    
  • 查看应用日志,确认是否有发送追踪数据的请求,以及是否存在隐藏错误。

4. 服务名称与资源配置检查

  • 确认SemanticResourceAttributes.SERVICE_NAME配置准确,SigNoz UI会以此名称过滤服务,检查UI中是否选中了imcoder-backend。
  • 可补充资源属性帮助定位:
    resource: new Resource({
      [SemanticResourceAttributes.SERVICE_NAME]: 'imcoder-backend',
      [SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
    }),
    

5. 依赖与仪器库验证

  • 确认安装所有必要依赖:
    npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources @opentelemetry/semantic-conventions
    
  • 检查@opentelemetry/instrumentation-nestjs-core是否正常启用,避免其他仪器库覆盖NestJS追踪逻辑。

6. SigNoz自身配置验证

  • 检查SigNoz的Otel Collector配置文件(otel-collector-config.yaml),确认已开启OTLP HTTP接收端点:
    receivers:
      otlp:
        protocols:
          http:
            endpoint: 0.0.0.0:4318
    
  • 查看SigNoz的Otel Collector日志,确认是否接收到来自NestJS的追踪数据。

内容的提问来源于stack exchange,提问作者Adel Benyahia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 18:21:12