@@ -16,9 +16,10 @@ package telemetry
1616
1717import (
1818 "context"
19- "fmt"
2019 "time"
2120
21+ "github.com/kptdev/porch/api/porch/v1alpha1"
22+ "github.com/kptdev/porch/api/porch/v1alpha2"
2223 "github.com/kptdev/porch/pkg/repository"
2324 "go.opentelemetry.io/otel"
2425 "go.opentelemetry.io/otel/attribute"
@@ -30,30 +31,21 @@ import (
3031const meterName = "github.com/kptdev/porch"
3132
3233const (
33- APIVersionV1Alpha1 = "v1alpha1"
34- APIVersionV1Alpha2 = "v1alpha2"
35- ControllerUser = "packagerevision-controller"
34+ ControllerUser = "packagerevision-controller"
3635
3736 ResourcePackageRevision = "PackageRevision"
3837 ResourcePackageRevisionResources = "PackageRevisionResources"
3938 ResourceExternalRepo = "ExternalRepo"
4039)
4140
4241var (
42+ APIVersionV1Alpha1 = v1alpha1 .SchemeGroupVersion .Version
43+ APIVersionV1Alpha2 = v1alpha2 .SchemeGroupVersion .Version
44+
4345 apiCallDurationSeconds metric.Float64Histogram
44- RequestsTotal metric.Float64Counter
46+ requestsTotal metric.Float64Counter
4547 prResourceSizeHistogram metric.Int64Histogram
4648 prResourceSizeGauge metric.Int64Gauge
47-
48- // Performance tests related vars
49- perfOperationDuration metric.Float64Histogram
50- perfOperationCounter metric.Float64Counter
51- perfRepositoryCounter metric.Float64Counter
52- perfPackageCounter metric.Float64Counter
53- perfPackageRevisionCounter metric.Float64Counter
54- perfLifecycleTransitionDuration metric.Float64Histogram
55- perfTestRunInfoGauge metric.Float64Gauge
56- perfActiveOperations metric.Float64UpDownCounter
5749)
5850
5951func InitMetrics () (err error ) {
@@ -69,15 +61,17 @@ func InitMetrics() (err error) {
6961 ),
7062 )
7163 if err != nil {
72- panic (fmt .Sprintf ("failed to create porch_api_call_duration_seconds: %v" , err ))
64+ klog .Errorf ("failed to create porch_api_call_duration_seconds: %v" , err )
65+ return
7366 }
7467
75- RequestsTotal , err = m .Float64Counter (
68+ requestsTotal , err = m .Float64Counter (
7669 "porch_api_requests_by_user" ,
7770 metric .WithDescription ("Total number of requests tracked by BurstCounter, broken down by resource, operation, and user." ),
7871 )
7972 if err != nil {
80- panic (fmt .Sprintf ("failed to create porch_api_requests_by_user: %v" , err ))
73+ klog .Errorf ("failed to create porch_api_requests_by_user: %v" , err )
74+ return
8175 }
8276
8377 prResourceSizeHistogram , err = m .Int64Histogram (
@@ -101,81 +95,13 @@ func InitMetrics() (err error) {
10195 return
10296 }
10397
104- // Performance test related metrics
105- perfOperationDuration , err = m .Float64Histogram (
106- "porch_perf_operation_duration_seconds" ,
107- metric .WithDescription ("Duration of Porch performance test operations in seconds" ),
108- metric .WithUnit ("s" ),
109- metric .WithExplicitBucketBoundaries (0.01 , 0.05 , 0.1 , 0.5 , 1 , 2 , 5 , 10 , 30 , 60 , 120 ),
110- )
111- if err != nil {
112- panic (fmt .Sprintf ("failed to create porch_perf_operation_duration_seconds: %v" , err ))
113- }
114-
115- perfOperationCounter , err = m .Float64Counter (
116- "porch_perf_operations_total" ,
117- metric .WithDescription ("Total number of Porch performance test operations" ),
118- )
119- if err != nil {
120- panic (fmt .Sprintf ("failed to create porch_perf_operations_total: %v" , err ))
121- }
122-
123- perfRepositoryCounter , err = m .Float64Counter (
124- "porch_perf_repositories_created_total" ,
125- metric .WithDescription ("Total number of repositories created in performance tests" ),
126- )
127- if err != nil {
128- panic (fmt .Sprintf ("failed to create porch_perf_repositories_created_total: %v" , err ))
129- }
130-
131- perfPackageCounter , err = m .Float64Counter (
132- "porch_perf_packages_created_total" ,
133- metric .WithDescription ("Total number of packages created in performance tests" ),
134- )
135- if err != nil {
136- panic (fmt .Sprintf ("failed to create porch_perf_packages_created_total: %v" , err ))
137- }
138-
139- perfPackageRevisionCounter , err = m .Float64Counter (
140- "porch_perf_package_revisions_total" ,
141- metric .WithDescription ("Total number of package revisions created in performance tests" ),
142- )
143- if err != nil {
144- panic (fmt .Sprintf ("failed to create porch_perf_package_revisions_total: %v" , err ))
145- }
146-
147- perfLifecycleTransitionDuration , err = m .Float64Histogram (
148- "porch_perf_lifecycle_transition_duration_seconds" ,
149- metric .WithDescription ("Duration of package lifecycle transitions in seconds" ),
150- metric .WithUnit ("s" ),
151- metric .WithExplicitBucketBoundaries (0.1 , 0.5 , 1 , 2 , 5 , 10 , 30 , 60 ),
152- )
153- if err != nil {
154- panic (fmt .Sprintf ("failed to create porch_perf_lifecycle_transition_duration_seconds: %v" , err ))
155- }
156-
157- perfTestRunInfoGauge , err = m .Float64Gauge (
158- "porch_perf_test_run_info" ,
159- metric .WithDescription ("Information about the current performance test run" ),
160- )
161- if err != nil {
162- panic (fmt .Sprintf ("failed to create porch_perf_test_run_info: %v" , err ))
163- }
164-
165- perfActiveOperations , err = m .Float64UpDownCounter (
166- "porch_perf_active_operations" ,
167- metric .WithDescription ("Number of currently active operations" ),
168- )
169- if err != nil {
170- panic (fmt .Sprintf ("failed to create porch_perf_active_operations: %v" , err ))
171- }
172-
17398 return nil
17499}
175100
176101// Porch server and function runner metric recording functions
177102func RecordAPICallDuration (resource , verb , apiVersion string , durationSeconds float64 ) {
178103 if apiCallDurationSeconds == nil {
104+ klog .Warning ("apiCallDurationSeconds is nil - was InitMetrics() called?" )
179105 return
180106 }
181107 apiCallDurationSeconds .Record (context .Background (), durationSeconds ,
@@ -188,14 +114,16 @@ func RecordAPICallDuration(resource, verb, apiVersion string, durationSeconds fl
188114}
189115
190116func RecordRequestCount (ctx context.Context , resource , op , apiVersion string ) {
191- if RequestsTotal == nil {
117+ if requestsTotal == nil {
118+ klog .Warning ("requestsTotal is nil - was InitMetrics() called?" )
192119 return
193120 }
194121 recordRequestCount (resource , op , apiVersion , getK8sUserName (ctx ))
195122}
196123
197124func RecordControllerRequestCount (resource , op , apiVersion string ) {
198- if RequestsTotal == nil {
125+ if requestsTotal == nil {
126+ klog .Warning ("requestsTotal is nil - was InitMetrics() called?" )
199127 return
200128 }
201129 recordRequestCount (resource , op , apiVersion , ControllerUser )
@@ -208,7 +136,7 @@ func RecordControllerOperation(resource, verb string, start time.Time) {
208136}
209137
210138func recordRequestCount (resource , op , apiVersion , user string ) {
211- RequestsTotal .Add (context .Background (), 1 ,
139+ requestsTotal .Add (context .Background (), 1 ,
212140 metric .WithAttributes (
213141 attribute .String ("resource" , resource ),
214142 attribute .String ("op" , op ),
@@ -226,6 +154,7 @@ func RecordExternalRepoOperation(ctx context.Context, op string, start time.Time
226154
227155func recordExternalRepoDuration (op string , durationSeconds float64 ) {
228156 if apiCallDurationSeconds == nil {
157+ klog .Warning ("apiCallDurationSeconds is nil - was InitMetrics() called?" )
229158 return
230159 }
231160 apiCallDurationSeconds .Record (context .Background (), durationSeconds ,
@@ -237,10 +166,11 @@ func recordExternalRepoDuration(op string, durationSeconds float64) {
237166}
238167
239168func RecordExternalRepoRequestCount (ctx context.Context , op string ) {
240- if RequestsTotal == nil {
169+ if requestsTotal == nil {
170+ klog .Warning ("requestsTotal is nil - was InitMetrics() called?" )
241171 return
242172 }
243- RequestsTotal .Add (context .Background (), 1 ,
173+ requestsTotal .Add (context .Background (), 1 ,
244174 metric .WithAttributes (
245175 attribute .String ("resource" , ResourceExternalRepo ),
246176 attribute .String ("op" , op ),
@@ -283,77 +213,6 @@ func RecordPackageRevisionResourcesSize(ctx context.Context, prKey repository.Pa
283213 prResourceSizeGauge .Record (ctx , resourcesSize , metric .WithAttributeSet (attributes ))
284214}
285215
286- // Performance test metric recording functions
287- func PerfTestRecordMetric (operation , apiVersion , repoName , pkgName string , duration time.Duration , err error ) {
288- attrs := metric .WithAttributes (
289- attribute .String ("operation" , operation ),
290- attribute .String ("api_version" , apiVersion ),
291- attribute .String ("repository" , repoName ),
292- attribute .String ("package" , pkgName ),
293- attribute .String ("status" , statusLabel (err )),
294- )
295- ctx := context .Background ()
296- perfOperationDuration .Record (ctx , duration .Seconds (), attrs )
297- perfOperationCounter .Add (ctx , 1 , attrs )
298- }
299-
300- func PerfTestRecordLifecycleTransition (fromState , toState , apiVersion , repoName , pkgName string , duration time.Duration , err error ) {
301- perfLifecycleTransitionDuration .Record (context .Background (), duration .Seconds (),
302- metric .WithAttributes (
303- attribute .String ("from_state" , fromState ),
304- attribute .String ("to_state" , toState ),
305- attribute .String ("api_version" , apiVersion ),
306- attribute .String ("repository" , repoName ),
307- attribute .String ("package" , pkgName ),
308- attribute .String ("status" , statusLabel (err )),
309- ),
310- )
311- }
312-
313- func PerfTestRecordPackageRevision (operation string , err error ) {
314- perfPackageRevisionCounter .Add (context .Background (), 1 ,
315- metric .WithAttributes (
316- attribute .String ("operation" , operation ),
317- attribute .String ("status" , statusLabel (err )),
318- ),
319- )
320- }
321-
322- func PerfTestSetTestRunInfo (testName , namespace , apiVersion string , startTime time.Time ) {
323- perfTestRunInfoGauge .Record (context .Background (), 1 ,
324- metric .WithAttributes (
325- attribute .String ("test_name" , testName ),
326- attribute .String ("namespace" , namespace ),
327- attribute .String ("api_version" , apiVersion ),
328- attribute .String ("start_time" , startTime .Format (time .RFC3339 )),
329- ),
330- )
331- }
332-
333- func PerfTestRecordActiveOperation (operation , apiVersion string , delta float64 ) {
334- perfActiveOperations .Add (context .Background (), delta ,
335- metric .WithAttributes (
336- attribute .String ("operation" , operation ),
337- attribute .String ("api_version" , apiVersion ),
338- ),
339- )
340- }
341-
342- func PerfTestIncrementRepositoryCounter () {
343- perfRepositoryCounter .Add (context .Background (), 1 )
344- }
345-
346- func PerfTestIncrementPackageCounter () {
347- perfPackageCounter .Add (context .Background (), 1 )
348- }
349-
350- func statusLabel (err error ) string {
351- if err != nil {
352- return "error"
353- }
354- return "success"
355- }
356-
357216func getK8sUserName (ctx context.Context ) string {
358217 if user , ok := request .UserFrom (ctx ); ok {
359218 return user .GetName ()
0 commit comments