{"_id":"@defra/logging-metrics","_rev":"14-f1a5e31fe0390d0c3a6d2f98ad790dae","name":"@defra/logging-metrics","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@defra/logging-metrics","version":"1.0.0","author":{"name":"Department for Environment, Food and Rural Affairs https://www.gov.uk/government/organisations/department-for-environment-food-rural-affairs."},"license":"OGL-UK-3.0","_id":"@defra/logging-metrics@1.0.0","maintainers":[{"name":"niki.wycherley","email":"niki.wycherley@defra.gov.uk"},{"name":"richard-mohammed-def","email":"richard.mohammed@defra.gov.uk"},{"name":"stuaa78","email":"stuart.adair@defra.gov.uk"},{"name":"maxcbc-defra","email":"max.bladen-clark@defra.gov.uk"},{"name":"pmshaw15","email":"paul.m.shaw@environment-agency.gov.uk"},{"name":"kealan.parr","email":"kealan.parr@capgemini.com"},{"name":"z424bravenpm","email":"damian.kelly@defra.gov.uk"},{"name":"pwadmore.defra","email":"paul.wadmore@environment-agency.gov.uk"},{"name":"tjmason.defra","email":"tedd.mason@environment-agency.gov.uk"},{"name":"grahamwillis","email":"graham.willis@defra.gov.uk"},{"name":"npm-envage","email":"npm.envage@gmail.com"},{"name":"defradigitaladmin","email":"defradigital@gmail.com"},{"name":"defradigitalci","email":"defradigitalci@gmail.com"},{"name":"jaucourt","email":"philbens@gmail.com"},{"name":"bensagar-ea","email":"ben.sagar1@defra.gov.uk"}],"homepage":"https://github.com/DEFRA/defra-logging-metrics#readme","bugs":{"url":"https://github.com/DEFRA/defra-logging-metrics/issues"},"env":{"test":{"plugins":["@babel/plugin-transform-modules-commonjs"]}},"dist":{"shasum":"02d3cdd9450f90665bd78720d0abc752e932ba67","tarball":"https://registry.npmjs.org/@defra/logging-metrics/-/logging-metrics-1.0.0.tgz","fileCount":12,"integrity":"sha512-H/tqcHdIbkdOmHeCkr4yaQMf5Xx8T7kB8QVxpPhgGxeDvu591po7/TPN71eUVYql9mVdH3zoNjOvHbTqX/GATw==","signatures":[{"sig":"MEUCIFKLwJramzIdNeSTILZ/34PGh5JN5kzIeutqryKCry3QAiEApnmsTeoJKAB06umrtc2r7MJzliyuzsb/cCU9/BWNs0g=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":72087},"jest":{"rootDir":".","transform":{},"resetMocks":true,"setupFiles":["<rootDir>/.jest/test.env.js"],"collectCoverage":true,"testEnvironment":"jest-environment-node","coverageReporters":["lcov","text"],"setupFilesAfterEnv":["<rootDir>/.jest/setup.js"],"collectCoverageFrom":["<rootDir>/**/src/**/*.js"]},"main":"src/metrics.js","type":"module","module":"commonjs","engines":{"node":">=16"},"gitHead":"47a69ccbb21971032977ff655388fdef85d0544f","private":false,"scripts":{"lint":"eslint . --fix","test":"node --experimental-vm-modules node_modules/jest/bin/jest.js"},"_npmUser":{"name":"defradigitalci","email":"defradigitalci@gmail.com"},"repository":{"url":"git+https://github.com/DEFRA/defra-logging-metrics.git","type":"git"},"_npmVersion":"10.2.3","description":"Defra Common Platform logging metrics","directories":{},"_nodeVersion":"20.10.0","dependencies":{"applicationinsights":"^2.9.0"},"eslintConfig":{"env":{"es6":true,"jest":true,"node":true,"es2021":true,"browser":true},"extends":"eslint:recommended","plugins":["jest"],"verbose":true,"clearMocks":true,"parserOptions":{"sourceType":"module","ecmaVersion":"latest"},"ignorePatterns":["**/dist/*.js"],"moduleFileExtensions":["js","ts"]},"_hasShrinkwrap":false,"devDependencies":{"jest":"^29.7.0","eslint":"^8.52.0","eslint-plugin-jest":"^27.4.3"},"_npmOperationalInternal":{"tmp":"tmp/logging-metrics_1.0.0_1704703861133_0.7121439792307958","host":"s3://npm-registry-packages"}},"1.0.1":{"name":"@defra/logging-metrics","version":"1.0.1","author":{"name":"Department for Environment, Food and Rural Affairs https://www.gov.uk/government/organisations/department-for-environment-food-rural-affairs."},"license":"OGL-UK-3.0","_id":"@defra/logging-metrics@1.0.1","maintainers":[{"name":"niki.wycherley","email":"niki.wycherley@defra.gov.uk"},{"name":"richard-mohammed-def","email":"richard.mohammed@defra.gov.uk"},{"name":"stuaa78","email":"stuart.adair@defra.gov.uk"},{"name":"maxcbc-defra","email":"max.bladen-clark@defra.gov.uk"},{"name":"pmshaw15","email":"paul.m.shaw@environment-agency.gov.uk"},{"name":"kealan.parr","email":"kealan.parr@capgemini.com"},{"name":"z424bravenpm","email":"damian.kelly@defra.gov.uk"},{"name":"pwadmore.defra","email":"paul.wadmore@environment-agency.gov.uk"},{"name":"tjmason.defra","email":"tedd.mason@environment-agency.gov.uk"},{"name":"grahamwillis","email":"graham.willis@defra.gov.uk"},{"name":"npm-envage","email":"npm.envage@gmail.com"},{"name":"defradigitaladmin","email":"defradigital@gmail.com"},{"name":"defradigitalci","email":"defradigitalci@gmail.com"},{"name":"jaucourt","email":"philbens@gmail.com"},{"name":"bensagar-ea","email":"ben.sagar1@defra.gov.uk"}],"homepage":"https://github.com/DEFRA/defra-logging-metrics#readme","bugs":{"url":"https://github.com/DEFRA/defra-logging-metrics/issues"},"env":{"test":{"plugins":["@babel/plugin-transform-modules-commonjs"]}},"dist":{"shasum":"569d3325a2fbbb4ff82fd295c1bcb318e52f0078","tarball":"https://registry.npmjs.org/@defra/logging-metrics/-/logging-metrics-1.0.1.tgz","fileCount":12,"integrity":"sha512-idP+mGy0afaQY56qwL6NEWtBMUxS1soraUqhD9PXlcV3ce80aYpuEx/U6VO0z11qIRvNXXxyYXDT0CRIZFPXrA==","signatures":[{"sig":"MEUCIAd1al5iT36f7UlF0mCMwHt1SGofV8Vp33VnTxdgG4vRAiEAgq+DRv4wjuIEkHjRHjvW65m3X+TEYSitL3O+IO+Jfzs=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":71788},"jest":{"rootDir":".","transform":{},"resetMocks":true,"setupFiles":["<rootDir>/.jest/test.env.js"],"collectCoverage":true,"testEnvironment":"jest-environment-node","coverageReporters":["lcov","text"],"setupFilesAfterEnv":["<rootDir>/.jest/setup.js"],"collectCoverageFrom":["<rootDir>/**/src/**/*.js"]},"main":"src/metrics.js","type":"module","module":"commonjs","engines":{"node":">=16"},"gitHead":"0604731264a154b29a01959f612367b4caa0a851","private":false,"scripts":{"lint":"eslint . --fix","test":"node --experimental-vm-modules node_modules/jest/bin/jest.js"},"_npmUser":{"name":"defradigitalci","email":"defradigitalci@gmail.com"},"standard":{"globals":["describe","beforeEach","afterEach","afterAll","expect","it"]},"repository":{"url":"git+https://github.com/DEFRA/defra-logging-metrics.git","type":"git"},"_npmVersion":"10.2.3","description":"Defra Common Platform logging metrics","directories":{},"_nodeVersion":"20.10.0","dependencies":{"applicationinsights":"^2.9.0"},"eslintConfig":{"env":{"es6":true,"jest":true,"node":true,"es2021":true,"browser":true},"extends":"eslint:recommended","plugins":["jest"],"verbose":true,"clearMocks":true,"parserOptions":{"sourceType":"module","ecmaVersion":"latest"},"ignorePatterns":["**/dist/*.js"],"moduleFileExtensions":["js","ts"]},"_hasShrinkwrap":false,"devDependencies":{"jest":"^29.7.0","eslint":"^8.52.0","standard":"^17.1.0","eslint-plugin-jest":"^27.4.3"},"_npmOperationalInternal":{"tmp":"tmp/logging-metrics_1.0.1_1706029142676_0.653684569990687","host":"s3://npm-registry-packages"}}},"time":{"created":"2024-01-08T08:51:01.047Z","modified":"2026-08-12T10:52:03.734Z","1.0.0":"2024-01-08T08:51:01.279Z","1.0.1":"2024-01-23T16:59:02.828Z"},"bugs":{"url":"https://github.com/DEFRA/defra-logging-metrics/issues"},"author":{"name":"Department for Environment, Food and Rural Affairs https://www.gov.uk/government/organisations/department-for-environment-food-rural-affairs."},"license":"OGL-UK-3.0","homepage":"https://github.com/DEFRA/defra-logging-metrics#readme","repository":{"url":"git+https://github.com/DEFRA/defra-logging-metrics.git","type":"git"},"description":"Defra Common Platform logging metrics","maintainers":[{"email":"stuart.adair1@digital.cabinet-office.gov.uk","name":"stuaa78"},{"email":"npm.envage@gmail.com","name":"npm-envage"},{"email":"richard.mohammed@defra.gov.uk","name":"richard-mohammed-def"},{"email":"defradigital@gmail.com","name":"defradigitaladmin"},{"email":"defradigitalci@gmail.com","name":"defradigitalci"},{"email":"philbens@gmail.com","name":"jaucourt"},{"email":"ben.sagar1@defra.gov.uk","name":"bensagar-ea"},{"email":"damian.kelly@defra.gov.uk","name":"z424bravenpm"},{"email":"tedd.mason@environment-agency.gov.uk","name":"tjmason.defra"},{"email":"paul.wadmore@environment-agency.gov.uk","name":"pwadmore.defra"},{"email":"sunny_chotai@me.com","name":"chotai"},{"email":"niki.wycherley@defra.gov.uk","name":"niki.wycherley"},{"email":"paul.andrews@defra.gov.uk","name":"psandrews"},{"email":"alexgluckett@gmail.com","name":"alexluckett"},{"email":"max.bladen-clark@defra.gov.uk","name":"maxcbc-defra"},{"email":"paul.m.shaw@environment-agency.gov.uk","name":"pmshaw15"},{"email":"mark.fee@defra.gov.uk","name":"markfee"},{"email":"lee.gordon@defra.gov.uk","name":"geordiefoo"},{"email":"dan.leech@defra.gov.uk","name":"dannyleech"},{"email":"johnwatson484@hotmail.com","name":"johnwatson484"},{"email":"christopherjturner@gmail.com","name":"christopherjturner"},{"email":"yankui.def@gmail.com","name":"yfeng26"},{"email":"nick.richardson@defra.gov.uk","name":"nrichardson_defra"}],"readme":"# Defra Logging Metrics\r\n\r\nThis Defra Common Platform npm package provides a library to measure the duration of functions execution and log them into Azure Application Insights as custom metrics with optional custom dimensions.\r\n\r\n## Run tests\r\n\r\n```shell\r\nnpm install\r\n```\r\n\r\n```shell\r\nnpm test\r\n```\r\n\r\n## Use the package from npm\r\n\r\n1. Install the package\r\n\r\n   ```shell\r\n   npm install @defra/logging-metrics --save\r\n   ```\r\n\r\n1. Import the module in your script\r\n\r\n   ```js\r\n   import MetricsService from '@defra/logging-metrics'\r\n   ```\r\n\r\n1. Initialize the metrics service\r\n\r\n   ```js\r\n   let metricsService = new MetricsService()\r\n   ```\r\n\r\n1. If you are already running application insights and already have a `TelemetryClient` configured in your application, you can create the metrics service and pass the telemetry client to the constructor:\r\n\r\n   ```js\r\n   let metricsService = new MetricsService(telemetryClient)\r\n   ```\r\n\r\n   Alternatively you can create a new telemetry client and provide the configuration with a connection string and any other [configuration options](https://learn.microsoft.com/en-us/azure/azure-monitor/app/nodejs#advanced-configuration-options) you wish e.g.:\r\n   \r\n   ```js\r\n   let telemetryClientConfig = {\r\n      connectionString: \"InstrumentationKey=myKey;IngestionEndpoint=https://applicationinsights.azure.com/;LiveEndpoint=https://livediagnostics.monitor.azure.com/\",\r\n      samplingPercentage: 100\r\n   }\r\n\r\n   [...]\r\n\r\n   let { duration, result } = metricsService.execute(myFunction, metric, telemetryClientConfig)\r\n   ```\r\n\r\n   If you don't provide a telemetry client in the constructor and you don't provide a telemetry client configuration, a new telemetry client will be created by reading the connection string from the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable.\r\n\r\n 1. Name the metric you want to log with along with any other custom properties to be recorded e.g.:\r\n\r\n    ```js\r\n    let metric = {\r\n       name: \"timeToCallDatabase\",\r\n       properties: { source: \"CosmosDB\" }\r\n    }\r\n    ```\r\n\r\n1. Execute your function via one of the following methods, providing the `config` and `metric` from the previous steps along with the function you want to call and measure:\r\n\r\n   - If your function does not return any kind of `Promise`:\r\n\r\n   ```js\r\n   let { duration, result } = metricsService.execute(myFunction, metric, config)\r\n   console.log(`Took ${duration} seconds for ${result}`)\r\n   ```\r\n\r\n   - If your function returns a `Promise`:\r\n\r\n   ```js\r\n   metricsService.executeAsync(asyncFunction, metric, config)\r\n   .then(({ result, duration }) => {\r\n      console.log(`Took ${duration} seconds for ${result}`)\r\n   })\r\n   ```\r\n\r\n   - If you need to use a `Promise.all()`-like behavior and record each function call:\r\n\r\n   ```js\r\n   metricsService.executeAllFailFastAsync(() => [asyncFunction1, asyncFunction2], metric, config)\r\n   .then(({ results, durations }) => {\r\n      results.forEach((result, i) => {\r\n         console.log(`Took ${durations[i]} seconds for ${result}`)\r\n      })\r\n   })\r\n   ```\r\n\r\n   - If you need to use a `Promise.allSettled()`-like behavior and record each function call:\r\n\r\n   ```js\r\n   metricsService.executeAllAsync(() => [asyncFunction1, asyncFunction2], metric, config)\r\n   .then(({ results, durations }) => {\r\n      results.forEach((result, i) => {\r\n         console.log(`Took ${durations[i]} seconds for ${result}`)\r\n      })\r\n   })\r\n   ```\r\n\r\n1. As advised in Application Insights documentation:\r\n   \r\n   > If your application has a short lifespan, such as a CLI tool, it might be necessary to manually flush your buffered telemetry [...]   \r\n\r\n   Flush is supported by calling `metricsService.flushClient()`\r\n\r\n1. If you wish to further use, configure or access the Microsoft Azure Application Insights `TelemetryClient` within the metrics service, it can be accessed through the `metricsService.telemetryClient` variable\r\n\r\n## Full example\r\n\r\nA full example can be found [here](sample/README.md).","readmeFilename":"README.md"}