Skip to content

Commit 0073e6f

Browse files
committed
Migrate metrics from OpenCensus to OpenTelemetry
Replace OpenCensus imports (go.opencensus.io/stats, stats/view, tag) with OpenTelemetry (go.opentelemetry.io/otel, otel/attribute, otel/metric). Also updates config-observability.yaml to use OTel configuration keys (metrics-protocol, tracing-protocol) and removes legacy OpenCensus keys. Signed-off-by: Shubham Bhardwaj <shubbhar@redhat.com>
1 parent 40cf3e0 commit 0073e6f

File tree

6 files changed

+248
-306
lines changed

6 files changed

+248
-306
lines changed

config/base/config-observability.yaml

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ metadata:
1919
labels:
2020
app.kubernetes.io/instance: default
2121
data:
22+
metrics-protocol: prometheus
2223
_example: |
2324
################################
2425
# #
2526
# EXAMPLE CONFIGURATION #
2627
# #
2728
################################
29+
2830
# This block is not actually functional configuration,
2931
# but serves to illustrate the available configuration
3032
# options and document them in a way that is accessible
@@ -33,18 +35,42 @@ data:
3335
# These sample configuration options may be copied out of
3436
# this example block and unindented to be in the data block
3537
# to actually change the configuration.
36-
# metrics.backend-destination field specifies the system metrics destination.
37-
# It supports either prometheus (the default) or stackdriver.
38-
# Note: Using Stackdriver will incur additional charges.
39-
metrics.backend-destination: prometheus
40-
# metrics.stackdriver-project-id field specifies the Stackdriver project ID. This
41-
# field is optional. When running on GCE, application default credentials will be
42-
# used and metrics will be sent to the cluster's project if this field is
43-
# not provided.
44-
metrics.stackdriver-project-id: "<your stackdriver project id>"
45-
# metrics.allow-stackdriver-custom-metrics indicates whether it is allowed
46-
# to send metrics to Stackdriver using "global" resource type and custom
47-
# metric type. Setting this flag to "true" could cause extra Stackdriver
48-
# charge. If metrics.backend-destination is not Stackdriver, this is
49-
# ignored.
50-
metrics.allow-stackdriver-custom-metrics: "false"
38+
39+
# OpenTelemetry Metrics Configuration
40+
# Protocol for metrics export (prometheus, grpc, http/protobuf, none)
41+
# Default: prometheus
42+
metrics-protocol: prometheus
43+
44+
# Metrics endpoint (for grpc/http protocols)
45+
# Default: empty (uses default OTLP endpoint)
46+
metrics-endpoint: ""
47+
48+
# Metrics export interval (e.g., "30s", "1m")
49+
# Default: empty (uses SDK default)
50+
metrics-export-interval: ""
51+
52+
# OpenTelemetry Tracing Configuration
53+
# Protocol for tracing export (grpc, http/protobuf, none, stdout)
54+
# Default: none
55+
tracing-protocol: none
56+
57+
# Tracing endpoint (for grpc/http protocols)
58+
# Default: empty
59+
tracing-endpoint: ""
60+
61+
# Tracing sampling rate (0.0 to 1.0)
62+
# Default: 1.0 (100% sampling)
63+
tracing-sampling-rate: "1.0"
64+
65+
# Runtime Configuration
66+
# Enable profiling (enabled, disabled)
67+
# Default: disabled
68+
runtime-profiling: disabled
69+
70+
# Runtime export interval (e.g., "15s")
71+
# Default: 15s
72+
runtime-export-interval: "15s"
73+
74+
# Note: Legacy OpenCensus configuration keys (metrics.backend-destination,
75+
# metrics.stackdriver-project-id, metrics.allow-stackdriver-custom-metrics)
76+
# are no longer recognized. Use the OpenTelemetry options above.

pkg/reconciler/kubernetes/tektonchain/metrics.go

Lines changed: 45 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -19,135 +19,62 @@ package tektonchain
1919
import (
2020
"context"
2121
"fmt"
22+
"sync"
2223
"time"
2324

2425
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
25-
"go.opencensus.io/stats"
26-
"go.opencensus.io/stats/view"
27-
"go.opencensus.io/tag"
26+
"go.opentelemetry.io/otel"
27+
"go.opentelemetry.io/otel/attribute"
28+
"go.opentelemetry.io/otel/metric"
2829
"go.uber.org/zap"
29-
"knative.dev/pkg/metrics"
3030
)
3131

3232
var (
33-
rReconcileCount = stats.Float64("chains_reconciled",
34-
"metrics of chains reconciled with labels",
35-
stats.UnitDimensionless)
33+
rReconcileCount metric.Int64Counter
34+
once sync.Once
35+
errInitMetrics error
3636
)
3737

38+
func initMetrics() error {
39+
meter := otel.Meter("github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektonchain")
40+
var err error
41+
rReconcileCount, err = meter.Int64Counter(
42+
"tekton_operator_lifecycle_chains_reconciled_total",
43+
metric.WithDescription("metrics of chains reconciled with labels"),
44+
)
45+
if err != nil {
46+
return fmt.Errorf("failed to create chains_reconciled counter: %w", err)
47+
}
48+
return nil
49+
}
50+
3851
// Recorder holds keys for Tekton metrics
3952
type Recorder struct {
40-
initialized bool
41-
version tag.Key
42-
taskrunFormat tag.Key
43-
taskrunStorage tag.Key
44-
taskrunSigner tag.Key
45-
pipelinerunFormat tag.Key
46-
pipelinerunStorage tag.Key
47-
pipelinerunSigner tag.Key
48-
ociFormat tag.Key
49-
ociStorage tag.Key
50-
ociSigner tag.Key
51-
53+
initialized bool
5254
ReportingPeriod time.Duration
5355
}
5456

5557
// NewRecorder creates a new metrics recorder instance
56-
// to log the PipelineRun related metrics
5758
func NewRecorder() (*Recorder, error) {
58-
r := &Recorder{
59-
initialized: true,
60-
61-
// Default to 30s intervals.
62-
ReportingPeriod: 30 * time.Second,
63-
}
64-
65-
version, err := tag.NewKey("version")
66-
if err != nil {
67-
return nil, err
68-
}
69-
r.version = version
70-
71-
taskrunFormat, err := tag.NewKey("taskrun_format")
72-
if err != nil {
73-
return nil, err
74-
}
75-
r.taskrunFormat = taskrunFormat
76-
77-
taskrunStorage, err := tag.NewKey("taskrun_storage")
78-
if err != nil {
79-
return nil, err
80-
}
81-
r.taskrunStorage = taskrunStorage
82-
83-
taskrunSigner, err := tag.NewKey("taskrun_signer")
84-
if err != nil {
85-
return nil, err
86-
}
87-
r.taskrunSigner = taskrunSigner
88-
89-
pipelinerunFormat, err := tag.NewKey("pipelinerun_format")
90-
if err != nil {
91-
return nil, err
59+
once.Do(func() {
60+
errInitMetrics = initMetrics()
61+
})
62+
if errInitMetrics != nil {
63+
return nil, errInitMetrics
9264
}
93-
r.pipelinerunFormat = pipelinerunFormat
9465

95-
pipelinerunStorage, err := tag.NewKey("pipelinerun_storage")
96-
if err != nil {
97-
return nil, err
98-
}
99-
r.pipelinerunStorage = pipelinerunStorage
100-
101-
pipelinerunSigner, err := tag.NewKey("pipelinerun_signer")
102-
if err != nil {
103-
return nil, err
104-
}
105-
r.pipelinerunSigner = pipelinerunSigner
106-
107-
ociFormat, err := tag.NewKey("oci_format")
108-
if err != nil {
109-
return nil, err
110-
}
111-
r.ociFormat = ociFormat
112-
113-
ociStorage, err := tag.NewKey("oci_storage")
114-
if err != nil {
115-
return nil, err
116-
}
117-
r.ociStorage = ociStorage
118-
119-
ociSigner, err := tag.NewKey("oci_signer")
120-
if err != nil {
121-
return nil, err
122-
}
123-
r.ociSigner = ociSigner
124-
125-
err = view.Register(
126-
&view.View{
127-
Description: rReconcileCount.Description(),
128-
Measure: rReconcileCount,
129-
Aggregation: view.Count(),
130-
TagKeys: []tag.Key{r.version,
131-
r.taskrunFormat, r.taskrunStorage, r.taskrunSigner,
132-
r.pipelinerunFormat, r.pipelinerunStorage, r.pipelinerunSigner,
133-
r.ociFormat, r.ociStorage, r.ociSigner},
134-
},
135-
)
136-
137-
if err != nil {
138-
r.initialized = false
139-
return r, err
66+
r := &Recorder{
67+
initialized: true,
68+
ReportingPeriod: 30 * time.Second,
14069
}
141-
14270
return r, nil
14371
}
14472

145-
// Logs when chains is reconciled with version and
146-
// config labels.
73+
// Count logs when chains is reconciled with version and config labels.
14774
func (r *Recorder) Count(version string, spec v1alpha1.TektonChainSpec) error {
14875
if !r.initialized {
14976
return fmt.Errorf(
150-
"ignoring the metrics recording for pipelinee failed to initialize the metrics recorder")
77+
"failed to initialize metrics recorder for chains")
15178
}
15279

15380
var taskrunStorage, pipelinerunStorage, ociStorage string
@@ -161,25 +88,20 @@ func (r *Recorder) Count(version string, spec v1alpha1.TektonChainSpec) error {
16188
ociStorage = *spec.ArtifactsOCIStorage
16289
}
16390

164-
ctx, err := tag.New(
165-
context.Background(),
166-
tag.Insert(r.version, version),
167-
tag.Insert(r.taskrunFormat, spec.ArtifactsTaskRunFormat),
168-
tag.Insert(r.taskrunStorage, taskrunStorage),
169-
tag.Insert(r.taskrunSigner, spec.ArtifactsTaskRunSigner),
170-
tag.Insert(r.pipelinerunFormat, spec.ArtifactsPipelineRunFormat),
171-
tag.Insert(r.pipelinerunStorage, pipelinerunStorage),
172-
tag.Insert(r.pipelinerunSigner, spec.ArtifactsPipelineRunSigner),
173-
tag.Insert(r.ociFormat, spec.ArtifactsOCIFormat),
174-
tag.Insert(r.ociStorage, ociStorage),
175-
tag.Insert(r.ociSigner, spec.ArtifactsOCISigner),
91+
rReconcileCount.Add(context.Background(), 1,
92+
metric.WithAttributes(
93+
attribute.String("version", version),
94+
attribute.String("taskrun_format", spec.ArtifactsTaskRunFormat),
95+
attribute.String("taskrun_storage", taskrunStorage),
96+
attribute.String("taskrun_signer", spec.ArtifactsTaskRunSigner),
97+
attribute.String("pipelinerun_format", spec.ArtifactsPipelineRunFormat),
98+
attribute.String("pipelinerun_storage", pipelinerunStorage),
99+
attribute.String("pipelinerun_signer", spec.ArtifactsPipelineRunSigner),
100+
attribute.String("oci_format", spec.ArtifactsOCIFormat),
101+
attribute.String("oci_storage", ociStorage),
102+
attribute.String("oci_signer", spec.ArtifactsOCISigner),
103+
),
176104
)
177-
178-
if err != nil {
179-
return err
180-
}
181-
182-
metrics.Record(ctx, rReconcileCount.M(1))
183105
return nil
184106
}
185107

@@ -192,7 +114,7 @@ func (m *Recorder) LogMetricsWithSpec(version string, spec v1alpha1.TektonChainS
192114

193115
func (m *Recorder) LogMetrics(status, version string, logger *zap.SugaredLogger) {
194116
var newSpec v1alpha1.TektonChainSpec
195-
err := m.Count(status, newSpec)
117+
err := m.Count(version, newSpec)
196118
if err != nil {
197119
logger.Warnf("%v: Failed to log the metrics : %v", resourceKind, err)
198120
}

0 commit comments

Comments
 (0)