dd-trace 5.102.0 → 5.104.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 (201) hide show
  1. package/ext/exporters.js +1 -0
  2. package/index.d.ts +25 -3
  3. package/package.json +15 -13
  4. package/packages/datadog-esbuild/src/utils.js +2 -2
  5. package/packages/datadog-instrumentations/src/ai.js +1 -1
  6. package/packages/datadog-instrumentations/src/aws-sdk.js +2 -2
  7. package/packages/datadog-instrumentations/src/cassandra-driver.js +5 -2
  8. package/packages/datadog-instrumentations/src/confluentinc-kafka-javascript.js +32 -15
  9. package/packages/datadog-instrumentations/src/couchbase.js +69 -220
  10. package/packages/datadog-instrumentations/src/cucumber.js +104 -31
  11. package/packages/datadog-instrumentations/src/elasticsearch.js +4 -4
  12. package/packages/datadog-instrumentations/src/electron/preload.js +42 -0
  13. package/packages/datadog-instrumentations/src/electron.js +240 -0
  14. package/packages/datadog-instrumentations/src/fetch.js +5 -5
  15. package/packages/datadog-instrumentations/src/graphql.js +13 -17
  16. package/packages/datadog-instrumentations/src/grpc/client.js +48 -32
  17. package/packages/datadog-instrumentations/src/helpers/callback-instrumentor.js +2 -2
  18. package/packages/datadog-instrumentations/src/helpers/hook.js +4 -1
  19. package/packages/datadog-instrumentations/src/helpers/hooks.js +1 -0
  20. package/packages/datadog-instrumentations/src/helpers/instrument.js +2 -2
  21. package/packages/datadog-instrumentations/src/helpers/kafka.js +58 -0
  22. package/packages/datadog-instrumentations/src/helpers/rewriter/compiler.js +3 -2
  23. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +19 -5
  24. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +14 -13
  25. package/packages/datadog-instrumentations/src/http/client.js +2 -2
  26. package/packages/datadog-instrumentations/src/ioredis.js +18 -14
  27. package/packages/datadog-instrumentations/src/jest.js +382 -84
  28. package/packages/datadog-instrumentations/src/kafkajs.js +184 -174
  29. package/packages/datadog-instrumentations/src/mariadb.js +1 -1
  30. package/packages/datadog-instrumentations/src/memcached.js +2 -1
  31. package/packages/datadog-instrumentations/src/mocha/main.js +309 -56
  32. package/packages/datadog-instrumentations/src/mocha/utils.js +48 -8
  33. package/packages/datadog-instrumentations/src/mongodb-core.js +34 -9
  34. package/packages/datadog-instrumentations/src/mongoose.js +10 -12
  35. package/packages/datadog-instrumentations/src/mysql.js +2 -2
  36. package/packages/datadog-instrumentations/src/mysql2.js +1 -1
  37. package/packages/datadog-instrumentations/src/pg.js +25 -11
  38. package/packages/datadog-instrumentations/src/playwright.js +449 -60
  39. package/packages/datadog-instrumentations/src/redis.js +19 -10
  40. package/packages/datadog-instrumentations/src/router.js +4 -2
  41. package/packages/datadog-instrumentations/src/vitest.js +246 -149
  42. package/packages/datadog-plugin-apollo/src/gateway/request.js +1 -21
  43. package/packages/datadog-plugin-aws-sdk/src/base.js +18 -24
  44. package/packages/datadog-plugin-aws-sdk/src/services/cloudwatchlogs.js +1 -1
  45. package/packages/datadog-plugin-aws-sdk/src/services/eventbridge.js +1 -1
  46. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +1 -1
  47. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +1 -1
  48. package/packages/datadog-plugin-aws-sdk/src/services/redshift.js +1 -1
  49. package/packages/datadog-plugin-aws-sdk/src/services/s3.js +1 -1
  50. package/packages/datadog-plugin-aws-sdk/src/services/sns.js +2 -2
  51. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +1 -1
  52. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +1 -1
  53. package/packages/datadog-plugin-couchbase/src/index.js +58 -52
  54. package/packages/datadog-plugin-cucumber/src/index.js +1 -0
  55. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +239 -40
  56. package/packages/datadog-plugin-cypress/src/support.js +13 -1
  57. package/packages/datadog-plugin-elasticsearch/src/index.js +28 -8
  58. package/packages/datadog-plugin-electron/src/index.js +17 -0
  59. package/packages/datadog-plugin-electron/src/ipc.js +143 -0
  60. package/packages/datadog-plugin-electron/src/net.js +82 -0
  61. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +27 -18
  62. package/packages/datadog-plugin-graphql/src/execute.js +6 -28
  63. package/packages/datadog-plugin-graphql/src/resolve.js +30 -35
  64. package/packages/datadog-plugin-graphql/src/tools/signature.js +32 -7
  65. package/packages/datadog-plugin-graphql/src/tools/transforms.js +118 -100
  66. package/packages/datadog-plugin-graphql/src/utils.js +33 -1
  67. package/packages/datadog-plugin-grpc/src/client.js +6 -7
  68. package/packages/datadog-plugin-grpc/src/util.js +57 -22
  69. package/packages/datadog-plugin-http/src/client.js +2 -2
  70. package/packages/datadog-plugin-jest/src/index.js +92 -50
  71. package/packages/datadog-plugin-kafkajs/src/producer.js +32 -0
  72. package/packages/datadog-plugin-mocha/src/index.js +1 -0
  73. package/packages/datadog-plugin-mongodb-core/src/index.js +70 -69
  74. package/packages/datadog-plugin-mysql/src/index.js +1 -1
  75. package/packages/datadog-plugin-openai/src/services.js +2 -1
  76. package/packages/datadog-plugin-pg/src/index.js +3 -3
  77. package/packages/datadog-plugin-playwright/src/index.js +4 -0
  78. package/packages/datadog-plugin-redis/src/index.js +54 -24
  79. package/packages/datadog-plugin-undici/src/index.js +19 -0
  80. package/packages/datadog-plugin-vitest/src/index.js +19 -7
  81. package/packages/datadog-shimmer/src/shimmer.js +35 -0
  82. package/packages/dd-trace/src/aiguard/index.js +3 -1
  83. package/packages/dd-trace/src/aiguard/sdk.js +36 -30
  84. package/packages/dd-trace/src/aiguard/tags.js +20 -11
  85. package/packages/dd-trace/src/appsec/blocking.js +2 -2
  86. package/packages/dd-trace/src/appsec/iast/taint-tracking/rewriter.js +2 -2
  87. package/packages/dd-trace/src/appsec/iast/vulnerability-reporter.js +1 -1
  88. package/packages/dd-trace/src/appsec/index.js +10 -3
  89. package/packages/dd-trace/src/appsec/reporter.js +19 -5
  90. package/packages/dd-trace/src/azure_metadata.js +17 -6
  91. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/index.js +4 -4
  92. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +4 -2
  93. package/packages/dd-trace/src/ci-visibility/exporters/test-worker/index.js +6 -4
  94. package/packages/dd-trace/src/ci-visibility/requests/fs-cache.js +1 -1
  95. package/packages/dd-trace/src/ci-visibility/requests/request.js +3 -1
  96. package/packages/dd-trace/src/ci-visibility/test-api-manual/test-api-manual-plugin.js +5 -3
  97. package/packages/dd-trace/src/config/defaults.js +3 -14
  98. package/packages/dd-trace/src/config/generated-config-types.d.ts +4 -1
  99. package/packages/dd-trace/src/config/helper.js +4 -0
  100. package/packages/dd-trace/src/config/index.js +2 -2
  101. package/packages/dd-trace/src/config/major-overrides.js +98 -0
  102. package/packages/dd-trace/src/config/parsers.js +7 -1
  103. package/packages/dd-trace/src/config/supported-configurations.json +60 -38
  104. package/packages/dd-trace/src/crashtracking/crashtracker.js +15 -3
  105. package/packages/dd-trace/src/datastreams/checkpointer.js +2 -2
  106. package/packages/dd-trace/src/datastreams/context.js +4 -2
  107. package/packages/dd-trace/src/datastreams/manager.js +1 -1
  108. package/packages/dd-trace/src/datastreams/processor.js +2 -2
  109. package/packages/dd-trace/src/debugger/devtools_client/snapshot/collector.js +2 -2
  110. package/packages/dd-trace/src/debugger/devtools_client/source-maps.js +1 -1
  111. package/packages/dd-trace/src/debugger/devtools_client/state.js +2 -1
  112. package/packages/dd-trace/src/debugger/index.js +7 -7
  113. package/packages/dd-trace/src/dogstatsd.js +2 -2
  114. package/packages/dd-trace/src/encode/0.4.js +45 -54
  115. package/packages/dd-trace/src/encode/0.5.js +34 -3
  116. package/packages/dd-trace/src/encode/agentless-ci-visibility.js +26 -19
  117. package/packages/dd-trace/src/encode/agentless-json.js +1 -1
  118. package/packages/dd-trace/src/exporter.js +2 -0
  119. package/packages/dd-trace/src/exporters/agent/index.js +2 -1
  120. package/packages/dd-trace/src/exporters/agentless/index.js +3 -2
  121. package/packages/dd-trace/src/exporters/agentless/writer.js +2 -2
  122. package/packages/dd-trace/src/exporters/common/agents.js +3 -1
  123. package/packages/dd-trace/src/exporters/common/buffering-exporter.js +2 -1
  124. package/packages/dd-trace/src/exporters/common/request.js +4 -2
  125. package/packages/dd-trace/src/exporters/electron/index.js +49 -0
  126. package/packages/dd-trace/src/external-logger/src/index.js +2 -1
  127. package/packages/dd-trace/src/git_metadata.js +10 -8
  128. package/packages/dd-trace/src/id.js +17 -4
  129. package/packages/dd-trace/src/lambda/handler-paths.js +52 -0
  130. package/packages/dd-trace/src/lambda/handler.js +2 -4
  131. package/packages/dd-trace/src/lambda/index.js +62 -14
  132. package/packages/dd-trace/src/lambda/runtime/patch.js +21 -46
  133. package/packages/dd-trace/src/llmobs/index.js +13 -2
  134. package/packages/dd-trace/src/llmobs/plugins/bedrockruntime.js +45 -15
  135. package/packages/dd-trace/src/llmobs/sdk.js +10 -0
  136. package/packages/dd-trace/src/llmobs/writers/base.js +2 -1
  137. package/packages/dd-trace/src/log/writer.js +3 -1
  138. package/packages/dd-trace/src/noop/span.js +3 -1
  139. package/packages/dd-trace/src/openfeature/writers/base.js +2 -1
  140. package/packages/dd-trace/src/openfeature/writers/exposures.js +51 -20
  141. package/packages/dd-trace/src/opentelemetry/metrics/periodic_metric_reader.js +3 -2
  142. package/packages/dd-trace/src/opentracing/propagation/text_map.js +20 -9
  143. package/packages/dd-trace/src/payload-tagging/config/index.js +2 -2
  144. package/packages/dd-trace/src/plugins/apollo.js +3 -1
  145. package/packages/dd-trace/src/plugins/ci_plugin.js +52 -17
  146. package/packages/dd-trace/src/plugins/database.js +54 -12
  147. package/packages/dd-trace/src/plugins/index.js +1 -0
  148. package/packages/dd-trace/src/plugins/log_plugin.js +3 -1
  149. package/packages/dd-trace/src/plugins/plugin.js +2 -4
  150. package/packages/dd-trace/src/plugins/tracing.js +5 -3
  151. package/packages/dd-trace/src/plugins/util/ci.js +8 -8
  152. package/packages/dd-trace/src/plugins/util/git-cache.js +20 -18
  153. package/packages/dd-trace/src/plugins/util/git.js +3 -1
  154. package/packages/dd-trace/src/plugins/util/stacktrace.js +2 -2
  155. package/packages/dd-trace/src/plugins/util/test.js +119 -5
  156. package/packages/dd-trace/src/plugins/util/user-provided-git.js +17 -15
  157. package/packages/dd-trace/src/plugins/util/web.js +11 -0
  158. package/packages/dd-trace/src/priority_sampler.js +1 -1
  159. package/packages/dd-trace/src/profiling/profiler.js +1 -1
  160. package/packages/dd-trace/src/profiling/profilers/wall.js +1 -1
  161. package/packages/dd-trace/src/profiling/ssi-heuristics.js +1 -1
  162. package/packages/dd-trace/src/rate_limiter.js +1 -1
  163. package/packages/dd-trace/src/remote_config/scheduler.js +1 -1
  164. package/packages/dd-trace/src/ritm.js +2 -1
  165. package/packages/dd-trace/src/runtime_metrics/runtime_metrics.js +5 -8
  166. package/packages/dd-trace/src/scope.js +7 -5
  167. package/packages/dd-trace/src/serverless.js +5 -2
  168. package/packages/dd-trace/src/service-naming/extra-services.js +14 -0
  169. package/packages/dd-trace/src/service-naming/schemas/v0/messaging.js +20 -0
  170. package/packages/dd-trace/src/service-naming/schemas/v0/web.js +4 -0
  171. package/packages/dd-trace/src/service-naming/schemas/v1/messaging.js +20 -0
  172. package/packages/dd-trace/src/service-naming/schemas/v1/web.js +4 -0
  173. package/packages/dd-trace/src/span_stats.js +1 -1
  174. package/packages/dd-trace/src/telemetry/dependencies.js +1 -1
  175. package/packages/dd-trace/src/telemetry/endpoints.js +1 -1
  176. package/packages/dd-trace/src/telemetry/telemetry.js +2 -2
  177. package/packages/dd-trace/src/lambda/runtime/ritm.js +0 -133
  178. package/vendor/dist/opentracing/LICENSE +0 -201
  179. package/vendor/dist/opentracing/binary_carrier.d.ts +0 -11
  180. package/vendor/dist/opentracing/constants.d.ts +0 -61
  181. package/vendor/dist/opentracing/examples/demo/demo.d.ts +0 -2
  182. package/vendor/dist/opentracing/ext/tags.d.ts +0 -90
  183. package/vendor/dist/opentracing/functions.d.ts +0 -20
  184. package/vendor/dist/opentracing/global_tracer.d.ts +0 -14
  185. package/vendor/dist/opentracing/index.d.ts +0 -12
  186. package/vendor/dist/opentracing/index.js +0 -1
  187. package/vendor/dist/opentracing/mock_tracer/index.d.ts +0 -5
  188. package/vendor/dist/opentracing/mock_tracer/mock_context.d.ts +0 -13
  189. package/vendor/dist/opentracing/mock_tracer/mock_report.d.ts +0 -16
  190. package/vendor/dist/opentracing/mock_tracer/mock_span.d.ts +0 -50
  191. package/vendor/dist/opentracing/mock_tracer/mock_tracer.d.ts +0 -26
  192. package/vendor/dist/opentracing/noop.d.ts +0 -8
  193. package/vendor/dist/opentracing/reference.d.ts +0 -33
  194. package/vendor/dist/opentracing/span.d.ts +0 -147
  195. package/vendor/dist/opentracing/span_context.d.ts +0 -26
  196. package/vendor/dist/opentracing/test/api_compatibility.d.ts +0 -16
  197. package/vendor/dist/opentracing/test/mocktracer_implemenation.d.ts +0 -3
  198. package/vendor/dist/opentracing/test/noop_implementation.d.ts +0 -4
  199. package/vendor/dist/opentracing/test/opentracing_api.d.ts +0 -3
  200. package/vendor/dist/opentracing/test/unittest.d.ts +0 -2
  201. package/vendor/dist/opentracing/tracer.d.ts +0 -127
@@ -7,6 +7,11 @@ const {
7
7
  channel,
8
8
  addHook,
9
9
  } = require('./helpers/instrument')
10
+ const {
11
+ brokerSupportsMessageHeaders,
12
+ clientToCluster,
13
+ cloneMessages,
14
+ } = require('./helpers/kafka')
10
15
 
11
16
  const producerStartCh = channel('apm:kafkajs:produce:start')
12
17
  const producerCommitCh = channel('apm:kafkajs:produce:commit')
@@ -22,7 +27,27 @@ const batchConsumerStartCh = channel('apm:kafkajs:consume-batch:start')
22
27
  const batchConsumerFinishCh = channel('apm:kafkajs:consume-batch:finish')
23
28
  const batchConsumerErrorCh = channel('apm:kafkajs:consume-batch:error')
24
29
 
25
- const disabledHeaderWeakSet = new WeakSet()
30
+ const noop = () => {}
31
+
32
+ addHook({ name: 'kafkajs', file: 'src/producer/index.js', versions: ['>=1.4'] }, (createProducer) =>
33
+ shimmer.wrapFunction(createProducer, original => function wrappedCreateProducer (params) {
34
+ const producer = original(params)
35
+ if (params?.cluster) {
36
+ clientToCluster.set(producer, params.cluster)
37
+ }
38
+ return producer
39
+ })
40
+ )
41
+
42
+ addHook({ name: 'kafkajs', file: 'src/consumer/index.js', versions: ['>=1.4'] }, (createConsumer) =>
43
+ shimmer.wrapFunction(createConsumer, original => function wrappedCreateConsumer (params) {
44
+ const consumer = original(params)
45
+ if (params?.cluster) {
46
+ clientToCluster.set(consumer, params.cluster)
47
+ }
48
+ return consumer
49
+ })
50
+ )
26
51
 
27
52
  addHook({ name: 'kafkajs', file: 'src/index.js', versions: ['>=1.4'] }, (BaseKafka) => {
28
53
  class Kafka extends BaseKafka {
@@ -34,79 +59,113 @@ addHook({ name: 'kafkajs', file: 'src/index.js', versions: ['>=1.4'] }, (BaseKaf
34
59
  }
35
60
  }
36
61
 
37
- shimmer.wrap(Kafka.prototype, 'producer', createProducer => function (...args) {
38
- const producer = createProducer.apply(this, args)
39
- const send = producer.send
62
+ shimmer.wrap(Kafka.prototype, 'producer', createProducer => function () {
63
+ const producer = createProducer.apply(this, arguments)
64
+ const originalSend = producer.send
40
65
  const bootstrapServers = this._brokers
66
+ const cluster = clientToCluster.get(producer)
67
+
68
+ let disableHeaderInjection = false
41
69
 
42
- const kafkaClusterIdPromise = getKafkaClusterId(this)
70
+ let refreshHeaderSupport = () => {
71
+ if (!brokerSupportsMessageHeaders(cluster?.brokerPool)) {
72
+ disableHeaderInjection = true
73
+ refreshHeaderSupport = noop
74
+ log.info('kafkajs broker negotiated Produce <v3; tracer header injection disabled.')
75
+ }
76
+ }
43
77
 
44
78
  producer.send = function (...args) {
45
- const wrappedSend = (clusterId) => {
46
- const { topic, messages = [] } = args[0]
47
-
48
- const ctx = {
49
- bootstrapServers,
50
- clusterId,
51
- disableHeaderInjection: disabledHeaderWeakSet.has(producer),
52
- messages,
53
- topic,
54
- }
79
+ if (!producerStartCh.hasSubscribers) {
80
+ return originalSend.apply(this, args)
81
+ }
55
82
 
56
- for (const message of messages) {
57
- if (message !== null && typeof message === 'object' && !ctx.disableHeaderInjection) {
58
- message.headers = message.headers || {}
59
- }
60
- }
83
+ // Fast path: kafkajs has fetched metadata, so versions and clusterId
84
+ // are already on the broker pool.
85
+ const metadata = cluster?.brokerPool?.metadata
86
+ if (metadata) {
87
+ refreshHeaderSupport()
88
+ return runSend.call(this, args, metadata.clusterId)
89
+ }
61
90
 
62
- return producerStartCh.runStores(ctx, () => {
63
- try {
64
- const result = send.apply(this, args)
65
- result.then(
66
- (res) => {
67
- ctx.result = res
68
- producerFinishCh.publish(ctx)
69
- producerCommitCh.publish(ctx)
70
- },
71
- (err) => {
72
- ctx.error = err
73
- if (err) {
74
- // Fixes bug where we would inject message headers for kafka brokers that don't support headers
75
- // (version <0.11). On the error, we disable header injection.
76
- // Unfortunately the error name / type is not more specific.
77
- // This approach is implemented by other tracers as well.
78
- if (err.name === 'KafkaJSProtocolError' && err.type === 'UNKNOWN') {
79
- disabledHeaderWeakSet.add(producer)
80
- log.error(
81
- // eslint-disable-next-line @stylistic/max-len
82
- 'Kafka Broker responded with UNKNOWN_SERVER_ERROR (-1). Please look at broker logs for more information. Tracer message header injection for Kafka is disabled.'
83
- )
84
- }
85
- producerErrorCh.publish(err)
86
- }
87
- producerFinishCh.publish(ctx)
88
- })
89
-
90
- return result
91
- } catch (e) {
92
- ctx.error = e
93
- producerErrorCh.publish(ctx)
94
- producerFinishCh.publish(ctx)
95
- throw e
96
- }
97
- })
91
+ // Slow path, taken at most once per producer connect cycle. Prime the
92
+ // metadata fetch kafkajs's send would do internally a few stack frames
93
+ // later. `sharedPromiseTo` collapses our call and kafkajs's call into a
94
+ // single round trip, so total latency is unchanged.
95
+ if (typeof cluster?.refreshMetadataIfNecessary !== 'function') {
96
+ return runSend.call(this, args)
97
+ }
98
+ return cluster.refreshMetadataIfNecessary().then(
99
+ () => {
100
+ refreshHeaderSupport()
101
+ return runSend.call(this, args, cluster.brokerPool?.metadata?.clusterId)
102
+ },
103
+ () => runSend.call(this, args)
104
+ )
105
+ }
106
+
107
+ function runSend (args, clusterId) {
108
+ const arg0 = args[0]
109
+ const topic = arg0?.topic
110
+ const inputMessages = Array.isArray(arg0?.messages) ? arg0.messages : []
111
+
112
+ // Hand kafkajs and the plugin a shallow clone so injection writes to
113
+ // tracer-owned objects instead of the caller's. With injection
114
+ // disabled the clone must not seed `headers: {}` either: brokers that
115
+ // reject any header field cannot recover otherwise.
116
+ let messages = inputMessages
117
+ if (inputMessages.length > 0) {
118
+ messages = cloneMessages(inputMessages, !disableHeaderInjection)
119
+ args[0] = { ...arg0, messages }
98
120
  }
99
121
 
100
- if (isPromise(kafkaClusterIdPromise)) {
101
- // promise is not resolved
102
- return kafkaClusterIdPromise.then((clusterId) => {
103
- return wrappedSend(clusterId)
104
- })
122
+ const ctx = {
123
+ bootstrapServers,
124
+ clusterId,
125
+ disableHeaderInjection,
126
+ messages,
127
+ topic,
105
128
  }
106
129
 
107
- // promise is already resolved
108
- return wrappedSend(kafkaClusterIdPromise)
130
+ return producerStartCh.runStores(ctx, () => {
131
+ try {
132
+ const result = originalSend.apply(this, args)
133
+ result.then(
134
+ (res) => {
135
+ ctx.result = res
136
+ producerFinishCh.publish(ctx)
137
+ producerCommitCh.publish(ctx)
138
+ },
139
+ (error) => {
140
+ ctx.error = error
141
+ if (error) {
142
+ // Safety net for mixed-version clusters where the seed
143
+ // broker advertised Produce v3+ but the leader we shipped to
144
+ // could not parse the headers, surfacing as
145
+ // KafkaJSProtocolError UNKNOWN (server error code -1).
146
+ if (error.name === 'KafkaJSProtocolError' && error.type === 'UNKNOWN') {
147
+ disableHeaderInjection = true
148
+ refreshHeaderSupport = noop
149
+ log.error(
150
+ // eslint-disable-next-line @stylistic/max-len
151
+ 'Kafka Broker responded with UNKNOWN_SERVER_ERROR (-1). Please look at broker logs for more information. Tracer message header injection for Kafka is disabled.'
152
+ )
153
+ }
154
+ producerErrorCh.publish(error)
155
+ }
156
+ producerFinishCh.publish(ctx)
157
+ }
158
+ )
159
+ return result
160
+ } catch (error) {
161
+ ctx.error = error
162
+ producerErrorCh.publish(ctx)
163
+ producerFinishCh.publish(ctx)
164
+ throw error
165
+ }
166
+ })
109
167
  }
168
+
110
169
  return producer
111
170
  })
112
171
 
@@ -115,24 +174,26 @@ addHook({ name: 'kafkajs', file: 'src/index.js', versions: ['>=1.4'] }, (BaseKaf
115
174
  return createConsumer.apply(this, args)
116
175
  }
117
176
 
118
- const kafkaClusterIdPromise = getKafkaClusterId(this)
119
- let resolvedClusterId = null
177
+ const consumer = createConsumer.apply(this, arguments)
178
+ const cluster = clientToCluster.get(consumer)
179
+ const groupId = arguments[0].groupId
120
180
 
121
- const eachMessageExtractor = (args, clusterId) => {
181
+ const readClusterId = () => cluster?.brokerPool?.metadata?.clusterId
182
+
183
+ const eachMessageExtractor = (args) => {
122
184
  const { topic, partition, message } = args[0]
123
- return { topic, partition, message, groupId, clusterId }
185
+ return { topic, partition, message, groupId, clusterId: readClusterId() }
124
186
  }
125
187
 
126
- const eachBatchExtractor = (args, clusterId) => {
188
+ const eachBatchExtractor = (args) => {
127
189
  const { batch } = args[0]
128
190
  const { topic, partition, messages } = batch
129
- return { topic, partition, messages, groupId, clusterId }
191
+ return { topic, partition, messages, groupId, clusterId: readClusterId() }
130
192
  }
131
193
 
132
- const consumer = createConsumer.apply(this, args)
133
-
134
194
  consumer.on(consumer.events.COMMIT_OFFSETS, (event) => {
135
195
  const { payload: { groupId: commitGroupId, topics } } = event
196
+ const clusterId = readClusterId()
136
197
  const commitList = []
137
198
  for (const { topic, partitions } of topics) {
138
199
  for (const { partition, offset } of partitions) {
@@ -141,7 +202,7 @@ addHook({ name: 'kafkajs', file: 'src/index.js', versions: ['>=1.4'] }, (BaseKaf
141
202
  partition,
142
203
  offset,
143
204
  topic,
144
- clusterId: resolvedClusterId,
205
+ clusterId,
145
206
  })
146
207
  }
147
208
  }
@@ -149,118 +210,67 @@ addHook({ name: 'kafkajs', file: 'src/index.js', versions: ['>=1.4'] }, (BaseKaf
149
210
  })
150
211
 
151
212
  const run = consumer.run
152
- const groupId = args[0].groupId
153
213
 
154
214
  consumer.run = function ({ eachMessage, eachBatch, ...runArgs }) {
155
- const wrapConsume = (clusterId) => {
156
- // In kafkajs COMMIT_OFFSETS always happens in the context of one synchronous run
157
- // So this will always reference a correct cluster id
158
- resolvedClusterId = clusterId
159
- return run({
160
- eachMessage: wrappedCallback(
161
- eachMessage,
162
- consumerStartCh,
163
- consumerFinishCh,
164
- consumerErrorCh,
165
- eachMessageExtractor,
166
- clusterId
167
- ),
168
- eachBatch: wrappedCallback(
169
- eachBatch,
170
- batchConsumerStartCh,
171
- batchConsumerFinishCh,
172
- batchConsumerErrorCh,
173
- eachBatchExtractor,
174
- clusterId
175
- ),
176
- ...runArgs,
177
- })
178
- }
179
-
180
- if (isPromise(kafkaClusterIdPromise)) {
181
- // promise is not resolved
182
- return kafkaClusterIdPromise.then((clusterId) => {
183
- return wrapConsume(clusterId)
184
- })
185
- }
186
-
187
- // promise is already resolved
188
- return wrapConsume(kafkaClusterIdPromise)
215
+ return run({
216
+ eachMessage: wrappedCallback(
217
+ eachMessage,
218
+ consumerStartCh,
219
+ consumerFinishCh,
220
+ consumerErrorCh,
221
+ eachMessageExtractor
222
+ ),
223
+ eachBatch: wrappedCallback(
224
+ eachBatch,
225
+ batchConsumerStartCh,
226
+ batchConsumerFinishCh,
227
+ batchConsumerErrorCh,
228
+ eachBatchExtractor
229
+ ),
230
+ ...runArgs,
231
+ })
189
232
  }
233
+
190
234
  return consumer
191
235
  })
236
+
192
237
  return Kafka
193
238
  })
194
239
 
195
- const wrappedCallback = (fn, startCh, finishCh, errorCh, extractArgs, clusterId) => {
196
- return typeof fn === 'function'
197
- ? function (...args) {
198
- const extractedArgs = extractArgs(args, clusterId)
199
- const ctx = {
200
- extractedArgs,
201
- }
240
+ const wrappedCallback = (fn, startCh, finishCh, errorCh, extractArgs) => {
241
+ if (typeof fn !== 'function') return fn
242
+ return function (...args) {
243
+ const ctx = {
244
+ extractedArgs: extractArgs(args),
245
+ }
202
246
 
203
- return startCh.runStores(ctx, () => {
204
- try {
205
- const result = fn.apply(this, args)
206
- if (result && typeof result.then === 'function') {
207
- result.then(
208
- (res) => {
209
- ctx.result = res
210
- finishCh.publish(ctx)
211
- },
212
- (err) => {
213
- ctx.error = err
214
- if (err) {
215
- errorCh.publish(ctx)
216
- }
217
- finishCh.publish(ctx)
218
- })
219
- } else {
220
- finishCh.publish(ctx)
221
- }
222
- return result
223
- } catch (e) {
224
- ctx.error = e
225
- errorCh.publish(ctx)
247
+ return startCh.runStores(ctx, () => {
248
+ try {
249
+ const result = fn.apply(this, args)
250
+ if (result && typeof result.then === 'function') {
251
+ result.then(
252
+ (res) => {
253
+ ctx.result = res
254
+ finishCh.publish(ctx)
255
+ },
256
+ (error) => {
257
+ ctx.error = error
258
+ if (error) {
259
+ errorCh.publish(ctx)
260
+ }
261
+ finishCh.publish(ctx)
262
+ }
263
+ )
264
+ } else {
226
265
  finishCh.publish(ctx)
227
- throw e
228
266
  }
229
- })
230
- }
231
- : fn
232
- }
233
-
234
- const getKafkaClusterId = (kafka) => {
235
- if (kafka._ddKafkaClusterId) {
236
- return kafka._ddKafkaClusterId
237
- }
238
-
239
- if (!kafka.admin) {
240
- return null
241
- }
242
-
243
- const admin = kafka.admin()
244
-
245
- if (!admin.describeCluster) {
246
- return null
247
- }
248
-
249
- return admin.connect()
250
- .then(() => {
251
- return admin.describeCluster()
252
- })
253
- .then((clusterInfo) => {
254
- const clusterId = clusterInfo?.clusterId
255
- kafka._ddKafkaClusterId = clusterId
256
- admin.disconnect()
257
- return clusterId
258
- })
259
- .catch((error) => {
260
- throw error
267
+ return result
268
+ } catch (error) {
269
+ ctx.error = error
270
+ errorCh.publish(ctx)
271
+ finishCh.publish(ctx)
272
+ throw error
273
+ }
261
274
  })
262
- }
263
-
264
- function isPromise (obj) {
265
- return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
275
+ }
266
276
  }
@@ -93,7 +93,7 @@ function createWrapQueryCallback (options) {
93
93
  }
94
94
 
95
95
  if (typeof cb === 'function') {
96
- arguments[arguments.length - 1] = shimmer.wrapFunction(cb, wrapper)
96
+ arguments[arguments.length - 1] = shimmer.wrapCallback(cb, wrapper)
97
97
  } else {
98
98
  arguments.length += 1
99
99
  arguments[arguments.length - 1] = wrapper()
@@ -23,7 +23,8 @@ addHook({ name: 'memcached', versions: ['>=2.2'] }, Memcached => {
23
23
 
24
24
  const ctx = { client, server, query }
25
25
  startCh.runStores(ctx, () => {
26
- query.callback = shimmer.wrapFunction(query.callback, callback => function (err) {
26
+ if (typeof query.callback !== 'function') return
27
+ query.callback = shimmer.wrapCallback(query.callback, callback => function (err) {
27
28
  if (err) {
28
29
  ctx.error = err
29
30
  errorCh.publish(ctx)