<commit_before>import assert = require('assert')
import * as Sentry from '@sentry/node'
import readPkgUp = require('read-pkg-up')
import lsmod = require('lsmod')

// keys to be banned
const BAN_LIST = {
  msg: true,
  time: true,
  hostname: true,
  name: true,
  level: true,
}

/**
 * Sentry stream for Pino
 */
class SentryStream {
  private release: string
  private env?: string = process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV
  private modules?: any = lsmod()

  constructor(opts: any) {
    this.release = opts.release
  }

  /**
   * Method call by Pino to save log record
   * msg is a stringified set of data
   */
  public write(msg: string) {
    const event = JSON.parse(msg)
    const extra = Object.create(null)

    for (const [key, value] of Object.entries<any>(event)) {
      // @ts-ignore
      if (BAN_LIST[key] === true) continue
      extra[key] = value
    }

    Sentry.captureEvent({
      extra,
      message: event.msg,
      timestamp: parseInt(event.time, 10),
      level: this.getSentryLevel(event.level),
      platform: 'node',
      server_name: event.hostname,
      logger: event.name,
      release: this.release,
      environment: this.env,
      sdk: {
        name: Sentry.SDK_NAME,
        version: Sentry.SDK_VERSION,
      },
      modules: this.modules,
      fingerprint: ['{{ default }}'],
      stacktrace: event.err ? event.err.stack : event.stack,
    })

    return true
  }

  /**
   * Error deserialiazing function. Bunyan serialize the error to object:
   * https://github.com/trentm/node-bunyan/blob/master/lib/bunyan.js#L1089
   * @param  {object} data serialized Bunyan
   * @return {Error}      the deserialiazed error
   */<commit_msg>fix: stacktrace extraction<commit_after>import assert = require('assert')
import * as Sentry from '@sentry/node'
import readPkgUp = require('read-pkg-up')
import lsmod = require('lsmod')

// keys to be banned
const BAN_LIST = {
  msg: true,
  time: true,
  hostname: true,
  name: true,
  level: true,
}

/**
 * Sentry stream for Pino
 */
class SentryStream {
  private release: string
  private env?: string = process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV
  private modules?: any = lsmod()

  constructor(opts: any) {
    this.release = opts.release
  }

  /**
   * Method call by Pino to save log record
   * msg is a stringified set of data
   */
  public write(msg: string) {
    const event = JSON.parse(msg)
    const extra = Object.create(null)

    for (const [key, value] of Object.entries<any>(event)) {
      // @ts-ignore
      if (BAN_LIST[key] === true) continue
      extra[key] = value
    }

    (async () => {
      let stacktrace = undefined
      if (event.err && event.err.stack) {
        try {
          const stack = await Sentry.Parsers.extractStackFromError(event.err)
          const frames = await Sentry.Parsers.parseStack(stack)
          stacktrace = { frames }
        } catch (e) { /* ignore */ }
      }
      Sentry.captureEvent({
        extra,
        stacktrace,
        message: event.msg,
        timestamp: parseInt(event.time, 10),
        level: this.getSentryLevel(event.level),
        platform: 'node',
        server_name: event.hostname,
        logger: event.name,
        release: this.release,
        environment: this.env,
        sdk: {
          name: Sentry.SDK_NAME,
          version: Sentry.SDK_VERSION,
        },
        modules: this.modules,
        fingerprint: ['{{ default }}'],
      })
    })()

    return true
  }

  /**
   * Error deserialiazing function. Bunyan serialize the error to object:
   * https://github.com/trentm/node-bunyan/blob/master/lib/bunyan.js#L1089
   * @param  {object} data serialized Bunyan
   * @return {Error}      the deserialiazed error
   */
{'fraction_non_alphanumeric': 0.1241342567927544, 'fraction_numerical': 0.0031965903036760787, 'mean_word_length': 2.5357815442561207, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, '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': '1364561', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1194, 'n_words': 306}