{"id":20184850,"url":"https://github.com/azrod/go-prometheus-metrics-builder","last_synced_at":"2025-03-03T06:40:30.646Z","repository":{"id":262523461,"uuid":"887530047","full_name":"azrod/go-prometheus-metrics-builder","owner":"azrod","description":"This library provides a simple way to create Prometheus metrics in Go.","archived":false,"fork":false,"pushed_at":"2024-11-12T21:34:36.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-13T17:48:39.576Z","etag":null,"topics":["golang","golang-library","prometheus","prometheus-client"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azrod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-11-12T21:31:33.000Z","updated_at":"2024-11-13T08:03:05.000Z","dependencies_parsed_at":"2024-11-12T22:39:08.380Z","dependency_job_id":null,"html_url":"https://github.com/azrod/go-prometheus-metrics-builder","commit_stats":null,"previous_names":["azrod/go-prometheus-metrics-builder"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azrod%2Fgo-prometheus-metrics-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azrod%2Fgo-prometheus-metrics-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azrod%2Fgo-prometheus-metrics-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azrod%2Fgo-prometheus-metrics-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azrod","download_url":"https://codeload.github.com/azrod/go-prometheus-metrics-builder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241622594,"owners_count":19992501,"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":["golang","golang-library","prometheus","prometheus-client"],"created_at":"2024-11-14T03:09:29.531Z","updated_at":"2025-03-03T06:40:30.622Z","avatar_url":"https://github.com/azrod.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n    \u003ca href=\"https://github.com/azrod/go-prometheus-metrics-builder/releases/latest\"\u003e\n      \u003cimg alt=\"Latest release\" src=\"https://img.shields.io/github/v/release/azrod/go-prometheus-metrics-builder?style=for-the-badge\u0026logo=starship\u0026color=C9CBFF\u0026logoColor=D9E0EE\u0026labelColor=302D41\u0026include_prerelease\u0026sort=semver\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/azrod/go-prometheus-metrics-builder/pulse\"\u003e\n      \u003cimg alt=\"Last commit\" src=\"https://img.shields.io/github/last-commit/azrod/go-prometheus-metrics-builder?style=for-the-badge\u0026logo=starship\u0026color=8bd5ca\u0026logoColor=D9E0EE\u0026labelColor=302D41\"/\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/azrod/go-prometheus-metrics-builder/blob/main/LICENSE\"\u003e\n      \u003cimg alt=\"License\" src=\"https://img.shields.io/github/license/azrod/go-prometheus-metrics-builder?style=for-the-badge\u0026logo=starship\u0026color=ee999f\u0026logoColor=D9E0EE\u0026labelColor=302D41\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/azrod/go-prometheus-metrics-builder/stargazers\"\u003e\n      \u003cimg alt=\"Stars\" src=\"https://img.shields.io/github/stars/azrod/go-prometheus-metrics-builder?style=for-the-badge\u0026logo=starship\u0026color=c69ff5\u0026logoColor=D9E0EE\u0026labelColor=302D41\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/azrod/go-prometheus-metrics-builder/issues\"\u003e\n      \u003cimg alt=\"Issues\" src=\"https://img.shields.io/github/issues/azrod/go-prometheus-metrics-builder?style=for-the-badge\u0026logo=bilibili\u0026color=F5E0DC\u0026logoColor=D9E0EE\u0026labelColor=302D41\" /\u003e\n    \u003c/a\u003e\n\u003c/div\u003e\n\n# Go Prometheus Metrics Builder\n\n\u003e [!CAUTION]\n\u003e This project is in early development and is not yet ready for production use. You are welcome to try it out and provide feedback, but be aware that the library may change at any time.\n\nThis library provides a simple way to create Prometheus metrics in Go. It is designed to be easy to use and to provide a simple way to create metrics without having to worry about the underlying details of the Prometheus client library.\n\n## Example\n\n```go\npackage main\n\nimport (\n  \"context\"\n\n  pmbuilder \"github.com/azrod/go-prometheus-metrics-builder\"\n  \"github.com/azrod/go-prometheus-metrics-builder/types\"\n)\n\ntype demo struct {\n  pmbuilder.InstanceInterface\n  API struct {\n    DB struct {\n      Get *types.Counter `help:\"Database get counter\"`\n      Set *types.Counter `help:\"Database set counter\"`\n    } `name:\"database\"`\n    Redis struct {\n      Get *types.Counter `help:\"Redis get counter\"`\n      Set *types.Counter `help:\"Redis set counter\"`\n    }\n  }\n}\nfunc main() {\n  metrics := \u0026demo{\n    InstanceInterface: \u0026pmbuilder.DefaultInstance{\n    PrefixMetric: \"myapp\",\n    },\n  }\n\n  pmbuilder.New(metrics)\n\n  go func() {\n    for {\n    metrics.API.Redis.Get.Inc()\n    time.Sleep(1 * time.Second)\n    }\n  }()\n\n  go metrics.ListenAndServe(context.Background(), \":8080\")\n\n  select {}\n}\n```\n\nIn this example, we create a new instance of the `demo` struct, which implements the `InstanceInterface` interface. We then create two counters, `Get` and `Set`, for the `database` and `Redis` APIs. We then create a new `demo` instance and start incrementing the `Get` counter for the `Redis` API every second. Finally, we start the metrics server on port 8080.\n\n```shell\n$ curl localhost:8080/metrics\n[...]\n# HELP myapp_api_database_get Database get counter\n# TYPE myapp_api_database_get counter\nmyapp_api_database_get 2\n# HELP myapp_api_database_set Database set counter\n# TYPE myapp_api_database_set counter\nmyapp_api_database_set 0\n# HELP myapp_api_redis_get Redis get counter\n# TYPE myapp_api_redis_get counter\nmyapp_api_redis_get 0\n# HELP myapp_api_redis_set Redis set counter\n# TYPE myapp_api_redis_set counter\nmyapp_api_redis_set 0\n[...]\n```\n\n## Installation\n\nTo install the library, you can use `go get`:\n\n```shell\ngo get github.com/azrod/go-prometheus-metrics-builder\n```\n\n## Usage\n\nTo use the library, you need to create a struct that implements the `InstanceInterface` interface. pmbuilder provides a default implementation of this interface, `DefaultInstance`, which you can embed in your struct to get the default behavior.\n\n```go\ntype demo struct {\n  pmbuilder.InstanceInterface\n}\n```\n\nYou can then define the metrics you want to create as fields in your struct. The type of the field should be a pointer to one of the metric types provided by the library, such as `Counter`, `Gauge`, `Summary` or `Histogram`.\n\n```go\ntype demo struct {\n  pmbuilder.InstanceInterface\n  API struct {\n    DB struct {\n      Get *types.Counter `help:\"Database get counter\"`\n      Set *types.Counter `help:\"Database set counter\"`\n    } `name:\"database\"`\n    Redis struct {\n      Get *types.Counter `help:\"Redis get counter\"`\n      Set *types.Counter `help:\"Redis set counter\"`\n    }\n  }\n}\n```\n\nYou can then create a new instance of your struct and call the `New` function to create the metrics.\n\n```go\nmetrics := \u0026demo{\n  InstanceInterface: \u0026pmbuilder.DefaultInstance{\n    PrefixMetric: \"myapp\",\n  },\n}\n\npmbuilder.New(metrics)\n```\n\n### Tags in the struct\n\nA following golang tags are available to customize the metrics:\n\n- `name`: The name of the metric. If not provided, the name of the field will be used.\n- `help`: The help text for the metric. **Required.**\n- `labels`: A comma-separated list of labels for the metric. The labels will be added to the metric as tags.\n- `namespace`: The namespace for the metric. Used to group metrics together.\n- `subsystem`: The subsystem for the metric. Used to further categorize metrics within a namespace.\n\nAll tags are optional, except for `help`.\n\nA special tag `name` is available to customize the name of the metric. If not provided, the name of the field will be used. This is useful when you want to use a different name for the metric than the name of the field.\n\n```go\ntype demo struct {\n  pmbuilder.InstanceInterface\n  API struct {\n    DB struct {\n      Get *types.Counter `help:\"Database get counter\" name:\"get_counter\"`\n      Set *types.Counter `help:\"Database set counter\" name:\"set_counter\"`\n    } `name:\"database\"`\n    [...]\n  }\n}\n```\n\nIn this example, we use the `name` tag on the `DB` struct to set the name of the metric to `database`. We also use the `name` tag on the `Get` and `Set` fields to set the name of the metrics to `get_counter` and `set_counter`, respectively.\n\nName generated : `myapp_api_database_get_counter` and `myapp_api_database_set_counter`\n\n### Labels\n\nNot yet implemented.\n\n## License\n\nThis project is licensed under the Apache2 License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazrod%2Fgo-prometheus-metrics-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazrod%2Fgo-prometheus-metrics-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazrod%2Fgo-prometheus-metrics-builder/lists"}