From e80b7187dfcec872a4e2b4af3f3c56803e22dc7f Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Wed, 7 Aug 2019 15:29:57 -0600 Subject: [PATCH] [newrelic] Allow promise transaction handlers. (#37417) https://docs.newrelic.com/docs/agents/nodejs-agent/supported-features/nodejs-custom-instrumentation#background-txn --- types/newrelic/index.d.ts | 20 +++++++++++--------- types/newrelic/newrelic-tests.ts | 5 ++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/types/newrelic/index.d.ts b/types/newrelic/index.d.ts index 10700022df..419591e267 100644 --- a/types/newrelic/index.d.ts +++ b/types/newrelic/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for newrelic 4.11 +// Type definitions for newrelic 5.11 // Project: http://github.com/newrelic/node-newrelic // Definitions by: Matt R. Wilson // Brooks Patton @@ -176,7 +176,6 @@ export function startSegment any>(name: string, * The agent begins timing the segment when createTracer is called, and ends the segment when the callback * defined by the callback argument finishes executing. * - * @deprecated * This method has been deprecated in favor of newrelic.startSegment() */ export function createTracer any>(name: string, handle: T): T; @@ -192,7 +191,7 @@ export function createTracer any>(name: string, ha * transaction as externally handled. In this case the transaction * will be ended when `TransactionHandle#end` is called in the user's code. * - * @example + * Example: * var newrelic = require('newrelic') * newrelic.startWebTransaction('/some/url/path', function() { * var transaction = newrelic.getTransaction() @@ -205,7 +204,8 @@ export function createTracer any>(name: string, ha * The `url` is used to name and group related transactions in APM, * so it should be a generic name and not include any variable parameters. */ -export function startWebTransaction(url: string, handle: (...args: any[]) => any): any; +export function startWebTransaction(url: string, handle: Promise): Promise; +export function startWebTransaction(url: string, handle: (...args: any[]) => T): T; /** * Creates and starts a background transaction to record work done in the handle supplied. @@ -218,7 +218,7 @@ export function startWebTransaction(url: string, handle: (...args: any[]) => any * transaction as externally handled. In this case the transaction * will be ended when `TransactionHandle#end` is called in the user's code. * - * @example + * Example: * var newrelic = require('newrelic') * newrelic.startBackgroundTransaction('Red October', 'Subs', function() { * var transaction = newrelic.getTransaction() @@ -235,8 +235,10 @@ export function startWebTransaction(url: string, handle: (...args: any[]) => any * For more information see: * https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page#txn-type-dropdown */ -export function startBackgroundTransaction(name: string, handle: (...args: any[]) => any): any; -export function startBackgroundTransaction(name: string, group: string, handle: (...args: any[]) => any): any; +export function startBackgroundTransaction(name: string, handle: Promise): Promise; +export function startBackgroundTransaction(name: string, handle: (...args: any[]) => T): T; +export function startBackgroundTransaction(name: string, group: string, handle: Promise): Promise; +export function startBackgroundTransaction(name: string, group: string, handle: (...args: any[]) => T): T; /** * End the current web or background custom transaction. @@ -325,8 +327,8 @@ export function shutdown(options?: { collectPendingData?: boolean, timeout?: num /** * Wraps an AWS Lambda function with NewRelic instrumentation and returns the value of the handler * - * @param handler a callback function whose value is returned from setLambdaHandler - * @returns the value returned by handler + * The handler is a callback function whose value is returned from setLambdaHandler + * Returns the value returned by handler */ export function setLambdaHandler(handler: (...args: any[]) => T): T; diff --git a/types/newrelic/newrelic-tests.ts b/types/newrelic/newrelic-tests.ts index 9bc909d57c..700013ef8d 100644 --- a/types/newrelic/newrelic-tests.ts +++ b/types/newrelic/newrelic-tests.ts @@ -44,14 +44,17 @@ const wrappedResult: number = wrappedFn(42); newrelic.startWebTransaction('/some/url/path', () => { const transaction = newrelic.getTransaction(); - Promise.all([]); setTimeout(() => { // do some work transaction.end(); }, 100); }); +newrelic.startWebTransaction('/some/url/path', Promise.resolve(7)); // $ExpectType Promise + newrelic.startBackgroundTransaction('Red October', (foo) => foo); // $ExpectType any +newrelic.startBackgroundTransaction('Red October', () => 7); // $ExpectType number +newrelic.startBackgroundTransaction('Red October', Promise.resolve(7)); // $ExpectType Promise newrelic.startBackgroundTransaction('Red October', 'Subs', () => { const transaction = newrelic.getTransaction(); setTimeout(() => {