dd-trace 4.9.0 → 4.11.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 (45) hide show
  1. package/CONTRIBUTING.md +66 -0
  2. package/package.json +1 -1
  3. package/packages/datadog-instrumentations/src/couchbase.js +41 -5
  4. package/packages/datadog-instrumentations/src/http2/client.js +2 -0
  5. package/packages/datadog-plugin-aws-sdk/src/base.js +22 -10
  6. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +16 -0
  7. package/packages/datadog-plugin-aws-sdk/src/services/sns.js +18 -0
  8. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +23 -0
  9. package/packages/datadog-plugin-cassandra-driver/src/index.js +1 -1
  10. package/packages/datadog-plugin-couchbase/src/index.js +32 -30
  11. package/packages/datadog-plugin-elasticsearch/src/index.js +1 -1
  12. package/packages/datadog-plugin-http/src/client.js +14 -23
  13. package/packages/datadog-plugin-http/src/server.js +10 -2
  14. package/packages/datadog-plugin-http2/src/client.js +2 -16
  15. package/packages/datadog-plugin-http2/src/server.js +10 -1
  16. package/packages/datadog-plugin-memcached/src/index.js +1 -1
  17. package/packages/datadog-plugin-mongodb-core/src/index.js +1 -1
  18. package/packages/datadog-plugin-mysql/src/index.js +1 -1
  19. package/packages/datadog-plugin-next/src/index.js +2 -2
  20. package/packages/datadog-plugin-oracledb/src/index.js +1 -1
  21. package/packages/datadog-plugin-pg/src/index.js +2 -2
  22. package/packages/datadog-plugin-redis/src/index.js +1 -1
  23. package/packages/datadog-plugin-tedious/src/index.js +1 -1
  24. package/packages/dd-trace/src/appsec/remote_config/index.js +2 -0
  25. package/packages/dd-trace/src/appsec/remote_config/manager.js +9 -4
  26. package/packages/dd-trace/src/config.js +138 -16
  27. package/packages/dd-trace/src/noop/tracer.js +2 -0
  28. package/packages/dd-trace/src/plugin_manager.js +2 -0
  29. package/packages/dd-trace/src/plugins/database.js +12 -0
  30. package/packages/dd-trace/src/plugins/tracing.js +16 -9
  31. package/packages/dd-trace/src/plugins/util/web.js +6 -4
  32. package/packages/dd-trace/src/priority_sampler.js +6 -3
  33. package/packages/dd-trace/src/proxy.js +18 -3
  34. package/packages/dd-trace/src/service-naming/index.js +7 -11
  35. package/packages/dd-trace/src/service-naming/schemas/definition.js +4 -4
  36. package/packages/dd-trace/src/service-naming/schemas/util.js +21 -3
  37. package/packages/dd-trace/src/service-naming/schemas/v0/messaging.js +20 -8
  38. package/packages/dd-trace/src/service-naming/schemas/v0/storage.js +33 -23
  39. package/packages/dd-trace/src/service-naming/schemas/v0/web.js +33 -1
  40. package/packages/dd-trace/src/service-naming/schemas/v1/messaging.js +14 -2
  41. package/packages/dd-trace/src/service-naming/schemas/v1/storage.js +12 -9
  42. package/packages/dd-trace/src/service-naming/schemas/v1/web.js +33 -1
  43. package/packages/dd-trace/src/startup-log.js +1 -1
  44. package/packages/dd-trace/src/telemetry/index.js +31 -3
  45. package/packages/dd-trace/src/tracer.js +4 -0
package/CONTRIBUTING.md CHANGED
@@ -4,4 +4,70 @@ Please reach out before starting work on any major code changes.
4
4
  This will ensure we avoid duplicating work, or that your code can't be merged due to a rapidly changing
5
5
  base. If you would like support for a module that is not listed, [contact support][1] to share a request.
6
6
 
7
+ ## Keep changes small and incremental
8
+
9
+ Changes should be incremental and understandable. As much as possible, large-scale efforts should be broken up into many PRs over time for better reviewability. If a feature would require more changes to be "complete" it's fine to land partial changes if they are not wired up to anything yet, so long as tests are included which at least prove those parts work in isolation.
10
+
11
+ There are great benefits to taking a measured and iterative approach to improvement. When working on code in fewer places there is far less risk of running into merge conflicts or incompatibilities with other systems. Keeping contributions small makes them easy to review which makes that much quicker to land. Additionally, keeping things small and iterative makes it easier for other teams to review and understand what the code does.
12
+
13
+ ## Be descriptive
14
+
15
+ Sometimes code can be self-documenting, but often it can't. That is especially true to someone reviewing code they haven't worked on. Be conscious of writing code in a self-describing way and leave comments anywhere that self-description fails. This goes a long way towards making even complex code coherent to one not already familiar with it.
16
+
17
+ Try to write code in a way the describes the intent when read. For example, verbs can be used for function and method names to communicate that they are used to do some specific action. In doing so it becomes clear when referenced by name elsewhere that it is a function and what the function is meant to do. If a function can not be described with a simple verb it's probably too complex or does too many things.
18
+
19
+ ## Give your code space
20
+
21
+ Very dense code is hard to read. It helps to make use of empty lines to separate logical groupings of statements. Long lines should be split up into multiple lines to make them more readable. Complex objects or arrays should generally be split over several lines. Sometimes it's a good idea to assign a variable only to immediately use it in a call as it can be more descriptive than just using the expression in place. It's not always clear what an argument is for if it doesn't visibly have a name somehow. Remember, lines are free, our time is not.
22
+
23
+ ## Avoid large refactors
24
+
25
+ Large refactors should generally be avoided in favour of iterative approaches. For example, rather than rewriting how every plugin works, one might make a special-case plugin that works a bit different for their particular use-case. If several dozen files need to change to add a feature we've probably done something wrong.
26
+
27
+ Sometimes new patterns or new ideas emerge which would be a substantial improvement over the existing state. It can be tempting to want to go all-in on a new way to do something, but the code churn can be hard to manage. It's best to introduce such new things incrementally and advocate for their adoption gradually through the rest of the codebase. As old systems are gradually phased out, the infrastructure which supports them can be deleted or relegated to lazy-loading only if and when that specific part of the system needs to be used.
28
+
29
+ ## Test everything
30
+
31
+ It's very difficult to know if a change is valid unless there are tests to prove it. As an extension of that, it's also difficult to know the _use_ of that code is valid if the way it is integrated is not propertly tested. For this reason we generally favour integration tests over unit tests. If an API is expected to be used in different places or in different ways then it should generally include unit tests too for each unique scenario, however great care should be taken to ensure unit tests are actually testing the _logic_ and not just testing the _mocks_. It's a very common mistake to write a unit test that abstracts away the actual use of the interface so much that it doesn't actually test how that interface works in real-world scenarios. Remember to test how it handles failures, how it operates under heavy load, and how it impacts usability of what its purpose is.
32
+
33
+ ## Don't forget benchmarks
34
+
35
+ Observability products tend to have quite a bit of their behaviour running in app code hot paths. It's important we extensively benchmark anything we expect to have heavy use to ensure it performs well and we don't cause any significant regressions through future changes. Measuring once at the time of writing is insufficient--a graph with just one data point is not going to tell you much of anything.
36
+
37
+ ## Always consider backportability
38
+
39
+ To reduce delta between release lines and make it easier for us to support older versions we try as much as possible to backport every change we can. We should be diligent about keeping breaking changes to a minimum and ensuring we don't use language or runtime features which are too new. This way we can generally be confident that a change can be backported.
40
+
41
+ To reduce the surface area of a breaking change, the breaking aspects could be placed behind a flag which is disabled by default or isolated to a function. In the next major the change would then be just to change the default of the flag or to start or stop calling the isolated function. By isolating the breaking logic it also becomes easier to delete later when it's no longer relevant on any release line.
42
+
43
+ Currently we do not have CI to test PRs for mergeability to past release lines, but we intend to expand our CI to include that in the future. For the time being, it's recommended when developing locally to try to cherry-pick your changes onto the previous vN.x branches to see if the tests pass there too.
44
+
45
+ ## Respect semantic versioning
46
+
47
+ This library follows the semantic versioning standard, but there are some subtleties left under-specified so this section is meant to clarify exactly how we interpret the meaning of semver. Additionally, it exists to communicate that we also use semver labels on all PRs to indicate which type of release the change should land in. Outside contributions should be evaluated and a semver label selected by the relevant team.
48
+
49
+ ### semver-patch
50
+
51
+ If the change is a bug or security fix, it should be labelled as semver-patch. These changes should generally not alter existing behaviour in any way other than to correct the specific issue.
52
+
53
+ ### semver-minor
54
+
55
+ Any addition of new functionality should be labelled as semver-minor and should not change any existing behaviour either in how any existing API works or in changing the contents or value of any existing data being reported except in purely additive cases where all existing data retains its prior state. Such changes may include new configuration options which when used will change behaviour, or may include the addition of new data being captured such as a new instrumentation, but should not impact the current operating design of any existing features.
56
+
57
+ ### semver-major
58
+
59
+ In the event that some existing functionality _does_ need to change, as much as possible the non-breaking aspects of that change should be made in a semver-minor PR and the actually breaking aspects should be done via a follow-up PR with only the specific aspects which are breaking. Remember to [always consider backportability](#always-consider-backportability).
60
+
61
+ ## Indicate intended release targets
62
+
63
+ When writing major changes we use a series of labels in the form of `dont-land-on-vN.x` where N is the major release line which a PR should not land in. Every PR marked as semver-major should include these tags. These tags allow our [branch-diff](https://github.com/bengl/branch-diff) tooling to work smoothly as we can exclude PRs not intended for the release line we're preparing a release proposal for. The `semver-major` labels on their own are not sufficient as they don't encode any indication of from _which_ releases they are a major change.
64
+
65
+ For outside contributions we will have the relevant team add these labels when they review and determine when they plan to release it.
66
+
67
+ ## Ensure all tests are green
68
+
69
+ We follow an all-green policy which means that for any PR to be merged _all_ tests must be passing. If a test is flaky or failing consistently the owner of that test should make it a priority to fix that test and unblock other teams from landing changes. For outside contributors there are currently several tests which will always fail as full CI permission is required. For these PRs our current process is for the relevant team to copy the PR and resubmit it to run tests as a user with full CI permission.
70
+
71
+ Eventually we plan to look into putting these permission-required tests behind a label which team members can add to their PRs at creation to run the full CI and can add to outside contributor PRs to trigger the CI from their own user credentials. If the label is not present there will be another action which checks the label is present. Rather than showing a bunch of confusing failures to new contributors it would just show a single job failure which indicates an additional label is required, and we can name it in a way that makes it clear that it's not the responsibility of the outside contributor to add it. Something like `approve-full-ci` is one possible choice there.
72
+
7
73
  [1]: https://docs.datadoghq.com/help
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dd-trace",
3
- "version": "4.9.0",
3
+ "version": "4.11.0",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -74,7 +74,7 @@ function wrap (prefix, fn) {
74
74
  return asyncResource.runInAsyncScope(() => {
75
75
  const cb = callbackResource.bind(arguments[callbackIndex])
76
76
 
77
- startCh.publish({ bucket: { name: this.name || this._name } })
77
+ startCh.publish({ bucket: { name: this.name || this._name }, seedNodes: this._dd_hosts })
78
78
 
79
79
  arguments[callbackIndex] = asyncResource.bind(function (error, result) {
80
80
  if (error) {
@@ -146,7 +146,8 @@ function wrapWithName (name) {
146
146
  return function () { // no arguments used by us
147
147
  return wrapCBandPromise(operation, name, {
148
148
  collection: { name: this._name || '_default' },
149
- bucket: { name: this._scope._bucket._name }
149
+ bucket: { name: this._scope._bucket._name },
150
+ seedNodes: this._dd_connStr
150
151
  }, this, arguments)
151
152
  }
152
153
  }
@@ -155,7 +156,7 @@ function wrapWithName (name) {
155
156
  function wrapV3Query (query) {
156
157
  return function (q) {
157
158
  const resource = getQueryResource(q)
158
- return wrapCBandPromise(query, 'query', { resource }, this, arguments)
159
+ return wrapCBandPromise(query, 'query', { resource, seedNodes: this._connStr }, this, arguments)
159
160
  }
160
161
  }
161
162
 
@@ -179,7 +180,7 @@ addHook({ name: 'couchbase', file: 'lib/bucket.js', versions: ['^2.6.12'] }, Buc
179
180
 
180
181
  const asyncResource = new AsyncResource('bound-anonymous-fn')
181
182
  return asyncResource.runInAsyncScope(() => {
182
- startCh.publish({ resource: n1qlQuery, bucket: { name: this.name || this._name } })
183
+ startCh.publish({ resource: n1qlQuery, bucket: { name: this.name || this._name }, seedNodes: this._dd_hosts })
183
184
 
184
185
  emitter.once('rows', asyncResource.bind(() => {
185
186
  finishCh.publish(undefined)
@@ -212,11 +213,32 @@ addHook({ name: 'couchbase', file: 'lib/cluster.js', versions: ['^2.6.12'] }, Cl
212
213
  Cluster.prototype._maybeInvoke = wrapMaybeInvoke(Cluster.prototype._maybeInvoke)
213
214
  Cluster.prototype.query = wrapQuery(Cluster.prototype.query)
214
215
 
216
+ shimmer.wrap(Cluster.prototype, 'openBucket', openBucket => {
217
+ return function () {
218
+ const bucket = openBucket.apply(this, arguments)
219
+ const hosts = this.dsnObj.hosts
220
+ bucket._dd_hosts = hosts.map(hostAndPort => hostAndPort.join(':')).join(',')
221
+ return bucket
222
+ }
223
+ })
215
224
  return Cluster
216
225
  })
217
226
 
218
227
  // semver >=3 <3.2.0
219
228
 
229
+ addHook({ name: 'couchbase', file: 'lib/bucket.js', versions: ['^3.0.7', '^3.1.3'] }, Bucket => {
230
+ shimmer.wrap(Bucket.prototype, 'collection', getCollection => {
231
+ return function () {
232
+ const collection = getCollection.apply(this, arguments)
233
+ const connStr = this._cluster._connStr
234
+ collection._dd_connStr = connStr
235
+ return collection
236
+ }
237
+ })
238
+
239
+ return Bucket
240
+ })
241
+
220
242
  addHook({ name: 'couchbase', file: 'lib/collection.js', versions: ['^3.0.7', '^3.1.3'] }, Collection => {
221
243
  wrapAllNames(['upsert', 'insert', 'replace'], name => {
222
244
  shimmer.wrap(Collection.prototype, name, wrapWithName(name))
@@ -242,7 +264,21 @@ addHook({ name: 'couchbase', file: 'dist/collection.js', versions: ['>=3.2.0'] }
242
264
  return collection
243
265
  })
244
266
 
245
- addHook({ name: 'couchbase', file: 'dist/cluster.js', versions: ['3.2.0 - 3.2.1', '>=3.2.2'] }, cluster => {
267
+ addHook({ name: 'couchbase', file: 'dist/bucket.js', versions: ['>=3.2.0'] }, bucket => {
268
+ const Bucket = bucket.Bucket
269
+ shimmer.wrap(Bucket.prototype, 'collection', getCollection => {
270
+ return function () {
271
+ const collection = getCollection.apply(this, arguments)
272
+ const connStr = this._cluster._connStr
273
+ collection._dd_connStr = connStr
274
+ return collection
275
+ }
276
+ })
277
+
278
+ return bucket
279
+ })
280
+
281
+ addHook({ name: 'couchbase', file: 'dist/cluster.js', versions: ['3.2.0 - 3.2.1', '>=3.2.2'] }, (cluster) => {
246
282
  const Cluster = cluster.Cluster
247
283
 
248
284
  shimmer.wrap(Cluster.prototype, 'query', wrapV3Query)
@@ -30,6 +30,8 @@ function createWrapEmit (ctx) {
30
30
  function createWrapRequest (authority, options) {
31
31
  return function wrapRequest (request) {
32
32
  return function (headers) {
33
+ if (!startChannel.hasSubscribers) return request.apply(this, arguments)
34
+
33
35
  const ctx = { headers, authority, options }
34
36
 
35
37
  return startChannel.runStores(ctx, () => {
@@ -31,11 +31,10 @@ class BaseAwsSdkPlugin extends ClientPlugin {
31
31
  if (!this.isEnabled(request)) {
32
32
  return
33
33
  }
34
- const serviceName = this.getServiceName()
35
34
  const childOf = this.tracer.scope().active()
36
35
  const tags = {
37
36
  'span.kind': 'client',
38
- 'service.name': serviceName,
37
+ 'service.name': this.serviceName(),
39
38
  'aws.operation': operation,
40
39
  'aws.region': awsRegion,
41
40
  'region': awsRegion,
@@ -45,7 +44,7 @@ class BaseAwsSdkPlugin extends ClientPlugin {
45
44
  }
46
45
  if (this.requestTags) this.requestTags.set(request, tags)
47
46
 
48
- const span = this.tracer.startSpan('aws.request', { childOf, tags })
47
+ const span = this.tracer.startSpan(this.operationFromRequest(request), { childOf, tags })
49
48
 
50
49
  analyticsSampler.sample(span, this.config.measured)
51
50
 
@@ -79,6 +78,26 @@ class BaseAwsSdkPlugin extends ClientPlugin {
79
78
  // implemented by subclasses, or not
80
79
  }
81
80
 
81
+ operationFromRequest (request) {
82
+ // can be overriden by subclasses
83
+ return this.operationName({
84
+ id: 'aws',
85
+ type: 'web',
86
+ kind: 'client',
87
+ awsService: this.serviceIdentifier
88
+ })
89
+ }
90
+
91
+ serviceName () {
92
+ return this.config.service ||
93
+ super.serviceName({
94
+ id: 'aws',
95
+ type: 'web',
96
+ kind: 'client',
97
+ awsService: this.serviceIdentifier
98
+ })
99
+ }
100
+
82
101
  isEnabled (request) {
83
102
  const serviceId = this.serviceIdentifier.toUpperCase()
84
103
  const envVarValue = process.env[`DD_TRACE_AWS_SDK_${serviceId}_ENABLED`]
@@ -122,13 +141,6 @@ class BaseAwsSdkPlugin extends ClientPlugin {
122
141
  configure (config) {
123
142
  super.configure(normalizeConfig(config, this.serviceIdentifier))
124
143
  }
125
-
126
- // TODO: test splitByAwsService when the test suite is fixed
127
- getServiceName () {
128
- return this.config.service
129
- ? this.config.service
130
- : `${this.tracer._service}-aws-${this.serviceIdentifier}`
131
- }
132
144
  }
133
145
 
134
146
  function normalizeConfig (config, serviceIdentifier) {
@@ -48,6 +48,22 @@ class Lambda extends BaseAwsSdkPlugin {
48
48
  }
49
49
  }
50
50
  }
51
+
52
+ operationFromRequest (request) {
53
+ if (request.operation === 'invoke') {
54
+ return this.operationName({
55
+ type: 'web',
56
+ kind: 'client'
57
+ })
58
+ }
59
+
60
+ return this.operationName({
61
+ id: 'aws',
62
+ type: 'web',
63
+ kind: 'client',
64
+ awsService: 'lambda'
65
+ })
66
+ }
51
67
  }
52
68
 
53
69
  module.exports = Lambda
@@ -27,6 +27,24 @@ class Sns extends BaseAwsSdkPlugin {
27
27
  // for example if it contains a phone number?
28
28
  }
29
29
 
30
+ operationFromRequest (request) {
31
+ switch (request.operation) {
32
+ case 'publish':
33
+ case 'publishBatch':
34
+ return this.operationName({
35
+ type: 'messaging',
36
+ kind: 'producer'
37
+ })
38
+ }
39
+
40
+ return this.operationName({
41
+ id: 'aws',
42
+ type: 'web',
43
+ kind: 'client',
44
+ awsService: 'sns'
45
+ })
46
+ }
47
+
30
48
  requestInject (span, request) {
31
49
  const { operation, params } = request
32
50
 
@@ -41,6 +41,29 @@ class Sqs extends BaseAwsSdkPlugin {
41
41
  })
42
42
  }
43
43
 
44
+ operationFromRequest (request) {
45
+ switch (request.operation) {
46
+ case 'receiveMessage':
47
+ return this.operationName({
48
+ type: 'messaging',
49
+ kind: 'consumer'
50
+ })
51
+ case 'sendMessage':
52
+ case 'sendMessageBatch':
53
+ return this.operationName({
54
+ type: 'messaging',
55
+ kind: 'producer'
56
+ })
57
+ }
58
+
59
+ return this.operationName({
60
+ id: 'aws',
61
+ type: 'web',
62
+ kind: 'client',
63
+ awsService: 'sqs'
64
+ })
65
+ }
66
+
44
67
  isEnabled (request) {
45
68
  // TODO(bengl) Figure out a way to make separate plugins for consumer and producer so that
46
69
  // config can be isolated to `.configure()` instead of this whole isEnabled() thing.
@@ -14,7 +14,7 @@ class CassandraDriverPlugin extends DatabasePlugin {
14
14
  }
15
15
 
16
16
  this.startSpan(this.operationName(), {
17
- service: this.serviceName(this.config, this.system),
17
+ service: this.serviceName({ pluginConfig: this.config, system: this.system }),
18
18
  resource: trim(query, 5000),
19
19
  type: 'cassandra',
20
20
  kind: 'client',
@@ -1,51 +1,57 @@
1
1
  'use strict'
2
2
 
3
- const Plugin = require('../../dd-trace/src/plugins/plugin')
3
+ const StoragePlugin = require('../../dd-trace/src/plugins/storage')
4
4
  const { storage } = require('../../datadog-core')
5
- const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
6
5
 
7
- class CouchBasePlugin extends Plugin {
8
- static get id () {
9
- return 'couchbase'
10
- }
6
+ class CouchBasePlugin extends StoragePlugin {
7
+ static get id () { return 'couchbase' }
8
+ static get peerServicePrecursors () { return ['db.couchbase.seed.nodes'] }
11
9
 
12
- addSubs (func, start, finish = defaultFinish) {
10
+ addSubs (func, start) {
13
11
  this.addSub(`apm:couchbase:${func}:start`, start)
14
- this.addSub(`apm:couchbase:${func}:error`, this.addError)
15
- this.addSub(`apm:couchbase:${func}:finish`, finish)
12
+ this.addSub(`apm:couchbase:${func}:error`, error => this.addError(error))
13
+ this.addSub(`apm:couchbase:${func}:finish`, message => this.finish(message))
16
14
  }
17
15
 
18
- startSpan (operation, customTags, store, { bucket, collection }) {
16
+ startSpan (operation, customTags, store, { bucket, collection, seedNodes }) {
19
17
  const tags = {
20
18
  'db.type': 'couchbase',
21
19
  'component': 'couchbase',
22
- 'service.name': this.config.service || `${this.tracer._service}-couchbase`,
23
20
  'resource.name': `couchbase.${operation}`,
24
- 'span.kind': 'client'
21
+ 'span.kind': this.constructor.kind,
22
+ 'db.couchbase.seed.nodes': seedNodes
25
23
  }
26
24
 
25
+ if (bucket) tags['couchbase.bucket.name'] = bucket.name
26
+ if (collection) tags['couchbase.collection.name'] = collection.name
27
+
27
28
  for (const tag in customTags) {
28
29
  tags[tag] = customTags[tag]
29
30
  }
30
- const span = this.tracer.startSpan(`couchbase.${operation}`, {
31
- childOf: store ? store.span : null,
32
- tags
33
- })
34
31
 
35
- if (bucket) span.setTag(`couchbase.bucket.name`, bucket.name)
36
- if (collection) span.setTag(`couchbase.collection.name`, collection.name)
37
-
38
- analyticsSampler.sample(span, this.config.measured)
39
- return span
32
+ return super.startSpan(
33
+ this.operationName({ operation }),
34
+ {
35
+ service: this.serviceName({ pluginConfig: this.config }),
36
+ meta: tags
37
+ }
38
+ )
40
39
  }
41
40
 
42
41
  constructor (...args) {
43
42
  super(...args)
44
43
 
45
- this.addSubs('query', ({ resource, bucket }) => {
44
+ this.addSubs('query', ({ resource, bucket, seedNodes }) => {
46
45
  const store = storage.getStore()
47
- const span = this.startSpan('query', { 'span.type': 'sql', 'resource.name': resource },
48
- store, { bucket })
46
+ const span = this.startSpan(
47
+ 'query', {
48
+ 'span.type': 'sql',
49
+ 'resource.name': resource,
50
+ 'span.kind': this.constructor.kind
51
+ },
52
+ store,
53
+ { bucket, seedNodes }
54
+ )
49
55
  this.enter(span, store)
50
56
  })
51
57
 
@@ -56,16 +62,12 @@ class CouchBasePlugin extends Plugin {
56
62
  this._addCommandSubs('prepend')
57
63
  }
58
64
  _addCommandSubs (name) {
59
- this.addSubs(name, ({ bucket, collection }) => {
65
+ this.addSubs(name, ({ bucket, collection, seedNodes }) => {
60
66
  const store = storage.getStore()
61
- const span = this.startSpan(name, {}, store, { bucket, collection })
67
+ const span = this.startSpan(name, {}, store, { bucket, collection, seedNodes })
62
68
  this.enter(span, store)
63
69
  })
64
70
  }
65
71
  }
66
72
 
67
- function defaultFinish () {
68
- storage.getStore().span.finish()
69
- }
70
-
71
73
  module.exports = CouchBasePlugin
@@ -9,7 +9,7 @@ class ElasticsearchPlugin extends DatabasePlugin {
9
9
  const body = getBody(params.body || params.bulkBody)
10
10
 
11
11
  this.startSpan(this.operationName(), {
12
- service: this.serviceName(this.config),
12
+ service: this.serviceName({ pluginConfig: this.config }),
13
13
  resource: `${params.method} ${quantizePath(params.path)}`,
14
14
  type: 'elasticsearch',
15
15
  kind: 'client',
@@ -8,8 +8,8 @@ const formats = require('../../../ext/formats')
8
8
  const HTTP_HEADERS = formats.HTTP_HEADERS
9
9
  const urlFilter = require('../../dd-trace/src/plugins/util/urlfilter')
10
10
  const log = require('../../dd-trace/src/log')
11
- const url = require('url')
12
11
  const { CLIENT_PORT_KEY, COMPONENT, ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
12
+ const { URL } = require('url')
13
13
 
14
14
  const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
15
15
  const HTTP_REQUEST_HEADERS = tags.HTTP_REQUEST_HEADERS
@@ -36,12 +36,12 @@ class HttpClientPlugin extends ClientPlugin {
36
36
  const method = (options.method || 'GET').toUpperCase()
37
37
  const childOf = store && allowed ? store.span : null
38
38
  // TODO delegate to super.startspan
39
- const span = this.startSpan('http.request', {
39
+ const span = this.startSpan(this.operationName(), {
40
40
  childOf,
41
41
  meta: {
42
42
  [COMPONENT]: this.constructor.id,
43
43
  'span.kind': 'client',
44
- 'service.name': getServiceName(this.tracer, this.config, options),
44
+ 'service.name': this.serviceName({ pluginConfig: this.config, sessionDetails: extractSessionDetails(options) }),
45
45
  'resource.name': method,
46
46
  'span.type': 'http',
47
47
  'http.method': method,
@@ -121,11 +121,11 @@ function addResponseHeaders (res, span, config) {
121
121
  ? Object.fromEntries(res.headers.entries())
122
122
  : res.headers
123
123
 
124
- config.headers.forEach(key => {
124
+ config.headers.forEach(([key, tag]) => {
125
125
  const value = headers[key]
126
126
 
127
127
  if (value) {
128
- span.setTag(`${HTTP_RESPONSE_HEADERS}.${key}`, value)
128
+ span.setTag(tag || `${HTTP_RESPONSE_HEADERS}.${key}`, value)
129
129
  }
130
130
  })
131
131
  }
@@ -135,11 +135,11 @@ function addRequestHeaders (req, span, config) {
135
135
  ? Object.fromEntries(req.headers.entries())
136
136
  : req.headers || req.getHeaders()
137
137
 
138
- config.headers.forEach(key => {
139
- const value = headers[key]
138
+ config.headers.forEach(([key, tag]) => {
139
+ const value = Array.isArray(headers[key]) ? headers[key].toString() : headers[key]
140
140
 
141
141
  if (value) {
142
- span.setTag(`${HTTP_REQUEST_HEADERS}.${key}`, Array.isArray(value) ? value.toString() : value)
142
+ span.setTag(tag || `${HTTP_REQUEST_HEADERS}.${key}`, value)
143
143
  }
144
144
  })
145
145
  }
@@ -182,7 +182,8 @@ function getHeaders (config) {
182
182
 
183
183
  return config.headers
184
184
  .filter(key => typeof key === 'string')
185
- .map(key => key.toLowerCase())
185
+ .map(h => h.split(':'))
186
+ .map(([key, tag]) => [key.toLowerCase(), tag])
186
187
  }
187
188
 
188
189
  function getHooks (config) {
@@ -217,25 +218,15 @@ function hasAmazonSignature (options) {
217
218
  return search && search.toLowerCase().indexOf('x-amz-signature=') !== -1
218
219
  }
219
220
 
220
- function getServiceName (tracer, config, options) {
221
- if (config.splitByDomain) {
222
- return getHost(options)
223
- } else if (config.service) {
224
- return config.service
225
- }
226
-
227
- return tracer._service
228
- }
229
-
230
- function getHost (options) {
221
+ function extractSessionDetails (options) {
231
222
  if (typeof options === 'string') {
232
- return url.parse(options).host
223
+ return new URL(options).host
233
224
  }
234
225
 
235
- const hostname = options.hostname || options.host || 'localhost'
226
+ const host = options.hostname || options.host || 'localhost'
236
227
  const port = options.port
237
228
 
238
- return [hostname, port].filter(val => val).join(':')
229
+ return { host, port }
239
230
  }
240
231
 
241
232
  function startsWith (searchString) {
@@ -23,8 +23,16 @@ class HttpServerPlugin extends ServerPlugin {
23
23
 
24
24
  start ({ req, res, abortController }) {
25
25
  const store = storage.getStore()
26
- const span = web.startSpan(this.tracer, this.config, req, res, 'web.request')
27
-
26
+ const span = web.startSpan(
27
+ this.tracer,
28
+ {
29
+ ...this.config,
30
+ service: this.config.service || this.serviceName()
31
+ },
32
+ req,
33
+ res,
34
+ this.operationName()
35
+ )
28
36
  span.setTag(COMPONENT, this.constructor.id)
29
37
 
30
38
  this._parentStore = store
@@ -38,12 +38,12 @@ class Http2ClientPlugin extends ClientPlugin {
38
38
 
39
39
  const store = storage.getStore()
40
40
  const childOf = store && allowed ? store.span : null
41
- const span = this.startSpan('http.request', {
41
+ const span = this.startSpan(this.operationName(), {
42
42
  childOf,
43
43
  meta: {
44
44
  [COMPONENT]: this.constructor.id,
45
45
  [SPAN_KIND]: CLIENT,
46
- 'service.name': getServiceName(this.tracer, this.config, sessionDetails),
46
+ 'service.name': this.serviceName({ pluginConfig: this.config, sessionDetails }),
47
47
  'resource.name': method,
48
48
  'span.type': 'http',
49
49
  'http.method': method,
@@ -133,20 +133,6 @@ function extractSessionDetails (authority, options) {
133
133
  return { protocol, port, host }
134
134
  }
135
135
 
136
- function getFormattedHostString (host, port) {
137
- return [host, port].filter(val => val).join(':')
138
- }
139
-
140
- function getServiceName (tracer, config, sessionDetails) {
141
- if (config.splitByDomain) {
142
- return getFormattedHostString(sessionDetails.host, sessionDetails.port)
143
- } else if (config.service) {
144
- return config.service
145
- }
146
-
147
- return tracer._service
148
- }
149
-
150
136
  function hasAmazonSignature (headers, path) {
151
137
  if (headers) {
152
138
  headers = Object.keys(headers)
@@ -18,7 +18,16 @@ class Http2ServerPlugin extends ServerPlugin {
18
18
 
19
19
  start ({ req, res }) {
20
20
  const store = storage.getStore()
21
- const span = web.startSpan(this.tracer, this.config, req, res, 'web.request')
21
+ const span = web.startSpan(
22
+ this.tracer,
23
+ {
24
+ ...this.config,
25
+ service: this.config.service || this.serviceName()
26
+ },
27
+ req,
28
+ res,
29
+ this.operationName()
30
+ )
22
31
 
23
32
  span.setTag(COMPONENT, this.constructor.id)
24
33
 
@@ -10,7 +10,7 @@ class MemcachedPlugin extends CachePlugin {
10
10
  const address = getAddress(client, server, query)
11
11
 
12
12
  this.startSpan({
13
- service: this.serviceName(this.config, this.system),
13
+ service: this.serviceName({ pluginConfig: this.config, system: this.system }),
14
14
  resource: query.type,
15
15
  type: 'memcached',
16
16
  meta: {
@@ -12,7 +12,7 @@ class MongodbCorePlugin extends DatabasePlugin {
12
12
  const query = getQuery(ops)
13
13
  const resource = truncate(getResource(this, ns, query, name))
14
14
  this.startSpan(this.operationName(), {
15
- service: this.serviceName(this.config),
15
+ service: this.serviceName({ pluginConfig: this.config }),
16
16
  resource,
17
17
  type: 'mongodb',
18
18
  kind: 'client',
@@ -8,7 +8,7 @@ class MySQLPlugin extends DatabasePlugin {
8
8
  static get system () { return 'mysql' }
9
9
 
10
10
  start (payload) {
11
- const service = this.serviceName(this.config, payload.conf, this.system)
11
+ const service = this.serviceName({ pluginConfig: this.config, dbConfig: payload.conf, system: this.system })
12
12
  this.startSpan(this.operationName(), {
13
13
  service,
14
14
  resource: payload.sql,