-
-
Notifications
You must be signed in to change notification settings - Fork 347
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
improve api telemetry #2769
Merged
Merged
improve api telemetry #2769
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,13 @@ import { renderWelcomeEmail } from '@altairgraphql/emails'; | |
import { UserService } from 'src/auth/user/user.service'; | ||
import { Config } from 'src/common/config'; | ||
import { User } from '@altairgraphql/db'; | ||
import { Agent, getAgent } from 'src/newrelic/newrelic'; | ||
|
||
@Injectable() | ||
export class EmailService { | ||
private resend: Resend; | ||
private agent = getAgent(); | ||
|
||
constructor( | ||
private configService: ConfigService<Config>, | ||
private readonly userService: UserService | ||
|
@@ -52,12 +55,8 @@ export class EmailService { | |
|
||
async sendWelcomeEmail(userId: string) { | ||
const user = await this.userService.mustGetUser(userId); | ||
const { data, error } = await this.resend.emails.send({ | ||
from: | ||
this.configService.get('email.defaultFrom', { infer: true }) ?? | ||
'[email protected]', | ||
const { data, error } = await this.sendEmail({ | ||
to: user.email, | ||
replyTo: this.configService.get('email.replyTo', { infer: true }), | ||
subject: 'Welcome to Altair GraphQL Cloud', | ||
html: await renderWelcomeEmail({ username: this.getFirstName(user) }), | ||
}); | ||
|
@@ -70,12 +69,9 @@ export class EmailService { | |
|
||
async sendGoodbyeEmail(userId: string) { | ||
const user = await this.userService.mustGetUser(userId); | ||
const { data, error } = await this.resend.emails.send({ | ||
from: | ||
this.configService.get('email.defaultFrom', { infer: true }) ?? | ||
'[email protected]', | ||
|
||
const { data, error } = await this.sendEmail({ | ||
to: user.email, | ||
replyTo: this.configService.get('email.replyTo', { infer: true }), | ||
subject: 'Sorry to see you go 👋🏾', | ||
html: `Hey ${this.getFirstName(user)}, | ||
<br><br> | ||
|
@@ -100,6 +96,32 @@ export class EmailService { | |
return { data, error }; | ||
} | ||
|
||
private async sendEmail({ | ||
to, | ||
subject, | ||
html, | ||
}: { | ||
to: string; | ||
subject: string; | ||
html: string; | ||
}) { | ||
const { data, error } = await this.resend.emails.send({ | ||
from: | ||
this.configService.get('email.defaultFrom', { infer: true }) ?? | ||
'[email protected]', | ||
to, | ||
replyTo: this.configService.get('email.replyTo', { infer: true }), | ||
subject, | ||
html, | ||
}); | ||
if (error) { | ||
this.agent?.incrementMetric('email.send.error'); | ||
} | ||
|
||
this.agent?.incrementMetric('email.send.success'); | ||
return { data, error }; | ||
} | ||
|
||
private getFirstName(user: User) { | ||
return user.firstName ?? user.email; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { | ||
startWebTransaction, | ||
getTransaction, | ||
recordMetric, | ||
incrementMetric, | ||
} from 'newrelic'; | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const newrelic = require('newrelic'); | ||
|
||
export interface Agent { | ||
startWebTransaction: typeof startWebTransaction; | ||
getTransaction: typeof getTransaction; | ||
recordMetric: typeof recordMetric; | ||
incrementMetric: typeof incrementMetric; | ||
} | ||
|
||
const prodAgent: Agent = { | ||
startWebTransaction: newrelic.startWebTransaction, | ||
getTransaction: newrelic.getTransaction, | ||
recordMetric: newrelic.recordMetric, | ||
incrementMetric: newrelic.incrementMetric, | ||
}; | ||
|
||
export const getAgent = () => { | ||
if (process.env.NEW_RELIC_APP_NAME) { | ||
return prodAgent; | ||
} | ||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider centralizing the metric reporting logic using a decorator to wrap service methods.
Consider centralizing the metric reporting to reduce the interleaved instrumentation code. For example, you might create a decorator to wrap your service methods. This way, you can remove repetitive metric calls from the business logic while keeping the behavior intact. For instance:
Then annotate your service methods accordingly:
This decorator-based approach reduces duplication and clearly separates concerns while keeping functionality unchanged.