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

[Feat][DEVX-364] Record Custom Metric (Response Time) #861

Merged
merged 10 commits into from
Jan 15, 2025

Conversation

ajimae
Copy link
Contributor

@ajimae ajimae commented Nov 27, 2024

Summary

Record custom metrics (request-response time)

Completed Tasks

  • add a helper to get individual request start and end times
  • add APM agents to record and send custom metrics to respective APMs

Depends On

#877

@ajimae ajimae requested a review from a team as a code owner November 27, 2024 00:00
Copy link

changeset-bot bot commented Nov 27, 2024

🦋 Changeset detected

Latest commit: 9d1df0b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@commercetools/ts-sdk-apm Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ajimae ajimae marked this pull request as draft November 27, 2024 00:01
@ajimae ajimae changed the title [Feat][Dev-364] Record Custom Metric (Response Time) [Feat][DEVX-364] Record Custom Metric (Response Time) Nov 27, 2024
@ajimae ajimae force-pushed the feat/add-requests-response-time-and-custom-metrics branch from e834325 to 5f7d9bb Compare November 27, 2024 00:17
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.05%. Comparing base (1f119a4) to head (9d1df0b).
Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #861   +/-   ##
=======================================
  Coverage   93.05%   93.05%           
=======================================
  Files          25       25           
  Lines         288      288           
  Branches       14       14           
=======================================
  Hits          268      268           
  Misses         20       20           
Flag Coverage Δ
integrationtests 93.05% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ajimae ajimae marked this pull request as ready for review November 27, 2024 15:36
@ajimae ajimae force-pushed the feat/add-requests-response-time-and-custom-metrics branch from b63f0b8 to 8fb70b5 Compare November 27, 2024 15:39
@ajimae ajimae marked this pull request as draft November 27, 2024 15:54
- record and send custom metrics to respective APMs
- fix tests for old (legacy) apm middleware
- improve typescript types for ts-sdk-apm package
- add more custom metric example snippets to example directory
- add tests to validate functionality
- add node-fetch dependency to apm package
@ajimae ajimae force-pushed the feat/add-requests-response-time-and-custom-metrics branch from 6e60c49 to bcbbc6a Compare January 13, 2025 02:48
- rebase and update branch
- move test to integration tests directory
- update dependencies
@ajimae ajimae force-pushed the feat/add-requests-response-time-and-custom-metrics branch from bcbbc6a to 609a0c9 Compare January 13, 2025 02:53
@ajimae ajimae marked this pull request as ready for review January 13, 2025 03:08
@ajimae ajimae requested review from lojzatran and barbara79 January 13, 2025 03:08
Copy link
Contributor

@lojzatran lojzatran left a comment

Choose a reason for hiding this comment

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

Good job 👍

examples/newrelic-express-apm/package.json Outdated Show resolved Hide resolved
packages/ts-sdk-apm/src/apm-legacy.ts Outdated Show resolved Hide resolved
return ResponseHandler.successResponse(
res,
data.statusCode || data.body.statusCode,
data.message || data.body.message,
data.body
)
}

const end = performance.now()
newrelic.recordMetric('Commercetools/Test/Response/Time/Total', end - start)
Copy link
Contributor

Choose a reason for hiding this comment

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

You have duplicated code.

btw. should this be recorded inside the ts-apm-sdk? There is a metric Commercetools/Client/Response/Total already. Why would the user need the second metric here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uhmmm not necessarily a duplicate. What I was looking at is how the custom can instrument an individual function in their codebase. As you may have seen, the only thing we recorded in the telemetryMiddleware was the response time. However with this, they can record any other metric include a status code, execution time of a function etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I was not clear. By duplicated I mean this part is twice. It could be done before if once:

      const end = performance.now()
      newrelic.recordMetric(
        'Commercetools/Test/Response/Time/Total',
        end - start
      )

Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to show a custom metric created by user, then try to use a different name and don't start with Commercetools. I'm also not sure if we should provide newrelic in our package. This should be the responsibility of the user's app to use newrelic dependency directly. Instead we could add some more metrics to the telemetryMiddleware, like count of errors and success requests. WDYT?

Copy link
Contributor Author

@ajimae ajimae Jan 14, 2025

Choose a reason for hiding this comment

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

That is why we have a separate package for telemetry. I think the newrelic and dd-trace dependencies are both absolutely necessary here and should be part of the offering. These dependencies shouldn't be coming from the customer's application.

Also, you cannot totally account for all the possible scenario where or what the customer would want to instrument. I would suggest giving them the freedom to record any metric of their choice, except you want us to limit what they can monitor?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I see your point. I find it a bit weird that we offer also newrelic, datadog etc. when the customer could actually use the original newrelic or datadog packages by themselves.
We do not add anything extra to it, so I'm wondering why would the customer wants to use newrelic from ts-sdk-apm package if they just want to use newrelic for their custom metrics? Would it be better for them to use directly the package from newrelic directly?

examples/newrelic-express-apm/src/utils/response.js Outdated Show resolved Hide resolved
packages/ts-sdk-apm/src/apm.ts Outdated Show resolved Hide resolved
packages/ts-sdk-apm/src/helpers.ts Show resolved Hide resolved
packages/ts-sdk-apm/src/index.ts Outdated Show resolved Hide resolved
- implement PR review feedback
- implement PR review feedback
Copy link
Contributor

@barbara79 barbara79 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@ajimae ajimae merged commit e8a0e92 into master Jan 15, 2025
6 checks passed
@ajimae ajimae deleted the feat/add-requests-response-time-and-custom-metrics branch January 15, 2025 09:48
@ct-changesets ct-changesets bot mentioned this pull request Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants