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

Collect prometheus metrics for custom resources #57682

Merged
merged 2 commits into from
Feb 9, 2018

Conversation

nikhita
Copy link
Member

@nikhita nikhita commented Dec 28, 2017

Enables apiserver metrics for custom resources.

Fixes #55146

Release note:

Enable apiserver metrics for custom resources.

/cc sttts deads2k kargakis brancz

@k8s-ci-robot k8s-ci-robot added the release-note-none Denotes a PR that doesn't merit a release note. label Dec 28, 2017
@k8s-ci-robot k8s-ci-robot requested a review from sttts December 28, 2017 09:18
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 28, 2017
@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Dec 28, 2017
@nikhita
Copy link
Member Author

nikhita commented Dec 28, 2017

/area custom-resources

httpCode = rw.Status()
}
}
metrics.Record(req, requestInfo, w.Header().Get("Content-Type"), httpCode, responseLength, time.Now().Sub(reqStart))
Copy link
Contributor

@sttts sttts Jan 2, 2018

Choose a reason for hiding this comment

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

is this go func duplicated from the normal endpoints? Any chance to factor out the later and reuse it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, will do. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@nikhita nikhita force-pushed the customresource-metrics branch from 3fd9f50 to 6ccdd75 Compare January 2, 2018 13:12
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 2, 2018
@nikhita nikhita force-pushed the customresource-metrics branch from 6ccdd75 to 5cdf4ec Compare January 2, 2018 13:24
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 2, 2018
@@ -57,16 +57,7 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

var httpCode int
var requestInfo *request.RequestInfo
defer func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

this is only the special proxy handler. Something similar must be in the other endpoints.

Copy link
Member

@brancz brancz left a comment

Choose a reason for hiding this comment

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

Just one comment, otherwise lgtm

@@ -152,6 +154,9 @@ func (r *crdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

var httpCode int
defer handlers.RecordMetrics(w, req, requestInfo, httpCode, reqStart)
Copy link
Member

Choose a reason for hiding this comment

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

Do we really want to add this code to each and every handler? Seems like it would make sense to wrap each handler in a handler that records this.

Copy link
Contributor

Choose a reason for hiding this comment

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

You probably overestimate the number of handlers. We have the proxy handler, the crd handler and basically all other API handlers (impemented in a generic way).

Though having this as a decorator would be better. Not sure that's feasible code-wise?

Copy link
Member

Choose a reason for hiding this comment

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

I see, then I'm fine with this, I just thought we're doing this for every API avilable.

@0xmichalis
Copy link
Contributor

Is the crdHandler handling CRDs or CRs?

@nikhita
Copy link
Member Author

nikhita commented Jan 3, 2018

Is the crdHandler handling CRDs or CRs?

CRs.

@nikhita
Copy link
Member Author

nikhita commented Jan 3, 2018

Is the crdHandler handling CRDs or CRs?

To be precise, it is named as customresource_handler, not crdHandler in reality.

@sttts
Copy link
Contributor

sttts commented Jan 3, 2018

Is the crdHandler handling CRDs or CRs?

CRDs use the normal code-path of native resources. Do we have metrics problems as well for CRDs?

@0xmichalis
Copy link
Contributor

Do we have metrics problems as well for CRDs?

I think we already collect metrics for CRDs but personally I am not interested in those. User CRD requests to the k8s API are almost zero (one per resource during registration). I just wanted to make sure because the name of the handler type is confusing.

@nikhita
Copy link
Member Author

nikhita commented Jan 3, 2018

Do we have metrics problems as well for CRDs?

No, metrics for CRDs works fine.

@nikhita
Copy link
Member Author

nikhita commented Jan 3, 2018

/retest

@nikhita nikhita force-pushed the customresource-metrics branch from 5cdf4ec to 86c2aee Compare January 3, 2018 16:06
@@ -283,3 +274,14 @@ func singleJoiningSlash(a, b string) string {
}
return a + b
}

func RecordMetrics(w http.ResponseWriter, req *http.Request, requestInfo *request.RequestInfo, httpCode int, reqStart time.Time) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Compare

func InstrumentRouteFunc(verb, resource, subresource, scope string, routeFunc restful.RouteFunction) restful.RouteFunction {
. Can you move this there and align the signature?

Copy link
Contributor

Choose a reason for hiding this comment

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

Any blockers here?

Copy link
Member Author

@nikhita nikhita Jan 17, 2018

Choose a reason for hiding this comment

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

Any blockers here?

We don't have a restful.RouteFunction here in this handler. Any reason why the RecordMetrics is incorrect?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@sttts updated to use InstrumentHandlerFunc. ptal, thanks.

@nikhita nikhita force-pushed the customresource-metrics branch from 86c2aee to 336f33b Compare February 7, 2018 20:24
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 7, 2018
}
}

func CleanScope(requestInfo *request.RequestInfo) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

godoc

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@sttts
Copy link
Contributor

sttts commented Feb 8, 2018

@brancz do you know the metrics code in the apiserver? Can you take a look?

Copy link
Member

@brancz brancz left a comment

Choose a reason for hiding this comment

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

@sttts I wouldn't say I'm intimately familiar with it, but from a general metrics perspective and digging through the code for some time now, I think this looks good.

handler(w, req)
return
case "deletecollection":
checkBody := true
handler := handlers.DeleteCollection(storage, checkBody, requestScope, r.admission)
handler = metrics.InstrumentHandlerFunc(verb, resource, subresource, scope, handler)
Copy link
Member

Choose a reason for hiding this comment

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

These metrics.InstrumentHandlerFunc calls seem duplicated, maybe we can just set the handler in each of these blocks and call this once?

Copy link
Contributor

Choose a reason for hiding this comment

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

with a global handler var, we could remove the instrumentation call, the handler(w,req) call and the return and do those uniformely for all non-default cases below the switch.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@nikhita nikhita force-pushed the customresource-metrics branch from e03a11b to 6043876 Compare February 8, 2018 11:35
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 8, 2018
Since we have a custom handler for apiextensions-apiserver,
we need to record the metrics here.
@nikhita
Copy link
Member Author

nikhita commented Feb 8, 2018

/retest

@brancz
Copy link
Member

brancz commented Feb 9, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 9, 2018
@sttts
Copy link
Contributor

sttts commented Feb 9, 2018

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brancz, nikhita, sttts

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these OWNERS Files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 9, 2018
@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit 8eae0a8 into kubernetes:master Feb 9, 2018
@nikhita nikhita deleted the customresource-metrics branch July 10, 2018 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/custom-resources cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants