|
| 1 | +/* |
| 2 | +Copyright 2018 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package reconciler |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "go.opencensus.io/stats" |
| 25 | + "go.opencensus.io/stats/view" |
| 26 | + "go.opencensus.io/tag" |
| 27 | + "knative.dev/pkg/metrics" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // reconcileCountN is the number of successful reconcile operations. |
| 32 | + reconcileCountN = "knative_operator_reconcile_count" |
| 33 | + // reconcileLatencyN is the time it takes for a sucessful reconcile operation. |
| 34 | + reconcileLatencyN = "knative_operator_reconcile_latency" |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + reconcileCountStat = stats.Int64( |
| 39 | + reconcileCountN, |
| 40 | + "Number of reconcile operations", |
| 41 | + stats.UnitNone) |
| 42 | + reconcileLatencyStat = stats.Int64( |
| 43 | + reconcileLatencyN, |
| 44 | + "Latency of reconcile operations", |
| 45 | + stats.UnitMilliseconds) |
| 46 | + |
| 47 | + // Create the tag keys that will be used to add tags to our measurements. |
| 48 | + // Tag keys must conform to the restrictions described in |
| 49 | + // go.opencensus.io/tag/validate.go. Currently those restrictions are: |
| 50 | + // - length between 1 and 255 inclusive |
| 51 | + // - characters are printable US-ASCII |
| 52 | + reconcilerTagKey = tag.MustNewKey("reconciler") |
| 53 | + keyTagKey = tag.MustNewKey("key") |
| 54 | +) |
| 55 | + |
| 56 | +func init() { |
| 57 | + // Create views to see our measurements. This can return an error if |
| 58 | + // a previously-registered view has the same name with a different value. |
| 59 | + // View name defaults to the measure name if unspecified. |
| 60 | + if err := view.Register( |
| 61 | + &view.View{ |
| 62 | + Description: "Number of reconcile operations", |
| 63 | + Measure: reconcileCountStat, |
| 64 | + Aggregation: view.Count(), |
| 65 | + TagKeys: []tag.Key{reconcilerTagKey, keyTagKey}, |
| 66 | + }, |
| 67 | + &view.View{ |
| 68 | + Description: "Latency of reconcile operations", |
| 69 | + Measure: reconcileLatencyStat, |
| 70 | + Aggregation: view.LastValue(), |
| 71 | + TagKeys: []tag.Key{reconcilerTagKey, keyTagKey}, |
| 72 | + }, |
| 73 | + ); err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// StatsReporter defines the interface for sending metrics |
| 79 | +type StatsReporter interface { |
| 80 | + // ReportReconcile reports the count and latency metrics for a reconcile operation |
| 81 | + ReportReconcile(resourceNamespace, resourceName string, duration time.Duration) error |
| 82 | +} |
| 83 | + |
| 84 | +// Reporter holds cached metric objects to report metrics |
| 85 | +type reporter struct { |
| 86 | + reconciler string |
| 87 | + ctx context.Context |
| 88 | +} |
| 89 | + |
| 90 | +// srKey is used to associate StatsReporters with contexts. |
| 91 | +type srKey struct{} |
| 92 | + |
| 93 | +// WithStatsReporter attaches the given StatsReporter to the provided context |
| 94 | +// in the returned context. |
| 95 | +func WithStatsReporter(ctx context.Context, sr StatsReporter) context.Context { |
| 96 | + return context.WithValue(ctx, srKey{}, sr) |
| 97 | +} |
| 98 | + |
| 99 | +// GetStatsReporter attempts to look up the StatsReporter on a given context. |
| 100 | +// It may return null if none is found. |
| 101 | +func GetStatsReporter(ctx context.Context) StatsReporter { |
| 102 | + untyped := ctx.Value(srKey{}) |
| 103 | + if untyped == nil { |
| 104 | + return nil |
| 105 | + } |
| 106 | + return untyped.(StatsReporter) |
| 107 | +} |
| 108 | + |
| 109 | +// NewStatsReporter creates a reporter that collects and reports metrics |
| 110 | +func NewStatsReporter(reconciler string) (StatsReporter, error) { |
| 111 | + // Reconciler tag is static. Create a context containing that and cache it. |
| 112 | + ctx, err := tag.New( |
| 113 | + context.Background(), |
| 114 | + tag.Insert(reconcilerTagKey, reconciler)) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + return &reporter{reconciler: reconciler, ctx: ctx}, nil |
| 120 | +} |
| 121 | + |
| 122 | +// ReportReconcile reports the count and latency metrics for a reconcile operation |
| 123 | +func (r *reporter) ReportReconcile(resourceNamespace, resourceName string, duration time.Duration) error { |
| 124 | + key := fmt.Sprintf("%s/%s", resourceNamespace, resourceName) |
| 125 | + ctx, err := tag.New( |
| 126 | + context.Background(), |
| 127 | + tag.Insert(reconcilerTagKey, r.reconciler), |
| 128 | + tag.Insert(keyTagKey, key)) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + metrics.Record(ctx, reconcileCountStat.M(1)) |
| 134 | + metrics.Record(ctx, reconcileLatencyStat.M(duration.Milliseconds())) |
| 135 | + return nil |
| 136 | +} |
0 commit comments