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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .changeset/strong-poets-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools/ts-sdk-apm': minor
---

Add custom metric
2 changes: 2 additions & 0 deletions .jest/setEnvVars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.env.NEW_RELIC_APP_NAME = 'newrelic-app-name'
process.env.NEW_RELIC_LICENSE_KEY = 'newrelic-license-key'
5 changes: 3 additions & 2 deletions examples/newrelic-express-apm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"author": "chukwuemeka ajima",
"private": false,
"dependencies": {
"@commercetools/platform-sdk": "^4.11.0",
"@commercetools/platform-sdk": "latest",
"@commercetools/sdk-client-v2": "^2.2.0",
"@commercetools/ts-sdk-apm": "^1.0.0",
"@commercetools/ts-client": "latest",
"@commercetools/ts-sdk-apm": "latest",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.20.0",
Expand Down
5 changes: 3 additions & 2 deletions examples/newrelic-express-apm/src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { newrelic } = require('@commercetools/ts-sdk-apm')
const { config } = require('dotenv')
const express = require('express')
const cors = require('cors')
Expand All @@ -6,7 +7,6 @@ config()

const routes = require('./routes')
const request = require('./utils/request')
const agent = require('../agent')

const app = express()
const { NODE_ENV } = process.env
Expand All @@ -19,7 +19,8 @@ app.use(express.urlencoded({ extended: true }))

app.use(function (req, res, next) {
const total = count()
agent.recordMetric(`Commercetools/Client/Request/Total`, total)
newrelic.recordMetric(`Commercetools/Client/Request/Total`, total)
next()
})

// Global error handler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { newrelic } = require('@commercetools/ts-sdk-apm')
const ResponseHandler = require('../utils/response')

/**
Expand All @@ -10,16 +11,25 @@ class ProductController {
}

async getProducts(req, res) {
const start = performance.now()
const data = await this.productService.getProduct(req.body)

if (data.statusCode == 200) {
const end = performance.now()
newrelic.recordMetric(
'Commercetools/Test/Response/Time/Total',
end - start
)
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)
return ResponseHandler.errorResponse(
res,
data.statusCode || data.body.statusCode,
Expand Down
6 changes: 3 additions & 3 deletions examples/newrelic-express-apm/src/sdk-v2/sdk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ClientBuilder } = require('@commercetools/sdk-client-v2')
const { ClientBuilder } = require('@commercetools/ts-client')
// const { createTelemetryMiddleware } = require('@commercetools/ts-sdk-apm')
const {
createTelemetryMiddleware,
Expand All @@ -21,14 +21,14 @@ const authMiddlewareOptions = {
},
},
scopes: [`manage_project:${projectKey}`],
fetch,
httpClient: fetch,
}

const httpMiddlewareOptions = {
host: 'https://api.europe-west1.gcp.commercetools.com',
includeRequestInErrorResponse: false,
includeOriginalRequest: true,
fetch,
httpClient: fetch,
}

// newrelic options
Expand Down
6 changes: 3 additions & 3 deletions examples/newrelic-express-apm/src/utils/response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const agent = require('../../agent')
const { newrelic } = require('@commercetools/ts-sdk-apm')
/**
* @class Response
*
Expand Down Expand Up @@ -27,7 +27,7 @@ class ResponseHandler {
responseBody.data = data
}

agent.recordMetric(
newrelic.recordMetric(
`Commercetools/Client/Response/Success/${statusCode}`,
statusCode
)
Expand Down Expand Up @@ -58,7 +58,7 @@ class ResponseHandler {
responseBody.data = data
}

agent.recordMetric(
newrelic.recordMetric(
`Commercetools/Client/Response/Error/${statusCode}`,
statusCode
)
Expand Down
Loading