Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
exporterTracing string to define the exporter instead of bool
Browse files Browse the repository at this point in the history
exporterTracing string to define the exporter instead of bool
make tracing exporter secure if the right env variables are set
remove from dynamicconfig as it is not working

Signed-off-by: Melchior Moulin <[email protected]>
  • Loading branch information
melchiormoulin committed Jun 28, 2022
1 parent 81abf42 commit 4e84099
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 51 deletions.
18 changes: 8 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ var (

var saramaLogger = log.New(io.Discard, "[Sarama] ", log.LstdFlags)

func initTracerProvider(ActivateTracing bool, JaegerExporterTracing bool, OtlpExporterTracing bool) *sdktrace.TracerProvider {
func initTracerProvider(exporterType string) *sdktrace.TracerProvider {
resources, _ := resource.New(context.Background(),
resource.WithFromEnv(), // pull attributes from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME environment variables
resource.WithProcess(), // This option configures a set of Detectors that discover process information
)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}))
if ActivateTracing {
exporter := exporterTracing(JaegerExporterTracing, OtlpExporterTracing)
if exporterType != "" {
exporter := exporterTracing(exporterType)
tp := sdktrace.NewTracerProvider(
sdktrace.WithResource(resources),
sdktrace.WithBatcher(exporter),
Expand All @@ -65,14 +65,14 @@ func initTracerProvider(ActivateTracing bool, JaegerExporterTracing bool, OtlpEx
return nil
}

func exporterTracing(JaegerExporterTracing bool, OtlpExporterTracing bool) sdktrace.SpanExporter {
func exporterTracing(exporterType string) sdktrace.SpanExporter {
//Could not use OTEL_TRACES_EXPORTER https://github.com/open-telemetry/opentelemetry-go/issues/2310
var exporter sdktrace.SpanExporter
//If the env variables needed are defined we set the exporter to Jaeger else it is an opentelemetry exporter
if JaegerExporterTracing {
if exporterType == "jaeger" {
exporter, _ = jaeger.New(jaeger.WithAgentEndpoint()) //from env variable https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/jaeger#environment-variables
} else if OtlpExporterTracing {
exporter, _ = otlptracegrpc.New(context.Background(), otlptracegrpc.WithInsecure()) //from env variable https://github.com/open-telemetry/opentelemetry-go/blob/main/exporters/otlp/otlptrace/README.md
} else if exporterType == "otlp" {
exporter, _ = otlptracegrpc.New(context.Background()) //from env variable https://github.com/open-telemetry/opentelemetry-go/blob/main/exporters/otlp/otlptrace/README.md
}
return exporter
}
Expand All @@ -92,15 +92,14 @@ func main() {

glog.Infof("Starting Strimzi canary tool [%s] with config: %+v", version, canaryConfig)

tp := initTracerProvider(canaryConfig.ActivateTracing, canaryConfig.JaegerExporterTracing, canaryConfig.OtlpExporterTracing)
tp := initTracerProvider(canaryConfig.ExporterTypeTracing)
defer func() {
if tp != nil {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}
}()

dynamicConfigWatcher, err := config.NewDynamicConfigWatcher(canaryConfig, applyDynamicConfig, config.NewDynamicCanaryConfig)
if err != nil {
glog.Fatalf("Failed to create dynamic config watcher: %v", err)
Expand Down Expand Up @@ -191,7 +190,6 @@ func applyDynamicConfig(dynamicCanaryConfig *config.DynamicCanaryConfig) {
} else {
saramaLogger.SetOutput(io.Discard)
}
initTracerProvider(*dynamicCanaryConfig.ActivateTracing, *dynamicCanaryConfig.JaegerExporterTracing, *dynamicCanaryConfig.OtlpExporterTracing)

glog.Warningf("Applied dynamic config %s", dynamicCanaryConfig)
}
54 changes: 15 additions & 39 deletions internal/config/canary_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ const (
DynamicConfigFileEnvVar = "DYNAMIC_CONFIG_FILE"
DynamicConfigWatcherIntervalEnvVar = "DYNAMIC_CONFIG_WATCHER_INTERVAL"
//TODO: This will be removed when Support the OTEL_TRACES_EXPORTER env var is available in the SDK see: https://github.com/open-telemetry/opentelemetry-go/issues/2310
JaegerEndpointEnvVar = "OTEL_EXPORTER_JAEGER_ENDPOINT"
JaegerAgentHostEnvVar = "OTEL_EXPORTER_JAEGER_AGENT_HOST"
OtlpEndpointEnvVar = "OTEL_EXPORTER_OTLP_ENDPOINT"
OtlpTracesEndpointEnvVar = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
ActivateTracingEnvVar = "ACTIVATE_TRACING"
ExporterTypeTracing = "EXPORTER_TYPE_TRACING"
// default values for environment variables
BootstrapServersDefault = "localhost:9092"
BootstrapBackoffMaxAttemptsDefault = 10
Expand Down Expand Up @@ -82,19 +78,12 @@ const (
StatusTimeWindowDefault = 300000
DynamicConfigFileDefault = ""
DynamicConfigWatcherIntervalDefault = 30000
JaegerEndpointEnvVarDefault = ""
JaegerAgentHostEnvVarDefault = ""
OtlpEndpointEnvVarDefault = ""
OtlpTracesEndpointEnvVarDefault = ""
ActivateTracingEnvVarDefault = false
ExporterTypeTracingDefault = "" //if empty no tracing for now, possible values : "otlp" or "jaeger"
)

type DynamicCanaryConfig struct {
SaramaLogEnabled *bool `json:"saramaLogEnabled"`
VerbosityLogLevel *int `json:"verbosityLogLevel"`
OtlpExporterTracing *bool
JaegerExporterTracing *bool
ActivateTracing *bool `json:"activateTracing"`
SaramaLogEnabled *bool `json:"saramaLogEnabled"`
VerbosityLogLevel *int `json:"verbosityLogLevel"`
}

// CanaryConfig defines the canary tool configuration
Expand Down Expand Up @@ -126,24 +115,16 @@ type CanaryConfig struct {
StatusCheckInterval time.Duration
StatusTimeWindow time.Duration
DynamicConfigWatcherInterval time.Duration
OtlpExporterTracing bool
JaegerExporterTracing bool
ActivateTracing bool
ExporterTypeTracing string
}

func NewDynamicCanaryConfig() *DynamicCanaryConfig {
saramaLogEnabled := lookupBoolEnv(SaramaLogEnabledEnvVar, SaramaLogEnabledDefault)
verbosityLogLevel := lookupIntEnv(VerbosityLogLevelEnvVar, VerbosityLogLevelDefault)
activateTracing := lookupBoolEnv(ActivateTracingEnvVar, ActivateTracingEnvVarDefault)

jaegerExporterTracing := isJaegerTracingExporter()
otlpExporterTracing := isOtlpTracingExporter()
dynamicCanaryConfig := DynamicCanaryConfig{
SaramaLogEnabled: &saramaLogEnabled,
VerbosityLogLevel: &verbosityLogLevel,
OtlpExporterTracing: &otlpExporterTracing,
JaegerExporterTracing: &jaegerExporterTracing,
ActivateTracing: &activateTracing,
SaramaLogEnabled: &saramaLogEnabled,
VerbosityLogLevel: &verbosityLogLevel,
}
return &dynamicCanaryConfig
}
Expand All @@ -167,16 +148,6 @@ func (c DynamicCanaryConfig) String() string {
return str
}

//If the env variables needed are defined we set the exporter to Jaeger else it is an OTLP exporter
func isJaegerTracingExporter() bool {
return lookupStringEnv(JaegerEndpointEnvVar, JaegerEndpointEnvVarDefault) != "" || lookupStringEnv(JaegerAgentHostEnvVar, JaegerAgentHostEnvVarDefault) != ""
}

//If the env variables needed are defined we set the exporter to OTLP else it is an Jaeger exporter
func isOtlpTracingExporter() bool {
return lookupStringEnv(OtlpEndpointEnvVar, OtlpEndpointEnvVarDefault) != "" || lookupStringEnv(OtlpTracesEndpointEnvVar, OtlpTracesEndpointEnvVarDefault) != ""
}

// NewCanaryConfig returns an configuration instance from environment variables
func NewCanaryConfig() *CanaryConfig {
dynamicCanaryConfig := NewDynamicCanaryConfig()
Expand Down Expand Up @@ -209,9 +180,7 @@ func NewCanaryConfig() *CanaryConfig {
StatusTimeWindow: time.Duration(lookupIntEnv(StatusTimeWindowEnvVar, StatusTimeWindowDefault)),
DynamicConfigFile: lookupStringEnv(DynamicConfigFileEnvVar, DynamicConfigFileDefault),
DynamicConfigWatcherInterval: time.Duration(lookupIntEnv(DynamicConfigWatcherIntervalEnvVar, DynamicConfigWatcherIntervalDefault)),
OtlpExporterTracing: isOtlpTracingExporter(),
JaegerExporterTracing: isJaegerTracingExporter(),
ActivateTracing: lookupBoolEnv(ActivateTracingEnvVar, ActivateTracingEnvVarDefault),
ExporterTypeTracing: exporterTypeTracing(),
}
return &config
}
Expand Down Expand Up @@ -317,3 +286,10 @@ func (c CanaryConfig) String() string {
c.ConnectionCheckInterval, c.ConnectionCheckLatencyBuckets, c.StatusCheckInterval, c.StatusTimeWindow,
c.DynamicConfigFile, c.DynamicCanaryConfig, c.DynamicConfigWatcherInterval)
}
func exporterTypeTracing() string {
exporterType := lookupStringEnv(ExporterTypeTracing, ExporterTypeTracingDefault)
if exporterType != "jaeger" && exporterType != "otlp" && exporterType != "" {
panic(fmt.Errorf("%s env variable possible values are : '' or 'jaeger' or 'otlp'", ExporterTypeTracing))
}
return exporterType
}
3 changes: 1 addition & 2 deletions internal/services/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ func NewConsumerService(canaryConfig *config.CanaryConfig, client sarama.Client)
func (cs *ConsumerService) Consume() {
backoff := NewBackoff(maxConsumeAttempts, 5000*time.Millisecond, MaxDefault)
for {
var h sarama.ConsumerGroupHandler
cgh := &consumerGroupHandler{
consumerService: cs,
}
h = otelsarama.WrapConsumerGroupHandler(cgh)
h := otelsarama.WrapConsumerGroupHandler(cgh)

// creating new context with cancellation, for exiting Consume when metadata refresh is needed
ctx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 4e84099

Please sign in to comment.