Skip to content

Commit 20741fd

Browse files
committed
Add a new option to enable otel roundtrip tracing
Currently, there is no option to enable otel tracing for ocm. Adding EnableOtel option to allow users to config the controller to support otel tracing by wrapping http.RoundTripper with one that starts a span, injects the span context into the outbound request headers, and enriches it with metrics. Signed-off-by: Vu Dinh <[email protected]>
1 parent df87f52 commit 20741fd

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

pkg/common/options/options.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,48 @@ import (
88
"github.com/spf13/pflag"
99
"k8s.io/apimachinery/pkg/version"
1010
"k8s.io/utils/clock"
11+
12+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1113
)
1214

1315
type Options struct {
14-
CmdConfig *controllercmd.ControllerCommandConfig
15-
Burst int
16-
QPS float32
16+
CmdConfig *controllercmd.ControllerCommandConfig
17+
Burst int
18+
QPS float32
19+
EnableOtel bool
1720
}
1821

1922
// NewOptions returns the flags with default value set
2023
func NewOptions() *Options {
2124
opts := &Options{
22-
QPS: 50,
23-
Burst: 100,
25+
QPS: 50,
26+
Burst: 100,
27+
EnableOtel: false,
2428
}
2529
return opts
2630
}
2731

2832
func (o *Options) NewControllerCommandConfig(
2933
componentName string, version version.Info, startFunc controllercmd.StartFunc, clock clock.Clock) *controllercmd.ControllerCommandConfig {
30-
o.CmdConfig = controllercmd.NewControllerCommandConfig(componentName, version, o.startWithQPS(startFunc), clock)
34+
o.CmdConfig = controllercmd.NewControllerCommandConfig(componentName, version, o.startWithOptions(startFunc), clock)
3135
return o.CmdConfig
3236
}
3337

34-
func (o *Options) startWithQPS(startFunc controllercmd.StartFunc) controllercmd.StartFunc {
38+
func (o *Options) startWithOptions(startFunc controllercmd.StartFunc) controllercmd.StartFunc {
3539
return func(ctx context.Context, controllerContext *controllercmd.ControllerContext) error {
3640
controllerContext.KubeConfig.QPS = o.QPS
3741
controllerContext.KubeConfig.Burst = o.Burst
42+
if o.EnableOtel {
43+
controllerContext.KubeConfig.Transport = otelhttp.NewTransport(controllerContext.KubeConfig.Transport)
44+
}
3845
return startFunc(ctx, controllerContext)
3946
}
4047
}
4148

4249
func (o *Options) AddFlags(flags *pflag.FlagSet) {
4350
flags.Float32Var(&o.QPS, "kube-api-qps", o.QPS, "QPS to use while talking with apiserver on spoke cluster.")
4451
flags.IntVar(&o.Burst, "kube-api-burst", o.Burst, "Burst to use while talking with apiserver on spoke cluster.")
52+
flags.BoolVar(&o.EnableOtel, "enable-otel-roundtrip", o.EnableOtel, "enable OpenTelemetry roundtrip.")
4553
if o.CmdConfig != nil {
4654
flags.BoolVar(&o.CmdConfig.DisableLeaderElection, "disable-leader-election", false, "Disable leader election.")
4755
flags.DurationVar(&o.CmdConfig.LeaseDuration.Duration, "leader-election-lease-duration", 137*time.Second, ""+

0 commit comments

Comments
 (0)