A Prometheus logger implementation for Casbin, providing event-driven metrics collection for authorization events.
casbin_enforce_total - Total number of enforce requests (labeled by allowed, domain)casbin_enforce_duration_seconds - Duration of enforce requests (labeled by allowed, domain)casbin_policy_operations_total - Total number of policy operations (labeled by operation, success)casbin_policy_operations_duration_seconds - Duration of policy operations (labeled by operation)casbin_policy_rules_count - Number of policy rules affected by operations (labeled by operation)go get github.com/casbin/casbin-prometheus-logger
package main import ( "net/http" prometheuslogger "github.com/casbin/casbin-prometheus-logger" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { // Create logger with default Prometheus registry logger := prometheuslogger.NewPrometheusLogger() defer logger.Unregister() // Or create with custom registry registry := prometheus.NewRegistry() logger := prometheuslogger.NewPrometheusLoggerWithRegistry(registry) defer logger.UnregisterFrom(registry) // Use with Casbin // enforcer.SetLogger(logger) // Expose metrics endpoint http.Handle("/metrics", promhttp.Handler()) http.ListenAndServe(":8080", nil) }
// Only log specific event types logger.SetEventTypes([]prometheuslogger.EventType{ prometheuslogger.EventEnforce, prometheuslogger.EventAddPolicy, })
// Add custom processing for log entries logger.SetLogCallback(func(entry *prometheuslogger.LogEntry) error { fmt.Printf("Event: %s, Duration: %v\n", entry.EventType, entry.Duration) return nil })
The logger supports the following event types:
EventEnforce - Authorization enforcement requestsEventAddPolicy - Policy addition operationsEventRemovePolicy - Policy removal operationsEventLoadPolicy - Policy loading operationsEventSavePolicy - Policy saving operationsThis section guides you through setting up Prometheus and Grafana to visualize Casbin metrics.
Install Prometheus: Follow the official guide at https://prometheus.io/docs/introduction/first_steps/
Configure Prometheus to scrape metrics from your application. Edit your prometheus.yml configuration file and add a new job under scrape_configs:
scrape_configs: - job_name: "casbin-prometheus-logger" static_configs: - targets: ["localhost:8080"]
Replace localhost:8080 with the actual address where your application exposes the /metrics endpoint.
http://localhost:9090/targets in your browser.Follow 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).
http://localhost:9090)A pre-built Grafana dashboard is available for visualizing Casbin metrics. You can import the dashboard JSON file from grafana-dashboard.json.
To import the dashboard:
grafana-dashboard.json file or paste its contentsThe dashboard includes the following panels organized into two sections:
See the examples/basic directory for a complete working example.
To run the example:
cd examples/basic go run main.go
Then visit http://localhost:8080/metrics to see the exported metrics.
A long-running test simulates RBAC, ABAC, and ReBAC authorization patterns for testing with Prometheus and Grafana.
Note: This test is excluded from normal CI runs to avoid timeouts. It must be run manually with the longrunning build tag.
go test -v -tags longrunning -run TestLongRunning -timeout 0
The test generates ~50-150 requests/second and exposes metrics on http://localhost:8080/metrics. Press Ctrl+C to stop.
localhost:8080/metricsgrafana-dashboard.jsonThis project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.