github.com/craicoverflow/tyk@v2.9.6-rc3+incompatible/config/opentracing_custom_env_loader.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/kelseyhightower/envconfig"
     7  	jaeger "github.com/uber/jaeger-client-go/config"
     8  )
     9  
    10  // ZipkinConfig configuration options used to initialize openzipkin opentracing
    11  // client.
    12  type ZipkinConfig struct {
    13  	Reporter Reporter `json:"reporter"`
    14  	Sampler  Sampler  `json:"sampler"`
    15  }
    16  
    17  type Reporter struct {
    18  	// URL connection url to the zipkin server
    19  	URL        string `json:"url"`
    20  	BatchSize  int    `json:"batch_size"`
    21  	MaxBacklog int    `json:"max_backlog"`
    22  }
    23  
    24  type Sampler struct {
    25  	//Name is the name of the sampler to use. Options are
    26  	//
    27  	// 	"boundary"
    28  	// is appropriate for high-traffic instrumentation who
    29  	// provision random trace ids, and make the sampling decision only once.
    30  	// It defends against nodes in the cluster selecting exactly the same ids.
    31  	//
    32  	//	"count"
    33  	// is appropriate for low-traffic instrumentation or
    34  	// those who do not provision random trace ids. It is not appropriate for
    35  	// collectors as the sampling decision isn't idempotent (consistent based
    36  	// on trace id).
    37  	//
    38  	// "mod"
    39  	// provides a generic type Sampler
    40  	Name string `json:"name"`
    41  	//Rate is used by both "boundary" and "count" samplers
    42  	Rate float64 `json:"rate"`
    43  	//Salt is used by "boundary" sampler
    44  	Salt int64 `json:"salt"`
    45  	// Mod is only used when sampler is mod
    46  	Mod uint64 `json:"mod"`
    47  }
    48  
    49  // DecodeJSON marshals src to json and tries to unmarshal the result into
    50  // dest.
    51  func DecodeJSON(dest, src interface{}) error {
    52  	b, err := json.Marshal(src)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return json.Unmarshal(b, dest)
    57  }
    58  
    59  // loadZipkin tries to lad zipkin configuration from environment variables.
    60  //
    61  // list of zipkin configuration env variables
    62  //
    63  // TYK_GW_TRACER_OPTIONS_REPORTER_URL
    64  // TYK_GW_TRACER_OPTIONS_REPORTER_BATCHSIZE
    65  // TYK_GW_TRACER_OPTIONS_REPORTER_MAXBACKLOG
    66  // TYK_GW_TRACER_OPTIONS_SAMPLER_NAME
    67  // TYK_GW_TRACER_OPTIONS_SAMPLER_RATE
    68  // TYK_GW_TRACER_OPTIONS_SAMPLER_SALT
    69  // TYK_GW_TRACER_OPTIONS_SAMPLER_MOD
    70  func loadZipkin(prefix string, c *Config) error {
    71  	if c.Tracer.Name != "zipkin" || c.Tracer.Options == nil {
    72  		return nil
    73  	}
    74  	var zip ZipkinConfig
    75  	if err := DecodeJSON(&zip, c.Tracer.Options); err != nil {
    76  		return err
    77  	}
    78  	qualifyPrefix := prefix + "_TRACER_OPTIONS"
    79  	err := envconfig.Process(qualifyPrefix, &zip)
    80  	if err != nil {
    81  		return err
    82  	}
    83  	o := make(map[string]interface{})
    84  	if err := DecodeJSON(&o, zip); err != nil {
    85  		return err
    86  	}
    87  	c.Tracer.Options = o
    88  	return nil
    89  }
    90  
    91  // loads jaeger configuration from environment variables.
    92  //
    93  // List of jaeger configuration env vars
    94  //
    95  // TYK_GW_TRACER_OPTIONS_SERVICENAME
    96  // TYK_GW_TRACER_OPTIONS_DISABLED
    97  // TYK_GW_TRACER_OPTIONS_RPCMETRICS
    98  // TYK_GW_TRACER_OPTIONS_TAGS
    99  // TYK_GW_TRACER_OPTIONS_SAMPLER_TYPE
   100  // TYK_GW_TRACER_OPTIONS_SAMPLER_PARAM
   101  // TYK_GW_TRACER_OPTIONS_SAMPLER_SAMPLINGSERVERURL
   102  // TYK_GW_TRACER_OPTIONS_SAMPLER_MAXOPERATIONS
   103  // TYK_GW_TRACER_OPTIONS_SAMPLER_SAMPLINGREFRESHINTERVAL
   104  // TYK_GW_TRACER_OPTIONS_REPORTER_QUEUESIZE
   105  // TYK_GW_TRACER_OPTIONS_REPORTER_BUFFERFLUSHINTERVAL
   106  // TYK_GW_TRACER_OPTIONS_REPORTER_LOGSPANS
   107  // TYK_GW_TRACER_OPTIONS_REPORTER_LOCALAGENTHOSTPORT
   108  // TYK_GW_TRACER_OPTIONS_REPORTER_COLLECTORENDPOINT
   109  // TYK_GW_TRACER_OPTIONS_REPORTER_USER
   110  // TYK_GW_TRACER_OPTIONS_REPORTER_PASSWORD
   111  // TYK_GW_TRACER_OPTIONS_HEADERS_JAEGERDEBUGHEADER
   112  // TYK_GW_TRACER_OPTIONS_HEADERS_JAEGERBAGGAGEHEADER
   113  // TYK_GW_TRACER_OPTIONS_HEADERS_TRACECONTEXTHEADERNAME
   114  // TYK_GW_TRACER_OPTIONS_HEADERS_TRACEBAGGAGEHEADERPREFIX
   115  // TYK_GW_TRACER_OPTIONS_BAGGAGERESTRICTIONS_DENYBAGGAGEONINITIALIZATIONFAILURE
   116  // TYK_GW_TRACER_OPTIONS_BAGGAGERESTRICTIONS_HOSTPORT
   117  // TYK_GW_TRACER_OPTIONS_BAGGAGERESTRICTIONS_REFRESHINTERVAL
   118  // TYK_GW_TRACER_OPTIONS_THROTTLER_HOSTPORT
   119  // TYK_GW_TRACER_OPTIONS_THROTTLER_REFRESHINTERVAL
   120  // TYK_GW_TRACER_OPTIONS_THROTTLER_SYNCHRONOUSINITIALIZATION
   121  func loadJaeger(prefix string, c *Config) error {
   122  	if c.Tracer.Name != "jaeger" || c.Tracer.Options == nil {
   123  		return nil
   124  	}
   125  	var j jaeger.Configuration
   126  	if err := DecodeJSON(&j, c.Tracer.Options); err != nil {
   127  		return err
   128  	}
   129  	qualifyPrefix := prefix + "_TRACER_OPTIONS"
   130  	err := envconfig.Process(qualifyPrefix, &j)
   131  	if err != nil {
   132  		return err
   133  	}
   134  	o := make(map[string]interface{})
   135  	if err := DecodeJSON(&o, j); err != nil {
   136  		return err
   137  	}
   138  	c.Tracer.Options = o
   139  	return nil
   140  }