Skip to content

Commit

Permalink
DEVX-523 Fix createTelemetryMiddleware and migrate from client v2 to …
Browse files Browse the repository at this point in the history
…v3 datadog demo app (#919)

* refactor: removed default value to the createTelemetryMiddleware and datadog demo app adapted to the client v3

* chore: removed test about default value since it has been removed

* chore: modified test in base of the changes made in the code

* Update examples/datadog-express-apm/host-agent/src/sdk-v3.js

Co-authored-by: Lam Tran <[email protected]>

* chore: modified in base of the PR's comments

* chore: comment modified

* chore: modified in base of the suggestions made in the pr

* chore: adding a throwing error message for apm and tracer in case they are missing

* chore: adding a warning to make sure that customers know that there is something wrong with APM and Tracer and so keeping the tests as it is

* chore: adding code changes in base of comments in the PR

* chore: adding some changes in base of comments made in the PR

* chore: removed commented line

---------

Co-authored-by: Lam Tran <[email protected]>
  • Loading branch information
barbara79 and lojzatran authored Feb 6, 2025
1 parent 624c5b2 commit f8d596e
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 364 deletions.
2 changes: 2 additions & 0 deletions examples/datadog-express-apm/host-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ Example to show how the Datadog APM can be used in the TypeScript SDK.
- Your Project must have existing Products containing Variants, and at least one Customer.
- If your Project is currently empty, you can install the [SUNRISE sample data](https://github.com/commercetools/commercetools-sunrise-data).
- For Datadog setup, follow the instructions stated in our [official documentation website](https://docs.commercetools.com/sdk/observability/datadog#typescript-sdk) to properly install and set up the Datadog agent.
- Make sure that `Node js` has been installed as [Integration](https://docs.datadoghq.com/integrations/) to run this Demo application.

## Installation

1. Clone/Download the example folder.
2. Navigate to the path `datadog-express-apm/host-agent/`.
3. Create a `.env` file in this path, add and update the content of `.env.sample`, the `DD_API_KEY` is required
- Make sure that the `name` in the `package.json` matches with the `DD_SERVICE` environment variable value.
4. Run `yarn install` (`npm` can also be used) to install all dependencies

### Start the application
Expand Down
1 change: 0 additions & 1 deletion examples/datadog-express-apm/host-agent/agent.js

This file was deleted.

4 changes: 2 additions & 2 deletions examples/datadog-express-apm/host-agent/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "datadog",
"name": "datadog-express-apm",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"dd-trace": "^5.4.0",
"express": "^4.19.2"
"express": "^4.21.2"
}
}
13 changes: 7 additions & 6 deletions examples/datadog-express-apm/host-agent/src/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require('dotenv').config()
const agent = require('dd-trace').init()

const cors = require('cors')
const express = require('express')

const { apiRoot } = require('./sdk-v2')
const { apiRoot } = require('./sdk-v3')
const ResponseHandler = require('../utils/response')
const request = require('../utils/request')
const agent = require('../agent').default

const app = express()
const count = request()
Expand All @@ -16,7 +17,7 @@ app.use(express.urlencoded({ extended: true }))

app.use(function (req, res, next) {
const total = count()
agent.init().dogstatsd.gauge(`Commercetools_Client_Request_Total`, total, {
agent.dogstatsd.gauge(`Commercetools_Client_Request_Total`, total, {
env: 'dev',
})
next()
Expand All @@ -25,7 +26,7 @@ app.use(function (req, res, next) {
app.get('/project', async function (req, res, next) {
const project = await apiRoot.get().execute()

if (project.statusCode == 200) {
if (project.statusCode === 200) {
return ResponseHandler.successResponse(
res,
project.statusCode || project.body.statusCode,
Expand All @@ -45,7 +46,7 @@ app.get('/project', async function (req, res, next) {
app.get('/customers', async function (req, res, next) {
const customers = await apiRoot.customers().get().execute()

if (customers.statusCode == 200) {
if (customers.statusCode === 200) {
return ResponseHandler.successResponse(
res,
customers.statusCode || customers.body.statusCode,
Expand All @@ -65,7 +66,7 @@ app.get('/customers', async function (req, res, next) {
app.get('/products', async function (req, res, next) {
const products = await apiRoot.products().get().execute()

if (products.statusCode == 200) {
if (products.statusCode === 200) {
return ResponseHandler.successResponse(
res,
products.statusCode || products.body.statusCode,
Expand Down
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 { createApiBuilderFromCtpClient } = require('@commercetools/platform-sdk')
const fetch = require('node-fetch')
Expand All @@ -16,23 +16,23 @@ 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,
}

// datadog options
const agent = require('path').join(__dirname, '..', 'tracer.js')
const telemetryOptions = {
createTelemetryMiddleware,
userAgent: 'typescript-sdk-middleware-datadog',
tracer: require(agent).init(),
// apm: () => require('dd-trace').init()
tracer: async () => await import('dd-trace').init(),
customMetrics: {
datadog: true,
},
}

const client = new ClientBuilder()
Expand Down
11 changes: 0 additions & 11 deletions examples/datadog-express-apm/host-agent/tracer.js

This file was deleted.

8 changes: 4 additions & 4 deletions examples/datadog-express-apm/host-agent/utils/response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const agent = require('../agent').default
const agent = require('dd-trace').default
/**
* @class Response
*
Expand Down Expand Up @@ -30,7 +30,7 @@ class ResponseHandler {
agent.init().dogstatsd.increment(`Commercetools_Client_Response`, 1, {
env: 'dev',
status_code: statusCode,
http_method: req.method,
http_method: response.req.method,
success: true,
})

Expand All @@ -43,7 +43,7 @@ class ResponseHandler {
*
* @description method to handle all error responses
*
* @parma { response object }
* @param response object
* @param statusCode
* @param message
* @param data
Expand All @@ -63,7 +63,7 @@ class ResponseHandler {
agent.init().dogstatsd.increment(`Commercetools_Client_Response`, 1, {
env: 'dev',
status_code: statusCode,
http_method: req.method,
http_method: response.req.method,
success: false,
})

Expand Down
Loading

0 comments on commit f8d596e

Please sign in to comment.