{"id":13461419,"url":"https://github.com/penglongli/gin-metrics","last_synced_at":"2025-05-15T03:02:58.185Z","repository":{"id":36954886,"uuid":"288620318","full_name":"penglongli/gin-metrics","owner":"penglongli","description":"gin-gonic/gin  metrics for prometheus. ","archived":false,"fork":false,"pushed_at":"2025-01-15T03:24:56.000Z","size":2400,"stargazers_count":281,"open_issues_count":11,"forks_count":64,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-21T03:56:04.790Z","etag":null,"topics":["gin","golang","metrics","prometheus-exporter"],"latest_commit_sha":null,"homepage":"","language":"Go","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/penglongli.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":"2020-08-19T03:07:02.000Z","updated_at":"2025-04-14T17:52:00.000Z","dependencies_parsed_at":"2024-01-16T21:52:20.725Z","dependency_job_id":"799af3f1-e0ad-4ab2-a39c-0530eb963663","html_url":"https://github.com/penglongli/gin-metrics","commit_stats":{"total_commits":27,"total_committers":13,"mean_commits":2.076923076923077,"dds":0.6296296296296297,"last_synced_commit":"9ad010ac0e876fec2cad5202e594cf0815a8870d"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/penglongli%2Fgin-metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/penglongli%2Fgin-metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/penglongli%2Fgin-metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/penglongli%2Fgin-metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/penglongli","download_url":"https://codeload.github.com/penglongli/gin-metrics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254264737,"owners_count":22041789,"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":["gin","golang","metrics","prometheus-exporter"],"created_at":"2024-07-31T11:00:38.492Z","updated_at":"2025-05-15T03:02:58.119Z","avatar_url":"https://github.com/penglongli.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# gin-metrics\ngin-gonic/gin metrics exporter for Prometheus.\n\n[中文](README_zh.md)\n\n- [Introduction](#Introduction)\n- [Grafana](#Grafana)\n- [Installation](#Installation)\n- [Usage](#Usage)\n- [Custom Metric](#Custom-Metric)\n- [Metric with separate port](#Metric-with-separate-port)\n- [Contributing](#Contributing)\n\n## Introduction\n\n`gin-metrics` defines some metrics for gin http-server. There have easy way to use it.\n\nBelow is the detailed description for every metric.\n\n| Metric                  | Type      | Description                                         |\n| ----------------------- | --------- | --------------------------------------------------- |\n| gin_request_total       | Counter   | all the server received request num.                |\n| gin_request_uv          | Counter   | all the server received ip num.                     |\n| gin_uri_request_total   | Counter   | all the server received request num with every uri. |\n| gin_request_body_total  | Counter   | the server received request body size, unit byte.   |\n| gin_response_body_total | Counter   | the server send response body size, unit byte.      |\n| gin_request_duration    | Histogram | the time server took to handle the request.         |\n| gin_slow_request_total  | Counter   | the server handled slow requests counter, t=%d.     |\n\n\n## Grafana\n\n\nSet the `grafana` directory for details.\n\n![grafana](./grafana/grafana.png)\n\n\n## Installation\n\n```bash\n$ go get github.com/penglongli/gin-metrics\n```\n\n## Usage\n\nYour can see some metrics across `http://localhost:8080/metrics`\n\n```go\npackage main\n\nimport (\n\t\"github.com/gin-gonic/gin\"\n\n\t\"github.com/penglongli/gin-metrics/ginmetrics\"\n)\n\nfunc main() {\n\tr := gin.Default()\n\n\t// get global Monitor object\n\tm := ginmetrics.GetMonitor()\n\n\t// +optional set metric path, default /debug/metrics\n\tm.SetMetricPath(\"/metrics\")\n\t// +optional set slow time, default 5s\n\tm.SetSlowTime(10)\n\t// +optional set request duration, default {0.1, 0.3, 1.2, 5, 10}\n\t// used to p95, p99\n\tm.SetDuration([]float64{0.1, 0.3, 1.2, 5, 10})\n\n\t// set middleware for gin\n\tm.Use(r)\n\n\tr.GET(\"/product/:id\", func(ctx *gin.Context) {\n\t\tctx.JSON(200, map[string]string{\n\t\t\t\"productId\": ctx.Param(\"id\"),\n\t\t})\n\t})\n\n\t_ = r.Run()\n}\n```\n\n## Custom Metric\n\n`gin-metric` provides ways to custom your own metric.\n\n### Gauge\n\nWith `Gauge` type metric, you can use three functions to change it's value.\n\nAnd you should define a `Gauge` Metric first, \n\n```go\ngaugeMetric := \u0026ginmetrics.Metric{\n    Type:        ginmetrics.Gauge,\n    Name:        \"example_gauge_metric\",\n    Description: \"an example of gauge type metric\",\n    Labels:      []string{\"label1\"},\n}\n\n// Add metric to global monitor object\n_ = ginmetrics.GetMonitor().AddMetric(gaugeMetric)\n```\n\n**SetGaugeValue** \n\n`SetGaugeValue` will setting metric value directly。\n\n```go\n_ = ginmetrics.GetMonitor().GetMetric(\"example_gauge_metric\").SetGaugeValue([]string{\"label_value1\"}, 0.1)\n```\n\n**Inc**\n\n`Inc` will increase 1 to metric value\n\n```go\n_ = ginmetrics.GetMonitor().GetMetric(\"example_gauge_metric\").Inc([]string{\"label_value1\"})\n```\n\n**Add**\n\n`Add` will add float64 num to metric value\n\n```go\n_ = ginmetrics.GetMonitor().GetMetric(\"example_gauge_metric\").Add([]string{\"label_value1\"}, 0.2)\n```\n\n### Counter\n\nWith `Counter` type metric, you can use `Inc` and `Add` function, don't use `SetGaugeValue`.\n\n\n### Histogram and Summary\n\nFor `Histogram` and `Summary` type metric, should use `Observe` function.\n\n## Metric with separate port\n\nFor some users, they don't want to merge the port of the metric with the port of the application.\n\nSo we provide a way to separate the metric port. Here is the example.\n\n```go\nfunc main() {\n\tappRouter := gin.Default()\n\tmetricRouter := gin.Default()\n\n\tm := ginmetrics.GetMonitor()\n\t// use metric middleware without expose metric path\n\tm.UseWithoutExposingEndpoint(appRouter)\n\t// set metric path expose to metric router\n\tm.Expose(metricRouter)\n\n\tappRouter.GET(\"/product/:id\", func(ctx *gin.Context) {\n\t\tctx.JSON(200, map[string]string{\n\t\t\t\"productId\": ctx.Param(\"id\"),\n\t\t})\n\t})\n\tgo func() {\n\t\t_ = metricRouter.Run(\":8081\")\n\t}()\n\t_ = appRouter.Run(\":8080\")\n}\n```\n\n## Contributing\n\nIf someone has a problem or suggestions, you can submit [new issues](https://github.com/penglongli/gin-metrics/issues/new) \nor [new pull requests](https://github.com/penglongli/gin-metrics/pulls). \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpenglongli%2Fgin-metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpenglongli%2Fgin-metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpenglongli%2Fgin-metrics/lists"}