{"id":20038646,"url":"https://github.com/defra/defra-logging-metrics","last_synced_at":"2026-06-13T09:32:22.882Z","repository":{"id":213124133,"uuid":"732066624","full_name":"DEFRA/defra-logging-metrics","owner":"DEFRA","description":"Provides a library to measure the duration of functions execution and log them into Azure Application Insights as custom metrics with optional custom dimensions","archived":false,"fork":false,"pushed_at":"2024-01-23T16:58:06.000Z","size":135,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-23T00:38:00.891Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DEFRA.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-15T14:59:31.000Z","updated_at":"2023-12-18T13:44:04.000Z","dependencies_parsed_at":"2023-12-18T16:38:23.950Z","dependency_job_id":"23891f13-f2b9-48c3-a7db-752833bfddd7","html_url":"https://github.com/DEFRA/defra-logging-metrics","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.4285714285714286,"last_synced_commit":"0604731264a154b29a01959f612367b4caa0a851"},"previous_names":["defra/defra-logging-metrics"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fdefra-logging-metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fdefra-logging-metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fdefra-logging-metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fdefra-logging-metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DEFRA","download_url":"https://codeload.github.com/DEFRA/defra-logging-metrics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241469019,"owners_count":19967980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T10:31:14.418Z","updated_at":"2026-06-13T09:32:22.842Z","avatar_url":"https://github.com/DEFRA.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Defra Logging Metrics\n\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.\n\n## Run tests\n\n```shell\nnpm install\n```\n\n```shell\nnpm test\n```\n\n## Use the package from npm\n\n1. Install the package\n\n   ```shell\n   npm install @defra/logging-metrics --save\n   ```\n\n1. Import the module in your script\n\n   ```js\n   import MetricsService from '@defra/logging-metrics'\n   ```\n\n1. Initialize the metrics service\n\n   ```js\n   let metricsService = new MetricsService()\n   ```\n\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:\n\n   ```js\n   let metricsService = new MetricsService(telemetryClient)\n   ```\n\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.:\n   \n   ```js\n   let telemetryClientConfig = {\n      connectionString: \"InstrumentationKey=myKey;IngestionEndpoint=https://applicationinsights.azure.com/;LiveEndpoint=https://livediagnostics.monitor.azure.com/\",\n      samplingPercentage: 100\n   }\n\n   [...]\n\n   let { duration, result } = metricsService.execute(myFunction, metric, telemetryClientConfig)\n   ```\n\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.\n\n 1. Name the metric you want to log with along with any other custom properties to be recorded e.g.:\n\n    ```js\n    let metric = {\n       name: \"timeToCallDatabase\",\n       properties: { source: \"CosmosDB\" }\n    }\n    ```\n\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:\n\n   - If your function does not return any kind of `Promise`:\n\n   ```js\n   let { duration, result } = metricsService.execute(myFunction, metric, config)\n   console.log(`Took ${duration} seconds for ${result}`)\n   ```\n\n   - If your function returns a `Promise`:\n\n   ```js\n   metricsService.executeAsync(asyncFunction, metric, config)\n   .then(({ result, duration }) =\u003e {\n      console.log(`Took ${duration} seconds for ${result}`)\n   })\n   ```\n\n   - If you need to use a `Promise.all()`-like behavior and record each function call:\n\n   ```js\n   metricsService.executeAllFailFastAsync(() =\u003e [asyncFunction1, asyncFunction2], metric, config)\n   .then(({ results, durations }) =\u003e {\n      results.forEach((result, i) =\u003e {\n         console.log(`Took ${durations[i]} seconds for ${result}`)\n      })\n   })\n   ```\n\n   - If you need to use a `Promise.allSettled()`-like behavior and record each function call:\n\n   ```js\n   metricsService.executeAllAsync(() =\u003e [asyncFunction1, asyncFunction2], metric, config)\n   .then(({ results, durations }) =\u003e {\n      results.forEach((result, i) =\u003e {\n         console.log(`Took ${durations[i]} seconds for ${result}`)\n      })\n   })\n   ```\n\n1. As advised in Application Insights documentation:\n   \n   \u003e If your application has a short lifespan, such as a CLI tool, it might be necessary to manually flush your buffered telemetry [...]   \n\n   Flush is supported by calling `metricsService.flushClient()`\n\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\n\n## Full example\n\nA full example can be found [here](sample/README.md).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefra%2Fdefra-logging-metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdefra%2Fdefra-logging-metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefra%2Fdefra-logging-metrics/lists"}