kubernetes / sample-controller

Repository for sample controller. Complements sample-apiserver
Apache License 2.0
3.14k stars 1.08k forks source link

Metrics #76

Closed aweis89 closed 3 years ago

aweis89 commented 3 years ago

Is there an easy way to add prometheus metrics to the controller?

ViviLearns2Code commented 3 years ago

Hello, I don't think there is a straight-forward way to do this. client-go allows you to do metrics, but you need to inject your own metrics provider to get them. You can see how this is done if you look up the implementation in controller-runtime: https://github.com/kubernetes-sigs/controller-runtime/tree/master/pkg/metrics

Ex: Workqueue metrics These are the client-go metrics for workqueues - you can see that by default it uses a dummy metrics provider

/* https://github.com/kubernetes/client-go/blob/master/util/workqueue/metrics.go */
type noopMetricsProvider struct{}

func (_ noopMetricsProvider) NewDepthMetric(name string) GaugeMetric {
    return noopMetric{}
}

/* more methods */

var globalMetricsFactory = queueMetricsFactory{
    metricsProvider: noopMetricsProvider{},
}

type queueMetricsFactory struct {
    metricsProvider MetricsProvider

    onlyOnce sync.Once
}

func (f *queueMetricsFactory) setProvider(mp MetricsProvider) {
    f.onlyOnce.Do(func() {
        f.metricsProvider = mp
    })
}

Now in your code, you need to set that metrics provider to something useful that you define yourself

/* https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/metrics/workqueue.go */
var (
    depth = prometheus.NewGaugeVec(prometheus.GaugeOpts{
        Subsystem: WorkQueueSubsystem,
        Name:      DepthKey,
        Help:      "Current depth of workqueue",
    }, []string{"name"})
)
func init() {
    Registry.MustRegister(depth)

    workqueue.SetProvider(workqueueMetricsProvider{})
}

type workqueueMetricsProvider struct{}

func (workqueueMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric {
    return depth.WithLabelValues(name)
}
fejta-bot commented 3 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale

k8s-triage-robot commented 3 years ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten

k8s-triage-robot commented 3 years ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

k8s-ci-robot commented 3 years ago

@k8s-triage-robot: Closing this issue.

In response to [this](https://github.com/kubernetes/sample-controller/issues/76#issuecomment-905879802): >The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. > >This bot triages issues and PRs according to the following rules: >- After 90d of inactivity, `lifecycle/stale` is applied >- After 30d of inactivity since `lifecycle/stale` was applied, `lifecycle/rotten` is applied >- After 30d of inactivity since `lifecycle/rotten` was applied, the issue is closed > >You can: >- Reopen this issue or PR with `/reopen` >- Mark this issue or PR as fresh with `/remove-lifecycle rotten` >- Offer to help out with [Issue Triage][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.