nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: newrelic/newrelic-node-apollo-server-plugin
Pull request #153 (2021-11-30 23:02:44)

Removes direct usage of internal tracer.
<!--
Thank you for submitting a Pull Request.

This code is leveraged to monitor critical services. Please consider the following:
* Tests are required.
* Performance matters.
* Features that are specific to just your app are unlikely to make it in.

Please fill out the relevant sections as follows:
* Proposed Release Notes: Bulleted list of recommended release notes for the change(s).
* Links: Any relevant links for the change.
* Details: In-depth description of changes, other technical notes, etc.
-->

## Proposed Release Notes

* Removed direct usage of internal tracer.

## Links

* https://github.com/newrelic/node-newrelic/blob/main/lib/transaction/tracer/index.js#L48-L55
* https://github.com/newrelic/node-newrelic/blob/main/lib/shim/shim.js#L1232-L1238
* https://github.com/newrelic/node-newrelic/blob/main/lib/errors/error-collector.js#L190-L214

## Details



Patch:
diff --git a/lib/create-plugin.js b/lib/create-plugin.js
index d1da7ae..901183d 100644
--- a/lib/create-plugin.js
+++ b/lib/create-plugin.js
@@ -101,8 +101,9 @@ function createPlugin(instrumentationApi, config = {}) {
       return {
         didResolveOperation(resolveContext) {
           if (shouldIgnoreTransaction(resolveContext.operation, config, logger)) {
-            const transaction = instrumentationApi.tracer.getTransaction()
-            if (transaction) {
+            const activeSegment = instrumentationApi.getActiveSegment()
+            if (activeSegment) {
+              const transaction = activeSegment.transaction
               transaction.setForceIgnore(true)
             }
           }
diff --git a/lib/error-helper.js b/lib/error-helper.js
index f307d06..aa110ca 100644
--- a/lib/error-helper.js
+++ b/lib/error-helper.js
@@ -29,7 +29,8 @@ class ErrorHelper {
   }
 
   noticeError(instrumentationApi, error) {
-    const transaction = instrumentationApi.tracer.getTransaction()
+    const activeSegment = instrumentationApi.getActiveSegment()
+    const transaction = activeSegment && activeSegment.transaction
     instrumentationApi.agent.errors.add(transaction, error)
   }
 }


Test patch:
diff --git a/tests/unit/error-helper.test.js b/tests/unit/error-helper.test.js
index 23fc199..b845449 100644
--- a/tests/unit/error-helper.test.js
+++ b/tests/unit/error-helper.test.js
@@ -18,16 +18,16 @@ class MockedInstrumentationApi {
         this.mockedLogsTrace.push(message)
       }
     }
-    ;(this.agent = {
+
+    this.agent = {
       errors: {
         add: function add(transaction, error) {
           parentThis.mockedCollectedErrors.push(error)
         }
       }
-    }),
-      (this.tracer = {
-        getTransaction: function getTransaction() {}
-      })
+    }
+
+    this.getActiveSegment = () => {}
   }
 }