{"id":36593101,"url":"https://github.com/casbin/casbin-prometheus-logger","last_synced_at":"2026-01-15T03:14:15.409Z","repository":{"id":332049563,"uuid":"1127316062","full_name":"casbin/casbin-prometheus-logger","owner":"casbin","description":"Casbin Prometheus logger","archived":false,"fork":false,"pushed_at":"2026-01-09T11:48:57.000Z","size":7397,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-12T16:31:23.670Z","etag":null,"topics":["abac","acl","auth","authorization","authz","casbin","log","logger","logging","prometheus","rbac"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/casbin","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/casbin.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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"casbin"}},"created_at":"2026-01-03T16:25:35.000Z","updated_at":"2026-01-09T11:47:51.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/casbin/casbin-prometheus-logger","commit_stats":null,"previous_names":["casbin/casbin-prometheus-logger"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/casbin/casbin-prometheus-logger","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-prometheus-logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-prometheus-logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-prometheus-logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-prometheus-logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin","download_url":"https://codeload.github.com/casbin/casbin-prometheus-logger/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-prometheus-logger/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28442248,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-15T00:55:22.719Z","status":"online","status_checked_at":"2026-01-15T02:00:08.019Z","response_time":62,"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":["abac","acl","auth","authorization","authz","casbin","log","logger","logging","prometheus","rbac"],"created_at":"2026-01-12T08:21:07.849Z","updated_at":"2026-01-15T03:14:15.403Z","avatar_url":"https://github.com/casbin.png","language":"Go","funding_links":["https://github.com/sponsors/casbin"],"categories":[],"sub_categories":[],"readme":"# casbin-prometheus-logger\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/casbin/casbin-prometheus-logger)](https://goreportcard.com/report/github.com/casbin/casbin-prometheus-logger)\n[![Go](https://github.com/casbin/casbin-prometheus-logger/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin/casbin-prometheus-logger/actions/workflows/ci.yml)\n[![Coverage Status](https://codecov.io/gh/casbin/casbin-prometheus-logger/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin/casbin-prometheus-logger)\n[![GoDoc](https://godoc.org/github.com/casbin/casbin-prometheus-logger?status.svg)](https://godoc.org/github.com/casbin/casbin-prometheus-logger)\n[![Release](https://img.shields.io/github/release/casbin/casbin-prometheus-logger.svg)](https://github.com/casbin/casbin-prometheus-logger/releases/latest)\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord\u0026label=discord\u0026color=5865F2)](https://discord.gg/S5UjpzGZjN)\n\nA Prometheus logger implementation for [Casbin](https://github.com/casbin/casbin), providing event-driven metrics collection for authorization events.\n\n![grafana](grafana.jpg)\n\n## Features\n\n- **Event-Driven Logging**: Implements the Casbin Logger interface with support for event-driven logging\n- **Prometheus Metrics**: Exports comprehensive metrics for Casbin operations\n- **Customizable Event Types**: Filter which event types to log\n- **Custom Callbacks**: Add custom processing for log entries\n- **Multiple Registries**: Support for both default and custom Prometheus registries\n\n## Metrics Exported\n\n### Enforce Metrics\n- `casbin_enforce_total` - Total number of enforce requests (labeled by `allowed`, `domain`)\n- `casbin_enforce_duration_seconds` - Duration of enforce requests (labeled by `allowed`, `domain`)\n\n### Policy Operation Metrics\n- `casbin_policy_operations_total` - Total number of policy operations (labeled by `operation`, `success`)\n- `casbin_policy_operations_duration_seconds` - Duration of policy operations (labeled by `operation`)\n- `casbin_policy_rules_count` - Number of policy rules affected by operations (labeled by `operation`)\n\n## Installation\n\n```bash\ngo get github.com/casbin/casbin-prometheus-logger\n```\n\n## Usage\n\n### Basic Usage\n\n```go\npackage main\n\nimport (\n    \"net/http\"\n    \n    prometheuslogger \"github.com/casbin/casbin-prometheus-logger\"\n    \"github.com/prometheus/client_golang/prometheus\"\n    \"github.com/prometheus/client_golang/prometheus/promhttp\"\n)\n\nfunc main() {\n    // Create logger with default Prometheus registry\n    logger := prometheuslogger.NewPrometheusLogger()\n    defer logger.Unregister()\n    \n    // Or create with custom registry\n    registry := prometheus.NewRegistry()\n    logger := prometheuslogger.NewPrometheusLoggerWithRegistry(registry)\n    defer logger.UnregisterFrom(registry)\n    \n    // Use with Casbin\n    // enforcer.SetLogger(logger)\n    \n    // Expose metrics endpoint\n    http.Handle(\"/metrics\", promhttp.Handler())\n    http.ListenAndServe(\":8080\", nil)\n}\n```\n\n### Configure Event Types\n\n```go\n// Only log specific event types\nlogger.SetEventTypes([]prometheuslogger.EventType{\n    prometheuslogger.EventEnforce,\n    prometheuslogger.EventAddPolicy,\n})\n```\n\n### Add Custom Callback\n\n```go\n// Add custom processing for log entries\nlogger.SetLogCallback(func(entry *prometheuslogger.LogEntry) error {\n    fmt.Printf(\"Event: %s, Duration: %v\\n\", entry.EventType, entry.Duration)\n    return nil\n})\n```\n\n## Event Types\n\nThe logger supports the following event types:\n\n- `EventEnforce` - Authorization enforcement requests\n- `EventAddPolicy` - Policy addition operations\n- `EventRemovePolicy` - Policy removal operations\n- `EventLoadPolicy` - Policy loading operations\n- `EventSavePolicy` - Policy saving operations\n\n## Prometheus + Grafana Setup\n\nThis section guides you through setting up Prometheus and Grafana to visualize Casbin metrics.\n\n### 1. Install and Configure Prometheus\n\n1. **Install Prometheus**: Follow the official guide at https://prometheus.io/docs/introduction/first_steps/\n\n2. **Configure Prometheus** to scrape metrics from your application. Edit your `prometheus.yml` configuration file and add a new job under `scrape_configs`:\n\n```yaml\nscrape_configs:\n  - job_name: \"casbin-prometheus-logger\"\n    static_configs:\n      - targets: [\"localhost:8080\"]\n```\n\nReplace `localhost:8080` with the actual address where your application exposes the `/metrics` endpoint.\n\n3. **Start Prometheus** and verify it's scraping metrics by visiting `http://localhost:9090/targets` in your browser.\n\n### 2. Install Grafana\n\nFollow the official Grafana installation guide at https://grafana.com/docs/grafana/latest/setup-grafana/installation/ for your platform. After installation, access Grafana via your browser (default: `http://localhost:3000`) and log in with the default credentials (admin/admin).\n\n### 3. Configure Grafana Data Source\n\n1. In Grafana, navigate to **Connections** → **Data Sources**\n2. Click **Add data source**\n3. Select **Prometheus**\n4. Set the URL to your Prometheus endpoint (e.g., `http://localhost:9090`)\n5. Click **Save \u0026 Test** to verify the connection\n\n### 4. Import Casbin Dashboard\n\nA pre-built Grafana dashboard is available for visualizing Casbin metrics. You can import the dashboard JSON file from [grafana-dashboard.json](grafana-dashboard.json).\n\n**To import the dashboard:**\n\n1. In Grafana, go to **Dashboards** → **Import**\n2. Upload the `grafana-dashboard.json` file or paste its contents\n3. Select the Prometheus data source you configured in the previous step\n4. Click **Import**\n\n### Dashboard Panels\n\nThe dashboard includes the following panels organized into two sections:\n\n#### Enforce Metrics\n- **Total Enforce Rate** - Overall rate of enforce requests per second\n- **Enforce Rate Detail (History)** - Historical view broken down by allowed/denied status and domain\n- **Enforce Duration (Latency Distribution)** - Histogram showing p50, p90, p95, and p99 latencies\n- **Enforce Duration by Status and Domain** - Average duration broken down by status and domain\n\n#### Policy Metrics\n- **Policy Operation Rate** - Current rate of policy operations (Add/Save/Load/Remove)\n- **Policy Operations (Success/Failure)** - Pie chart showing success vs failure distribution\n- **Policy Operation Rate History** - Historical view of policy operations activity\n- **Policy Rules Affected History** - Trend of the number of policy rules affected over time\n- **Policy Operation Duration (Latency Distribution)** - Histogram showing p50, p90, and p99 latencies for policy operations\n- **Policy Operation Average Duration** - Average duration by operation type\n\n## Example\n\nSee the [examples/basic](examples/basic/main.go) directory for a complete working example.\n\nTo run the example:\n\n```bash\ncd examples/basic\ngo run main.go\n```\n\nThen visit http://localhost:8080/metrics to see the exported metrics.\n\n## Long-Running Test\n\nA long-running test simulates RBAC, ABAC, and ReBAC authorization patterns for testing with Prometheus and Grafana.\n\n**Note**: This test is excluded from normal CI runs to avoid timeouts. It must be run manually with the `longrunning` build tag.\n\n### Usage\n\n```bash\ngo test -v -tags longrunning -run TestLongRunning -timeout 0\n```\n\nThe test generates ~50-150 requests/second and exposes metrics on `http://localhost:8080/metrics`. Press Ctrl+C to stop.\n\n### Integration with Prometheus/Grafana\n\n1. Start the test\n2. Configure Prometheus to scrape `localhost:8080/metrics` \n3. Import the Grafana dashboard from `grafana-dashboard.json`\n\n## License\n\nThis project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## Related Projects\n\n- [Casbin](https://github.com/casbin/casbin) - An authorization library that supports access control models\n- [Prometheus](https://prometheus.io/) - Monitoring system and time series database\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fcasbin-prometheus-logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin%2Fcasbin-prometheus-logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fcasbin-prometheus-logger/lists"}