Skip to content

Conversation

@JAORMX
Copy link
Collaborator

@JAORMX JAORMX commented Nov 6, 2025

Summary

Refactors OpenTelemetry configuration commands to use the generic config field framework introduced in #2483, demonstrating the framework's value by eliminating ~450 lines of boilerplate code.

Note: This PR is based on #2483 and includes those commits. It should be reviewed after #2483 is merged. The actual changes in this PR are only in the most recent commit.

Changes

New OTEL Field Registrations (pkg/config/fields_otel.go)

Registers 7 OTEL fields with the generic framework:

  • otel-endpoint: OTLP endpoint URL with protocol validation
  • otel-sampling-rate: Sampling rate with 0.0-1.0 range validation
  • otel-env-vars: Comma-separated environment variables
  • otel-metrics-enabled: Boolean metrics export flag
  • otel-tracing-enabled: Boolean tracing export flag
  • otel-insecure: Boolean insecure connection flag
  • otel-enable-prometheus-metrics-path: Boolean Prometheus metrics path flag

Refactored Commands (cmd/thv/app/otel.go)

  • Before: 577 lines with 18 hand-written command functions (set/get/unset × 7 fields)
  • After: 127 lines with 3 generic command generators
  • Reduction: 450 lines removed (78% reduction)

Replaced boilerplate functions with three generic helpers:

  • createOTELSetCommand() - Generic set command generator
  • createOTELGetCommand() - Generic get command generator
  • createOTELUnsetCommand() - Generic unset command generator

Benefits

Dramatic Code Reduction: 78% less code (577 → 127 lines)
Consistent Behavior: All commands use the same framework logic
Easier Maintenance: Changes to validation/error handling apply to all fields
Proven Pattern: Same approach used for ca-cert, registry-url, registry-file
No Behavioral Changes: All OTEL commands work identically

Before & After

Before (per field):

func setOtelEndpointCmdFunc(_ *cobra.Command, args []string) error {
    endpoint := args[0]
    if endpoint != "" && (strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://")) {
        return fmt.Errorf("endpoint URL should not start with http:// or https://")
    }
    err := config.UpdateConfig(func(c *config.Config) {
        c.OTEL.Endpoint = endpoint
    })
    if err != nil {
        return fmt.Errorf("failed to update configuration: %w", err)
    }
    fmt.Printf("Successfully set OpenTelemetry endpoint: %s\n", endpoint)
    return nil
}
// + getOtelEndpointCmdFunc + unsetOtelEndpointCmdFunc
// × 7 fields = 21 functions

After (all fields):

RegisterConfigField(ConfigFieldSpec{
    Name: "otel-endpoint",
    SetValidator: func(_ Provider, value string) error {
        if value != "" && (strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://")) {
            return fmt.Errorf("endpoint URL should not start with http:// or https://")
        }
        return nil
    },
    Setter: func(cfg *Config, value string) { cfg.OTEL.Endpoint = value },
    Getter: func(cfg *Config) string { return cfg.OTEL.Endpoint },
    Unsetter: func(cfg *Config) { cfg.OTEL.Endpoint = "" },
    // ... metadata
})

// Commands generated automatically via framework
OtelCmd.AddCommand(createOTELSetCommand("otel-endpoint", "endpoint", "endpoint URL", "https://api.honeycomb.io"))

Test Plan

  • All config tests pass
  • Build successful
  • OTEL commands generate correctly (verified via --help)
  • No behavioral changes to existing commands

Dependencies

Depends on #2483 - This PR is stacked on top of the config field framework PR. Review #2483 first.

🤖 Generated with Claude Code

Refactor OpenTelemetry configuration commands to use the generic config
field framework, eliminating ~450 lines of boilerplate code.

Changes:
- Add OTEL field registrations (otel-endpoint, otel-sampling-rate,
  otel-env-vars, otel-metrics-enabled, otel-tracing-enabled,
  otel-insecure, otel-enable-prometheus-metrics-path)
- Replace 18 hand-written command functions with 3 generic helpers
- Reduce cmd/thv/app/otel.go from 577 lines to 127 lines (78% reduction)

All OTEL commands maintain identical behavior with consistent validation
and error messages through the framework.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@JAORMX JAORMX closed this Nov 6, 2025
@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

❌ Patch coverage is 18.58974% with 127 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.98%. Comparing base (1a45143) to head (e917164).

Files with missing lines Patch % Lines
pkg/config/fields_otel.go 18.58% 120 Missing and 7 partials ⚠️
Additional details and impacted files
@@                       Coverage Diff                       @@
##           feat/config-field-framework    #2486      +/-   ##
===============================================================
- Coverage                        55.19%   54.98%   -0.21%     
===============================================================
  Files                              294      295       +1     
  Lines                            28060    28209     +149     
===============================================================
+ Hits                             15487    15510      +23     
- Misses                           11163    11283     +120     
- Partials                          1410     1416       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JAORMX JAORMX deleted the feat/otel-use-config-framework branch November 6, 2025 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants