-
Notifications
You must be signed in to change notification settings - Fork 762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add status metric to config #3656
Open
abhipatnala
wants to merge
2
commits into
open-policy-agent:master
Choose a base branch
from
abhipatnala:add_status_metric_to_config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
const ( | ||
cfgMetricName = "config" | ||
statusKey = "status" | ||
) | ||
|
||
func (r *reporter) observeConfig(_ context.Context, observer metric.Int64Observer) error { | ||
r.mux.RLock() | ||
defer r.mux.RUnlock() | ||
for t, v := range r.configReport { | ||
observer.Observe(v, metric.WithAttributes(attribute.String(statusKey, string(t)))) | ||
} | ||
return nil | ||
} | ||
|
||
func (r *reporter) reportConfig(_ context.Context, t metrics.Status, v int64) error { | ||
r.mux.Lock() | ||
defer r.mux.Unlock() | ||
if r.configReport == nil { | ||
r.configReport = make(map[metrics.Status]int64) | ||
} | ||
r.configReport[t] = v | ||
return nil | ||
} | ||
|
||
// newStatsReporter creates a reporter for audit metrics. | ||
func newStatsReporter() (*reporter, error) { | ||
r := &reporter{} | ||
var err error | ||
meter := otel.GetMeterProvider().Meter("gatekeeper") | ||
_, err = meter.Int64ObservableGauge( | ||
cfgMetricName, | ||
metric.WithDescription("Config Status"), metric.WithInt64Callback(r.observeConfig)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
type reporter struct { | ||
mux sync.RWMutex | ||
configReport map[metrics.Status]int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics" | ||
testmetric "github.com/open-policy-agent/gatekeeper/v3/test/metrics" | ||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
"go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" | ||
) | ||
|
||
func initializeTestInstruments(t *testing.T) (rdr *sdkmetric.PeriodicReader, r *reporter) { | ||
var err error | ||
rdr = sdkmetric.NewPeriodicReader(new(testmetric.FnExporter)) | ||
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(rdr)) | ||
r, err = newStatsReporter() | ||
assert.NoError(t, err) | ||
meter := mp.Meter("test") | ||
|
||
// Ensure the pipeline has a callback setup | ||
_, err = meter.Int64ObservableGauge(cfgMetricName, metric.WithInt64Callback(r.observeConfig)) | ||
assert.NoError(t, err) | ||
return rdr, r | ||
} | ||
|
||
func TestReportConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx context.Context | ||
expectedErr error | ||
want metricdata.Metrics | ||
}{ | ||
{ | ||
name: "reporting config status", | ||
ctx: context.Background(), | ||
expectedErr: nil, | ||
want: metricdata.Metrics{ | ||
Name: cfgMetricName, | ||
Data: metricdata.Gauge[int64]{ | ||
DataPoints: []metricdata.DataPoint[int64]{ | ||
{Attributes: attribute.NewSet(attribute.String(statusKey, string(metrics.ActiveStatus))), Value: 0}, | ||
{Attributes: attribute.NewSet(attribute.String(statusKey, string(metrics.ErrorStatus))), Value: 0}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rdr, r := initializeTestInstruments(t) | ||
for _, status := range metrics.AllStatuses { | ||
assert.NoError(t, r.reportConfig(tt.ctx, status, 0)) | ||
} | ||
|
||
rm := &metricdata.ResourceMetrics{} | ||
assert.Equal(t, tt.expectedErr, rdr.Collect(tt.ctx, rm)) | ||
metricdatatest.AssertEqual(t, tt.want, rm.ScopeMetrics[0].Metrics[0], metricdatatest.IgnoreTimestamp()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the observability goals for this metric? As in: what kinds of conclusions should consumers be able to draw by observing this metric? Is the data we are expressing sufficient to support those goals?
What are the gaps in reporting present in this current code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If GK admin and consumer of metrics are two different users, then I think having number of kinds getting synced in config resource might be helpful when access to config resource is resticted?