Skip to content

Commit

Permalink
add metrics to track span lifecycle in debug mode (#1896)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Mar 30, 2022
1 parent 6ec16b1 commit 90b4d32
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/dd-trace/src/opentracing/span.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
'use strict'

// TODO (new internal tracer): use DC events for lifecycle metrics and test them

const opentracing = require('opentracing')
const now = require('performance-now')
const semver = require('semver')
const Span = opentracing.Span
const SpanContext = require('./span_context')
const id = require('../id')
const tagger = require('../tagger')
const metrics = require('../metrics')
const log = require('../log')
const { storage } = require('../../../datadog-core')

const { DD_TRACE_EXPERIMENTAL_STATE_TRACKING } = process.env

const unfinishedRegistry = createRegistry('unfinished')
const finishedRegistry = createRegistry('finished')

class DatadogSpan extends Span {
constructor (tracer, processor, prioritySampler, fields, debug) {
super()
Expand All @@ -25,13 +32,21 @@ class DatadogSpan extends Span {
this._processor = processor
this._prioritySampler = prioritySampler
this._store = storage.getStore()
this._name = operationName

this._spanContext = this._createContext(parent)
this._spanContext._name = operationName
this._spanContext._tags = tags
this._spanContext._hostname = hostname

this._startTime = fields.startTime || this._getTime()

if (this._debug && unfinishedRegistry) {
metrics.increment('runtime.node.spans.unfinished')
metrics.increment('runtime.node.spans.unfinished.by.name', `span_name:${operationName}`)

unfinishedRegistry.register(this, operationName, this)
}
}

toString () {
Expand Down Expand Up @@ -122,6 +137,16 @@ class DatadogSpan extends Span {
}
}

if (this._debug && finishedRegistry) {
metrics.decrement('runtime.node.spans.unfinished')
metrics.decrement('runtime.node.spans.unfinished.by.name', `span_name:${this._name}`)
metrics.increment('runtime.node.spans.finished')
metrics.increment('runtime.node.spans.finished.by.name', `span_name:${this._name}`)

unfinishedRegistry.unregister(this)
finishedRegistry.register(this, this._name)
}

finishTime = parseFloat(finishTime) || this._getTime()

this._duration = finishTime - this._startTime
Expand All @@ -131,4 +156,13 @@ class DatadogSpan extends Span {
}
}

function createRegistry (type) {
if (!semver.satisfies(process.version, '>=14.6')) return

return new global.FinalizationRegistry(name => {
metrics.decrement(`runtime.node.spans.${type}`)
metrics.decrement(`runtime.node.spans.${type}.by.name`, [`span_name:${name}`])
})
}

module.exports = DatadogSpan

0 comments on commit 90b4d32

Please sign in to comment.