How to use the aws-xray-sdk.Segment function in aws-xray-sdk

To help you get started, we’ve selected a few aws-xray-sdk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github alphagov / pay-selfservice / app / services / clients / base_client / wrapper.js View on Github external
cb = args.find(arg => typeof arg === 'function')
    if (verb) opts.method = verb.toUpperCase()
    if (uri && !opts.uri && !opts.url) opts.url = uri
    const context = {
      correlationId: correlator.getId(),
      startTime: new Date(),
      url: joinURL(lodash.get(opts, 'baseUrl', ''), opts.url),
      method: opts.method,
      description: opts.description,
      service: opts.service
    }

    // Set headers and optional x-ray trace headers
    lodash.set(opts, `headers.${CORRELATION_HEADER}`, context.correlationId)
    if (clsSegment) {
      const subSegment = opts.subSegment || new AWSXRay.Segment('_request_nbc', null, clsSegment.trace_id)
      opts.headers['X-Amzn-Trace-Id'] = 'Root=' + clsSegment.trace_id + ';Parent=' + subSegment.id + ';Sampled=1'
    }
    opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json'

    // Set up post response and error handling method
    const transform = opts.transform || false
    const handleError = opts.baseClientErrorHandler || 'new'
    opts.transform = undefined
    opts.baseClientErrorHandler = undefined

    // start request
    requestLogger.logRequestStart(context)
    const call = method(opts, (err, response, body) => {
      if (cb) cb(err, response, body)
      if (err) {
        reject(err)
github alphagov / pay-frontend / app / services / clients / base_client / wrapper.js View on Github external
const optionalPort = proxiedUrl.port ? ':' + proxiedUrl.port : ''
    const finalUrl = proxiedUrl.protocol + '//' + proxiedUrl.hostname + optionalPort + proxiedUrl.pathname

    const context = {
      correlationId: opts.correlationId,
      startTime: new Date(),
      url: finalUrl,
      method: opts.method,
      description: opts.description,
      service: opts.service
    }

    // Set headers and optional x-ray trace headers
    _.set(opts, `headers.${CORRELATION_HEADER}`, context.correlationId)
    if (clsSegment) {
      const subSegment = opts.subSegment || new AWSXRay.Segment('_request_nbc', null, clsSegment.trace_id)
      opts.headers['X-Amzn-Trace-Id'] = 'Root=' + clsSegment.trace_id + ';Parent=' + subSegment.id + ';Sampled=1'
    }
    opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json'

    // start request
    requestLogger.logRequestStart(context)
    const startTime = new Date()

    method(opts).then(response => {
      logger.info('[%s] - %s to %s ended - total time %dms', opts.correlationId, opts.method, opts.url, new Date() - startTime)
      resolve(response)
    }).catch(err => {
      logger.info('[%s] - %s to %s ended - total time %dms', opts.correlationId, opts.method, opts.url, new Date() - startTime)
      logger.error(`Calling ${opts.service} threw exception -`, {
        service: opts.service,
        method: opts.method,
github alphagov / pay-frontend / test / integration / three_d_secure_ft_tests.js View on Github external
        get: () => new AWSXRay.Segment('stub-segment'),
        bindEmitter: () => { },
github alphagov / pay-frontend / test / integration / charge_ft_tests.js View on Github external
    captureAsyncFunc: (name, callback) => callback(new AWSXRay.Segment('stub-subsegment')),
    '@global': true
github alphagov / pay-frontend / test / integration / charge_ft_tests.js View on Github external
        get: () => new AWSXRay.Segment('stub-segment'),
        bindEmitter: () => {},
github alphagov / pay-frontend / test / integration / charge_ft_tests.js View on Github external
        get: () => new AWSXRay.Segment('stub-segment'),
        bindEmitter: () => {},
github alphagov / pay-frontend / test / integration / charge_ft_tests.js View on Github external
    captureAsyncFunc: (name, callback) => callback(new AWSXRay.Segment('stub-subsegment')),
    '@global': true
github alphagov / pay-selfservice / app / services / clients / old_base_client.js View on Github external
const getHeaders = function getHeaders (args, segmentData) {
  const requestContext = getRequestContext(args.correlationId || '') || {}

  let headers = {}
  headers['Content-Type'] = 'application/json'
  headers[CORRELATION_HEADER_NAME] = args.correlationId || ''
  headers[FEATURE_FLAGS_HEADER_NAME] = (requestContext.features || []).toString()

  if (segmentData.clsSegment) {
    const subSegment = segmentData.subSegment || new AWSXRay.Segment('_request', null, segmentData.clsSegment.trace_id)
    headers['X-Amzn-Trace-Id'] = 'Root=' + segmentData.clsSegment.trace_id + ';Parent=' + subSegment.id + ';Sampled=1'
  }
  _.merge(headers, args.headers)

  return headers
}
/**
github alphagov / pay-frontend / app / services / clients / base_client / base_client.js View on Github external
const getHeaders = function getHeaders (args, segmentData, url) {
  let headers = {}
  headers['Content-Type'] = 'application/json'
  headers[CORRELATION_HEADER_NAME] = args.correlationId || ''
  if (url) {
    const port = (urlParse(url).port) ? ':' + urlParse(url).port : ''
    headers['host'] = urlParse(url).hostname + port
  }

  if (segmentData.clsSegment) {
    const subSegment = segmentData.subSegment || new AWSXRay.Segment('_request', null, segmentData.clsSegment.trace_id)
    headers['X-Amzn-Trace-Id'] = [
      'Root=',
      segmentData.clsSegment.trace_id,
      ';Parent=',
      subSegment.id,
      ';Sampled=1'
    ].join('')
  }
  _.merge(headers, args.headers)

  return headers
}
/**
github alphagov / pay-selfservice / app / services / auth_service.js View on Github external
function deserializeUser (req, externalId, done) {
  const segment = new AWSXRay.Segment('deserialize')
  const namespace = getNamespace(clsXrayConfig.nameSpaceName)
  namespace.run(() => {
    namespace.set(clsXrayConfig.segmentKeyName, segment)
    AWSXRay.captureAsyncFunc('auth_service_deserializeUser', function (subSegment) {
      return userService.findByExternalId(externalId, req.headers[CORRELATION_HEADER] || '', subSegment)
        .then((user) => {
          segment.close()
          done(null, user)
        })
        .catch(err => {
          segment.close(err)
          logger.info(`[${req.correlationId}]: Failed to retrieve user, '${externalId}', from adminusers with statuscode: ${err.errorCode}`)
          done(err)
        })
    }, segment)
  })