Skip to content

Conversation

tiffanny29631
Copy link
Contributor

@tiffanny29631 tiffanny29631 commented Oct 21, 2025

Earlier discussions #1881

The migration replaces OpenCensus libraries with OpenTelemetry SDK while preserving:

  • Metric names, type and descriptions
  • Recording patterns
  • Pipeline architecture and data flow
  • Sidecar configurations
  • Export destinations (Prometheus, Cloud Monitoring, Cloud Monarch)

Key Changes

1. Library Dependencies

Before (OpenCensus):

import (
    "go.opencensus.io/stats"
    "go.opencensus.io/stats/view"
    "go.opencensus.io/tag"
    "contrib.go.opencensus.io/exporter/ocagent"
)

After (OpenTelemetry):

import (
    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/metric"
    "go.opentelemetry.io/otel/attribute"
    "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
    "go.opentelemetry.io/otel/sdk/metric"
    "go.opentelemetry.io/otel/sdk/resource"
)

2. Metric Instrument Types

OpenCensus OpenTelemetry Description
stats.Int64 metric.Int64Counter Counter metrics
stats.Int64 metric.Int64Gauge Gauge metrics
stats.Float64 metric.Float64Histogram Histogram metrics

3. Recording Patterns

Before (OpenCensus):

stats.Record(ctx, measurement)

After (OpenTelemetry):

instrument.Record(ctx, value, metric.WithAttributes(attrs...))

4. Tag/Attribute System

Before (OpenCensus):

tagCtx, _ := tag.New(ctx, tag.Upsert(KeyStatus, "success"))

After (OpenTelemetry):

attrs := []attribute.KeyValue{
    attribute.String("status", "success"),
}

5. Exporter Configuration

Before (OpenCensus):

oce, err := ocagent.NewExporter(ocagent.WithInsecure())

After (OpenTelemetry):

exporter, err := otlpmetricgrpc.New(
    context.Background(),
    otlpmetricgrpc.WithInsecure(),
    otlpmetricgrpc.WithEndpoint("otel-collector:4317"),
)

@tiffanny29631 tiffanny29631 requested a review from sdowell October 21, 2025 22:52
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from tiffanny29631. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

Summary of Changes

Hello @tiffanny29631, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request completes a significant migration by replacing the existing OpenCensus metrics implementation with OpenTelemetry. The core purpose is to standardize on OpenTelemetry for observability, ensuring that the system continues to collect and export metrics consistently to various destinations. The changes involve updating core metric definitions, initialization routines, and exporter configurations across multiple components, along with corresponding adjustments to documentation and test utilities.

Highlights

  • Migration to OpenTelemetry SDK: The codebase has been migrated from using OpenCensus libraries to the OpenTelemetry SDK for metrics collection. This change preserves existing metric names, types, descriptions, recording patterns, pipeline architecture, data flow, sidecar configurations, and export destinations (Prometheus, Cloud Monitoring, Cloud Monarch).
  • Updated Metric Initialization and Recording: All components (hydration-controller, reconciler-manager, reconciler, resourcegroup) now use new OpenTelemetry initialization functions (InitializeOTelKustomizeMetrics, InitializeOTelMetrics, InitializeOTelResourceGroupMetrics) and record metrics using the OpenTelemetry API (metric.Int64Gauge, metric.Float64Histogram, metric.Int64Counter) with attribute.KeyValue for labels.
  • OTLP Exporter Configuration: The metric exporters have been updated to use the OTLP gRPC exporter (otlpmetricgrpc.New) instead of the OpenCensus Agent exporter. This includes changes to how exporters are registered and shut down, and updates to related configuration files (otel-collector-config.yaml, otel-agent-cm.yaml, otel-agent-reconciler-cm.yaml) to use otlp receivers and endpoints.
  • Dependency Updates: The go.mod and go.sum files have been updated to remove OpenCensus dependencies (contrib.go.opencensus.io/exporter/ocagent, go.opencensus.io) and introduce OpenTelemetry dependencies (go.opentelemetry.io/otel, go.opentelemetry.io/otel/metric, go.opentelemetry.io/otel/sdk, go.opentelemetry.io/otel/sdk/metric, go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc). The github.com/cenkalti/backoff/v5 library has also been added.
  • Test Infrastructure Modernization: The testing framework has been updated to use testmetrics.NewTestExporter() and testmetrics.ResetGlobalMetrics() for metric validation, replacing the OpenCensus-specific testmetrics.RegisterMetrics() and view.RegisterExporter() calls. This ensures compatibility with the new OpenTelemetry metric system.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully migrates the project from OpenCensus to OpenTelemetry for metrics. The changes are comprehensive, covering library dependencies, metric instrumentation, recording patterns, and exporter configurations across the codebase. The test suite has also been updated to validate the new OpenTelemetry metrics. My review focuses on improving the robustness of the new implementation, particularly around shutdown contexts and resource attribute configuration.


defer func() {
if err := oce.Stop(); err != nil {
if err := oce.Shutdown(context.Background()); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using context.Background() for shutdown operations can cause the process to hang if the shutdown does not complete. It is recommended to use a context with a timeout to ensure a graceful but bounded shutdown. For example:

shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := oce.Shutdown(shutdownCtx); err != nil {
    // ...
}

This would require importing the time package.


defer func() {
if err := oce.Stop(); err != nil {
if err := oce.Shutdown(context.Background()); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using context.Background() for shutdown operations can cause the process to hang if the shutdown does not complete. It is recommended to use a context with a timeout to ensure a graceful but bounded shutdown. For example:

shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := oce.Shutdown(shutdownCtx); err != nil {
    // ...
}

This would require importing the time package.

setupLog.Error(err, "problem running manager")
// os.Exit(1) does not run deferred functions so explicitly stopping the OC Agent exporter.
if err := oce.Stop(); err != nil {
if err := oce.Shutdown(context.Background()); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using context.Background() for shutdown operations can cause the process to hang if the shutdown does not complete. It is recommended to use a context with a timeout to ensure a graceful but bounded shutdown. For example:

shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := oce.Shutdown(shutdownCtx); err != nil {
    // ...
}

This would require importing the time package.


defer func() {
if err := oce.Stop(); err != nil {
if err := oce.Shutdown(context.Background()); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using context.Background() for shutdown operations can cause the process to hang if the shutdown does not complete. It is recommended to use a context with a timeout to ensure a graceful but bounded shutdown. For example:

shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := oce.Shutdown(shutdownCtx); err != nil {
    // ...
}

This would require importing the time package.

Comment on lines 39 to 42
if APICallDuration == nil {
klog.Errorf("APICallDuration metric is not initialized!")
return
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Checking for nil here prevents a panic, but it also means metrics will be silently dropped if InitializeOTelMetrics is not called. This could hide an initialization problem. Consider panicking here to fail fast during development and testing if a metric is used without being initialized. This would enforce that InitializeOTelMetrics is always called before any metric is recorded.

@sdowell
Copy link
Contributor

sdowell commented Oct 21, 2025

@tiffanny29631 will you be responding/incorporating the feedback from #1881?

@tiffanny29631 tiffanny29631 force-pushed the oc-migration branch 3 times, most recently from 25f0018 to 44c34df Compare October 21, 2025 23:59
@tiffanny29631
Copy link
Contributor Author

@tiffanny29631 will you be responding/incorporating the feedback from #1881?

Working on it now

Change is meant to be transparent to user.

Using OTel SDK for metric composing in: kmetrics, core metrics, resource
group metrics;

Using otlp receiver in otel-agent and otel-collector;

Configured deployment for new ports and component;

Refactor metric composing and recording;

Metric prefix remain the same to minimize breaking change.

Tests updated.
- pass on context in register
Copy link

@tiffanny29631: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
kpt-config-sync-presubmit 0b90260 link true /test kpt-config-sync-presubmit

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants