Skip to content

Add policy state metrics and configurable enforce metric labels#11

Open
Copilot wants to merge 4 commits intomasterfrom
copilot/add-policy-state-metrics
Open

Add policy state metrics and configurable enforce metric labels#11
Copilot wants to merge 4 commits intomasterfrom
copilot/add-policy-state-metrics

Conversation

Copy link
Contributor

Copilot AI commented Jan 7, 2026

The logger lacked visibility into current policy state and provided insufficient granularity for debugging authorization patterns. No metric tracked loaded policy counts by type, and enforce metrics only exposed allowed and domain labels.

Changes

Policy State Tracking

  • New casbin_policy_state_count gauge metric labeled by ptype
  • UpdatePolicyState(ptype, count) method to maintain current counts
  • Supports arbitrary policy types: p, p2, g, g1, g2, g3, etc.

Configurable Enforce Labels

  • New PrometheusLoggerOptions struct with EnforceLabels field
  • Three optional label constants: EnforceLabelSubject, EnforceLabelObject, EnforceLabelAction
  • NewPrometheusLoggerWithOptions(registry, options) constructor
  • Dynamic label value construction in recordEnforceMetrics()

Usage

// Track policy state
logger.UpdatePolicyState("p", 100)
logger.UpdatePolicyState("g", 50)

// Enable fine-grained enforce metrics
options := &prometheuslogger.PrometheusLoggerOptions{
    EnforceLabels: []string{
        prometheuslogger.EnforceLabelSubject,
        prometheuslogger.EnforceLabelObject,
        prometheuslogger.EnforceLabelAction,
    },
}
logger := prometheuslogger.NewPrometheusLoggerWithOptions(registry, options)

Fully backward compatible. Existing constructors use default labels (allowed, domain only).

Original prompt

This section details on the original issue you should resolve

<issue_title>[Feature] Additional Policy State Metrics & Configurable Enforce Metric Granularity</issue_title>
<issue_description>This proposal adds two observability improvements to casbin-prometheus-logger:

  1. Expose current policy counts by type ( p, g, g1, g2 , g3 )
  2. Allow optional labels for enforce metrics

Currently, the prometheusLogger focuses on policy operation events (Add / Remove / Load / Save), but lacks visibility into policy state. There is no metric showing how many policy rules are loaded by type, making it hard to monitor permission growth.

In addition, the Enforce metrics are too coarse. casbin_enforce_total only includes allowed and domain (prometheus_logger.go)

enforceTotal: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Name: "casbin_enforce_total",
				Help: "Total number of enforce requests",
			},
			[]string{"allowed", "domain"},
		),

This makes it difficult to identify high-traffic subjects or debug subject-specific authorization issues.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@hsluoyz hsluoyz marked this pull request as ready for review January 7, 2026 09:13
Copilot AI and others added 3 commits January 7, 2026 09:18
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
…fault case

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI changed the title [WIP] Add additional policy state metrics and configurable granularity Add policy state metrics and configurable enforce metric labels Jan 7, 2026
Copilot AI requested a review from hsluoyz January 7, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Additional Policy State Metrics & Configurable Enforce Metric Granularity

3 participants