Skip to content

Commit

Permalink
feat(kuma-cp): make OpenTelemetry control plane tracing fully configu…
Browse files Browse the repository at this point in the history
…rable (kumahq#7936)

Signed-off-by: Mike Beaumont <[email protected]>
  • Loading branch information
michaelbeaumont authored Oct 3, 2023
1 parent 2697f09 commit 3fd5819
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
5 changes: 5 additions & 0 deletions pkg/config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,10 @@ proxy:
globalDownstreamMaxConnections: 1
eventBus:
bufferSize: 30
tracing:
openTelemetry:
enabled: true
endpoint: collector:4317
`,
}),
Entry("from env variables", testCase{
Expand Down Expand Up @@ -944,6 +948,7 @@ eventBus:
"KUMA_EXPERIMENTAL_KDS_EVENT_BASED_WATCHDOG_DELAY_FULL_RESYNC": "true",
"KUMA_PROXY_GATEWAY_GLOBAL_DOWNSTREAM_MAX_CONNECTIONS": "1",
"KUMA_TRACING_OPENTELEMETRY_ENDPOINT": "otel-collector:4317",
"KUMA_TRACING_OPENTELEMETRY_ENABLED": "true",
"KUMA_EVENT_BUS_BUFFER_SIZE": "30",
},
yamlFileConfig: "",
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/tracing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func (c Config) Validate() error {
}

type OpenTelemetry struct {
Enabled bool `json:"enabled,omitempty" envconfig:"kuma_tracing_opentelemetry_enabled"`
// This field is deprecated. Use generic OTEL_EXPORTER_OTLP_* variables
// instead.
// Address of OpenTelemetry collector.
// E.g. otel-collector:4317
Endpoint string `json:"endpoint,omitempty" envconfig:"kuma_tracing_opentelemetry_endpoint"`
Expand Down
24 changes: 9 additions & 15 deletions pkg/plugins/runtime/opentelemetry/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"
"time"

"github.com/go-logr/logr"
datadog "github.com/tonglil/opentelemetry-go-datadog-propagator"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/kumahq/kuma/pkg/config/tracing"
"github.com/kumahq/kuma/pkg/core"
Expand All @@ -38,7 +37,7 @@ type tracer struct {
var _ component.Component = &tracer{}

func (t *tracer) Start(stop <-chan struct{}) error {
shutdown, err := initOtel(context.Background(), t.config)
shutdown, err := initOtel(context.Background(), log, t.config)
if err != nil {
return err
}
Expand All @@ -58,7 +57,7 @@ func (t *tracer) NeedLeaderElection() bool {

func (p *plugin) Customize(rt core_runtime.Runtime) error {
otel := rt.Config().Tracing.OpenTelemetry
if otel.Endpoint == "" {
if !otel.Enabled || otel.Endpoint == "" {
return nil
}

Expand All @@ -72,7 +71,7 @@ func (p *plugin) Customize(rt core_runtime.Runtime) error {
return nil
}

func initOtel(ctx context.Context, otelConfig tracing.OpenTelemetry) (func(context.Context) error, error) {
func initOtel(ctx context.Context, log logr.Logger, otelConfig tracing.OpenTelemetry) (func(context.Context) error, error) {
res, err := resource.New(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
Expand All @@ -81,18 +80,13 @@ func initOtel(ctx context.Context, otelConfig tracing.OpenTelemetry) (func(conte
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

conn, err := grpc.DialContext(
ctx,
otelConfig.Endpoint,
grpc.WithTransportCredentials(
insecure.NewCredentials(),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
var opts []otlptracegrpc.Option
if otelConfig.Endpoint != "" {
log.Info("DEPRECATED: KUMA_TRACING_OPENTELEMETRY_ENDPOINT is deprecated, use OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_INSECURE instead")
opts = append(opts, otlptracegrpc.WithEndpoint(otelConfig.Endpoint), otlptracegrpc.WithInsecure())
}

traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
traceExporter, err := otlptracegrpc.New(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create trace exporter: %w", err)
}
Expand Down

0 comments on commit 3fd5819

Please sign in to comment.