Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not overwrite transactions with the same name #149

Merged
merged 4 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 27 February 2024

- * export isZodError function [`#126`](https://github.com/lokalise/fastify-extras/pull/126)
- - export isZodError function [`#126`](https://github.com/lokalise/fastify-extras/pull/126)

#### [v16.4.0](https://github.com/lokalise/fastify-extras/compare/v16.3.1...v16.4.0)

Expand Down Expand Up @@ -159,7 +159,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 20 September 2023

- * add FastifyReplyWithPayload type [`#82`](https://github.com/lokalise/fastify-extras/pull/82)
- - add FastifyReplyWithPayload type [`#82`](https://github.com/lokalise/fastify-extras/pull/82)
- Bump @opentelemetry/exporter-trace-otlp-grpc from 0.41.2 to 0.43.0 [`#80`](https://github.com/lokalise/fastify-extras/pull/80)
- Bump newrelic from 11.0.0 to 11.1.0 [`#79`](https://github.com/lokalise/fastify-extras/pull/79)

Expand Down
24 changes: 15 additions & 9 deletions lib/plugins/newrelicTransactionManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
shutdown as Shutdown,
getTransaction as GetTransaction,
} from 'newrelic'
import { FifoMap } from 'toad-cache'

interface Newrelic {
startBackgroundTransaction: typeof startBackgroundTransactionType
Expand All @@ -28,7 +29,7 @@ export interface NewRelicTransactionManagerOptions {

export class NewRelicTransactionManager {
private readonly isEnabled: boolean
private readonly transactionMap: Map<string, TransactionHandle>
private readonly transactionMap: FifoMap<TransactionHandle>

constructor(isNewRelicEnabled: boolean) {
if (isNewRelicEnabled) {
Expand All @@ -37,7 +38,7 @@ export class NewRelicTransactionManager {
}

this.isEnabled = isNewRelicEnabled
this.transactionMap = new Map()
this.transactionMap = new FifoMap(2000)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was a potential memory leak in the original implementation, if for whatever reason we do not close the transaction, it will keep hanging in the map forever.

}

public addCustomAttribute(attrName: string, attrValue: string | number | boolean) {
Expand All @@ -48,27 +49,32 @@ export class NewRelicTransactionManager {
newrelic.addCustomAttribute(attrName, attrValue)
}

public start(jobName: string): void {
/**
*
* @param transactionName - used for grouping similar transactions together
* @param uniqueTransactionKey - used for identifying specific ongoing transaction. Must be reasonably unique to reduce possibility of collisions
*/
public start(transactionName: string, uniqueTransactionKey: string): void {
if (!this.isEnabled) {
return
}

newrelic.startBackgroundTransaction(jobName, () => {
this.transactionMap.set(jobName, newrelic.getTransaction())
newrelic.startBackgroundTransaction(transactionName, () => {
this.transactionMap.set(uniqueTransactionKey, newrelic.getTransaction())
})
}

public stop(jobId: string): void {
public stop(uniqueTransactionKey: string): void {
if (!this.isEnabled) {
return
}

const transaction = this.transactionMap.get(jobId) ?? null
if (null === transaction) {
const transaction = this.transactionMap.get(uniqueTransactionKey) ?? null
if (!transaction) {
return
}
transaction.end()
this.transactionMap.delete(jobId)
this.transactionMap.delete(uniqueTransactionKey)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/requestContextProviderPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function plugin(fastify: FastifyInstance, opts: unknown, done: () => void) {
next: HookHandlerDoneFunction,
) {
req.reqContext = {
logger: req.log.child({
logger: (req.log as CommonLogger).child({
'x-request-id': req.id,
}),
reqId: req.id,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@amplitude/analytics-node": "^1.3.5",
"fastify-metrics": "^11.0.0",
"fastify-plugin": "^4.5.1",
"toad-cache": "^3.7.0",
"tslib": "^2.6.2"
},
"peerDependencies": {
Expand All @@ -68,7 +69,7 @@
},
"devDependencies": {
"@fastify/request-context": "^5.1.0",
"@lokalise/node-core": "^9.15.0",
"@lokalise/node-core": "^9.16.1",
"@types/newrelic": "^9.14.3",
"@types/node": "^20.12.7",
"@amplitude/analytics-types": "^2.5.0",
Expand Down
Loading