Skip to content

Commit

Permalink
Configuration for tracing in sources
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
  • Loading branch information
slinkydeveloper committed Apr 21, 2020
1 parent 846c645 commit 66186ac
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 6 deletions.
12 changes: 12 additions & 0 deletions pkg/adapter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/adapter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion pkg/adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion pkg/adapter/main_message_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
19 changes: 19 additions & 0 deletions pkg/adapter/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -81,6 +89,9 @@ type EnvConfigAccessor interface {
// Get the parsed logger.
GetLogger() *zap.SugaredLogger

// Setup tracing
SetupTracing(*zap.SugaredLogger) error

GetCloudEventOverrides() (*duckv1.CloudEventOverrides, error)
}

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/adapter/v2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions pkg/adapter/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
41 changes: 41 additions & 0 deletions vendor/knative.dev/pkg/tracing/config/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -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
}

0 comments on commit 66186ac

Please sign in to comment.