Skip to content

Commit ae8ce7f

Browse files
committed
feat: add the code for Prometheus Logger
1 parent f686200 commit ae8ce7f

File tree

10 files changed

+1316
-1
lines changed

10 files changed

+1316
-1
lines changed

.github/workflows/ci.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
test:
13+
name: Test
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
go-version: ['1.23']
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Set up Go
24+
uses: actions/setup-go@v5
25+
with:
26+
go-version: ${{ matrix.go-version }}
27+
28+
- name: Get dependencies
29+
run: go mod download
30+
31+
- name: Run tests
32+
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
33+
34+
- name: Upload coverage to Codecov
35+
uses: codecov/codecov-action@v4
36+
with:
37+
file: ./coverage.out
38+
flags: unittests
39+
name: codecov-umbrella
40+
41+
build:
42+
name: Build
43+
runs-on: ubuntu-latest
44+
strategy:
45+
matrix:
46+
go-version: ['1.23']
47+
48+
steps:
49+
- name: Checkout code
50+
uses: actions/checkout@v4
51+
52+
- name: Set up Go
53+
uses: actions/setup-go@v5
54+
with:
55+
go-version: ${{ matrix.go-version }}
56+
57+
- name: Build
58+
run: go build -v ./...
59+
60+
release:
61+
name: Release
62+
runs-on: ubuntu-latest
63+
needs: [test, build]
64+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
65+
66+
steps:
67+
- name: Checkout code
68+
uses: actions/checkout@v4
69+
with:
70+
fetch-depth: 0
71+
72+
- name: Set up Node.js
73+
uses: actions/setup-node@v4
74+
with:
75+
node-version: 'lts/*'
76+
77+
- name: Install semantic-release
78+
run: |
79+
npm install -g semantic-release@24 \
80+
@semantic-release/git@10 \
81+
@semantic-release/changelog@6 \
82+
conventional-changelog-conventionalcommits@8
83+
84+
- name: Release
85+
env:
86+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
87+
run: npx semantic-release

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ go.work.sum
3030
# Editor/IDE
3131
# .idea/
3232
# .vscode/
33+
34+
# Example binaries
35+
examples/*/example
36+
examples/*/go.mod
37+
examples/*/go.sum

.releaserc.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"branches": ["master"],
3+
"plugins": [
4+
["@semantic-release/commit-analyzer", {
5+
"preset": "conventionalcommits",
6+
"releaseRules": [
7+
{"type": "feat", "release": "minor"},
8+
{"type": "fix", "release": "patch"},
9+
{"type": "perf", "release": "patch"},
10+
{"type": "revert", "release": "patch"},
11+
{"type": "docs", "release": "patch"},
12+
{"type": "chore", "release": false},
13+
{"type": "refactor", "release": "patch"},
14+
{"type": "test", "release": false},
15+
{"type": "build", "release": false},
16+
{"type": "ci", "release": false}
17+
]
18+
}],
19+
["@semantic-release/release-notes-generator", {
20+
"preset": "conventionalcommits",
21+
"presetConfig": {
22+
"types": [
23+
{"type": "feat", "section": "Features"},
24+
{"type": "fix", "section": "Bug Fixes"},
25+
{"type": "perf", "section": "Performance Improvements"},
26+
{"type": "revert", "section": "Reverts"},
27+
{"type": "docs", "section": "Documentation"},
28+
{"type": "refactor", "section": "Code Refactoring"},
29+
{"type": "test", "section": "Tests", "hidden": true},
30+
{"type": "build", "section": "Build System", "hidden": true},
31+
{"type": "ci", "section": "CI", "hidden": true}
32+
]
33+
}
34+
}],
35+
["@semantic-release/changelog", {
36+
"changelogFile": "CHANGELOG.md"
37+
}],
38+
["@semantic-release/github"],
39+
["@semantic-release/git", {
40+
"assets": ["CHANGELOG.md"],
41+
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
42+
}]
43+
]
44+
}

README.md

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,125 @@
1-
# casbin-prometheus-logger
1+
# casbin-prometheus-logger
2+
3+
[![Go Report Card](https://goreportcard.com/badge/github.com/casbin/casbin-prometheus-logger)](https://goreportcard.com/report/github.com/casbin/casbin-prometheus-logger)
4+
[![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)
5+
[![Coverage Status](https://codecov.io/gh/casbin/casbin-prometheus-logger/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin/casbin-prometheus-logger)
6+
[![GoDoc](https://godoc.org/github.com/casbin/casbin-prometheus-logger?status.svg)](https://godoc.org/github.com/casbin/casbin-prometheus-logger)
7+
[![Release](https://img.shields.io/github/release/casbin/casbin-prometheus-logger.svg)](https://github.com/casbin/casbin-prometheus-logger/releases/latest)
8+
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](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

Comments
 (0)