Skip to content
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

Initial metrics integration #290

Merged
merged 53 commits into from
Dec 17, 2019

Conversation

sozercan
Copy link
Member

@sozercan sozercan commented Nov 4, 2019

Adds initial set up for OpenCensus metrics with Prometheus exporter and implements following metrics to get started:

Audit:

  • total violations per enforcement action
  • number of constraints per enforcement action
  • audit latency

Webhook: (with tags for admission allowed)

  • request count
  • request latency

Related #157

Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
Copy link
Contributor

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! Apologies for the delay, I was heads down on Kubebuilder v2.

I've done a first-pass review though I'd definitely like to take a closer look. I'd also like to get Erik (our metrics expert) to take a look.

return curMetricsExporter
}

func SetCurMetricsExporter(e view.Exporter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to separate this from NewMetricsExporter()? It looks like it's called immediately after NewMetricsExporter() anyway, and NewMetricsExporter() returns a cached object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated. let me know what you think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'm on the fence between GetMetricsExporter() and NewMetricsExporter() as the behavior changes depending on when this is called. Maybe just MetricsExporter()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM, NewMetricsExporter() always creates a new one. The name is apt.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit picks: avoid "Get" as a prefix, per https://golang.org/doc/effective_go.html#Getters . Also avoid package stutter, per https://golang.org/doc/effective_go.html#package-names
With those in mind, I suggest New() so that you have exporter.New(). This is coding style, so feel free defer for now.

pkg/metrics/exporter.go Outdated Show resolved Hide resolved
pkg/metrics/exporter.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
pkg/webhook/policy.go Outdated Show resolved Hide resolved
pkg/webhook/stats_reporter.go Outdated Show resolved Hide resolved
Copy link

@ekitson ekitson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! My name is Erik and I work with Max on various projects. I have some experience with setting up Prometheus metrics for other projects so he asked me to lend another pair of eyes to this review. I hope that's alright and feel free to ask for any clarification on my comments or suggestions.

pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
)

var (
metricsBackend = flag.String("metricsBackend", "Prometheus", "Backend used for metrics")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered exporting to the OpenCensus Agent instead? That would allow your users to choose for themselves if they want Prometheus, Jaeger, Datadog, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, we can add OC Agent as a follow up to limit the scope of this PR

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Since you already have the exporter behind a flag, you could just add "open-census-agent" as another value that you handle. Alternatively you could just always initialize both exporters and configure them on separate ports.

pkg/webhook/stats_reporter.go Outdated Show resolved Hide resolved
pkg/webhook/stats_reporter.go Outdated Show resolved Hide resolved
pkg/webhook/stats_reporter.go Outdated Show resolved Hide resolved
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>
pkg/webhook/stats_reporter.go Outdated Show resolved Hide resolved
pkg/webhook/stats_reporter.go Outdated Show resolved Hide resolved
pkg/webhook/policy.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
pkg/audit/manager.go Outdated Show resolved Hide resolved
pkg/webhook/stats_reporter.go Show resolved Hide resolved
sozercan and others added 5 commits November 12, 2019 18:00
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
pkg/audit/manager.go Outdated Show resolved Hide resolved
pkg/audit/stats_reporter.go Outdated Show resolved Hide resolved
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>

# Conflicts:
#	Gopkg.lock
#	main.go
#	pkg/audit/manager.go
#	pkg/webhook/policy.go
Copy link

@ekitson ekitson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I think these are probably my last suggestions for this PR.

Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
pkg/controller/constraint/constraint_controller.go Outdated Show resolved Hide resolved
@@ -112,6 +137,16 @@ func (r *ReconcileConstraint) Reconcile(request reconcile.Request) (reconcile.Re
return reconcile.Result{}, err
}

constraintKindName := strings.Join([]string{instance.GetKind(), instance.GetName()}, "/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I suggested string joining, I was forgetting that

struct {
 Kind string
 Name string
}

would also work.

Either method is fine with me.

@maxsmythe
Copy link
Contributor

Needs rebase and DCO check failing.

pkg/metrics/exporter.go Outdated Show resolved Hide resolved
pkg/metrics/exporter.go Outdated Show resolved Hide resolved
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
pkg/metrics/prometheus_exporter.go Outdated Show resolved Hide resolved
pkg/metrics/prometheus_exporter.go Outdated Show resolved Hide resolved
@@ -118,6 +131,15 @@ func (h *validationHandler) Handle(ctx context.Context, req admission.Request) a
return vResp
}

var requestResponse requestResponse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping

pkg/metrics/prometheus_exporter.go Outdated Show resolved Hide resolved
pkg/metrics/exporter.go Outdated Show resolved Hide resolved
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
pkg/webhook/policy.go Outdated Show resolved Hide resolved
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>
Copy link
Contributor

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sozercan sozercan force-pushed the opencensus-metrics branch 4 times, most recently from 0d17c64 to 0d4e561 Compare December 17, 2019 21:39
Signed-off-by: Sertaç Özercan <sozercan@users.noreply.github.com>
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
@sozercan sozercan force-pushed the opencensus-metrics branch 4 times, most recently from 972da17 to a246318 Compare December 17, 2019 21:50
sozercan and others added 3 commits December 17, 2019 13:51
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
* Add capability PSP control aspect to library

Signed-off-by: Max Smythe <smythe@google.com>

* Update PSP library README

Signed-off-by: Max Smythe <smythe@google.com>

* Fix template kind

Signed-off-by: Max Smythe <smythe@google.com>
Signed-off-by: Sertaç Özercan <sozercan@gmail.com>
@sozercan sozercan merged commit 6d1c641 into open-policy-agent:master Dec 17, 2019
@sozercan sozercan deleted the opencensus-metrics branch December 17, 2019 22:36
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.

5 participants