【问题标题】:Use Xray sdk in Lambda to send segments over UDP在 Lambda 中使用 Xray sdk 通过 UDP 发送段
【发布时间】:2021-11-16 19:03:09
【问题描述】:

我正在使用 Lambda。我想用自定义的 end_time 向 Xray 发送一个子段。我的 Lambda 中启用了 Xray。

当我使用aws-xray-sdk-coreaddNewSubsegment('postgres') 时,我找不到添加end_time 的可能性。当您 close() 段时,似乎正在设置 end_time

为了尝试解决这个限制,我根据以下内容使用 UDP 将自定义段发送到 Xray 守护程序。 Use UDP to send segment to XRay

以下代码未将 SubSegment 发送到 Xray。使用client.send(...) 发送段时,我没有收到任何错误。

是否有人知道更多关于设置自定义 end_time 的限制/知道是否可以在 Lambda 中使用 UDP?

      import AWSXRay from 'aws-xray-sdk-core'
      const traceDocument = {
        trace_id,
        id: generateRandomHex(16),
        name: 'postgres',
        start_time,
        end_time,
        type: 'subsegment',
        parent_id,
        sql: { sanitized_query: query },
      }

      const client = createSocket('udp4')

      const udpSegment = `{"format": "json", "version": 1}\n${traceDocument}`

      client.send(udpSegment, 2000, '127.0.0.1', (e, b) => {
        console.log(e, b)
      })

【问题讨论】:

  • 我目前正在尝试flush() 命令,它似乎在不设置end_time 的情况下发送子段。假设文档稀缺,我不得不查看 sdk 本身来弄清楚发生了什么。

标签: amazon-web-services aws-lambda aws-xray


【解决方案1】:

设法自己找出解决方案

结合使用 X-Ray SDK

addAttribute('in_progress', false)streamSubsegments() 将子段发送到 X-Ray

export const onQueryEvent = async (e) => {
  try {
   const segment = AWSXRay.getSegment()
     if (segment) {    
      const paramsArr = JSON.parse(e.params)
      const query = getQueryWithParams(e.query, paramsArr)      // X-Ray wants the time in seconds -> ms * 1e-3
      const start_time = e.timestamp.valueOf() * 1e-3 
      const end_time = (e.timestamp.valueOf() + e.duration) * 1e-3
     
      // Add a new Subsegment to parent Segment
      const subSegment = segment.addNewSubsegment('postgres')      // Add data to the segment
      subSegment.addSqlData({ sanitized_query: query })
      subSegment.addAttribute('start_time', start_time)
      subSegment.addAttribute('end_time', end_time)      // Set in_progress to false so subSegment 
      // will be send to xray on streamSubsegments()
      subSegment.addAttribute('in_progress', false)
      subSegment.streamSubsegments()
     }
  } catch (e) {
  console.log(e)
  }
}

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 2020-11-25
    • 1970-01-01
    • 2018-11-09
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    相关资源
    最近更新 更多