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

[DEVX-364] Add Request Time and Custom Metrics #752

Draft
wants to merge 3 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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
- restore_cache: *restore_yarn_cache_node_16
- run:
name: Installing dependencies
command: yarn install --frozen-lockfile
command: yarn install --ignore-engines --frozen-lockfile # temp ignore engine checks [will deprecate node v16 soon]
- save_cache: *save_yarn_cache_node_16
- run:
name: Install example dependencies
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"dependencies": {
"buffer": "^6.0.3",
"node-fetch": "^2.6.1"
"node-fetch": "^2.6.1",
"@commercetools/ts-sdk-apm": "latest"
},
"files": ["dist", "CHANGELOG.md"],
"author": "Chukwuemeka Ajima <meeky.ae@gmail.com>",
Expand Down
17 changes: 16 additions & 1 deletion packages/sdk-client/src/sdk-client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
ProcessOptions,
SuccessResult,
} from '../types/sdk.d'
import { parseURLString, stringifyURLString } from '../utils'
import { parseURLString, stringifyURLString, time } from '../utils'
import validate from './validate'
import { newrelic, datadog } from '@commercetools/ts-sdk-apm'

let _options: ClientOptions
export const PAGE_LIMIT = 20
Expand Down Expand Up @@ -190,6 +191,20 @@ export default function createClient(options: ClientOptions): Client {
error: undefined,
}
)

// collect metrics
const end = time()
const start = request.startTime

// record for newrelic
newrelic.recordMetric(`Commercetools/Client/Request/Total`, start - end)

// record for datadog
datadog
.init()
.dogstatsd.gauge(`Commercetools_Client_Request_Total`, start - end, {
env: 'dev',
})
})
},
}
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-client/src/types/sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type Dispatch = (

export type Middleware = (next: Dispatch) => Dispatch
export interface ClientRequest {
startTime: number
baseUri?: string
uri?: string
headers?: VariableMap
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { parseURLString, stringifyURLString } from './url';
export { default as time } from './time';
7 changes: 7 additions & 0 deletions packages/sdk-client/src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// export default typeof BigInt !== 'undefined'
// ? typeof process !== 'undefined' &&
// typeof process.hrtime !== 'undefined' &&
// typeof process.hrtime.bigint === 'function' ? () => process.hrtime.bigint()
// : () => BigInt(Date.now() * 1e6) : () => Date.now() * 1e6;

export default () => Date.now() * 1e6
2 changes: 2 additions & 0 deletions packages/ts-sdk-apm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@opentelemetry/auto-instrumentations-node": "^0.47.0",
"@opentelemetry/exporter-metrics-otlp-http": "^0.52.0",
"@opentelemetry/sdk-node": "^0.52.0",
"dd-trace": "^5.17.0",
"newrelic": "^11.22.0",
"uuid": "10.0.0"
},
"publishConfig": {
Expand Down
9 changes: 9 additions & 0 deletions packages/ts-sdk-apm/src/agents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// module.exports = require('dd-trace')
// module.exports = require('newrelic')

import datadog from 'dd-trace'
import newrelic from 'newrelic'

datadog.dogstatsd.increment

export { datadog, newrelic }
10 changes: 10 additions & 0 deletions packages/ts-sdk-apm/src/apm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ const defaultOptions = {
tracer: () => require('../opentelemetry'),
}

const startTime =
typeof BigInt !== 'undefined'
? typeof process !== 'undefined' &&
typeof process.hrtime !== 'undefined' &&
typeof process.hrtime.bigint === 'function'
? () => process.hrtime.bigint()
: () => BigInt(Date.now() * 1e6)
: () => Date.now() * 1e6

export default function createTelemetryMiddleware(
options: OTelemetryMiddlewareOptions
): Middleware {
Expand All @@ -45,6 +54,7 @@ export default function createTelemetryMiddleware(
const nextRequest = {
...request,
...options,
startTime,
}

next(nextRequest, response)
Expand Down
1 change: 1 addition & 0 deletions packages/ts-sdk-apm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from '../types/types.d'
export { default as createTelemetryMiddleware } from './apm'
export * from './agents';
4 changes: 2 additions & 2 deletions packages/ts-sdk-apm/test/apm.test/apm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('apm', () => {
describe('apm test - null tracer configurations', () => {
const response = createTestResponse({})
const telemetryMiddleware = createTelemetryMiddleware({
apm: null,
tracer: null,
apm: null as any,
tracer: null as any,
})

const next = (req: MiddlewareRequest) => {
Expand Down
Loading