-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathindex.js
1407 lines (1227 loc) · 42.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const errorHelper = require('../errors/helper')
const hashes = require('../util/hashes')
const logger = require('../logger').child({ component: 'transaction' })
const Metrics = require('../metrics')
const NAMES = require('../metrics/names')
const NameState = require('./name-state')
const props = require('../util/properties')
const Timer = require('../timer')
const Trace = require('./trace')
const synthetics = require('../synthetics')
const url = require('url')
const urltils = require('../util/urltils')
const TraceContext = require('./tracecontext').TraceContext
const Logs = require('./logs')
const DT_ACCEPT_PAYLOAD_EXCEPTION_METRIC = 'DistributedTrace/AcceptPayload/Exception'
const DT_ACCEPT_PAYLOAD_PARSE_EXCEPTION_METRIC = 'DistributedTrace/AcceptPayload/ParseException'
const REQUEST_PARAMS_PATH = 'request.parameters.'
/*
*
* CONSTANTS
*
*/
const DESTS = require('../config/attribute-filter').DESTINATIONS
const FROM_MILLIS = 1e-3
const TYPES = {
WEB: 'web',
BG: 'bg',
MESSAGE: 'message'
}
const TYPES_SET = _makeValueSet(TYPES)
const TYPE_METRICS = {
web: NAMES.WEB.RESPONSE_TIME,
bg: NAMES.OTHER_TRANSACTION.RESPONSE_TIME,
message: NAMES.OTHER_TRANSACTION.MESSAGE
}
const TRANSPORT_TYPES = {
AMQP: 'AMQP',
HTTP: 'HTTP',
HTTPS: 'HTTPS',
IRONMQ: 'IronMQ',
JMS: 'JMS',
KAFKA: 'Kafka',
OTHER: 'Other',
QUEUE: 'Queue',
UNKNOWN: 'Unknown'
}
const TRANSPORT_TYPES_SET = _makeValueSet(TRANSPORT_TYPES)
const REQUIRED_DT_KEYS = ['ty', 'ac', 'ap', 'tr', 'ti']
const DTPayload = require('./dt-payload')
const DTPayloadStub = DTPayload.Stub
const TRACE_CONTEXT_PARENT_HEADER = 'traceparent'
const TRACE_CONTEXT_STATE_HEADER = 'tracestate'
const NEWRELIC_TRACE_HEADER = 'newrelic'
const MULTIPLE_INSERT_MESSAGE =
'insertDistributedTraceHeaders called on headers object that already contains ' +
"distributed trace data. These may be overwritten. traceparent? '%s', newrelic? '%s'."
/**
* Bundle together the metrics and the trace segment for a single agent
* transaction.
*
* @param {object} agent The agent.
*
* @fires Agent#transactionStarted
*/
function Transaction(agent) {
if (!agent) {
throw new Error('every transaction must be bound to the agent')
}
this.agent = agent
this.metrics = new Metrics(agent.config.apdex_t, agent.mapper, agent.metricNameNormalizer)
++agent.activeTransactions
this.numSegments = 0
this.id = hashes.makeId(16)
this.trace = new Trace(this)
this.exceptions = []
this.userErrors = []
this.timer = new Timer()
this.timer.begin()
this._recorders = []
this._intrinsicAttributes = Object.create(null)
this._partialName = null
// If handledExternally is set to true the transaction will not ended
// automatically, instead it should be ended by user code.
this.handledExternally = false
// hidden class optimization
this.catResponseTime = 0
this.error = null
this.forceIgnore = null
this.forceName = null
this.ignore = false
this.incomingCatId = null
this.name = null
this.nameState = new NameState(null, null, null, null)
this.pathHashes = []
this.queueTime = 0
this.referringPathHash = null
this.referringTransactionGuid = null
this.invalidIncomingExternalTransaction = false
this.statusCode = null
this.syntheticsHeader = null
this.syntheticsInfoHeader = null
this.syntheticsData = null
this.syntheticsInfoData = null
this.url = null
this.parsedUrl = null
this.verb = null
this.baseSegment = null
this.type = TYPES.WEB
// DT fields
this.parentId = null
this.parentType = null
this.parentApp = null
this.parentAcct = null
this.parentTransportType = null
this.parentTransportDuration = null
this._traceId = null
Object.defineProperty(this, 'traceId', {
get() {
if (this._traceId === null) {
this._traceId = hashes.makeId(32)
}
return this._traceId
},
set(traceId) {
this._traceId = traceId
}
})
this.parentSpanId = null
this.isDistributedTrace = null
this.acceptedDistributedTrace = null
// Lazy evaluate the priority and sampling in case we end up accepting a payload.
this.priority = null
this.sampled = null
this.traceContext = new TraceContext(this)
this.logs = new Logs(agent)
this.ignoreApdex = false
agent.emit('transactionStarted', this)
}
Transaction.DESTINATIONS = DESTS
Transaction.TYPES = TYPES
Transaction.TYPES_SET = TYPES_SET
Transaction.TRANSPORT_TYPES = TRANSPORT_TYPES
Transaction.TRANSPORT_TYPES_SET = TRANSPORT_TYPES_SET
Transaction.TRACE_CONTEXT_PARENT_HEADER = TRACE_CONTEXT_PARENT_HEADER
/**
* Add a clear API method for determining whether a transaction is web or
* background.
*
* @returns {boolean} Whether this transaction has a URL.
*/
Transaction.prototype.isWeb = function isWeb() {
return this.type === TYPES.WEB
}
/**
* @returns {boolean} Is this transaction still alive?
*/
Transaction.prototype.isActive = function isActive() {
return this.timer.isActive()
}
/**
* Close out the current transaction and its associated trace. Remove any
* instances of this transaction annotated onto the call stack.
*
* @returns {(Transaction|undefined)} this transaction, or undefined
*
* @fires Agent#transactionFinished
*/
Transaction.prototype.end = function end() {
if (!this.timer.isActive()) {
return
}
if (!this.name) {
this.finalizeName(null) // Use existing partial name.
}
if (this.baseSegment) {
this.baseSegment.touch()
}
this.agent.recordSupportability('Nodejs/Transactions/Segments', this.numSegments)
this._calculatePriority()
this.trace.end()
this.timer.end()
// recorders must be run before the trace is collected
if (!this.ignore) {
this.record()
// This method currently must be called after all recorders have been fired due
// to some of the recorders (namely the db recorders) adding parameters to the
// segments.
this.trace.generateSpanEvents()
this.logs.flush(this.priority)
}
this.agent.emit('transactionFinished', this)
// Do after emit so all post-processing can complete
this._cleanUneededReferences()
return this
}
/**
* Cleans up references that will not be used later for processing such as
* transaction traces.
*
* Errors won't be needed for later processing but can contain extra details we
* don't want to hold in memory. Particularly, axios errors can result in indirect
* references to promises which will prevent them from being destroyed and result
* in a memory leak. This is due to the TraceSegment not getting removed from the
* async-hooks segmentMap because 'destroy' never fires.
*/
Transaction.prototype._cleanUneededReferences = function _cleanUneededReferences() {
this.userErrors = null
this.exceptions = null
}
/**
* For web transactions, this represents the time from when the request was received
* to when response was sent. For background transactions, it is equal to duration
* of the transaction trace (until last segment ended).
*
* @returns {number} timer or trace duration in milliseconds
*/
Transaction.prototype.getResponseTimeInMillis = function getResponseTimeInMillis() {
if (this.isWeb()) {
return this.timer.getDurationInMillis()
}
return this.trace.getDurationInMillis()
}
/**
* Executes the user and server provided naming rules to clean up the given url.
*
* @private
* @param {string} requestUrl - The URL to normalize.
* @returns {object} The normalization results after running user and server rules.
*/
Transaction.prototype._runUserNamingRules = function _runUserNamingRules(requestUrl) {
// 1. user normalization rules (set in configuration)
const normalized = this.agent.userNormalizer.normalize(requestUrl)
if (normalized.matched) {
// After applying user naming rule, apply server-side sent rules to
// further squash possible MGIs
const serverNormalized = this.agent.urlNormalizer.normalize(normalized.value)
if (serverNormalized.ignore) {
normalized.ignore = true
}
if (serverNormalized.matched) {
// NAMES.NORMALIZED is prepended by the sever rule normalizer
normalized.value = serverNormalized.value
} else {
normalized.value = NAMES.NORMALIZED + normalized.value
}
}
return normalized
}
/**
* Executes the user naming rules and applies the results to the transaction.
*
* @param {string} requestUrl - The URL to normalize and apply to this transaction.
*/
Transaction.prototype.applyUserNamingRules = function applyUserNamingRules(requestUrl) {
const normalized = this._runUserNamingRules(requestUrl)
if (normalized.ignore) {
this.ignore = normalized.ignore
}
if (normalized.matched) {
this._partialName = normalized.value
}
}
/**
* Set's the transaction partial name.
*
* The partial name is everything after the `WebTransaction/` part.
*
* @param {string} name - The new transaction partial name to use.
*/
Transaction.prototype.setPartialName = function setPartialName(name) {
this._partialName = name
}
/**
* Derive the transaction partial name from the given url and status code.
*
* @private
* @param {string} requestUrl - The URL to derive the name from.
* @param {number} status - The status code of the response.
* @returns {object} An object with the derived partial name in `value` and a
* boolean flag in `ignore`.
*/
Transaction.prototype._partialNameFromUri = _partialNameFromUri
function _partialNameFromUri(requestUrl, status) {
const scrubbedUrl = urltils.scrub(requestUrl)
// 0. If there is a name in the name-state stack, use it.
let partialName = this._partialName
let ignore = false
if (!this.nameState.isEmpty()) {
partialName = this.nameState.getFullName()
}
// 1. name set by the api
if (this.forceName !== null) {
partialName = this.forceName
}
// 2. user normalization rules (set in configuration) can override transaction
// naming from API
const userNormalized = this._runUserNamingRules(scrubbedUrl)
ignore = ignore || userNormalized.ignore
if (userNormalized.matched) {
partialName = userNormalized.value
}
// 3. URL normalization rules (sent by server).
// Nothing has already set a name for this transaction, so normalize and
// potentially apply the URL backstop now. Only do so if no user rules matched.
if (!partialName) {
// avoid polluting root path when 404
const statusName = this.nameState.getStatusName(status)
if (statusName) {
partialName = statusName
} else {
const normalized = this.agent.urlNormalizer.normalize(scrubbedUrl)
ignore = ignore || normalized.ignore
partialName = normalized.value
}
}
return {
ignore,
value: partialName
}
}
/**
* Set the forceIgnore value on the transaction. This will cause the
* transaction to clean up after itself without collecting any data.
*
* @param {boolean} ignore The value to assign to transaction.ignore
*/
Transaction.prototype.setForceIgnore = function setForceIgnore(ignore) {
if (ignore != null) {
this.forceIgnore = ignore
} else {
logger.debug('Transaction#setForceIgnore called with null value')
}
}
/**
*
* Gets the current ignore state for the transaction.
*
*/
Transaction.prototype.isIgnored = function getIgnore() {
return this.ignore || this.forceIgnore
}
/**
* Derives the transaction's name from the given URL and status code.
*
* The transaction's name will be set after this as well as its ignored status
* based on the derived name.
*
* @param {string} requestURL - The URL to derive the request's name and status from.
* @param {number} statusCode - The response status code.
*/
Transaction.prototype.finalizeNameFromUri = finalizeNameFromUri
function finalizeNameFromUri(requestURL, statusCode) {
if (logger.traceEnabled()) {
logger.trace(
{
requestURL,
statusCode,
transactionId: this.id,
transactionName: this.name
},
'Setting transaction name'
)
}
this.url = urltils.scrub(requestURL)
this.statusCode = statusCode
this.name = this.getFullName()
// If a namestate stack exists, copy route parameters over to the trace.
if (!this.nameState.isEmpty() && this.baseSegment) {
this.nameState.forEachParams(forEachRouteParams, this)
}
// Allow the API to explicitly set the ignored status.
if (this.forceIgnore !== null) {
this.ignore = this.forceIgnore
}
this.baseSegment && this._markAsWeb(requestURL)
this._copyNameToActiveSpan(this.name)
if (logger.traceEnabled()) {
logger.trace(
{
transactionId: this.id,
transactionName: this.name,
ignore: this.ignore
},
'Finished setting transaction name from Uri'
)
}
}
function forEachRouteParams(params) {
for (const key in params) {
if (props.hasOwn(params, key)) {
this.trace.attributes.addAttribute(DESTS.NONE, key, params[key])
const segment = this.agent.tracer.getSegment()
if (!segment) {
logger.trace(
'Active segment not available, not adding request.parameters.route attribute for %s',
key
)
} else {
segment.attributes.addAttribute(DESTS.NONE, key, params[key])
}
}
}
}
Transaction.prototype._copyNameToActiveSpan = function _copyNameToActiveSpan(name) {
const spanContext = this.agent.tracer.getSpanContext()
if (!spanContext) {
logger.trace('Span context not available, not adding transaction.name attribute for %s', name)
return
}
spanContext.addIntrinsicAttribute('transaction.name', name)
}
/**
* Copies final base segment parameters to trace attributes before reapplying
* them to the segment.
*
* Handles adding query parameters to `request.parameter.*` attributes
*
* @param {string} rawURL The URL, as it came in, for parameter extraction.
*/
Transaction.prototype._markAsWeb = function _markAsWeb(rawURL) {
// Because we are assured we have the URL here, lets grab query params.
const params = urltils.parseParameters(rawURL)
for (const key in params) {
if (props.hasOwn(params, key)) {
this.trace.attributes.addAttribute(DESTS.NONE, REQUEST_PARAMS_PATH + key, params[key])
const segment = this.agent.tracer.getSegment()
if (!segment) {
logger.trace(
'Active segment not available, not adding request.parameters span attribute for %s',
key
)
} else {
segment.attributes.addAttribute(DESTS.NONE, REQUEST_PARAMS_PATH + key, params[key])
}
}
}
this.baseSegment.markAsWeb(this)
}
/**
* Sets the transaction's name and determines if it will be ignored.
*
* @param {string} [name]
* Optional. The partial name to use for the finalized transaction. If omitted
* the current partial name is used.
* @returns {undefined} undefined, finalizing name as a side effect
*/
Transaction.prototype.finalizeName = function finalizeName(name) {
// If no name is given, and this is a web transaction with a url, then
// finalize the name using the stored url.
if (name == null && this.isWeb() && this.url) {
return this.finalizeNameFromUri(this.url, this.statusCode)
}
// this may seem out of place but certain API methods
// set the _partialName directly so use that as a fallback
this._partialName = name || this._partialName
name = this.getFullName()
if (!name) {
logger.debug('No name for transaction %s, not finalizing.', this.id)
return
}
this.name = name
if (this.forceIgnore !== null) {
this.ignore = this.forceIgnore
}
this.baseSegment && this.baseSegment.setNameFromTransaction(this)
this._copyNameToActiveSpan(this.name)
if (logger.traceEnabled()) {
logger.trace(
{
transactionId: this.id,
transactionName: this.name,
ignore: this.ignore
},
'Finished setting transaction name from string'
)
}
}
/**
* Gets the transaction name safely.
*
* Gathering the transaction name for WebTransactions is risky complicated
* business. OtherTransactions (aka background) are much simpler as they are
* always fully specified by the user at creation time.
*
* This has the potential of causing the normalizers run extra times, which can
* cause extra performance overhead. Once this is refactored we can make the
* caching better and eliminate this extra overhead. Be mindful of if/when this
* is called.
*
* @returns {string} finalized name value or partial name
*/
Transaction.prototype.getName = function getName() {
if (this.isWeb() && this.url) {
const finalName = this._partialNameFromUri(this.url, this.statusCode)
if (finalName.ignore) {
this.ignore = true
}
return finalName.value
}
return this._partialName
}
Transaction.prototype.getFullName = function getFullName() {
let name = null
// use value from `api.setTransaction`
if (this.forceName) {
name = this.forceName
// use value from previously finalized named
} else if (this.name) {
return this.name
// derive name from uri in web case
// or just use whatever was this._partialName
} else {
name = this.getName()
}
if (!name) {
return null
}
this._partialName = name
let fullName = TYPE_METRICS[this.type] + '/' + name
const normalized = this.agent.transactionNameNormalizer.normalize(fullName)
if (normalized.ignore) {
this.ignore = true
}
fullName = normalized.value
// apply transaction segment term normalizer
// only to web transactions
if (this.isWeb() && this.url) {
fullName = this.agent.txSegmentNormalizer.normalize(fullName).value
}
return fullName
}
/**
* Returns the full URL of the transaction with query, search, or hash portions
* removed. This is only applicable for web transactions.
*
* Caches to ._scrubbedUrl, pulls in from .parsedUrl if it is available,
* otherwise it will parse .url, store it on .parsedUrl, then scrub the URL and
* store it in the cache.
*
* @returns {(string|undefined)} Returns a string or undefined.
*/
Transaction.prototype.getScrubbedUrl = function getScrubbedUrl() {
if (!this.isWeb()) {
return
}
if (this._scrubbedUrl) {
return this._scrubbedUrl
}
// If we don't have a parsedUrl, lets populate it from .url
if (!this.parsedUrl) {
// At time of writing .url should always be set by the time we get here
// because that is what .isWeb() checks against. In the future it may be
// instead checking a enum or other property so guard ourselves just in
// case.
if (!this.url) {
return
}
this.parsedUrl = url.parse(this.url)
}
const scrubbedParsedUrl = Object.assign(Object.create(null), this.parsedUrl)
scrubbedParsedUrl.search = null
scrubbedParsedUrl.query = null
scrubbedParsedUrl.href = null
scrubbedParsedUrl.path = null
scrubbedParsedUrl.hash = null
this._scrubbedUrl = url.format(scrubbedParsedUrl)
return this._scrubbedUrl
}
/**
* The instrumentation associates metrics with the different kinds of trace
* segments. The metrics recorders are dependent on the transaction name to
* collect their scoped metrics, and so must wait for the transaction's
* name to be finalized before the recording process. Segments are only
* responsible for their own life cycle, so responsibility for understanding
* when the transaction name has been finalized is handed off to the trace,
* which for now defers running these recorders until the trace is ended.
*
* @param {Function} recorder The callback which records metrics. Takes a
* single parameter, which is the transaction's
* name.
*/
Transaction.prototype.addRecorder = function addRecorder(recorder) {
this._recorders.push(recorder)
}
/**
* Run the metrics recorders for this trace. If the transaction's name /
* scope hasn't been set yet, the recorder will be passed an undefined name,
* and should be written to handle this.
*/
Transaction.prototype.record = function record() {
const name = this.name
for (let i = 0, l = this._recorders.length; i < l; ++i) {
this._recorders[i](name, this)
}
}
/**
* Measure the duration of an operation named by a metric, optionally
* belonging to a scope.
*
* @param {string} name The name of the metric to gather.
* @param {string} scope (optional) Scope to which the metric is bound.
* @param {number} duration The time taken by the operation, in milliseconds.
* @param {number} exclusive The time exclusively taken by an operation, and
* not its children.
*/
Transaction.prototype.measure = function measure(name, scope, duration, exclusive) {
this.metrics.measureMilliseconds(name, scope, duration, exclusive)
}
/**
* Based on the status code and the duration of a web transaction, either
* mark the transaction as frustrating, or record its time for apdex purposes.
*
* @param {string} name Metric name.
* @param {number} duration Duration of the transaction, in milliseconds.
* @param {number} keyApdexInMillis Duration sent to the metrics getOrCreateApdexMetric method, to
* derive apdex from timing in milliseconds
*/
Transaction.prototype._setApdex = function _setApdex(name, duration, keyApdexInMillis) {
if (this.ignoreApdex) {
logger.warn('Ignoring the collection of apdex stats for %s as ignoreApdex is true', this.name)
return
}
const apdexStats = this.metrics.getOrCreateApdexMetric(name, null, keyApdexInMillis)
// if we have an error-like status code, and all the errors are
// expected, we know the status code was caused by an expected
// error, so we will not report "frustrating." Otherwise, we
// don't know which error triggered the error-like status code,
// and will still increment "frustrating." If this is an issue,
// users can either set a status code as expected, or ignore the
// specific error to avoid incrementing to frustrating
if (
urltils.isError(this.agent.config, this.statusCode) &&
!urltils.isExpectedError(this.agent.config, this.statusCode) &&
!this.hasOnlyExpectedErrors()
) {
apdexStats.incrementFrustrating()
} else {
apdexStats.recordValueInMillis(duration, keyApdexInMillis)
}
}
/**
* Store first 10 unique path hashes calculated for a transaction.
*
* @param {string} pathHash Path hash
* @returns {undefined}
*/
Transaction.prototype.pushPathHash = function pushPathHash(pathHash) {
if (this.pathHashes.length >= 10 || this.pathHashes.indexOf(pathHash) !== -1) {
return
}
this.pathHashes.unshift(pathHash)
}
/**
* Return whether transaction spawned any outbound requests.
*
* @returns {boolean} if there are more than zero pathHashes
*/
Transaction.prototype.includesOutboundRequests = function includesOutboundRequests() {
return this.pathHashes.length > 0
}
/**
* Get unique previous path hashes for a transaction. Does not include
* current path hash.
*
* @returns {(string|null)} Returns sorted altHashes joined by commas, or null.
*/
Transaction.prototype.alternatePathHashes = function alternatePathHashes() {
const curHash = hashes.calculatePathHash(
this.agent.config.applications()[0],
this.getFullName(),
this.referringPathHash
)
const altHashes = this.pathHashes.slice()
const curIndex = altHashes.indexOf(curHash)
if (curIndex !== -1) {
altHashes.splice(curIndex, 1)
}
return altHashes.length === 0 ? null : altHashes.sort().join(',')
}
/**
* Add the error information to the current segment and add the segment ID as
* an attribute onto the exception.
*
* @param {Exception} exception The exception object to be collected.
*/
Transaction.prototype._linkExceptionToSegment = _linkExceptionToSegment
function _linkExceptionToSegment(exception) {
const segment = this.agent.tracer.getSegment()
if (!segment) {
return
}
const spanContext = segment.getSpanContext()
if (spanContext) {
// Exception attributes will be added to span unless transaction
// status code has been ignored. Last error wins.
const config = this.agent.config
const details = exception.getErrorDetails(config)
spanContext.setError(details)
}
// Add the span/segment ID to the exception as agent attributes
exception.agentAttributes.spanId = segment.id
}
/**
* Associate an exception with the transaction. When the transaction ends,
* the exception will be collected along with the transaction details.
*
* @param {Exception} exception The exception object to be collected.
*/
Transaction.prototype.addException = _addException
function _addException(exception) {
if (!this.isActive()) {
logger.trace('Transaction is not active. Not capturing error: ', exception)
return
}
this._linkExceptionToSegment(exception)
this.exceptions.push(exception)
}
/**
* Associate a user error (reported using the noticeError() API) with the transaction.
* When the transaction ends, the exception will be collected along with the transaction
* details.
*
* @param {Exception} exception The exception object to be collected.
*/
Transaction.prototype.addUserError = _addUserError
function _addUserError(exception) {
if (!this.isActive()) {
logger.trace('Transaction is not active. Not capturing user error: ', exception)
return
}
this._linkExceptionToSegment(exception)
this.userErrors.push(exception)
}
/**
* @returns {boolean} true if the transaction's current status code is errored
* but considered ignored via the config.
*/
Transaction.prototype.hasIgnoredErrorStatusCode = function _hasIgnoredErrorStatusCode() {
return urltils.isIgnoredError(this.agent.config, this.statusCode)
}
/**
* @returns {boolean} true if an error happened during the transaction or if the transaction itself is
* considered to be an error.
*/
Transaction.prototype.hasErrors = function _hasErrors() {
const isErroredTransaction = urltils.isError(this.agent.config, this.statusCode)
const transactionHasExceptions = this.exceptions.length > 0
const transactionHasuserErrors = this.userErrors.length > 0
return transactionHasExceptions || transactionHasuserErrors || isErroredTransaction
}
/**
* @returns {boolean} true if all the errors/exceptions collected so far are expected errors
*/
Transaction.prototype.hasOnlyExpectedErrors = function hasOnlyExpectedErrors() {
if (this.exceptions.length === 0) {
return false
}
for (let i = 0; i < this.exceptions.length; i++) {
const exception = this.exceptions[i]
// this exception is neither expected nor ignored
const isUnexpected = !(
errorHelper.isExpectedException(this, exception, this.agent.config, urltils) ||
errorHelper.shouldIgnoreError(this, exception.error, this.agent.config)
)
if (isUnexpected) {
return false
}
}
return true
}
/**
* @returns {object} agent intrinsic attribute for this transaction.
*/
Transaction.prototype.getIntrinsicAttributes = function getIntrinsicAttributes() {
if (!this._intrinsicAttributes.totalTime) {
const config = this.agent.config
this._intrinsicAttributes.totalTime = this.trace.getTotalTimeDurationInMillis() * FROM_MILLIS
if (config.distributed_tracing.enabled) {
this.addDistributedTraceIntrinsics(this._intrinsicAttributes)
} else if (config.cross_application_tracer.enabled) {
this._intrinsicAttributes.path_hash = hashes.calculatePathHash(
config.applications()[0],
this.name || this._partialName,
this.referringPathHash
)
this._intrinsicAttributes.trip_id = this.tripId || this.id
if (this.referringTransactionGuid) {
this._intrinsicAttributes.referring_transaction_guid = this.referringTransactionGuid
}
if (this.incomingCatId) {
this._intrinsicAttributes.client_cross_process_id = this.incomingCatId
}
}
synthetics.assignIntrinsicsToTransaction(this)
}
return Object.assign(Object.create(null), this._intrinsicAttributes)
}
/**
* Parsing incoming headers for use in a distributed trace.
* W3C TraceContext format is preferred over the NewRelic DT format.
* NewRelic DT format will be used if no `traceparent` header is found.
*
* @param {string} [transport='Unknown'] - The transport type that delivered the trace.
* @param {object} headers - Headers to search for supported trace formats. Keys must be lowercase.
*/
Transaction.prototype.acceptDistributedTraceHeaders = acceptDistributedTraceHeaders
function acceptDistributedTraceHeaders(transportType, headers) {
if (headers == null || typeof headers !== 'object') {
logger.trace(
'Ignoring distributed trace headers for transaction %s. Headers not passed in as object.',
this.id
)
return
}
const transport = TRANSPORT_TYPES_SET[transportType] ? transportType : TRANSPORT_TYPES.UNKNOWN
// assumes header keys already lowercase
const traceparent = headers[TRACE_CONTEXT_PARENT_HEADER]
if (traceparent) {
logger.trace('Accepting trace context DT payload for transaction %s', this.id)
// assumes header keys already lowercase
const tracestate = headers[TRACE_CONTEXT_STATE_HEADER]
this.acceptTraceContextPayload(traceparent, tracestate, transport)
} else if (NEWRELIC_TRACE_HEADER in headers) {
logger.trace('Accepting newrelic DT payload for transaction %s', this.id)
// assumes header keys already lowercase
const payload = headers[NEWRELIC_TRACE_HEADER]
this._acceptDistributedTracePayload(payload, transport)
}
}
/**
* Inserts distributed trace headers into the provided headers map.
*
* @param {object} headers
*/
Transaction.prototype.insertDistributedTraceHeaders = insertDistributedTraceHeaders
function insertDistributedTraceHeaders(headers) {
if (!headers) {
logger.trace('insertDistributedTraceHeaders called without headers.')
return
}
checkForExistingNrTraceHeaders(headers)
// Ensure we have priority before generating trace headers.
this._calculatePriority()
this.traceContext.addTraceContextHeaders(headers)
this.isDistributedTrace = true
logger.trace('Added outbound request w3c trace context headers in transaction %s', this.id)
if (this.agent.config.distributed_tracing.exclude_newrelic_header) {
logger.trace('Excluding newrelic header due to exclude_newrelic_header: true')
return
}
try {
const newrelicFormatData = this._createDistributedTracePayload().httpSafe()
headers[NEWRELIC_TRACE_HEADER] = newrelicFormatData
logger.trace('Added outbound request distributed tracing headers in transaction %s', this.id)
} catch (error) {
logger.trace(error, 'Failed to create distributed trace payload')
}
}
function checkForExistingNrTraceHeaders(headers) {
const traceparentHeader = headers[TRACE_CONTEXT_PARENT_HEADER]
const newrelicHeader = headers[NEWRELIC_TRACE_HEADER]
const hasExisting = traceparentHeader || newrelicHeader
if (hasExisting) {
logger.trace(MULTIPLE_INSERT_MESSAGE, traceparentHeader, newrelicHeader)
}
}
Transaction.prototype.acceptTraceContextPayload = acceptTraceContextPayload
function acceptTraceContextPayload(traceparent, tracestate, transport) {
if (this.isDistributedTrace) {
logger.warn(
'Already accepted or created a distributed trace payload for transaction %s, ignoring call',
this.id
)