Skip to content

Commit

Permalink
update tracer to be extendable and extend it
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed May 8, 2024
1 parent b0a023e commit 86cd058
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
24 changes: 14 additions & 10 deletions packages/dd-trace/src/opentracing/span.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ class DatadogSpan {
constructor (tracer, processor, prioritySampler, fields, debug) {
const operationName = fields.operationName
const parent = fields.parent || null
const tags = Object.assign({}, fields.tags)
const hostname = fields.hostname

this._parentTracer = tracer
this._debug = debug
this._processor = processor
this._prioritySampler = prioritySampler
this._store = storage.getStore()
this._duration = undefined

// For internal use only. You probably want `context()._name`.
// This name property is not updated when the span name changes.
Expand All @@ -76,13 +72,8 @@ class DatadogSpan {
getIntegrationCounter('spans_created', this._integrationName).inc()

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

this._spanContext._trace.started.push(this)

this._startTime = fields.startTime || this._getTime()
this._start(tracer, processor, prioritySampler, fields, debug)

this._links = []
fields.links && fields.links.forEach(link => this.addLink(link.context, link.attributes))
Expand Down Expand Up @@ -198,6 +189,19 @@ class DatadogSpan {
this._processor.process(this)
}

_start (_tracer, processor, _prioritySampler, fields) {
const operationName = fields.operationName
const tags = Object.assign({}, fields.tags)
const hostname = fields.hostname

this._processor = processor
this._duration = undefined
this._spanContext._name = operationName
this._spanContext._tags = tags
this._spanContext._hostname = hostname
this._spanContext._trace.started.push(this)
}

_sanitizeAttributes (attributes = {}) {
const sanitizedAttributes = {}

Expand Down
10 changes: 9 additions & 1 deletion packages/dd-trace/src/opentracing/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DatadogTracer {
'service.name': this._service
}

const span = new Span(this, this._processor, this._prioritySampler, {
const span = this._initSpan(this, this._processor, this._prioritySampler, {
operationName: options.operationName || name,
parent,
tags,
Expand Down Expand Up @@ -94,6 +94,14 @@ class DatadogTracer {
return null
}
}

_initSpan (...args) {
return new Span(...args)
}

_setUrl (url) {
this._exporter.setUrl(url)
}
}

function getContext (spanContext) {
Expand Down
10 changes: 8 additions & 2 deletions packages/dd-trace/src/tracer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Tracer = require('./opentracing/tracer')
const Tracer = getTracerClass()
const tags = require('../../../ext/tags')
const Scope = require('./scope')
const { storage } = require('../../datadog-core')
Expand Down Expand Up @@ -134,7 +134,7 @@ class DatadogTracer extends Tracer {
}

setUrl (url) {
this._exporter.setUrl(url)
this._setUrl(url)
this._dataStreamsProcessor.setUrl(url)
}

Expand Down Expand Up @@ -177,4 +177,10 @@ function addTags (span, options) {
span.addTags(tags)
}

function getTracerClass () {
return globalThis.__dd_collector
? require('./collector/tracer')
: require('./opentracing/tracer')
}

module.exports = DatadogTracer

0 comments on commit 86cd058

Please sign in to comment.