{"id":23020964,"url":"https://github.com/osskit/monitor","last_synced_at":"2025-08-14T09:32:37.474Z","repository":{"id":37032183,"uuid":"388803012","full_name":"osskit/monitor","owner":"osskit","description":"Monitor your services declaratively","archived":false,"fork":false,"pushed_at":"2024-06-16T11:21:06.000Z","size":315,"stargazers_count":19,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-07T02:12:00.953Z","etag":null,"topics":["logging","metrics","monitor","monitoring","prometheus"],"latest_commit_sha":null,"homepage":"","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/osskit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2021-07-23T13:00:57.000Z","updated_at":"2024-08-26T09:05:10.000Z","dependencies_parsed_at":"2022-07-13T09:00:42.918Z","dependency_job_id":"8f530c02-78d0-4ed0-ab94-06b7154c4046","html_url":"https://github.com/osskit/monitor","commit_stats":{"total_commits":85,"total_committers":5,"mean_commits":17.0,"dds":0.5058823529411764,"last_synced_commit":"de283ba5b74a1aa849a1b0f54ac72887ee0ce554"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osskit%2Fmonitor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osskit%2Fmonitor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osskit%2Fmonitor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osskit%2Fmonitor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osskit","download_url":"https://codeload.github.com/osskit/monitor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229815915,"owners_count":18128512,"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":["logging","metrics","monitor","monitoring","prometheus"],"created_at":"2024-12-15T12:15:59.726Z","updated_at":"2024-12-15T12:16:00.441Z","avatar_url":"https://github.com/osskit.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=\"250\" height=\"250\" src=\"https://user-images.githubusercontent.com/15312980/174908438-b6f5eaea-7b81-4008-9cad-8a7c2a45bbaf.svg\"\u003e\n\u003c/p\u003e\n\n\u003cdiv align=\"center\"\u003e\n \n  ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/osskit/monitor/bump) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/osskit/monitor/blob/master/LICENSE.md) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n  \n Utilize a declarative API to wrap your functions to capture Prometheus metrics \u0026 logs for each function\n\u003c/div\u003e\n\n## Install\n```sh\nyarn add @osskit/monitor\n```\n## Usage\n### Scoped\n```ts\nimport { createMonitor } from '@osskit/monitor'\n\nexport const monitor = createMonitor({ scope: 'metrics' });\n\nconst result1 = await monitor('query', async () =\u003e db.query());\nconst result2 = await monitor('update', async () =\u003e db.update());\n\n// Custom labeling\nexport const monitor = createMonitor\u003c['my_label']\u003e({ scope: 'metrics' });\n\nconst customLabels = (id: string) =\u003e await monitor('query', async () =\u003e db.query(id), {context: {key: 'myId' }, labeling: { 'my_label': 'label' } });\n```\n### Unscoped\n```ts\nimport monitor from '@osskit/monitor'\n\nconst result = await monitor('query', async () =\u003e db.query());\n\n// Custom labeling \nconst customLabels = (id: string) =\u003e await monitor('query', async () =\u003e db.query(id), {context: {key: 'myId' }, labeling: { 'my_label': 'label' } });\n```\n### With monitor options\n```ts\nimport { createMonitor } from '@osskit/monitor'\n\nexport const monitor = createMonitor({ scope: 'metrics' });\n\n// Context\nconst result = (id: string) =\u003e await monitor('query', async () =\u003e db.query(id), { context: { id } });\n\n// Parse \u0026 Log Results\nconst logResults = (id: string) =\u003e await monitor('query', async () =\u003e db.query(id), { logResult: true, parseResult: (res) =\u003e res.prop });\n\n// Parse Error\nconst errored = (id: string) =\u003e await monitor('query', async () =\u003e db.query(id), { logResult: true, parseError: (e) =\u003e e.statusCode });\n\n// Log Execution Start\nconst executionStart = (id: string) =\u003e await monitor('query', async () =\u003e db.query(id), { logExecutionStart: true });\n\n```\n\n### With global options\n```ts\nimport { setGlobalOptions, setGlobalContext } from '@osskit/monitor';\nimport  logger from './logger.js';\n\nsetGlobalOptions({\n  context: { globalContextId: 'bla' },\n  logResult: true,\n  logExecutionStart: false,\n  parseError: (res) =\u003e console.log(res),\n  prometheusBuckets: [0.0001, 0.1, 0.5, 10],\n  logger,\n  errorLogLevel: 'fatal'\n});\n\nsetGlobalContext(() =\u003e getDynamicContext());\n```\n\n## API\n### createMonitor({ scope: string, options?: MonitorOptions })\n#### scope\nType: `string`\n\nThe scope of the monitor's metrics\n\nWill be used as the Prometheus metric name\n\nReturns an instance of a function that calls `monitor` - `\u003cT\u003e(method: string, callable: () =\u003e T, options?: MonitorOptions\u003cT\u003e)`\n\n### monitor(method: string, callable: () =\u003e T, options?: MonitorOptions)\n#### method\nType: `string`\n\nWill be used for the `method` label of the metric, or the metric name if no parent scope was declared\n\n### setGlobalOptions({ options: MonitorGlobalOptions })\nSet a number of options that will be used globally for all monitor invocations\n\n### setGlobalContext(value: () =\u003e Record\u003cstring, string\u003e)\nInvoke a function that returns a global context to use in all monitor invocation logs\n\n### Parameters\n\n#### MonitorOptions\n\n|              Parameter              | Description                                                                 |\n|:-----------------------------------:|-----------------------------------------------------------------------------|\n|         `context?: boolean`         | add context that will be logged in all method's logs                        | \n|        `logResult?: boolean`        | log the method's result                                                     | \n|    `logExecutionStart?: boolean`    | log the start of the method's execution `method.start`                      |\n|   `parseResult?: (e: any) =\u003e any`   | transform the method's result that will be returned                         |\n|   `parseError?: (e: any) =\u003e any`    | if the method errored, transform the error that will be thrown              |\n|    `errorLogLevel?: pino.Level`     | if the method errored, which level should the message be, default - `error` |\n| `labeling?: Record\u003cstring, string\u003e` | add custom labeled counters using keys and values                           |  \n\n#### GlobalOptions\n  \n|           Parameter            | Description                                                                 |\n|:------------------------------:|-----------------------------------------------------------------------------|\n|     `logResult?: boolean`      | log the monitored methods results                                           | \n| `logExecutionStart?: boolean`  | log the start of the method's execution `method.start`                      |\n| `parseError?: (e: any) =\u003e any` | if the method errored, transform the error that will be thrown              |\n| `prometheusBuckets?: number[]` | use the following prometheus bucket list for monitor metrics across methods |\n|     `logger?: BaseLogger`      | supply a `pino` `BaseLogger` for monitor to use in logging results          |\n|  `errorLogLevel?: pino.Level`  | if the method errored, which level should the message be, default - `error` |\n\n## License\n[MIT License](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosskit%2Fmonitor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosskit%2Fmonitor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosskit%2Fmonitor/lists"}