Skip to content

Commit

Permalink
Hide non-config properties on config object
Browse files Browse the repository at this point in the history
Hide internal-only properties so they are not exposed to the rest of the
app. This makes reasoning about what's a config option and what's not
easier.
  • Loading branch information
watson committed Jul 18, 2024
1 parent ba21058 commit 35c4813
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions packages/dd-trace/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function propagationStyle (key, option, defaultValue) {

class Config {
constructor (options = {}) {
options = this.options = {
options = {
...options,
appsec: options.appsec != null ? options.appsec : options.experimental?.appsec,
iast: options.iast != null ? options.iast : options.experimental?.iast
Expand Down Expand Up @@ -476,7 +476,7 @@ class Config {
pkg.name ||
'node'

const defaults = this._defaults = {}
const defaults = setHiddenProperty(this, '_defaults', {})

this._setValue(defaults, 'appsec.blockedTemplateHtml', undefined)
this._setValue(defaults, 'appsec.blockedTemplateJson', undefined)
Expand Down Expand Up @@ -661,8 +661,8 @@ class Config {
} = process.env

const tags = {}
const env = this._env = {}
this._envUnprocessed = {}
const env = setHiddenProperty(this, '_env', {})
setHiddenProperty(this, '_envUnprocessed', {})

tagger.add(tags, OTEL_RESOURCE_ATTRIBUTES, true)
tagger.add(tags, DD_TAGS)
Expand Down Expand Up @@ -802,11 +802,11 @@ class Config {
}

_applyOptions (options) {
const opts = this._options = this._options || {}
const opts = setHiddenProperty(this, '_options', this._options || {})
const tags = {}
this._optsUnprocessed = {}
setHiddenProperty(this, '_optsUnprocessed', {})

options = this.options = Object.assign({ ingestion: {} }, options, opts)
options = setHiddenProperty(this, '_optionsArg', Object.assign({ ingestion: {} }, options, opts))

tagger.add(tags, options.tags)

Expand Down Expand Up @@ -904,7 +904,7 @@ class Config {

_isCiVisibility () {
return coalesce(
this.options.isCiVisibility,
this._optionsArg.isCiVisibility,
this._defaults.isCiVisibility
)
}
Expand All @@ -920,9 +920,9 @@ class Config {
const DD_CIVISIBILITY_AGENTLESS_URL = process.env.DD_CIVISIBILITY_AGENTLESS_URL
const url = DD_CIVISIBILITY_AGENTLESS_URL
? new URL(DD_CIVISIBILITY_AGENTLESS_URL)
: getAgentUrl(this._getTraceAgentUrl(), this.options)
: getAgentUrl(this._getTraceAgentUrl(), this._optionsArg)
const DD_AGENT_HOST = coalesce(
this.options.hostname,
this._optionsArg.hostname,
process.env.DD_AGENT_HOST,
process.env.DD_TRACE_AGENT_HOSTNAME,
'127.0.0.1'
Expand All @@ -933,17 +933,17 @@ class Config {
_getSpanComputePeerService () {
const DD_TRACE_SPAN_ATTRIBUTE_SCHEMA = validateNamingVersion(
coalesce(
this.options.spanAttributeSchema,
this._optionsArg.spanAttributeSchema,
process.env.DD_TRACE_SPAN_ATTRIBUTE_SCHEMA
)
)

const peerServiceSet = (
this.options.hasOwnProperty('spanComputePeerService') ||
this._optionsArg.hasOwnProperty('spanComputePeerService') ||
process.env.hasOwnProperty('DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED')
)
const peerServiceValue = coalesce(
this.options.spanComputePeerService,
this._optionsArg.spanComputePeerService,
process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED
)

Expand Down Expand Up @@ -974,15 +974,15 @@ class Config {

_isTraceStatsComputationEnabled () {
return coalesce(
this.options.stats,
this._optionsArg.stats,
process.env.DD_TRACE_STATS_COMPUTATION_ENABLED,
getIsGCPFunction() || getIsAzureFunction()
)
}

_getTraceAgentUrl () {
return coalesce(
this.options.url,
this._optionsArg.url,
process.env.DD_TRACE_AGENT_URL,
process.env.DD_TRACE_URL,
null
Expand All @@ -991,7 +991,7 @@ class Config {

// handles values calculated from a mixture of options and env vars
_applyCalculated () {
const calc = this._calculated = {}
const calc = setHiddenProperty(this, '_calculated', {})

const {
DD_CIVISIBILITY_AGENTLESS_URL
Expand All @@ -1000,7 +1000,7 @@ class Config {
if (DD_CIVISIBILITY_AGENTLESS_URL) {
this._setValue(calc, 'url', new URL(DD_CIVISIBILITY_AGENTLESS_URL))
} else {
this._setValue(calc, 'url', getAgentUrl(this._getTraceAgentUrl(), this.options))
this._setValue(calc, 'url', getAgentUrl(this._getTraceAgentUrl(), this._optionsArg))
}
if (this._isCiVisibility()) {
this._setBoolean(calc, 'isEarlyFlakeDetectionEnabled',
Expand All @@ -1016,8 +1016,8 @@ class Config {
}

_applyRemote (options) {
const opts = this._remote = this._remote || {}
this._remoteUnprocessed = {}
const opts = setHiddenProperty(this, '_remote', this._remote || {})
setHiddenProperty(this, '_remoteUnprocessed', {})
const tags = {}
const headerTags = options.tracing_header_tags
? options.tracing_header_tags.map(tag => {
Expand Down Expand Up @@ -1186,4 +1186,13 @@ function getAgentUrl (url, options) {
}
}

function setHiddenProperty (obj, name, value) {
Object.defineProperty(obj, name, {
value,
enumerable: false,
writable: true
})
return obj[name]
}

module.exports = Config

0 comments on commit 35c4813

Please sign in to comment.