dd-trace 3.0.0-pre.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/LICENSE-3rdparty.csv +2 -2
  2. package/MIGRATING.md +119 -0
  3. package/ci/init.js +0 -1
  4. package/ext/formats.js +3 -5
  5. package/index.d.ts +1 -11
  6. package/package.json +7 -7
  7. package/packages/datadog-core/src/storage/async_resource.js +19 -1
  8. package/packages/datadog-core/src/storage/index.js +1 -1
  9. package/packages/datadog-instrumentations/index.js +1 -52
  10. package/packages/datadog-instrumentations/src/connect.js +1 -1
  11. package/packages/datadog-instrumentations/src/cucumber.js +15 -0
  12. package/packages/datadog-instrumentations/src/grpc/client.js +2 -2
  13. package/packages/datadog-instrumentations/src/grpc/server.js +1 -1
  14. package/packages/datadog-instrumentations/src/hapi.js +3 -31
  15. package/packages/datadog-instrumentations/src/helpers/hooks.js +68 -0
  16. package/packages/datadog-instrumentations/src/helpers/instrument.js +5 -34
  17. package/packages/datadog-instrumentations/src/helpers/instrumentations.js +7 -0
  18. package/packages/datadog-instrumentations/src/helpers/register.js +59 -0
  19. package/packages/datadog-instrumentations/src/jest.js +33 -11
  20. package/packages/datadog-instrumentations/src/koa.js +1 -1
  21. package/packages/datadog-instrumentations/src/mocha.js +4 -1
  22. package/packages/datadog-instrumentations/src/pg.js +2 -2
  23. package/packages/datadog-instrumentations/src/restify.js +27 -5
  24. package/packages/datadog-instrumentations/src/router.js +1 -1
  25. package/packages/datadog-plugin-aws-sdk/src/base.js +1 -2
  26. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +1 -2
  27. package/packages/datadog-plugin-cucumber/src/index.js +4 -0
  28. package/packages/datadog-plugin-jest/src/index.js +25 -4
  29. package/packages/datadog-plugin-mocha/src/index.js +2 -2
  30. package/packages/datadog-plugin-mongodb-core/src/index.js +21 -6
  31. package/packages/datadog-plugin-oracledb/src/index.js +12 -4
  32. package/packages/datadog-plugin-restify/src/index.js +7 -0
  33. package/packages/dd-trace/index.js +1 -1
  34. package/packages/dd-trace/src/ci-visibility/exporters/agentless/coverage-writer.js +50 -0
  35. package/packages/dd-trace/src/ci-visibility/exporters/agentless/index.js +53 -8
  36. package/packages/dd-trace/src/ci-visibility/exporters/agentless/writer.js +23 -24
  37. package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +220 -0
  38. package/packages/dd-trace/src/config.js +13 -0
  39. package/packages/dd-trace/src/encode/0.4.js +51 -58
  40. package/packages/dd-trace/src/encode/agentless-ci-visibility.js +13 -34
  41. package/packages/dd-trace/src/encode/coverage-ci-visibility.js +84 -0
  42. package/packages/dd-trace/src/exporters/agent/index.js +13 -7
  43. package/packages/dd-trace/src/exporters/agent/writer.js +1 -1
  44. package/packages/dd-trace/src/{profiling/exporters → exporters/common}/form-data.js +0 -0
  45. package/packages/dd-trace/src/exporters/common/request.js +23 -11
  46. package/packages/dd-trace/src/exporters/common/writer.js +9 -6
  47. package/packages/dd-trace/src/id.js +16 -13
  48. package/packages/dd-trace/src/iitm.js +1 -1
  49. package/packages/dd-trace/src/index.js +10 -0
  50. package/packages/dd-trace/src/noop/proxy.js +77 -0
  51. package/packages/dd-trace/src/noop/scope.js +2 -6
  52. package/packages/dd-trace/src/noop/span.js +12 -12
  53. package/packages/dd-trace/src/noop/tracer.js +8 -5
  54. package/packages/dd-trace/src/opentracing/propagation/text_map.js +6 -6
  55. package/packages/dd-trace/src/opentracing/span.js +63 -49
  56. package/packages/dd-trace/src/opentracing/span_context.js +1 -5
  57. package/packages/dd-trace/src/opentracing/tracer.js +31 -36
  58. package/packages/dd-trace/src/plugin_manager.js +101 -68
  59. package/packages/dd-trace/src/plugins/index.js +57 -44
  60. package/packages/dd-trace/src/plugins/util/ci.js +34 -9
  61. package/packages/dd-trace/src/plugins/util/git.js +52 -2
  62. package/packages/dd-trace/src/plugins/util/tags.js +4 -1
  63. package/packages/dd-trace/src/plugins/util/web.js +1 -1
  64. package/packages/dd-trace/src/profiling/exporters/agent.js +1 -1
  65. package/packages/dd-trace/src/profiling/profilers/cpu.js +3 -3
  66. package/packages/dd-trace/src/proxy.js +18 -71
  67. package/packages/dd-trace/src/scope.js +1 -58
  68. package/packages/dd-trace/src/startup-log.js +8 -19
  69. package/packages/dd-trace/src/telemetry.js +2 -15
  70. package/scripts/install_plugin_modules.js +17 -26
  71. package/ci/jest/env.js +0 -38
@@ -2,7 +2,7 @@
2
2
 
3
3
  const uniq = require('lodash.uniq')
4
4
  const analyticsSampler = require('../../analytics_sampler')
5
- const FORMAT_HTTP_HEADERS = require('opentracing').FORMAT_HTTP_HEADERS
5
+ const FORMAT_HTTP_HEADERS = 'http_headers'
6
6
  const log = require('../../log')
7
7
  const tags = require('../../../../../ext/tags')
8
8
  const types = require('../../../../../ext/types')
@@ -2,10 +2,10 @@
2
2
 
3
3
  const retry = require('retry')
4
4
  const { request } = require('http')
5
- const FormData = require('./form-data')
6
5
 
7
6
  // TODO: avoid using dd-trace internals. Make this a separate module?
8
7
  const docker = require('../../exporters/common/docker')
8
+ const FormData = require('../../exporters/common/form-data')
9
9
  const version = require('../../../../../package.json').version
10
10
 
11
11
  const containerId = docker.id()
@@ -20,7 +20,7 @@ function getStartedSpans (activeSpan) {
20
20
  }
21
21
 
22
22
  function getSpanContextTags (span) {
23
- return span._context()._tags
23
+ return span.context()._tags
24
24
  }
25
25
 
26
26
  function isWebServerSpan (tags) {
@@ -55,13 +55,13 @@ class NativeCpuProfiler {
55
55
  const active = getActiveSpan()
56
56
  if (!active) return
57
57
 
58
- const activeCtx = active._context()
58
+ const activeCtx = active.context()
59
59
  if (!activeCtx) return
60
60
 
61
61
  const spans = getStartedSpans(active)
62
62
  if (!spans || !spans.length) return
63
63
 
64
- const firstCtx = spans[0]._context()
64
+ const firstCtx = spans[0].context()
65
65
  if (!firstCtx) return
66
66
 
67
67
  const labels = {
@@ -1,31 +1,25 @@
1
1
  'use strict'
2
2
 
3
- const BaseTracer = require('opentracing').Tracer
4
- const NoopTracer = require('./noop/tracer')
3
+ const NoopProxy = require('./noop/proxy')
5
4
  const DatadogTracer = require('./tracer')
6
5
  const Config = require('./config')
7
- const Instrumenter = require('./instrumenter')
8
- const PluginManager = require('./plugin_manager')
9
6
  const metrics = require('./metrics')
10
7
  const log = require('./log')
11
- const { isFalse } = require('./util')
12
- const { setStartupLogInstrumenter } = require('./startup-log')
8
+ const { setStartupLogPluginManager } = require('./startup-log')
13
9
  const telemetry = require('./telemetry')
10
+ const PluginManager = require('./plugin_manager')
11
+ const { sendGitMetadata } = require('./ci-visibility/exporters/git/git_metadata')
14
12
 
15
- const noop = new NoopTracer()
16
-
17
- class Tracer extends BaseTracer {
13
+ class Tracer extends NoopProxy {
18
14
  constructor () {
19
15
  super()
20
16
 
21
17
  this._initialized = false
22
- this._tracer = noop
23
- this._instrumenter = new Instrumenter(this)
24
18
  this._pluginManager = new PluginManager(this)
25
19
  }
26
20
 
27
21
  init (options) {
28
- if (isFalse(process.env.DD_TRACE_ENABLED) || this._initialized) return this
22
+ if (this._initialized) return this
29
23
 
30
24
  this._initialized = true
31
25
 
@@ -56,10 +50,19 @@ class Tracer extends BaseTracer {
56
50
  }
57
51
 
58
52
  this._tracer = new DatadogTracer(config)
59
- this._instrumenter.enable(config)
60
53
  this._pluginManager.configure(config)
61
- setStartupLogInstrumenter(this._instrumenter)
62
- telemetry.start(config, this._instrumenter, this._pluginManager)
54
+ setStartupLogPluginManager(this._pluginManager)
55
+ telemetry.start(config, this._pluginManager)
56
+ }
57
+
58
+ if (config.isGitUploadEnabled) {
59
+ sendGitMetadata(config.site, (err) => {
60
+ if (err) {
61
+ log.error(`Error uploading git metadata: ${err}`)
62
+ } else {
63
+ log.debug('Successfully uploaded git metadata')
64
+ }
65
+ })
63
66
  }
64
67
  } catch (e) {
65
68
  log.error(e)
@@ -69,65 +72,9 @@ class Tracer extends BaseTracer {
69
72
  }
70
73
 
71
74
  use () {
72
- this._instrumenter.use(...arguments)
73
75
  this._pluginManager.configurePlugin(...arguments)
74
76
  return this
75
77
  }
76
-
77
- trace (name, options, fn) {
78
- if (!fn) {
79
- fn = options
80
- options = {}
81
- }
82
-
83
- if (typeof fn !== 'function') return
84
-
85
- options = options || {}
86
-
87
- return this._tracer.trace(name, options, fn)
88
- }
89
-
90
- wrap (name, options, fn) {
91
- if (!fn) {
92
- fn = options
93
- options = {}
94
- }
95
-
96
- if (typeof fn !== 'function') return fn
97
-
98
- options = options || {}
99
-
100
- return this._tracer.wrap(name, options, fn)
101
- }
102
-
103
- setUrl () {
104
- this._tracer.setUrl.apply(this._tracer, arguments)
105
- return this
106
- }
107
-
108
- startSpan () {
109
- return this._tracer.startSpan.apply(this._tracer, arguments)
110
- }
111
-
112
- inject () {
113
- return this._tracer.inject.apply(this._tracer, arguments)
114
- }
115
-
116
- extract () {
117
- return this._tracer.extract.apply(this._tracer, arguments)
118
- }
119
-
120
- scope () {
121
- return this._tracer.scope.apply(this._tracer, arguments)
122
- }
123
-
124
- getRumData () {
125
- return this._tracer.getRumData.apply(this._tracer, arguments)
126
- }
127
-
128
- setUser () {
129
- return this._tracer.setUser.apply(this.tracer, arguments)
130
- }
131
78
  }
132
79
 
133
80
  module.exports = Tracer
@@ -34,21 +34,7 @@ class Scope {
34
34
  }
35
35
  }
36
36
 
37
- bind (target, span) {
38
- target = this._bindPromise(target, span)
39
- target = this._bindFn(target, span)
40
-
41
- return target
42
- }
43
-
44
- unbind (target) {
45
- target = this._unbindFn(target)
46
- target = this._unbindPromise(target)
47
-
48
- return target
49
- }
50
-
51
- _bindFn (fn, span) {
37
+ bind (fn, span) {
52
38
  if (typeof fn !== 'function') return fn
53
39
 
54
40
  const scope = this
@@ -65,28 +51,6 @@ class Scope {
65
51
  return bound
66
52
  }
67
53
 
68
- _unbindFn (fn) {
69
- if (typeof fn !== 'function') return fn
70
-
71
- return originals.get(fn) || fn
72
- }
73
-
74
- _bindPromise (promise, span) {
75
- if (!this._isPromise(promise)) return promise
76
-
77
- wrapMethod(promise, 'then', wrapThen, this, span)
78
-
79
- return promise
80
- }
81
-
82
- _unbindPromise (promise) {
83
- if (!this._isPromise(promise)) return promise
84
-
85
- promise.then = originals.get(promise.then) || promise.then
86
-
87
- return promise
88
- }
89
-
90
54
  _spanOrActive (span) {
91
55
  return span !== undefined ? span : this.active()
92
56
  }
@@ -96,25 +60,4 @@ class Scope {
96
60
  }
97
61
  }
98
62
 
99
- function wrapThen (then, scope, span) {
100
- return function thenWithTrace (onFulfilled, onRejected) {
101
- const args = new Array(arguments.length)
102
-
103
- for (let i = 0, l = args.length; i < l; i++) {
104
- args[i] = scope.bind(arguments[i], span)
105
- }
106
-
107
- return then.apply(this, args)
108
- }
109
- }
110
-
111
- function wrapMethod (target, name, wrapper, ...args) {
112
- if (!target[name] || originals.has(target[name])) return
113
-
114
- const original = target[name]
115
-
116
- target[name] = wrapper(target[name], ...args)
117
- originals.set(target[name], original)
118
- }
119
-
120
63
  module.exports = Scope
@@ -5,38 +5,27 @@ const mainLogger = require('./log')
5
5
  const os = require('os')
6
6
  const { inspect } = require('util')
7
7
  const tracerVersion = require('../../../package.json').version
8
- const requirePackageJson = require('./require-package-json')
9
8
 
10
9
  const logger = Object.create(mainLogger)
11
10
  logger._enabled = true
12
11
 
13
12
  let config
14
- let instrumenter
13
+ let pluginManager
15
14
  let samplingRules = []
16
-
17
15
  let alreadyRan = false
18
16
 
19
17
  function getIntegrationsAndAnalytics () {
20
18
  const integrations = new Set()
21
19
  const extras = {}
22
- for (const plugin of instrumenter._instrumented.keys()) {
23
- if (plugin.versions) {
24
- try {
25
- const version = requirePackageJson(plugin.name, module).version
26
- integrations.add(`${plugin.name}@${version}`)
27
- } catch (e) {
28
- integrations.add(plugin.name)
29
- }
30
- } else {
31
- integrations.add(plugin.name)
32
- }
20
+ for (const pluginName in pluginManager._pluginsByName) {
21
+ integrations.add(pluginName)
33
22
  }
34
23
  extras.integrations_loaded = Array.from(integrations)
35
24
  return extras
36
25
  }
37
26
 
38
27
  function startupLog ({ agentError } = {}) {
39
- if (!config || !instrumenter) {
28
+ if (!config || !pluginManager) {
40
29
  return
41
30
  }
42
31
 
@@ -106,7 +95,7 @@ function startupLog ({ agentError } = {}) {
106
95
  }
107
96
 
108
97
  config = undefined
109
- instrumenter = undefined
98
+ pluginManager = undefined
110
99
  samplingRules = undefined
111
100
  }
112
101
 
@@ -114,8 +103,8 @@ function setStartupLogConfig (aConfig) {
114
103
  config = aConfig
115
104
  }
116
105
 
117
- function setStartupLogInstrumenter (theInstrumenter) {
118
- instrumenter = theInstrumenter
106
+ function setStartupLogPluginManager (thePluginManager) {
107
+ pluginManager = thePluginManager
119
108
  }
120
109
 
121
110
  function setSamplingRules (theRules) {
@@ -125,6 +114,6 @@ function setSamplingRules (theRules) {
125
114
  module.exports = {
126
115
  startupLog,
127
116
  setStartupLogConfig,
128
- setStartupLogInstrumenter,
117
+ setStartupLogPluginManager,
129
118
  setSamplingRules
130
119
  }
@@ -9,7 +9,6 @@ const os = require('os')
9
9
  const request = require('./exporters/common/request')
10
10
 
11
11
  let config
12
- let instrumenter
13
12
  let pluginManager
14
13
 
15
14
  let seqId = 0
@@ -20,17 +19,6 @@ const sentIntegrations = new Set()
20
19
 
21
20
  function getIntegrations () {
22
21
  const newIntegrations = []
23
- for (const plugin of instrumenter._instrumented.keys()) {
24
- if (sentIntegrations.has(plugin.name)) {
25
- continue
26
- }
27
- newIntegrations.push({
28
- name: plugin.name,
29
- enabled: true,
30
- auto_enabled: true
31
- })
32
- sentIntegrations.add(plugin.name)
33
- }
34
22
  for (const pluginName in pluginManager._pluginsByName) {
35
23
  if (sentIntegrations.has(pluginName)) {
36
24
  continue
@@ -141,17 +129,16 @@ function sendData (reqType, payload = {}) {
141
129
  host
142
130
  })
143
131
 
144
- request(data, options, true, () => {
132
+ request(data, options, () => {
145
133
  // ignore errors
146
134
  })
147
135
  }
148
136
 
149
- function start (aConfig, theInstrumenter, thePluginManager) {
137
+ function start (aConfig, thePluginManager) {
150
138
  if (!aConfig.telemetryEnabled) {
151
139
  return
152
140
  }
153
141
  config = aConfig
154
- instrumenter = theInstrumenter
155
142
  pluginManager = thePluginManager
156
143
  application = createAppObject()
157
144
  host = createHostObject()
@@ -7,8 +7,6 @@ const semver = require('semver')
7
7
  const proxyquire = require('proxyquire')
8
8
  const exec = require('./helpers/exec')
9
9
  const childProcess = require('child_process')
10
- const plugins = require('../packages/dd-trace/src/plugins')
11
- const Plugin = require('../packages/dd-trace/src/plugins/plugin')
12
10
  const externals = require('../packages/dd-trace/test/plugins/externals')
13
11
 
14
12
  const requirePackageJsonPath = require.resolve('../packages/dd-trace/src/require-package-json')
@@ -16,6 +14,9 @@ const requirePackageJsonPath = require.resolve('../packages/dd-trace/src/require
16
14
  const workspaces = new Set()
17
15
  const versionLists = {}
18
16
  const deps = {}
17
+ const names = []
18
+ const filter = process.env.hasOwnProperty('PLUGINS') && process.env.PLUGINS.split('|')
19
+
19
20
  Object.keys(externals).forEach(external => externals[external].forEach(thing => {
20
21
  if (thing.dep) {
21
22
  if (!deps[external]) {
@@ -29,7 +30,10 @@ fs.readdirSync(path.join(__dirname, '../packages/datadog-instrumentations/src'))
29
30
  .filter(file => file.endsWith('js'))
30
31
  .forEach(file => {
31
32
  file = file.replace('.js', '')
32
- plugins[file] = { name: file, prototype: Object.create(Plugin.prototype) }
33
+
34
+ if (!filter || filter.includes(file)) {
35
+ names.push(file)
36
+ }
33
37
  })
34
38
 
35
39
  run()
@@ -42,32 +46,19 @@ async function run () {
42
46
  }
43
47
 
44
48
  async function assertVersions () {
45
- let filter = []
46
- let names = Object.keys(plugins)
47
-
48
- if (process.env.hasOwnProperty('PLUGINS')) {
49
- filter = process.env.PLUGINS.split('|')
50
- names = names.filter(name => ~filter.indexOf(name))
51
- }
52
-
53
49
  const internals = names
54
50
  .map(key => {
55
- const plugin = plugins[key]
56
- if (plugin.prototype instanceof Plugin) {
57
- const instrumentations = []
58
- const name = plugin.name
59
-
60
- try {
61
- loadInstFile(`${name}/server.js`, instrumentations)
62
- loadInstFile(`${name}/client.js`, instrumentations)
63
- } catch (e) {
64
- loadInstFile(`${name}.js`, instrumentations)
65
- }
66
-
67
- return instrumentations
68
- } else {
69
- return plugin
51
+ const instrumentations = []
52
+ const name = key
53
+
54
+ try {
55
+ loadInstFile(`${name}/server.js`, instrumentations)
56
+ loadInstFile(`${name}/client.js`, instrumentations)
57
+ } catch (e) {
58
+ loadInstFile(`${name}.js`, instrumentations)
70
59
  }
60
+
61
+ return instrumentations
71
62
  })
72
63
  .reduce((prev, next) => prev.concat(next), [])
73
64
 
package/ci/jest/env.js DELETED
@@ -1,38 +0,0 @@
1
- /* eslint-disable no-console */
2
-
3
- const tracer = require('../../packages/dd-trace')
4
- const { ORIGIN_KEY } = require('../../packages/dd-trace/src/constants')
5
- const { isTrue } = require('../../packages/dd-trace/src/util')
6
-
7
- const options = {
8
- startupLogs: false,
9
- tags: {
10
- [ORIGIN_KEY]: 'ciapp-test'
11
- }
12
- }
13
-
14
- const isAgentlessEnabled = isTrue(process.env.DD_CIVISIBILITY_AGENTLESS_ENABLED)
15
-
16
- if (isAgentlessEnabled) {
17
- if (process.env.DATADOG_API_KEY || process.env.DD_API_KEY) {
18
- tracer.init({
19
- ...options,
20
- experimental: {
21
- exporter: 'datadog'
22
- }
23
- })
24
- } else {
25
- console.error(`DD_CIVISIBILITY_AGENTLESS_ENABLED is set, \
26
- but neither DD_API_KEY nor DATADOG_API_KEY are set in your environment, \
27
- so dd-trace will not be initialized.`)
28
- }
29
- } else {
30
- tracer.init({
31
- ...options,
32
- flushInterval: 400000
33
- })
34
- }
35
-
36
- tracer.use('fs', false)
37
-
38
- module.exports = tracer