|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var truncate = require('unicode-byte-truncate') |
| 4 | + |
| 5 | +var config = require('./config') |
| 6 | + |
| 7 | +exports.transaction = truncTransaction |
| 8 | +exports.span = truncSpan |
| 9 | +exports.error = truncError |
| 10 | + |
| 11 | +function truncTransaction (trans) { |
| 12 | + trans.name = truncate(String(trans.name), config.INTAKE_STRING_MAX_SIZE) |
| 13 | + trans.type = truncate(String(trans.type), config.INTAKE_STRING_MAX_SIZE) |
| 14 | + trans.result = truncate(String(trans.result), config.INTAKE_STRING_MAX_SIZE) |
| 15 | + |
| 16 | + // Unless sampled, context will be null |
| 17 | + if (trans.sampled) truncContext(trans.context) |
| 18 | +} |
| 19 | + |
| 20 | +function truncSpan (span) { |
| 21 | + span.name = truncate(String(span.name), config.INTAKE_STRING_MAX_SIZE) |
| 22 | + span.type = truncate(String(span.type), config.INTAKE_STRING_MAX_SIZE) |
| 23 | + if (span.stacktrace) span.stacktrace = truncFrames(span.stacktrace) |
| 24 | +} |
| 25 | + |
| 26 | +function truncError (error, conf) { |
| 27 | + if (error.log) { |
| 28 | + if (error.log.level) { |
| 29 | + error.log.level = truncate(String(error.log.level), config.INTAKE_STRING_MAX_SIZE) |
| 30 | + } |
| 31 | + if (error.log.logger_name) { |
| 32 | + error.log.logger_name = truncate(String(error.log.logger_name), config.INTAKE_STRING_MAX_SIZE) |
| 33 | + } |
| 34 | + if (error.log.message && conf.errorMessageMaxLength >= 0) { |
| 35 | + error.log.message = truncate(String(error.log.message), conf.errorMessageMaxLength) |
| 36 | + } |
| 37 | + if (error.log.param_message) { |
| 38 | + error.log.param_message = truncate(String(error.log.param_message), config.INTAKE_STRING_MAX_SIZE) |
| 39 | + } |
| 40 | + if (error.log.stacktrace) { |
| 41 | + error.log.stacktrace = truncFrames(error.log.stacktrace) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (error.exception) { |
| 46 | + if (error.exception.message && conf.errorMessageMaxLength >= 0) { |
| 47 | + error.exception.message = truncate(String(error.exception.message), conf.errorMessageMaxLength) |
| 48 | + } |
| 49 | + if (error.exception.type) { |
| 50 | + error.exception.type = truncate(String(error.exception.type), config.INTAKE_STRING_MAX_SIZE) |
| 51 | + } |
| 52 | + if (error.exception.code) { |
| 53 | + error.exception.code = truncate(String(error.exception.code), config.INTAKE_STRING_MAX_SIZE) |
| 54 | + } |
| 55 | + if (error.exception.module) { |
| 56 | + error.exception.module = truncate(String(error.exception.module), config.INTAKE_STRING_MAX_SIZE) |
| 57 | + } |
| 58 | + if (error.exception.stacktrace) { |
| 59 | + error.exception.stacktrace = truncFrames(error.exception.stacktrace) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + truncContext(error.context) |
| 64 | +} |
| 65 | + |
| 66 | +function truncContext (context) { |
| 67 | + if (!context) return |
| 68 | + |
| 69 | + if (context.request) { |
| 70 | + if (context.request.method) { |
| 71 | + context.request.method = truncate(String(context.request.method), config.INTAKE_STRING_MAX_SIZE) |
| 72 | + } |
| 73 | + if (context.request.url) { |
| 74 | + if (context.request.url.protocol) { |
| 75 | + context.request.url.protocol = truncate(String(context.request.url.protocol), config.INTAKE_STRING_MAX_SIZE) |
| 76 | + } |
| 77 | + if (context.request.url.hostname) { |
| 78 | + context.request.url.hostname = truncate(String(context.request.url.hostname), config.INTAKE_STRING_MAX_SIZE) |
| 79 | + } |
| 80 | + if (context.request.url.port) { |
| 81 | + context.request.url.port = truncate(String(context.request.url.port), config.INTAKE_STRING_MAX_SIZE) |
| 82 | + } |
| 83 | + if (context.request.url.pathname) { |
| 84 | + context.request.url.pathname = truncate(String(context.request.url.pathname), config.INTAKE_STRING_MAX_SIZE) |
| 85 | + } |
| 86 | + if (context.request.url.search) { |
| 87 | + context.request.url.search = truncate(String(context.request.url.search), config.INTAKE_STRING_MAX_SIZE) |
| 88 | + } |
| 89 | + if (context.request.url.hash) { |
| 90 | + context.request.url.hash = truncate(String(context.request.url.hash), config.INTAKE_STRING_MAX_SIZE) |
| 91 | + } |
| 92 | + if (context.request.url.raw) { |
| 93 | + context.request.url.raw = truncate(String(context.request.url.raw), config.INTAKE_STRING_MAX_SIZE) |
| 94 | + } |
| 95 | + if (context.request.url.full) { |
| 96 | + context.request.url.full = truncate(String(context.request.url.full), config.INTAKE_STRING_MAX_SIZE) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + if (context.user) { |
| 101 | + if (context.user.id) { |
| 102 | + context.user.id = truncate(String(context.user.id), config.INTAKE_STRING_MAX_SIZE) |
| 103 | + } |
| 104 | + if (context.user.email) { |
| 105 | + context.user.email = truncate(String(context.user.email), config.INTAKE_STRING_MAX_SIZE) |
| 106 | + } |
| 107 | + if (context.user.username) { |
| 108 | + context.user.username = truncate(String(context.user.username), config.INTAKE_STRING_MAX_SIZE) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function truncFrames (frames) { |
| 114 | + frames.forEach(function (frame, i) { |
| 115 | + if (frame.pre_context) frame.pre_context = truncEach(frame.pre_context, 1000) |
| 116 | + if (frame.context_line) frame.context_line = truncate(String(frame.context_line), 1000) |
| 117 | + if (frame.post_context) frame.post_context = truncEach(frame.post_context, 1000) |
| 118 | + }) |
| 119 | + |
| 120 | + return frames |
| 121 | +} |
| 122 | + |
| 123 | +function truncEach (arr, len) { |
| 124 | + return arr.map(function (str) { |
| 125 | + return truncate(String(str), len) |
| 126 | + }) |
| 127 | +} |
0 commit comments