Skip to content

Enabled metrics for apiserversource#1761

Closed
sayanh wants to merge 9 commits into
knative:masterfrom
sayanh:enable-metrics-for-apiserversource
Closed

Enabled metrics for apiserversource#1761
sayanh wants to merge 9 commits into
knative:masterfrom
sayanh:enable-metrics-for-apiserversource

Conversation

@sayanh
Copy link
Copy Markdown
Contributor

@sayanh sayanh commented Aug 29, 2019

Helps #1693

Proposed Changes

  • Enabled metrics for apiserversource

Release Note

 ApiServerSource now reports event counts

@knative-prow-robot knative-prow-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 29, 2019
@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Aug 29, 2019
@knative-prow-robot knative-prow-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 29, 2019
@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sayanh
To complete the pull request process, please assign grantr
You can assign the PR to them by writing /assign @grantr in a comment when ready.

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

Details Needs approval from an approver in each of these files:

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

@knative-prow-robot knative-prow-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 29, 2019
@knative-prow-robot
Copy link
Copy Markdown
Contributor

Hi @sayanh. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@knative-prow-robot knative-prow-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 30, 2019
@sayanh sayanh force-pushed the enable-metrics-for-apiserversource branch from 626dc4e to 5ce5b0e Compare August 30, 2019 15:06
@sayanh sayanh marked this pull request as ready for review August 30, 2019 15:07
@knative-prow-robot knative-prow-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 30, 2019
@sayanh sayanh changed the title Added metrics endpoint Enabled metrics for apiserversource Aug 30, 2019
@nachocano
Copy link
Copy Markdown
Contributor

/ok-to-test

@knative-prow-robot knative-prow-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 30, 2019
@nachocano
Copy link
Copy Markdown
Contributor

Can you add in the release notes that the ApiServerSource now reports event counts

Comment thread cmd/apiserver_receive_adapter/main.go Outdated
)

const (
component = "ApiServerSource::ReceiveAdapter"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we just call it apiserversource? This will end up changing to be importer later on, but not right now as we need some other changes to be merged in

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure, fixed it

Comment thread cmd/apiserver_receive_adapter/main.go Outdated
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"

"knative.dev/eventing/pkg/reconciler/apiserversource/resources"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove whitespace and run go importer formatting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

// Convert base64 encoded json logging.Config to logging.Config.
loggingConfig, err := resources.Base64ToLoggingConfig(
env.LoggingConfigBase64)
if err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we log an error, and try to use the default one. If it fails, then panic...
As here:
https://github.com/google/knative-gcp/blob/cabd0d258db1d23246d51fac8b181fd4c363c28e/cmd/pubsub/receive_adapter/main.go#L55

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed it

Comment thread cmd/apiserver_receive_adapter/main.go Outdated
metricsConfig, err := resources.Base64ToMetricsOptions(
env.MetricsConfigBase64)
if err != nil {
panic(err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

logger.Errorf("failed to process metrics options: %s", err.Error())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed

Comment thread cmd/apiserver_receive_adapter/main.go Outdated
panic(err)
}

logger, _ := logging.NewLoggerFromConfig(loggingConfig, component)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

put this above the call to resources.Base64ToMetricsOptions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed


package apiserver

// Type gets the eventtype.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

update comment.

Can you add a TODO saying we should use HTTP status codes once this is merged: cloudevents/sdk-go#177

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a TODO

Comment thread pkg/adapter/apiserver/ref.go Outdated
return err
}
return nil
return a.sendEvent(context.Background(), event, a.reporter)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

no need to pass the reporter as it's in the adapter already

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

correct, fixed it

Comment thread pkg/adapter/apiserver/ref.go Outdated
}

func (a *ref) addControllerWatch(gvr schema.GroupVersionResource) {
reportArgs := &ReportArgs{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed

Comment thread pkg/adapter/apiserver/ref.go Outdated
}
a.reporter.ReportEventCount(reportArgs, nil)

if _, err := a.ce.Send(ctx, *event); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

	_, err := a.ce.Send(ctx, *event)
        if err != nil {
	    a.logger.Info("event delivery failed", zap.Error(err))
        }
        a.reporter.ReportEventCount(reportArgs, err)
        return err

	

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed it

Comment thread pkg/adapter/apiserver/resource.go Outdated
}
a.reporter.ReportEventCount(reportArgs, nil)

if _, err := a.ce.Send(ctx, *event); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same as previous comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed it

Comment thread pkg/adapter/apiserver/stats_reporter.go Outdated

var (
// eventCountM is a counter which records the number of events received
// by a Trigger.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

update comment: .... number of events sent by an Importer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated

Comment thread pkg/adapter/apiserver/stats_reporter.go Outdated
eventTypeKey tag.Key
eventSourceKey tag.Key
resultKey tag.Key
filterResultKey tag.Key
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove filterResultKey

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed

Comment thread pkg/adapter/apiserver/stats_reporter.go Outdated
filterResultKey tag.Key
}

// NewStatsReporter creates a reporter that collects and reports filter metrics.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NewStatsReporter creates a reporter that collects and reports apiserversource metrics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed it

Comment thread pkg/adapter/apiserver/stats_reporter.go Outdated
return nil, err
}
r.eventSourceKey = eventSourceTag
filterResultTag, err := tag.NewKey(metricskey.FilterResult)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove filterTag

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed

Comment thread pkg/adapter/apiserver/stats_reporter.go Outdated
&view.View{
Description: eventCountM.Description(),
Measure: eventCountM,
// TODO count or sum aggregation?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove TODO... is count

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed

Comment thread pkg/adapter/apiserver/stats_reporter.go Outdated
return tag.New(
context.Background(),
tag.Insert(r.namespaceTagKey, args.ns),
tag.Insert(r.eventSourceKey, valueOrAny(args.eventSource)),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we do not need valueOrAny here. As they will not be empty... Can you remove that, and also the note?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed

}

wantTags1 := map[string]string(wantTags)
wantTags1[metricskey.Result] = "success"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should be part of the wantTags, right? No need for a wantTags1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

right, fixed it


}

func TestReporterEmptySourceAndType(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this one will never happen as we are setting up the event source and type in the CloudEvent, otherwise it's not a valid CloudEvent.
It should be fine to remove, or maybe change it to test for the error condition... pass an error to ReportEventCount and see that instead of success you have the error label

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a new test for now TestReporterForErrorTag

Comment thread pkg/metrics/metricskey/constants.go Outdated
EventType = "event_type"

// EventSource is the label for the CloudEvents source context attribute.
EventSource = "event_source"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

all these constants are in knative.dev/pkg/metrics/metricskey now, as LabelEventSource, etc. Can we use those instead? I'll make the changes in the other places, and remove these constants

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed it

@@ -0,0 +1,134 @@
/*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it would be nice to move this down to pkg as we are going to use it in many places.
@n3wscott what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How about pkg/utils?

}

func (a *ref) sendEvent(ctx context.Context, event *cloudevents.Event, reporter StatsReporter) error {
reportArgs := &ReportArgs{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We will also need to add the importerName and the importerResourceGroup... So that we can also filter based on those attributes. E.g., give me the event_count of all apiserversource importers, and so on...

The name you might need to send it in an env variable to the receive_adapter (as we do in the broker ingress)...
The importerResourceGroup can be hardcoded to apiserversources.sources.eventing.knative.dev (put it in a constant)

See labels for tags here: https://github.com/knative/pkg/blob/ec2f20ae67fbbccfac425061f2c7c089d1932105/metrics/metricskey/constants_eventing.go#L52-54

Copy link
Copy Markdown
Contributor Author

@sayanh sayanh Sep 1, 2019

Choose a reason for hiding this comment

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

Added those tags now. A sample metrics looks like:

apiserversource_event_count{event_source="https://1.2.3.4:443",event_type="dev.knative.apiserver.resource.update",importer_name="testevents",importer_resource_group="apiserversources.sources.eventing.knative.dev",namespace_name="default"} 3

@knative-prow-robot knative-prow-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 1, 2019
@sayanh sayanh force-pushed the enable-metrics-for-apiserversource branch from 9b50089 to 0f3b53d Compare September 1, 2019 19:50
@knative-metrics-robot
Copy link
Copy Markdown

The following is the coverage report on pkg/.
Say /test pull-knative-eventing-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/adapter/apiserver/metrics.go Do not exist 100.0%
pkg/adapter/apiserver/ref.go 83.3% 90.2% 6.9
pkg/adapter/apiserver/resource.go 80.6% 90.3% 9.7
pkg/adapter/apiserver/stats_reporter.go Do not exist 76.3%
pkg/reconciler/apiserversource/apiserversource.go 77.5% 77.9% 0.4
pkg/utils/config.go Do not exist 72.9%

@knative-prow-robot
Copy link
Copy Markdown
Contributor

@sayanh: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-knative-eventing-build-tests 7cb9e28 link /test pull-knative-eventing-build-tests
pull-knative-eventing-unit-tests 7cb9e28 link /test pull-knative-eventing-unit-tests

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@nachocano
Copy link
Copy Markdown
Contributor

@sayanh can this be closed and we will use instead #1786?

@sayanh
Copy link
Copy Markdown
Contributor Author

sayanh commented Sep 5, 2019

Closing this as this is a duplicate for #1786

@sayanh sayanh closed this Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Indicates the PR's author has signed the CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants