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

Commit

Permalink
Changed FromEnv methods to be static and with a single-value return
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling committed Mar 28, 2018
1 parent bdbd31e commit 1e1299d
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 144 deletions.
211 changes: 101 additions & 110 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ import (

const defaultSamplingProbability = 0.001

const (
// environment variable names
envServiceName = "JAEGER_SERVICE_NAME"
envDisabled = "JAEGER_DISABLED"
envRPCMetrics = "JAEGER_RPC_METRICS"
envSamplerType = "JAEGER_SAMPLER_TYPE"
envSamplerParam = "JAEGER_SAMPLER_PARAM"
envSamplerManagerHostPort = "JAEGER_SAMPLER_MANAGER_HOST_PORT"
envSamplerMaxOperations = "JAEGER_SAMPLER_MAX_OPERATIONS"
envSamplerRefreshInterval = "JAEGER_SAMPLER_REFRESH_INTERVAL"
envReporterMaxQueueSize = "JAEGER_REPORTER_MAX_QUEUE_SIZE"
envReporterFlushInterval = "JAEGER_REPORTER_FLUSH_INTERVAL"
envReporterLogSpans = "JAEGER_REPORTER_LOG_SPANS"
envAgentHost = "JAEGER_AGENT_HOST"
envAgentPort = "JAEGER_AGENT_PORT"
)

// Configuration configures and creates Jaeger Tracer
type Configuration struct {
// ServiceName specifies the service name to use on the tracer.
Expand Down Expand Up @@ -134,9 +151,14 @@ func (c Configuration) New(
serviceName string,
options ...Option,
) (opentracing.Tracer, io.Closer, error) {
if serviceName == "" {
if serviceName == "" && c.ServiceName == "" {
return nil, nil, errors.New("no service name provided")
}

if serviceName != "" {
c.ServiceName = serviceName
}

if c.Disabled {
return &opentracing.NoopTracer{}, &nullCloser{}, nil
}
Expand All @@ -160,14 +182,14 @@ func (c Configuration) New(
c.Reporter = &ReporterConfig{}
}

sampler, err := c.Sampler.NewSampler(serviceName, tracerMetrics)
sampler, err := c.Sampler.NewSampler(c.ServiceName, tracerMetrics)
if err != nil {
return nil, nil, err
}

reporter := opts.reporter
if reporter == nil {
r, err := c.Reporter.NewReporter(serviceName, tracerMetrics, opts.logger)
r, err := c.Reporter.NewReporter(c.ServiceName, tracerMetrics, opts.logger)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -204,7 +226,7 @@ func (c Configuration) New(

if c.BaggageRestrictions != nil {
mgr := remote.NewRestrictionManager(
serviceName,
c.ServiceName,
remote.Options.Metrics(tracerMetrics),
remote.Options.Logger(opts.logger),
remote.Options.HostPort(c.BaggageRestrictions.HostPort),
Expand All @@ -217,7 +239,7 @@ func (c Configuration) New(
}

tracer, closer := jaeger.NewTracer(
serviceName,
c.ServiceName,
sampler,
reporter,
tracerOptions...,
Expand All @@ -226,55 +248,6 @@ func (c Configuration) New(
return tracer, closer, nil
}

// FromEnv uses environment variables to set the tracer's Configuration
func (c *Configuration) FromEnv() (*Configuration, error) {
if e := os.Getenv("JAEGER_SERVICE_NAME"); e != "" {
c.ServiceName = e
}

if e := os.Getenv("JAEGER_RPC_METRICS"); e != "" {
if value, err := strconv.ParseBool(e); err == nil {
c.RPCMetrics = value
} else {
return nil, err
}
}

if e := os.Getenv("JAEGER_DISABLED"); e != "" {
if value, err := strconv.ParseBool(e); err == nil {
c.Disabled = value
} else {
return nil, err
}
}

sc := &SamplerConfig{}
if sce, err := sc.FromEnv(); err == nil {
c.Sampler = sce
} else {
return nil, err
}

rc := &ReporterConfig{}
if rce, err := rc.FromEnv(); err == nil {
c.Reporter = rce
} else {
return nil, err
}

return c, nil
}

// NewFromEnv returns a new tracer configured using environment variables
func (c *Configuration) NewFromEnv(options ...Option) (opentracing.Tracer, io.Closer, error) {
if os.Getenv("JAEGER_SERVICE_NAME") == "" {
return nil, nil, errors.New("no service name provided, expected to find one as JAEGER_SERVICE_NAME")
}

c, _ = c.FromEnv()
return c.New(c.ServiceName, options...)
}

// InitGlobalTracer creates a new Jaeger Tracer, and sets it as global OpenTracing Tracer.
// It returns a closer func that can be used to flush buffers before shutdown.
func (c Configuration) InitGlobalTracer(
Expand Down Expand Up @@ -336,110 +309,128 @@ func (sc *SamplerConfig) NewSampler(
return nil, fmt.Errorf("Unknown sampler type %v", sc.Type)
}

// FromEnv creates a new SamplerConfig based on the environment variables
func (sc *SamplerConfig) FromEnv() (*SamplerConfig, error) {
if e := os.Getenv("JAEGER_SAMPLER_TYPE"); e != "" {
// NewReporter instantiates a new reporter that submits spans to tcollector
func (rc *ReporterConfig) NewReporter(
serviceName string,
metrics *jaeger.Metrics,
logger jaeger.Logger,
) (jaeger.Reporter, error) {
sender, err := rc.newTransport()
if err != nil {
return nil, err
}
reporter := jaeger.NewRemoteReporter(
sender,
jaeger.ReporterOptions.QueueSize(rc.QueueSize),
jaeger.ReporterOptions.BufferFlushInterval(rc.BufferFlushInterval),
jaeger.ReporterOptions.Logger(logger),
jaeger.ReporterOptions.Metrics(metrics))
if rc.LogSpans && logger != nil {
logger.Infof("Initializing logging reporter\n")
reporter = jaeger.NewCompositeReporter(jaeger.NewLoggingReporter(logger), reporter)
}
return reporter, err
}

func (rc *ReporterConfig) newTransport() (jaeger.Transport, error) {
return jaeger.NewUDPTransport(rc.LocalAgentHostPort, 0)
}

// FromEnv uses environment variables to set the tracer's Configuration
func FromEnv() *Configuration {
c := &Configuration{}

if e := os.Getenv(envServiceName); e != "" {
// We are ignoring any errors, so that we have a single return value
// This pattern repeats itself across all the "FromEnv" functions.
// TODO: should we at least log when something bad happens, instead of just ignoring?
c.ServiceName = e
}

if e := os.Getenv(envRPCMetrics); e != "" {
if value, err := strconv.ParseBool(e); err == nil {
c.RPCMetrics = value
}
}

if e := os.Getenv(envDisabled); e != "" {
if value, err := strconv.ParseBool(e); err == nil {
c.Disabled = value
}
}

c.Sampler = SamplerConfigFromEnv()
c.Reporter = ReporterConfigFromEnv()
return c
}

// SamplerConfigFromEnv creates a new SamplerConfig based on the environment variables
func SamplerConfigFromEnv() *SamplerConfig {
sc := &SamplerConfig{}

if e := os.Getenv(envSamplerType); e != "" {
sc.Type = e
}

if e := os.Getenv("JAEGER_SAMPLER_PARAM"); e != "" {
if e := os.Getenv(envSamplerParam); e != "" {
if value, err := strconv.ParseFloat(e, 64); err == nil {
sc.Param = value
} else {
return nil, err
}
}

if e := os.Getenv("JAEGER_SAMPLER_MANAGER_HOST_PORT"); e != "" {
if e := os.Getenv(envSamplerManagerHostPort); e != "" {
sc.SamplingServerURL = e
}

if e := os.Getenv("JAEGER_SAMPLER_MAX_OPERATIONS"); e != "" {
if e := os.Getenv(envSamplerMaxOperations); e != "" {
if value, err := strconv.ParseInt(e, 10, 0); err == nil {
sc.MaxOperations = int(value)
} else {
return nil, err
}
}

if e := os.Getenv("JAEGER_SAMPLER_REFRESH_INTERVAL"); e != "" {
if e := os.Getenv(envSamplerRefreshInterval); e != "" {
if value, err := time.ParseDuration(e); err == nil {
sc.SamplingRefreshInterval = value
} else {
return nil, err
}
}

return sc, nil
return sc
}

// NewReporter instantiates a new reporter that submits spans to tcollector
func (rc *ReporterConfig) NewReporter(
serviceName string,
metrics *jaeger.Metrics,
logger jaeger.Logger,
) (jaeger.Reporter, error) {
sender, err := rc.newTransport()
if err != nil {
return nil, err
}
reporter := jaeger.NewRemoteReporter(
sender,
jaeger.ReporterOptions.QueueSize(rc.QueueSize),
jaeger.ReporterOptions.BufferFlushInterval(rc.BufferFlushInterval),
jaeger.ReporterOptions.Logger(logger),
jaeger.ReporterOptions.Metrics(metrics))
if rc.LogSpans && logger != nil {
logger.Infof("Initializing logging reporter\n")
reporter = jaeger.NewCompositeReporter(jaeger.NewLoggingReporter(logger), reporter)
}
return reporter, err
}
// ReporterConfigFromEnv creates a new ReporterConfig based on the environment variables
func ReporterConfigFromEnv() *ReporterConfig {
rc := &ReporterConfig{}

// FromEnv creates a new ReporterConfig based on the environment variables
func (rc *ReporterConfig) FromEnv() (*ReporterConfig, error) {
if e := os.Getenv("JAEGER_REPORTER_MAX_QUEUE_SIZE"); e != "" {
if e := os.Getenv(envReporterMaxQueueSize); e != "" {
if value, err := strconv.ParseInt(e, 10, 0); err == nil {
rc.QueueSize = int(value)
} else {
return nil, err
}
}

if e := os.Getenv("JAEGER_REPORTER_FLUSH_INTERVAL"); e != "" {
if e := os.Getenv(envReporterFlushInterval); e != "" {
if value, err := time.ParseDuration(e); err == nil {
rc.BufferFlushInterval = value
} else {
return nil, err
}
}

if e := os.Getenv("JAEGER_REPORTER_LOG_SPANS"); e != "" {
if e := os.Getenv(envReporterLogSpans); e != "" {
if value, err := strconv.ParseBool(e); err == nil {
rc.LogSpans = value
} else {
return nil, err
}
}

// JAEGER_AGENT_HOST:JAEGER_AGENT_PORT, like: localhost:6831
if e := os.Getenv("JAEGER_AGENT_HOST"); e != "" {
if e := os.Getenv(envAgentHost); e != "" {
host := e
port := 6831 // TODO: can we somehow consume the default from the transport itself?
if e := os.Getenv("JAEGER_AGENT_PORT"); e != "" {
if e := os.Getenv(envAgentPort); e != "" {
if value, err := strconv.ParseInt(e, 10, 0); err == nil {
port = int(value)
} else {
return nil, err
}
}

rc.LocalAgentHostPort = fmt.Sprintf("%s:%d", host, port)
}

return rc, nil
}

func (rc *ReporterConfig) newTransport() (jaeger.Transport, error) {
return jaeger.NewUDPTransport(rc.LocalAgentHostPort, 0)
return rc
}
Loading

0 comments on commit 1e1299d

Please sign in to comment.