|
1 | | -# casbin-prometheus-logger |
| 1 | +# casbin-prometheus-logger |
| 2 | + |
| 3 | +[](https://goreportcard.com/report/github.com/casbin/casbin-prometheus-logger) |
| 4 | +[](https://github.com/casbin/casbin-prometheus-logger/actions/workflows/ci.yml) |
| 5 | +[](https://codecov.io/gh/casbin/casbin-prometheus-logger) |
| 6 | +[](https://godoc.org/github.com/casbin/casbin-prometheus-logger) |
| 7 | +[](https://github.com/casbin/casbin-prometheus-logger/releases/latest) |
| 8 | +[](https://discord.gg/S5UjpzGZjN) |
| 9 | + |
| 10 | +A Prometheus logger implementation for [Casbin](https://github.com/casbin/casbin), providing event-driven metrics collection for authorization events. |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- **Event-Driven Logging**: Implements the Casbin Logger interface with support for event-driven logging |
| 15 | +- **Prometheus Metrics**: Exports comprehensive metrics for Casbin operations |
| 16 | +- **Customizable Event Types**: Filter which event types to log |
| 17 | +- **Custom Callbacks**: Add custom processing for log entries |
| 18 | +- **Multiple Registries**: Support for both default and custom Prometheus registries |
| 19 | + |
| 20 | +## Metrics Exported |
| 21 | + |
| 22 | +### Enforce Metrics |
| 23 | +- `casbin_enforce_total` - Total number of enforce requests (labeled by `allowed`, `domain`) |
| 24 | +- `casbin_enforce_duration_seconds` - Duration of enforce requests (labeled by `allowed`, `domain`) |
| 25 | + |
| 26 | +### Policy Operation Metrics |
| 27 | +- `casbin_policy_operations_total` - Total number of policy operations (labeled by `operation`, `success`) |
| 28 | +- `casbin_policy_operations_duration_seconds` - Duration of policy operations (labeled by `operation`) |
| 29 | +- `casbin_policy_rules_count` - Number of policy rules affected by operations (labeled by `operation`) |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +```bash |
| 34 | +go get github.com/casbin/casbin-prometheus-logger |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### Basic Usage |
| 40 | + |
| 41 | +```go |
| 42 | +package main |
| 43 | + |
| 44 | +import ( |
| 45 | + "net/http" |
| 46 | + |
| 47 | + prometheuslogger "github.com/casbin/casbin-prometheus-logger" |
| 48 | + "github.com/prometheus/client_golang/prometheus" |
| 49 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 50 | +) |
| 51 | + |
| 52 | +func main() { |
| 53 | + // Create logger with default Prometheus registry |
| 54 | + logger := prometheuslogger.NewPrometheusLogger() |
| 55 | + defer logger.Unregister() |
| 56 | + |
| 57 | + // Or create with custom registry |
| 58 | + registry := prometheus.NewRegistry() |
| 59 | + logger := prometheuslogger.NewPrometheusLoggerWithRegistry(registry) |
| 60 | + defer logger.UnregisterFrom(registry) |
| 61 | + |
| 62 | + // Use with Casbin |
| 63 | + // enforcer.SetLogger(logger) |
| 64 | + |
| 65 | + // Expose metrics endpoint |
| 66 | + http.Handle("/metrics", promhttp.Handler()) |
| 67 | + http.ListenAndServe(":8080", nil) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Configure Event Types |
| 72 | + |
| 73 | +```go |
| 74 | +// Only log specific event types |
| 75 | +logger.SetEventTypes([]prometheuslogger.EventType{ |
| 76 | + prometheuslogger.EventEnforce, |
| 77 | + prometheuslogger.EventAddPolicy, |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +### Add Custom Callback |
| 82 | + |
| 83 | +```go |
| 84 | +// Add custom processing for log entries |
| 85 | +logger.SetLogCallback(func(entry *prometheuslogger.LogEntry) error { |
| 86 | + fmt.Printf("Event: %s, Duration: %v\n", entry.EventType, entry.Duration) |
| 87 | + return nil |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +## Event Types |
| 92 | + |
| 93 | +The logger supports the following event types: |
| 94 | + |
| 95 | +- `EventEnforce` - Authorization enforcement requests |
| 96 | +- `EventAddPolicy` - Policy addition operations |
| 97 | +- `EventRemovePolicy` - Policy removal operations |
| 98 | +- `EventLoadPolicy` - Policy loading operations |
| 99 | +- `EventSavePolicy` - Policy saving operations |
| 100 | + |
| 101 | +## Example |
| 102 | + |
| 103 | +See the [examples/basic](examples/basic/main.go) directory for a complete working example. |
| 104 | + |
| 105 | +To run the example: |
| 106 | + |
| 107 | +```bash |
| 108 | +cd examples/basic |
| 109 | +go run main.go |
| 110 | +``` |
| 111 | + |
| 112 | +Then visit http://localhost:8080/metrics to see the exported metrics. |
| 113 | + |
| 114 | +## License |
| 115 | + |
| 116 | +This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details. |
| 117 | + |
| 118 | +## Contributing |
| 119 | + |
| 120 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 121 | + |
| 122 | +## Related Projects |
| 123 | + |
| 124 | +- [Casbin](https://github.com/casbin/casbin) - An authorization library that supports access control models |
| 125 | +- [Prometheus](https://prometheus.io/) - Monitoring system and time series database |
0 commit comments