-
Notifications
You must be signed in to change notification settings - Fork 342
Initial StatsReporter for Source #648
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b0fb211
source stats reporter... should be used by well-behaved sources
nachocano 4d44d80
public fields
nachocano 982a361
Merge remote-tracking branch 'upstream/master' into source_stats
nachocano 34cd21b
moving serialization/deserialization of metrics and logging maps to pkg
nachocano 72c4da3
nits
nachocano ec3fbb8
adding UT
nachocano 9078080
same order
nachocano d6363c1
updates
nachocano beb44c5
sock-puppet
nachocano 8abc305
unregistration problem?
nachocano 095955c
removing base64 encoding
nachocano 8c34e46
removing TODO
nachocano 375f008
Merge remote-tracking branch 'upstream/master' into source_stats
nachocano 0095819
removing config changes... done in another PR
nachocano 547fb6f
Merge remote-tracking branch 'upstream/master' into source_stats
nachocano e4275bc
Merge branch 'source_stats' of github.com:nachocano/pkg into source_s…
nachocano a3a69c4
to properly address the comment
nachocano 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 hidden or 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 hidden or 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,146 @@ | ||
| /* | ||
| * Copyright 2019 The Knative Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package metrics | ||
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
|
|
||
| "go.opencensus.io/stats" | ||
| "go.opencensus.io/stats/view" | ||
| "go.opencensus.io/tag" | ||
| "knative.dev/pkg/metrics/metricskey" | ||
| ) | ||
|
|
||
| var ( | ||
| // eventCountM is a counter which records the number of events sent by the source. | ||
| eventCountM = stats.Int64( | ||
| "event_count", | ||
| "Number of events sent", | ||
| stats.UnitDimensionless, | ||
| ) | ||
| ) | ||
|
|
||
| type ReportArgs struct { | ||
| Namespace string | ||
| EventType string | ||
| EventSource string | ||
| Name string | ||
| ResourceGroup string | ||
| } | ||
|
|
||
| // StatsReporter defines the interface for sending source metrics. | ||
| type StatsReporter interface { | ||
| // ReportEventCount captures the event count. It records one per call. | ||
| ReportEventCount(args *ReportArgs, responseCode int) error | ||
| } | ||
|
|
||
| var _ StatsReporter = (*reporter)(nil) | ||
|
|
||
| // reporter holds cached metric objects to report source metrics. | ||
| type reporter struct { | ||
| namespaceTagKey tag.Key | ||
| eventSourceTagKey tag.Key | ||
| eventTypeTagKey tag.Key | ||
| sourceNameTagKey tag.Key | ||
| sourceResourceGroupTagKey tag.Key | ||
| responseCodeKey tag.Key | ||
| responseCodeClassKey tag.Key | ||
| } | ||
|
|
||
| // NewStatsReporter creates a reporter that collects and reports source metrics. | ||
| func NewStatsReporter() (StatsReporter, error) { | ||
| var r = &reporter{} | ||
|
|
||
| // Create the tag keys that will be used to add tags to our measurements. | ||
| nsTag, err := tag.NewKey(metricskey.LabelNamespaceName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r.namespaceTagKey = nsTag | ||
|
|
||
| eventSourceTag, err := tag.NewKey(metricskey.LabelEventSource) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r.eventSourceTagKey = eventSourceTag | ||
|
|
||
| eventTypeTag, err := tag.NewKey(metricskey.LabelEventType) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r.eventTypeTagKey = eventTypeTag | ||
|
|
||
| nameTag, err := tag.NewKey(metricskey.LabelImporterName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r.sourceNameTagKey = nameTag | ||
|
|
||
| resourceGroupTag, err := tag.NewKey(metricskey.LabelImporterResourceGroup) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r.sourceResourceGroupTagKey = resourceGroupTag | ||
|
|
||
| responseCodeTag, err := tag.NewKey(metricskey.LabelResponseCode) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r.responseCodeKey = responseCodeTag | ||
| responseCodeClassTag, err := tag.NewKey(metricskey.LabelResponseCodeClass) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r.responseCodeClassKey = responseCodeClassTag | ||
|
|
||
| // Create view to see our measurements. | ||
| err = view.Register( | ||
| &view.View{ | ||
| Description: eventCountM.Description(), | ||
| Measure: eventCountM, | ||
| Aggregation: view.Count(), | ||
| TagKeys: []tag.Key{r.namespaceTagKey, r.eventSourceTagKey, r.eventTypeTagKey, r.sourceNameTagKey, r.sourceResourceGroupTagKey, r.responseCodeKey, r.responseCodeClassKey}, | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return r, nil | ||
| } | ||
|
|
||
| func (r *reporter) ReportEventCount(args *ReportArgs, responseCode int) error { | ||
| ctx, err := r.generateTag(args, responseCode) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| Record(ctx, eventCountM.M(1)) | ||
| return nil | ||
| } | ||
|
|
||
| func (r *reporter) generateTag(args *ReportArgs, responseCode int) (context.Context, error) { | ||
| return tag.New( | ||
| context.Background(), | ||
| tag.Insert(r.namespaceTagKey, args.Namespace), | ||
| tag.Insert(r.eventSourceTagKey, args.EventSource), | ||
| tag.Insert(r.eventTypeTagKey, args.EventType), | ||
| tag.Insert(r.sourceNameTagKey, args.Name), | ||
| tag.Insert(r.sourceResourceGroupTagKey, args.ResourceGroup), | ||
| tag.Insert(r.responseCodeKey, strconv.Itoa(responseCode)), | ||
| tag.Insert(r.responseCodeClassKey, ResponseCodeClass(responseCode))) | ||
| } | ||
This file contains hidden or 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,79 @@ | ||
| /* | ||
| Copyright 2019 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package metrics | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "knative.dev/pkg/metrics/metricskey" | ||
| "knative.dev/pkg/metrics/metricstest" | ||
| ) | ||
|
|
||
| // unregister, ehm, unregisters the metrics that were registered, by | ||
| // virtue of StatsReporter creation. | ||
| // Since golang executes test iterations within the same process, the stats reporter | ||
| // returns an error if the metric is already registered and the test panics. | ||
| func unregister() { | ||
| metricstest.Unregister("event_count") | ||
| } | ||
|
|
||
| func TestStatsReporter(t *testing.T) { | ||
| t.Skip("Fails in PROW but not locally, needs further investigation") | ||
|
|
||
| args := &ReportArgs{ | ||
| Namespace: "testns", | ||
| EventType: "dev.knative.event", | ||
| EventSource: "unit-test", | ||
| Name: "testimporter", | ||
| ResourceGroup: "testresourcegroup", | ||
| } | ||
|
|
||
| r, err := NewStatsReporter() | ||
| if err != nil { | ||
| t.Fatalf("Failed to create a new reporter: %v", err) | ||
| } | ||
| // Without this `go test ... -count=X`, where X > 1, fails, since | ||
| // we get an error about view already being registered. | ||
| defer unregister() | ||
|
|
||
| wantTags := map[string]string{ | ||
| metricskey.LabelNamespaceName: "testns", | ||
| metricskey.LabelEventType: "dev.knative.event", | ||
| metricskey.LabelEventSource: "unit-test", | ||
| metricskey.LabelImporterName: "testimporter", | ||
| metricskey.LabelImporterResourceGroup: "testresourcegroup", | ||
| metricskey.LabelResponseCode: "202", | ||
| metricskey.LabelResponseCodeClass: "2xx", | ||
| } | ||
|
|
||
| // test ReportEventCount | ||
| expectSuccess(t, func() error { | ||
| return r.ReportEventCount(args, http.StatusAccepted) | ||
| }) | ||
| expectSuccess(t, func() error { | ||
| return r.ReportEventCount(args, http.StatusAccepted) | ||
| }) | ||
| metricstest.CheckCountData(t, "event_count", wantTags, 2) | ||
| } | ||
|
|
||
| func expectSuccess(t *testing.T, f func() error) { | ||
| t.Helper() | ||
| if err := f(); err != nil { | ||
| t.Errorf("Reporter expected success but got error: %v", err) | ||
| } | ||
| } |
This file contains hidden or 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,26 @@ | ||
| /* | ||
| Copyright 2019 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package metrics | ||
|
|
||
| import "strconv" | ||
|
|
||
| // ResponseCodeClass converts an HTTP response code to a string representing its response code class. | ||
| // E.g., The response code class is "5xx" for response code 503. | ||
| func ResponseCodeClass(responseCode int) string { | ||
| // Get the hundred digit of the response code and concatenate "xx". | ||
| return strconv.Itoa(responseCode/100) + "xx" | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.