diff --git a/pkg/adapter/config.go b/pkg/adapter/config.go index 6509b1b779e..2439ffbefad 100644 --- a/pkg/adapter/config.go +++ b/pkg/adapter/config.go @@ -40,6 +40,11 @@ type EnvConfig struct { // This is used to configure the logging config, the config is stored in // a config map inside the controllers namespace and copied here. LoggingConfigJson string `envconfig:"K_LOGGING_CONFIG" required:"true"` + + // TracingConfigJson is a json string of tracing.Config. + // This is used to configure the tracing config, the config is stored in + // a config map inside the controllers namespace and copied here. + TracingConfigJson string `envconfig:"K_TRACING_CONFIG" required:"true"` } // EnvConfigAccessor defines accessors for the minimal @@ -56,6 +61,9 @@ type EnvConfigAccessor interface { // Get the json string of logging.Config. GetLoggingConfigJson() string + + // Get the json string of tracubg.Config. + GetTracingConfigJson() string } func (e *EnvConfig) GetMetricsConfigJson() string { @@ -66,6 +74,10 @@ func (e *EnvConfig) GetLoggingConfigJson() string { return e.LoggingConfigJson } +func (e *EnvConfig) GetTracingConfigJson() string { + return e.TracingConfigJson +} + func (e *EnvConfig) GetSinkURI() string { if e.Sink != "" { return e.Sink diff --git a/pkg/adapter/config_test.go b/pkg/adapter/config_test.go index 5e82d40105f..afe9d14604f 100644 --- a/pkg/adapter/config_test.go +++ b/pkg/adapter/config_test.go @@ -33,6 +33,7 @@ func TestEnvConfig(t *testing.T) { os.Setenv("NAMESPACE", "ns") os.Setenv("K_METRICS_CONFIG", "metrics") os.Setenv("K_LOGGING_CONFIG", "logging") + os.Setenv("K_TRACING_CONFIG", "tracing") os.Setenv("MODE", "mymode") var env myEnvConfig diff --git a/pkg/adapter/main.go b/pkg/adapter/main.go index e544ac0b4c0..60e08bd1415 100644 --- a/pkg/adapter/main.go +++ b/pkg/adapter/main.go @@ -36,6 +36,7 @@ import ( "knative.dev/pkg/profiling" "knative.dev/pkg/signals" "knative.dev/pkg/source" + tracingconfig "knative.dev/pkg/tracing/config" "knative.dev/eventing/pkg/kncloudevents" "knative.dev/eventing/pkg/tracing" @@ -115,7 +116,12 @@ func MainWithContext(ctx context.Context, component string, ector EnvConfigConst logger.Error("error building statsreporter", zap.Error(err)) } - if err = tracing.SetupStaticPublishing(logger, "", tracing.OnePercentSampling); err != nil { + // Retrieve tracing config + config, err := tracingconfig.JsonToTracingConfig(env.GetTracingConfigJson()) + if err != nil { + logger.Warn("failed to create tracing options, using defaults", zap.Error(err)) + } + if err := tracing.SetupStaticPublishing(logger, component, config); err != nil { // If tracing doesn't work, we will log an error, but allow the adapter // to continue to start. logger.Error("Error setting up trace publishing", zap.Error(err)) diff --git a/pkg/adapter/main_message_adapter.go b/pkg/adapter/main_message_adapter.go index b82a53d673b..117f64d7ff3 100644 --- a/pkg/adapter/main_message_adapter.go +++ b/pkg/adapter/main_message_adapter.go @@ -26,6 +26,7 @@ import ( "knative.dev/pkg/profiling" "knative.dev/pkg/signals" + tracingconfig "knative.dev/pkg/tracing/config" "github.com/kelseyhightower/envconfig" "go.opencensus.io/stats/view" @@ -112,7 +113,12 @@ func MainMessageAdapterWithContext(ctx context.Context, component string, ector logger.Error("error building statsreporter", zap.Error(err)) } - if err = tracing.SetupStaticPublishing(logger, "", tracing.OnePercentSampling); err != nil { + // Retrieve tracing config + config, err := tracingconfig.JsonToTracingConfig(env.GetTracingConfigJson()) + if err != nil { + logger.Warn("failed to create tracing options, using defaults", zap.Error(err)) + } + if err := tracing.SetupStaticPublishing(logger, component, config); err != nil { // If tracing doesn't work, we will log an error, but allow the adapter // to continue to start. logger.Error("Error setting up trace publishing", zap.Error(err)) diff --git a/pkg/adapter/v2/config.go b/pkg/adapter/v2/config.go index ae453518409..8f659cedf1f 100644 --- a/pkg/adapter/v2/config.go +++ b/pkg/adapter/v2/config.go @@ -19,10 +19,13 @@ import ( "encoding/json" "go.uber.org/zap" + tracingconfig "knative.dev/pkg/tracing/config" duckv1 "knative.dev/pkg/apis/duck/v1" "knative.dev/pkg/logging" "knative.dev/pkg/metrics" + + "knative.dev/eventing/pkg/tracing" ) type EnvConfigConstructor func() EnvConfigAccessor @@ -58,6 +61,11 @@ type EnvConfig struct { // This is used to configure the logging config, the config is stored in // a config map inside the controllers namespace and copied here. LoggingConfigJson string `envconfig:"K_LOGGING_CONFIG" required:"true"` + + // TracingConfigJson is a json string of tracing.Config. + // This is used to configure the tracing config, the config is stored in + // a config map inside the controllers namespace and copied here. + TracingConfigJson string `envconfig:"K_TRACING_CONFIG" required:"true"` } // EnvConfigAccessor defines accessors for the minimal @@ -81,6 +89,9 @@ type EnvConfigAccessor interface { // Get the parsed logger. GetLogger() *zap.SugaredLogger + // Setup tracing + SetupTracing(*zap.SugaredLogger) error + GetCloudEventOverrides() (*duckv1.CloudEventOverrides, error) } @@ -126,6 +137,14 @@ func (e *EnvConfig) GetName() string { return e.Name } +func (e *EnvConfig) SetupTracing(logger *zap.SugaredLogger) error { + config, err := tracingconfig.JsonToTracingConfig(e.TracingConfigJson) + if err != nil { + logger.Error("Tracing configuration is invalid, using the no-op default", zap.Error(err)) + } + return tracing.SetupStaticPublishing(logger, e.Component, config) +} + func (e *EnvConfig) GetCloudEventOverrides() (*duckv1.CloudEventOverrides, error) { var ceOverrides duckv1.CloudEventOverrides if len(e.CEOverrides) > 0 { diff --git a/pkg/adapter/v2/config_test.go b/pkg/adapter/v2/config_test.go index 13e5ecacfac..a19bcffc983 100644 --- a/pkg/adapter/v2/config_test.go +++ b/pkg/adapter/v2/config_test.go @@ -33,6 +33,7 @@ func TestEnvConfig(t *testing.T) { os.Setenv("NAMESPACE", "ns") os.Setenv("K_METRICS_CONFIG", "metrics") os.Setenv("K_LOGGING_CONFIG", "logging") + os.Setenv("K_TRACING_CONFIG", "tracing") os.Setenv("MODE", "mymode") // note: custom to this test impl var env myEnvConfig diff --git a/pkg/adapter/v2/main.go b/pkg/adapter/v2/main.go index 395df56bc33..8476342b72d 100644 --- a/pkg/adapter/v2/main.go +++ b/pkg/adapter/v2/main.go @@ -24,7 +24,6 @@ import ( "time" cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/kelseyhightower/envconfig" "go.opencensus.io/stats/view" "go.uber.org/zap" @@ -33,8 +32,6 @@ import ( "knative.dev/pkg/profiling" "knative.dev/pkg/signals" "knative.dev/pkg/source" - - "knative.dev/eventing/pkg/tracing" ) type Adapter interface { @@ -95,7 +92,8 @@ func MainWithContext(ctx context.Context, component string, ector EnvConfigConst logger.Error("error building statsreporter", zap.Error(err)) } - if err = tracing.SetupStaticPublishing(logger, "", tracing.OnePercentSampling); err != nil { + // Setup tracing + if err := env.SetupTracing(logger); err != nil { // If tracing doesn't work, we will log an error, but allow the adapter // to continue to start. logger.Error("Error setting up trace publishing", zap.Error(err)) diff --git a/vendor/knative.dev/pkg/tracing/config/tracing.go b/vendor/knative.dev/pkg/tracing/config/tracing.go index eaa2bb6d884..6cd2ba998f0 100644 --- a/vendor/knative.dev/pkg/tracing/config/tracing.go +++ b/vendor/knative.dev/pkg/tracing/config/tracing.go @@ -17,6 +17,7 @@ limitations under the License. package config import ( + "encoding/json" "errors" "fmt" "reflect" @@ -139,3 +140,43 @@ func NewTracingConfigFromMap(cfgMap map[string]string) (*Config, error) { func NewTracingConfigFromConfigMap(config *corev1.ConfigMap) (*Config, error) { return NewTracingConfigFromMap(config.Data) } + +// JsonToTracingConfig converts a json string of a Config. +// Returns a non-nil Config always and an eventual error. +func JsonToTracingConfig(jsonCfg string) (*Config, error) { + if jsonCfg == "" { + return defaultConfig(), errors.New("empty json tracing config") + } + + var configMap map[string]string + if err := json.Unmarshal([]byte(jsonCfg), &configMap); err != nil { + return defaultConfig(), err + } + + cfg, err := NewTracingConfigFromMap(configMap) + if err != nil { + return defaultConfig(), nil + } + return cfg, nil +} + +// TracingConfigToJson converts a Config to a json string. +func TracingConfigToJson(cfg *Config) (string, error) { + if cfg == nil { + return "", nil + } + + out := make(map[string]string, 5) + out[backendKey] = string(cfg.Backend) + if cfg.ZipkinEndpoint != "" { + out[zipkinEndpointKey] = cfg.ZipkinEndpoint + } + if cfg.StackdriverProjectID != "" { + out[stackdriverProjectIDKey] = cfg.StackdriverProjectID + } + out[debugKey] = strconv.FormatBool(cfg.Debug) + out[sampleRateKey] = strconv.FormatFloat(cfg.SampleRate, 'f', -1, 64) + + jsonCfg, err := json.Marshal(out) + return string(jsonCfg), err +}