Skip to content

Commit

Permalink
refactor and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlanabrennan committed Oct 14, 2024
1 parent f3a534f commit fb652fc
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 350 deletions.
2 changes: 1 addition & 1 deletion lib/transaction/trace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Trace.prototype.end = function end() {

while (segments.length) {
const segment = segments.pop()
segment.finalize(this.transaction)
segment.finalize()

const children = segment.getChildren()
for (let i = 0; i < children.length; ++i) {
Expand Down
32 changes: 18 additions & 14 deletions lib/transaction/trace/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict'

const { DESTINATIONS } = require('../../config/attribute-filter')
const logger = require('../../logger').child({ component: 'segment' })
const Timer = require('../../timer')
const hashes = require('../../util/hashes')

Expand Down Expand Up @@ -69,7 +70,7 @@ function TraceSegment({ config, name, collect, traceStacks, root, isRoot = false
this.ignore = false
this.traceStacks = traceStacks

this.traceStacks.probe('new TraceSegment', { segment: this.name })
this.probe('new TraceSegment', { segment: this.name })
}

TraceSegment.prototype.getSpanContext = function getSpanContext() {
Expand Down Expand Up @@ -128,7 +129,7 @@ TraceSegment.prototype.probe = function probe(action) {
* For use when a transaction is ending. The transaction segment should
* be named after the transaction it belongs to (which is only known by
* the end).
* @param transaction The transaction.
* @param {Transaction} transaction The transaction to which this segment will be bound.
*/

TraceSegment.prototype.setNameFromTransaction = function setNameFromTransaction(transaction) {
Expand All @@ -146,7 +147,7 @@ TraceSegment.prototype.setNameFromTransaction = function setNameFromTransaction(
* recording, it's also necessary to copy the transaction's partial name. And
* finally, marking the trace segment as being a web segment copies the
* segment's parameters onto the transaction.
* @param transaction The transaction.
* @param {Transaction} transaction The transaction to which this segment will be bound.
*/

TraceSegment.prototype.markAsWeb = function markAsWeb(transaction) {
Expand All @@ -164,12 +165,11 @@ TraceSegment.prototype.markAsWeb = function markAsWeb(transaction) {
* A segment attached to something evented (such as a database
* cursor) just finished an action, so set the timer to mark
* the timer as having a stop time.
* @param transaction
*/
TraceSegment.prototype.touch = function touch(transaction) {
this.traceStacks.probe('Touched', { segment: this.name })
TraceSegment.prototype.touch = function touch() {
this.probe('Touched', { segment: this.name })
this.timer.touch()
this._updateRootTimer(transaction)
this._updateRootTimer()
}

TraceSegment.prototype.overwriteDurationInMillis = overwriteDurationInMillis
Expand All @@ -183,20 +183,19 @@ TraceSegment.prototype.start = function start() {

/**
* Stop timing the related action.
* @param transaction
*/
TraceSegment.prototype.end = function end(transaction) {
TraceSegment.prototype.end = function end() {
if (!this.timer.isActive()) {
return
}
this.traceStacks.probe('Ended', { segment: this.name })
this.timer.end()
this._updateRootTimer(transaction)
this._updateRootTimer()
}

TraceSegment.prototype.finalize = function finalize(transaction) {
TraceSegment.prototype.finalize = function finalize() {
if (this.timer.softEnd()) {
this._updateRootTimer(transaction)
this._updateRootTimer()
// timer.softEnd() returns true if the timer was ended prematurely, so
// in that case we can name the segment as truncated
this.name = NAMES.TRUNCATED.PREFIX + this.name
Expand Down Expand Up @@ -242,6 +241,12 @@ TraceSegment.prototype._isEnded = function _isEnded() {
* @returns {TraceSegment} New nested TraceSegment.
*/
TraceSegment.prototype.add = function add({ config, name, collect, traceStacks, root }) {
// this is needed here to check when add is called directly on segment
if (this.opaque) {
logger.trace('Skipping child addition on opaque segment')
return this
}

const segment = new TraceSegment({ config, name, collect, traceStacks, root })

this.children.push(segment)
Expand Down Expand Up @@ -378,9 +383,8 @@ TraceSegment.prototype._getChildPairs = function _getChildPairs(end) {
* 5: a "label"
*
* FIXME: I don't know if it makes sense to add custom fields for Node. TBD
* @param transaction
*/
TraceSegment.prototype.toJSON = function toJSON(transaction) {
TraceSegment.prototype.toJSON = function toJSON() {
// use depth-first search on the segment tree using stack
const resultDest = []
// array of objects relating a segment and the destination for its
Expand Down
Loading

0 comments on commit fb652fc

Please sign in to comment.