-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Metrics `results_reconciled` which tells us what type of logging is being used for results installation. This metric can be use for telemetry.
- Loading branch information
1 parent
8265d80
commit 66473ca
Showing
9 changed files
with
818 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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,108 @@ | ||
/* | ||
Copyright 2023 The Tekton 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 tektonresult | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" | ||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
"go.opencensus.io/tag" | ||
"go.uber.org/zap" | ||
"knative.dev/pkg/metrics" | ||
) | ||
|
||
var ( | ||
rReconcileCount = stats.Float64("results_reconciled", | ||
"results reconciled with their log type", | ||
stats.UnitDimensionless) | ||
rReconcilerCountView *view.View | ||
|
||
errUninitializedRecorder = fmt.Errorf("ignoring the metrics recording for result failed to initialize the metrics recorder") | ||
) | ||
|
||
// Recorder holds keys for Tekton metrics | ||
type Recorder struct { | ||
initialized bool | ||
version tag.Key | ||
logType tag.Key | ||
} | ||
|
||
// NewRecorder creates a new metrics recorder instance | ||
// to log the PipelineRun related metrics | ||
func NewRecorder() (*Recorder, error) { | ||
r := &Recorder{ | ||
initialized: true, | ||
} | ||
|
||
version, err := tag.NewKey("version") | ||
if err != nil { | ||
return nil, err | ||
} | ||
r.version = version | ||
|
||
logType, err := tag.NewKey("log_type") | ||
if err != nil { | ||
return nil, err | ||
} | ||
r.logType = logType | ||
|
||
rReconcilerCountView = &view.View{ | ||
Description: rReconcileCount.Description(), | ||
Measure: rReconcileCount, | ||
Aggregation: view.LastValue(), | ||
TagKeys: []tag.Key{r.version, r.logType}, | ||
} | ||
|
||
err = view.Register(rReconcilerCountView) | ||
|
||
if err != nil { | ||
r.initialized = false | ||
return r, err | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
// Record the Results reconciled with their log type | ||
func (r *Recorder) Count(version, logType string) error { | ||
if !r.initialized { | ||
return errUninitializedRecorder | ||
} | ||
|
||
ctx, err := tag.New( | ||
context.Background(), | ||
tag.Insert(r.version, version), | ||
tag.Insert(r.logType, logType), | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
metrics.Record(ctx, rReconcileCount.M(float64(1))) | ||
return nil | ||
} | ||
|
||
func (m *Recorder) LogMetrics(version string, spec v1alpha1.TektonResultSpec, logger *zap.SugaredLogger) { | ||
err := m.Count(version, spec.LogsType) | ||
if err != nil { | ||
logger.Warnf("%v: Failed to log the metrics : %v", v1alpha1.KindTektonResult, err) | ||
} | ||
} |
This file contains 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,47 @@ | ||
/* | ||
Copyright 2023 The Tekton 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 tektonresult | ||
|
||
import ( | ||
"testing" | ||
|
||
"knative.dev/pkg/metrics/metricstest" // Required to setup metrics env for testing | ||
_ "knative.dev/pkg/metrics/testing" | ||
) | ||
|
||
func TestUninitializedMetrics(t *testing.T) { | ||
recorder := Recorder{} | ||
if err := recorder.Count("v0.1", "GCS"); err != errUninitializedRecorder { | ||
t.Errorf("recorder.Count recording expected to return error %s but got %s", errUninitializedRecorder.Error(), err.Error()) | ||
} | ||
} | ||
|
||
func TestMetricsCount(t *testing.T) { | ||
|
||
metricstest.Unregister("results_reconciled") | ||
|
||
recorder, err := NewRecorder() | ||
if err != nil { | ||
t.Errorf("failed to initilized recorder, got %s", err.Error()) | ||
|
||
} | ||
if err := recorder.Count("v0.1", "GCS"); err != nil { | ||
t.Errorf("recorder.Count recording failed got %s", err.Error()) | ||
} | ||
metricstest.CheckStatsReported(t, "results_reconciled") | ||
metricstest.CheckLastValueData(t, "results_reconciled", map[string]string{"version": "v0.1", "log_type": "GCS"}, float64(1)) | ||
} |
This file contains 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
Oops, something went wrong.