dd-trace 2.10.0 → 2.12.1

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 (49) hide show
  1. package/LICENSE-3rdparty.csv +0 -1
  2. package/index.d.ts +3 -3
  3. package/package.json +5 -6
  4. package/packages/datadog-core/src/storage/async_hooks.js +4 -4
  5. package/packages/datadog-core/src/storage/async_resource.js +14 -4
  6. package/packages/datadog-instrumentations/index.js +4 -0
  7. package/packages/datadog-instrumentations/src/connect.js +7 -4
  8. package/packages/datadog-instrumentations/src/couchbase.js +166 -61
  9. package/packages/datadog-instrumentations/src/fastify.js +14 -25
  10. package/packages/datadog-instrumentations/src/graphql.js +23 -9
  11. package/packages/datadog-instrumentations/src/grpc/client.js +250 -0
  12. package/packages/datadog-instrumentations/src/grpc/server.js +144 -0
  13. package/packages/{datadog-plugin-grpc/src/kinds.js → datadog-instrumentations/src/grpc/types.js} +0 -0
  14. package/packages/datadog-instrumentations/src/grpc.js +4 -0
  15. package/packages/datadog-instrumentations/src/http2/client.js +67 -0
  16. package/packages/datadog-instrumentations/src/http2/server.js +3 -0
  17. package/packages/datadog-instrumentations/src/http2.js +4 -0
  18. package/packages/datadog-instrumentations/src/koa.js +15 -6
  19. package/packages/datadog-instrumentations/src/microgateway-core.js +66 -0
  20. package/packages/datadog-instrumentations/src/mocha.js +198 -62
  21. package/packages/datadog-instrumentations/src/next.js +140 -0
  22. package/packages/datadog-instrumentations/src/restify.js +4 -8
  23. package/packages/datadog-instrumentations/src/router.js +7 -4
  24. package/packages/datadog-plugin-couchbase/src/index.js +8 -10
  25. package/packages/datadog-plugin-graphql/src/resolve.js +14 -17
  26. package/packages/datadog-plugin-grpc/src/client.js +54 -283
  27. package/packages/datadog-plugin-grpc/src/index.js +31 -3
  28. package/packages/datadog-plugin-grpc/src/server.js +50 -145
  29. package/packages/datadog-plugin-http/src/server.js +3 -8
  30. package/packages/datadog-plugin-http2/src/client.js +72 -120
  31. package/packages/datadog-plugin-http2/src/index.js +32 -3
  32. package/packages/datadog-plugin-http2/src/server.js +6 -214
  33. package/packages/datadog-plugin-microgateway-core/src/index.js +14 -145
  34. package/packages/datadog-plugin-mocha/src/index.js +88 -39
  35. package/packages/datadog-plugin-next/src/index.js +56 -168
  36. package/packages/datadog-plugin-router/src/index.js +46 -13
  37. package/packages/dd-trace/src/config.js +2 -1
  38. package/packages/dd-trace/src/encode/agentless-ci-visibility.js +111 -15
  39. package/packages/dd-trace/src/opentracing/span.js +6 -3
  40. package/packages/dd-trace/src/plugin_manager.js +49 -33
  41. package/packages/dd-trace/src/plugins/plugin.js +3 -1
  42. package/packages/dd-trace/src/plugins/util/test.js +32 -1
  43. package/packages/dd-trace/src/plugins/util/web.js +25 -24
  44. package/packages/dd-trace/src/profiling/config.js +10 -2
  45. package/packages/dd-trace/src/profiling/exporters/agent.js +1 -2
  46. package/packages/dd-trace/src/profiling/exporters/form-data.js +53 -0
  47. package/packages/dd-trace/src/profiling/index.js +2 -0
  48. package/packages/dd-trace/src/profiling/profiler.js +6 -1
  49. package/packages/dd-trace/src/profiling/profilers/cpu.js +126 -0
@@ -0,0 +1,126 @@
1
+ 'use strict'
2
+
3
+ const { storage } = require('../../../../datadog-core')
4
+
5
+ const dc = require('diagnostics_channel')
6
+
7
+ const beforeCh = dc.channel('dd-trace:storage:before')
8
+ const afterCh = dc.channel('dd-trace:storage:after')
9
+
10
+ function getActiveSpan () {
11
+ const store = storage.getStore()
12
+ if (!store) return
13
+ return store.span
14
+ }
15
+
16
+ function getStartedSpans (activeSpan) {
17
+ const context = activeSpan.context()
18
+ if (!context) return
19
+ return context._trace.started
20
+ }
21
+
22
+ function getSpanContextTags (span) {
23
+ return span._context()._tags
24
+ }
25
+
26
+ function isWebServerSpan (tags) {
27
+ return tags['span.type'] === 'web'
28
+ }
29
+
30
+ function endpointNameFromTags (tags) {
31
+ return tags['resource.name'] || [
32
+ tags['http.method'],
33
+ tags['http.route']
34
+ ].filter(v => v).join(' ')
35
+ }
36
+
37
+ class NativeCpuProfiler {
38
+ constructor (options = {}) {
39
+ this.type = 'cpu'
40
+ this._frequency = options.frequency || 99
41
+ this._mapper = undefined
42
+ this._pprof = undefined
43
+ this._started = false
44
+ this._cpuProfiler = undefined
45
+ this._endpointCollection = options.endpointCollection
46
+
47
+ // Bind to this so the same value can be used to unsubscribe later
48
+ this._enter = this._enter.bind(this)
49
+ this._exit = this._exit.bind(this)
50
+ }
51
+
52
+ _enter () {
53
+ if (!this._cpuProfiler) return
54
+
55
+ const active = getActiveSpan()
56
+ if (!active) return
57
+
58
+ const activeCtx = active._context()
59
+ if (!activeCtx) return
60
+
61
+ const spans = getStartedSpans(active)
62
+ if (!spans || !spans.length) return
63
+
64
+ const firstCtx = spans[0]._context()
65
+ if (!firstCtx) return
66
+
67
+ const labels = {
68
+ 'local root span id': firstCtx.toSpanId(),
69
+ 'span id': activeCtx.toSpanId()
70
+ }
71
+
72
+ if (this._endpointCollection) {
73
+ const webServerTags = spans
74
+ .map(getSpanContextTags)
75
+ .filter(isWebServerSpan)[0]
76
+
77
+ if (webServerTags) {
78
+ labels['trace endpoint'] = endpointNameFromTags(webServerTags)
79
+ }
80
+ }
81
+
82
+ this._cpuProfiler.labels = labels
83
+ }
84
+
85
+ _exit () {
86
+ if (!this._cpuProfiler) return
87
+ this._cpuProfiler.labels = {}
88
+ }
89
+
90
+ start ({ mapper } = {}) {
91
+ if (this._started) return
92
+ this._started = true
93
+
94
+ this._mapper = mapper
95
+ if (!this._pprof) {
96
+ this._pprof = require('@datadog/pprof')
97
+ this._cpuProfiler = new this._pprof.CpuProfiler()
98
+ }
99
+
100
+ this._cpuProfiler.start(this._frequency)
101
+
102
+ this._enter()
103
+ beforeCh.subscribe(this._enter)
104
+ afterCh.subscribe(this._exit)
105
+ }
106
+
107
+ profile () {
108
+ if (!this._started) return
109
+ return this._cpuProfiler.profile()
110
+ }
111
+
112
+ encode (profile) {
113
+ return this._pprof.encode(profile)
114
+ }
115
+
116
+ stop () {
117
+ if (!this._started) return
118
+ this._started = false
119
+
120
+ this._cpuProfiler.stop()
121
+ beforeCh.unsubscribe(this._enter)
122
+ afterCh.unsubscribe(this._exit)
123
+ }
124
+ }
125
+
126
+ module.exports = NativeCpuProfiler