<commit_before>const winston = require("winston");
const AWSXRay = require('aws-xray-sdk-core');

module.exports = function (req, res, next) {

  if (process.env.xray === "true") {
    let segment = AWSXRay.getSegment();
    let event = req.apiGateway ? req.apiGateway.event : null;

    if (event === null) {
      winston.info("Make sure to add the annot8 middleware after call to awsServerlessExpressMiddleware.eventContext()");
    } else {
      try {
        let query = event.queryStringParameters ? `?${Object.keys(event.queryStringParameters).map(k => `${k}=${event.queryStringParameters[k]}`).join('&')}` : "";
        segment.addAnnotation("Method", event.httpMethod);
        segment.addAnnotation("Url", (event.headers.Host.endsWith(".cimpress.io")) ? `${event.headers.Host}${event.path}` : `${event.headers.Host}/${event.requestContext.stage}${event.path}${query}`);
        segment.addAnnotation("ClientIp", event.requestContext.identity.sourceIp);
        segment.addAnnotation("UserAgent", event.headers["User-Agent"]);
        segment.addAnnotation("PrincipalId", event.requestContext.authorizer.principalId);
        segment.addAnnotation("Authorization", event.headers.Authorization);

        segment.addAnnotation("Stage", event.requestContext.stage);

        segment.addMetadata("query", event.queryStringParameters);
        segment.addMetadata("headers", event.headers);

      } catch (err) {
        winston.error(err);
      }
    }
  }
  if (next && typeof next === 'function') {
    next();
  }
};
<commit_msg>Make sure that the authorizer data is there before accessing principal.<commit_after>const winston = require("winston");
const AWSXRay = require('aws-xray-sdk-core');

module.exports = function (req, res, next) {

  if (process.env.xray === "true") {
    let segment = AWSXRay.getSegment();
    let event = req.apiGateway ? req.apiGateway.event : null;

    if (event === null) {
      winston.info("Make sure to add the annot8 middleware after call to awsServerlessExpressMiddleware.eventContext()");
    } else {
      try {
        let query = event.queryStringParameters ? `?${Object.keys(event.queryStringParameters).map(k => `${k}=${event.queryStringParameters[k]}`).join('&')}` : "";
        segment.addAnnotation("Method", event.httpMethod);
        segment.addAnnotation("Url", (event.headers.Host.endsWith(".cimpress.io")) ? `${event.headers.Host}${event.path}` : `${event.headers.Host}/${event.requestContext.stage}${event.path}${query}`);
        segment.addAnnotation("ClientIp", event.requestContext.identity.sourceIp);
        segment.addAnnotation("UserAgent", event.headers["User-Agent"]);
        if (event && event.requestContext && event.requestContext.authorizer) {
          segment.addAnnotation("PrincipalId", event.requestContext.authorizer.principalId);
        }
        segment.addAnnotation("Authorization", event.headers.Authorization);
        segment.addAnnotation("Stage", event.requestContext.stage);

        segment.addMetadata("query", event.queryStringParameters);
        segment.addMetadata("headers", event.headers);

      } catch (err) {
        winston.error(err);
      }
    }
  }
  if (next && typeof next === 'function') {
    next();
  }
};

{'fraction_non_alphanumeric': 0.16341689879294335, 'fraction_numerical': 0.0006190034045187249, 'mean_word_length': 3.882175226586103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5542718', 'n_tokens_mistral': 925, 'n_tokens_neox': 932, 'n_words': 158}