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

OpenTelemetry Serverless示例运行异常排查求助

解决方案

1. 强制在Lambda回调前刷新数据导出器

错误提示的核心问题是Lambda执行结束前未强制刷新OTel的导出器,导致追踪/指标数据没来得及发送到Collector。在handler函数返回前手动调用导出器的flush()方法:

const { trace } = require('@opentelemetry/api');
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');

// 假设你已初始化导出器实例
const traceExporter = /* 你的追踪导出器 */;
const metricExporter = new OTLPMetricExporter({ /* 你的Collector配置 */ });

exports.handler = async (event, context) => {
  try {
    // 业务逻辑代码...

    // 结束当前活跃Span并强制刷新
    const activeSpan = trace.getTracer('your-tracer-id').getActiveSpan();
    if (activeSpan) activeSpan.end();
    
    await traceExporter.flush();
    await metricExporter.flush();
  } catch (err) {
    console.error('业务执行错误:', err);
  }
  
  return { statusCode: 200, body: JSON.stringify({ message: 'Success' }) };
};

2. 禁用Instrumentation自动抑制逻辑

大量Instrumentation suppressed错误是因为OTel的Lambda检测默认会在检测到潜在冲突时禁用instrumentation。通过环境变量关闭这个抑制机制:

  • 在Lambda控制台的环境变量中添加:OTEL_LAMBDA_DISABLE_AUTO_INSTRUMENTATION_SUPPRESSION=true
  • 或者在代码开头手动设置:
    process.env.OTEL_LAMBDA_DISABLE_AUTO_INSTRUMENTATION_SUPPRESSION = 'true';
    

3. 检查NODE_OPTIONS配置的正确性

如果是混合使用自动instrumentation和手动配置,容易出现冲突:

  • 若你是手动注册instrumentations,删除NODE_OPTIONS中的--require @opentelemetry/auto-instrumentations-node/register项,改用代码手动注册所需的检测:
    const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
    const { registerInstrumentations } = require('@opentelemetry/instrumentation');
    const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
    const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
    
    const provider = new NodeTracerProvider();
    provider.register();
    
    registerInstrumentations({
      instrumentations: [
        new HttpInstrumentation(),
        new AwsLambdaInstrumentation()
      ],
    });
    

4. 验证Collector连接配置

确保Lambda能访问到你的Collector,且环境变量配置正确:

  • OTEL_EXPORTER_OTLP_ENDPOINT:设置为Collector的可访问地址(如VPC内的私有IP+端口)
  • OTEL_EXPORTER_OTLP_PROTOCOL:根据Collector配置选择http/protobuf或grpc
  • OTEL_SERVICE_NAME:设置为你的Lambda服务标识

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 04:35:06