{"id":23086801,"url":"https://github.com/papirosko/application-metrics","last_synced_at":"2026-06-09T19:31:49.885Z","repository":{"id":57182868,"uuid":"432991703","full_name":"papirosko/application-metrics","owner":"papirosko","description":null,"archived":false,"fork":false,"pushed_at":"2025-08-21T15:45:25.000Z","size":638,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-02T07:01:41.734Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/papirosko.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,"zenodo":null}},"created_at":"2021-11-29T10:07:41.000Z","updated_at":"2025-08-21T15:45:28.000Z","dependencies_parsed_at":"2022-09-11T22:40:30.391Z","dependency_job_id":"94c2e284-ca03-4e91-b90e-be7b7a5c2f9d","html_url":"https://github.com/papirosko/application-metrics","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"8339afd49213863e6d35b6c9c09a2fc9c8bb5970"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/papirosko/application-metrics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/papirosko%2Fapplication-metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/papirosko%2Fapplication-metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/papirosko%2Fapplication-metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/papirosko%2Fapplication-metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/papirosko","download_url":"https://codeload.github.com/papirosko/application-metrics/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/papirosko%2Fapplication-metrics/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34123171,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-12-16T19:31:10.844Z","updated_at":"2026-06-09T19:31:49.855Z","avatar_url":"https://github.com/papirosko.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"A set of metrics used for analyzing application state, based on [prometheus](https://prometheus.io/docs/concepts/metric_types/) metrics.\n\nUsage\n=====\n```\nnpm i application-metrics\n```\n\nCounter\n=======\n```typescript\nimport {MetricsService} from 'application-metrics';\n\n\nfor (let i = 0; i \u003c 100; i++) {\n    MetricsService.counter('items_processed').inc();\n}\n```\n\nLabels\n======\n```typescript\nimport {MetricsService} from 'application-metrics';\n\nconst conf = {};// read conf  \nMetricsService.label('conf_value', conf.someValue)\n```\n\nTimers\n======\n```typescript\nimport {MetricsService} from 'application-metrics';\n\nfunction process(): void {\n    for (let i = 0; i \u003c 100; i++) {\n        // do some stuff\n    }\n}\n\nasync function processAsync(): Promise\u003cvoid\u003e {\n    for (let i = 0; i \u003c 100; i++) {\n        // do some stuff\n    }\n}\n\nMetricsService.timer('process_ms').time(() =\u003e process());\nMetricsService.timer('process_async_ms').time(() =\u003e processAsync());\n```\n\nor use `@Metric()` to observe the method:\n\n```typescript\nimport {MetricsService} from 'application-metrics';\n\nclass EntriesDao {\n    \n    @Metric('saveEntries')\n    async saveEntries(entries: object[]): Promise\u003cvoid\u003e {\n        // ...\n    }\n}\n```\n\nMetric name can be skipped, the method name will be used instead:\n```typescript\nimport {MetricsService} from 'application-metrics';\n\nclass EntriesDao {\n    \n    @Metric()\n    async saveEntries(entries: object[]): Promise\u003cvoid\u003e {\n        // ...\n    }\n}\n```\n\nYou can also pass parameters to the underlying summary object. See [prom-client summary documentation](https://github.com/siimon/prom-client?tab=readme-ov-file#configuration-2).\n```typescript\nimport {MetricsService} from 'application-metrics';\n\nclass EntriesDao {\n    \n    @Metric({maxAgeSeconds: 300, ageBuckets: 5, pruneAgedBuckets: true})\n    async saveEntries(entries: object[]): Promise\u003cvoid\u003e {\n        // ...\n    }\n}\n```\n\n\nGauges\n======\n```typescript\nimport {MetricsService} from 'application-metrics';\n\nMetricsService.gauge('memory_external', () =\u003e process.memoryUsage().external);\nMetricsService.gauge('memory_rss', () =\u003e process.memoryUsage().rss);\nMetricsService.gauge('memory_heapTotal', () =\u003e process.memoryUsage().heapTotal);\nMetricsService.gauge('memory_heapUsed', () =\u003e process.memoryUsage().heapUsed);\n```\n\nHistograms\n======\n\n```typescript\nimport {MetricsService} from 'application-metrics';\nimport * as fs from 'fs';\n\nconst files = fs.readdirSync('.');\nfiles.forEach(f =\u003e {\n    MetricsService.histogram('file_size_bytes').observe(fs.statSync(f).size);\n});\n\nMetricsService.toConsole().then(msg =\u003e {\n    console.log(msg);\n});\n```\n\noutputs something like \n```\n****** METRICS ******\nHistograms:       \n  file_size_bytes:  {\"50\":543,\"90\":11328,\"95\":300966,\"99\":397512,\"count\":15}\n```\n\n\n\nOutput\n======\nYou can periodically print metrics to console:\n```typescript\nimport {MetricsService} from 'application-metrics';\n\nMetricsService.gauge('memory_external', () =\u003e process.memoryUsage().external);\nMetricsService.gauge('memory_rss', () =\u003e process.memoryUsage().rss);\nMetricsService.gauge('memory_heapTotal', () =\u003e process.memoryUsage().heapTotal);\nMetricsService.gauge('memory_heapUsed', () =\u003e process.memoryUsage().heapUsed);\n\n\nsetInterval(async () =\u003e {\n    console.log(await MetricsService.toConsole());\n}, 60000);\n```\nwill produce something like:\n```\n ****** METRICS ******\nGauges:                                                   \n  memory_external:                           104,763,523\n  memory_rss:                                440,160,256\n  memory_heapTotal:                          255,873,024\n  memory_heapUsed:                           229,729,176\n```\n\nAlso, you can have an endpoint to show metrics as json or in prometheus format (example uses [NestJS](https://nestjs.com/)):\n\n```typescript\nimport {MetricsService} from 'application-metrics';\nimport {Controller, Get, Header} from '@nestjs/common';\nimport {\n    ApiOkResponse,\n    ApiOperation,\n    ApiProduces,\n    ApiTags,\n} from '@nestjs/swagger';\n\n@Controller()\n@ApiTags('metrics')\nexport class MetricsController {\n    \n    @ApiOperation({\n        description: 'Get metrics in json format',\n        summary: 'Get metrics in json format',\n    })\n    @ApiOkResponse({description: 'Success', type: Object})\n    @Get('metrics')\n    @Header('Content-Type', 'application/json')\n    async getMetrics(): Promise\u003cstring\u003e {\n        return JSON.stringify(await MetricsService.toJson(), null, 4);\n    }\n\n    \n    @ApiOperation({\n        description: 'prometheus metrics export endpoint',\n        summary: 'Get metrics in prometheus format',\n    })\n    @ApiOkResponse({description: 'Success', type: String})\n    @ApiProduces('text/plain')\n    @Get('prometheusmetrics')\n    @Header('Content-Type', 'text/plain')\n    async getPrometheusMetrics(): Promise\u003cstring\u003e {\n        return MetricsService.toPrometheus();\n    }\n}\n```\n```shell\ncurl localhost:3000/metrics\n```\n```\n{\n    \"gauges\": {\n        \"memory_external\": 17936815,\n        \"memory_rss\": 291016704,\n        \"memory_heapTotal\": 202420224,\n        \"memory_heapUsed\": 180871624\n    }\n}\n```\n\n\n\n```shell\ncurl localhost:3000/prometheusmetrics\n```\n```\n# HELP gauge_memory_external gauge_memory_external\n# TYPE gauge_memory_external gauge\nmemory_external 85052806\n\n# HELP gauge_memory_rss gauge_memory_rss\n# TYPE gauge_memory_rss gauge\nmemory_rss 401350656\n\n# HELP gauge_memory_heapTotal gauge_memory_heapTotal\n# TYPE gauge_memory_heapTotal gauge\nmemory_heapTotal 224940032\n\n# HELP gauge_memory_heapUsed gauge_memory_heapUsed\n# TYPE gauge_memory_heapUsed gauge\nmemory_heapUsed 185016864\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpapirosko%2Fapplication-metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpapirosko%2Fapplication-metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpapirosko%2Fapplication-metrics/lists"}