Skip to content

casbin/casbin-prometheus-logger

Repository files navigation

casbin-prometheus-logger

Go Report Card Go Coverage Status GoDoc Release Discord

A Prometheus logger implementation for Casbin, providing event-driven metrics collection for authorization events.

Features

  • Event-Driven Logging: Implements the Casbin Logger interface with support for event-driven logging
  • Prometheus Metrics: Exports comprehensive metrics for Casbin operations
  • Customizable Event Types: Filter which event types to log
  • Custom Callbacks: Add custom processing for log entries
  • Multiple Registries: Support for both default and custom Prometheus registries

Metrics Exported

Enforce Metrics

  • casbin_enforce_total - Total number of enforce requests (labeled by allowed, domain)
  • casbin_enforce_duration_seconds - Duration of enforce requests (labeled by allowed, domain)

Policy Operation Metrics

  • 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)

Installation

go get github.com/casbin/casbin-prometheus-logger

Usage

Basic Usage

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)
}

Configure Event Types

// Only log specific event types
logger.SetEventTypes([]prometheuslogger.EventType{
    prometheuslogger.EventEnforce,
    prometheuslogger.EventAddPolicy,
})

Add Custom Callback

// 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
})

Event Types

The logger supports the following event types:

  • EventEnforce - Authorization enforcement requests
  • EventAddPolicy - Policy addition operations
  • EventRemovePolicy - Policy removal operations
  • EventLoadPolicy - Policy loading operations
  • EventSavePolicy - Policy saving operations

Example

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.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Related Projects

  • Casbin - An authorization library that supports access control models
  • Prometheus - Monitoring system and time series database

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages