mirror of https://github.com/knative/pkg.git
				
				
				
			remove source stats reporter (#2193)
it's been moved to knative.dev/eventing/pkg/metrics/source
This commit is contained in:
		
							parent
							
								
									591cb897dd
								
							
						
					
					
						commit
						8931f82991
					
				| 
						 | 
					@ -1,178 +0,0 @@
 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
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 source
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"go.opencensus.io/stats/view"
 | 
					 | 
				
			||||||
	"knative.dev/pkg/metrics"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"go.opencensus.io/stats"
 | 
					 | 
				
			||||||
	"go.opencensus.io/tag"
 | 
					 | 
				
			||||||
	"knative.dev/pkg/metrics/metricskey"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const (
 | 
					 | 
				
			||||||
	// LabelEventType is the label for the name of the event type.
 | 
					 | 
				
			||||||
	LabelEventType = "event_type"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// LabelName is the label for the name of the resource.
 | 
					 | 
				
			||||||
	LabelName = "name"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// LabelResourceGroup is the name of the resource CRD.
 | 
					 | 
				
			||||||
	LabelResourceGroup = "resource_group"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// LabelEventSource is the label for the name of the event source.
 | 
					 | 
				
			||||||
	LabelEventSource = "event_source"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// ResourceTypeKnativeSource is the resource type for Knative Sources
 | 
					 | 
				
			||||||
	ResourceTypeKnativeSource = "knative_source"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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,
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// retryEventCountM is a counter which records the number of events sent by the source in retries.
 | 
					 | 
				
			||||||
	retryEventCountM = stats.Int64(
 | 
					 | 
				
			||||||
		"retry_event_count",
 | 
					 | 
				
			||||||
		"Number of retry events sent",
 | 
					 | 
				
			||||||
		stats.UnitDimensionless,
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
	// Create the tag keys that will be used to add tags to our measurements.
 | 
					 | 
				
			||||||
	// Tag keys must conform to the restrictions described in
 | 
					 | 
				
			||||||
	// go.opencensus.io/tag/validate.go. Currently those restrictions are:
 | 
					 | 
				
			||||||
	// - length between 1 and 255 inclusive
 | 
					 | 
				
			||||||
	// - characters are printable US-ASCII
 | 
					 | 
				
			||||||
	eventSourceKey         = tag.MustNewKey(LabelEventSource)
 | 
					 | 
				
			||||||
	eventTypeKey           = tag.MustNewKey(LabelEventType)
 | 
					 | 
				
			||||||
	sourceNameKey          = tag.MustNewKey(LabelName)
 | 
					 | 
				
			||||||
	sourceResourceGroupKey = tag.MustNewKey(LabelResourceGroup)
 | 
					 | 
				
			||||||
	namespaceKey           = tag.MustNewKey(metricskey.LabelNamespaceName)
 | 
					 | 
				
			||||||
	responseCodeKey        = tag.MustNewKey(metricskey.LabelResponseCode)
 | 
					 | 
				
			||||||
	responseCodeClassKey   = tag.MustNewKey(metricskey.LabelResponseCodeClass)
 | 
					 | 
				
			||||||
	responseError          = tag.MustNewKey(metricskey.LabelResponseError)
 | 
					 | 
				
			||||||
	responseTimeout        = tag.MustNewKey(metricskey.LabelResponseTimeout)
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ReportArgs defines the arguments for reporting metrics.
 | 
					 | 
				
			||||||
type ReportArgs struct {
 | 
					 | 
				
			||||||
	Namespace     string
 | 
					 | 
				
			||||||
	EventType     string
 | 
					 | 
				
			||||||
	EventSource   string
 | 
					 | 
				
			||||||
	Name          string
 | 
					 | 
				
			||||||
	ResourceGroup string
 | 
					 | 
				
			||||||
	Error         string
 | 
					 | 
				
			||||||
	Timeout       bool
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func init() {
 | 
					 | 
				
			||||||
	register()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 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
 | 
					 | 
				
			||||||
	ReportRetryEventCount(args *ReportArgs, responseCode int) error
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var _ StatsReporter = (*reporter)(nil)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// reporter holds cached metric objects to report source metrics.
 | 
					 | 
				
			||||||
type reporter struct {
 | 
					 | 
				
			||||||
	ctx context.Context
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// NewStatsReporter creates a reporter that collects and reports source metrics.
 | 
					 | 
				
			||||||
func NewStatsReporter() (StatsReporter, error) {
 | 
					 | 
				
			||||||
	ctx, err := tag.New(
 | 
					 | 
				
			||||||
		context.Background(),
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return &reporter{ctx: ctx}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (r *reporter) ReportEventCount(args *ReportArgs, responseCode int) error {
 | 
					 | 
				
			||||||
	ctx, err := r.generateTag(args, responseCode)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	metrics.Record(ctx, eventCountM.M(1))
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (r *reporter) ReportRetryEventCount(args *ReportArgs, responseCode int) error {
 | 
					 | 
				
			||||||
	ctx, err := r.generateTag(args, responseCode)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	metrics.Record(ctx, retryEventCountM.M(1))
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (r *reporter) generateTag(args *ReportArgs, responseCode int) (context.Context, error) {
 | 
					 | 
				
			||||||
	return tag.New(
 | 
					 | 
				
			||||||
		r.ctx,
 | 
					 | 
				
			||||||
		tag.Insert(namespaceKey, args.Namespace),
 | 
					 | 
				
			||||||
		tag.Insert(eventSourceKey, args.EventSource),
 | 
					 | 
				
			||||||
		tag.Insert(eventTypeKey, args.EventType),
 | 
					 | 
				
			||||||
		tag.Insert(sourceNameKey, args.Name),
 | 
					 | 
				
			||||||
		tag.Insert(sourceResourceGroupKey, args.ResourceGroup),
 | 
					 | 
				
			||||||
		metrics.MaybeInsertIntTag(responseCodeKey, responseCode, responseCode > 0),
 | 
					 | 
				
			||||||
		metrics.MaybeInsertStringTag(responseCodeClassKey, metrics.ResponseCodeClass(responseCode), responseCode > 0),
 | 
					 | 
				
			||||||
		tag.Insert(responseError, args.Error),
 | 
					 | 
				
			||||||
		metrics.MaybeInsertBoolTag(responseTimeout, args.Timeout, args.Error != ""))
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func register() {
 | 
					 | 
				
			||||||
	tagKeys := []tag.Key{
 | 
					 | 
				
			||||||
		namespaceKey,
 | 
					 | 
				
			||||||
		eventSourceKey,
 | 
					 | 
				
			||||||
		eventTypeKey,
 | 
					 | 
				
			||||||
		sourceNameKey,
 | 
					 | 
				
			||||||
		sourceResourceGroupKey,
 | 
					 | 
				
			||||||
		responseCodeKey,
 | 
					 | 
				
			||||||
		responseCodeClassKey,
 | 
					 | 
				
			||||||
		responseError,
 | 
					 | 
				
			||||||
		responseTimeout}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Create view to see our measurements.
 | 
					 | 
				
			||||||
	if err := view.Register(
 | 
					 | 
				
			||||||
		&view.View{
 | 
					 | 
				
			||||||
			Description: eventCountM.Description(),
 | 
					 | 
				
			||||||
			Measure:     eventCountM,
 | 
					 | 
				
			||||||
			Aggregation: view.Count(),
 | 
					 | 
				
			||||||
			TagKeys:     tagKeys,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		&view.View{
 | 
					 | 
				
			||||||
			Description: retryEventCountM.Description(),
 | 
					 | 
				
			||||||
			Measure:     retryEventCountM,
 | 
					 | 
				
			||||||
			Aggregation: view.Count(),
 | 
					 | 
				
			||||||
			TagKeys:     tagKeys,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
	); err != nil {
 | 
					 | 
				
			||||||
		panic(err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,97 +0,0 @@
 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
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 source
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"net/http"
 | 
					 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"knative.dev/pkg/metrics/metricskey"
 | 
					 | 
				
			||||||
	"knative.dev/pkg/metrics/metricstest"
 | 
					 | 
				
			||||||
	_ "knative.dev/pkg/metrics/testing"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestStatsReporter(t *testing.T) {
 | 
					 | 
				
			||||||
	setup()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	args := &ReportArgs{
 | 
					 | 
				
			||||||
		Namespace:     "testns",
 | 
					 | 
				
			||||||
		EventType:     "dev.knative.event",
 | 
					 | 
				
			||||||
		EventSource:   "unit-test",
 | 
					 | 
				
			||||||
		Name:          "testsource",
 | 
					 | 
				
			||||||
		ResourceGroup: "testresourcegroup",
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	r, err := NewStatsReporter()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		t.Fatal("Failed to create a new reporter:", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	wantTags := map[string]string{
 | 
					 | 
				
			||||||
		LabelEventType:                    "dev.knative.event",
 | 
					 | 
				
			||||||
		LabelEventSource:                  "unit-test",
 | 
					 | 
				
			||||||
		LabelName:                         "testsource",
 | 
					 | 
				
			||||||
		LabelResourceGroup:                "testresourcegroup",
 | 
					 | 
				
			||||||
		metricskey.LabelNamespaceName:     "testns",
 | 
					 | 
				
			||||||
		metricskey.LabelResponseCode:      "202",
 | 
					 | 
				
			||||||
		metricskey.LabelResponseCodeClass: "2xx",
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	retryWantTags := map[string]string{
 | 
					 | 
				
			||||||
		LabelEventType:                    "dev.knative.event",
 | 
					 | 
				
			||||||
		LabelEventSource:                  "unit-test",
 | 
					 | 
				
			||||||
		LabelName:                         "testsource",
 | 
					 | 
				
			||||||
		LabelResourceGroup:                "testresourcegroup",
 | 
					 | 
				
			||||||
		metricskey.LabelNamespaceName:     "testns",
 | 
					 | 
				
			||||||
		metricskey.LabelResponseCode:      "503",
 | 
					 | 
				
			||||||
		metricskey.LabelResponseCodeClass: "5xx",
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// test ReportEventCount and ReportRetryEventCount
 | 
					 | 
				
			||||||
	expectSuccess(t, func() error {
 | 
					 | 
				
			||||||
		return r.ReportEventCount(args, http.StatusAccepted)
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
	expectSuccess(t, func() error {
 | 
					 | 
				
			||||||
		return r.ReportEventCount(args, http.StatusAccepted)
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
	expectSuccess(t, func() error {
 | 
					 | 
				
			||||||
		return r.ReportRetryEventCount(args, http.StatusServiceUnavailable)
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
	expectSuccess(t, func() error {
 | 
					 | 
				
			||||||
		return r.ReportRetryEventCount(args, http.StatusServiceUnavailable)
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
	metricstest.CheckCountData(t, "event_count", wantTags, 2)
 | 
					 | 
				
			||||||
	metricstest.CheckCountData(t, "retry_event_count", retryWantTags, 2)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func expectSuccess(t *testing.T, f func() error) {
 | 
					 | 
				
			||||||
	t.Helper()
 | 
					 | 
				
			||||||
	if err := f(); err != nil {
 | 
					 | 
				
			||||||
		t.Error("Reporter expected success but got error:", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func setup() {
 | 
					 | 
				
			||||||
	resetMetrics()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func resetMetrics() {
 | 
					 | 
				
			||||||
	// OpenCensus metrics carry global state that need to be reset between unit tests.
 | 
					 | 
				
			||||||
	metricstest.Unregister("event_count")
 | 
					 | 
				
			||||||
	metricstest.Unregister("retry_event_count")
 | 
					 | 
				
			||||||
	register()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Loading…
	
		Reference in New Issue