github.com/GuanceCloud/cliutils@v1.1.21/tracer/tracer.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  // Package tracer defined basic trace wraps.
     7  package tracer
     8  
     9  import (
    10  	"net"
    11  
    12  	"github.com/GuanceCloud/cliutils/logger"
    13  	"gopkg.in/CodapeWild/dd-trace-go.v1/ddtrace/tracer"
    14  )
    15  
    16  var log = logger.DefaultSLogger("ddtrace")
    17  
    18  type DiscardLogger struct{}
    19  
    20  func (*DiscardLogger) Log(msg string) {}
    21  
    22  type StdLogger struct{}
    23  
    24  func (*StdLogger) Log(msg string) {
    25  	log.Debug(msg)
    26  }
    27  
    28  type Tracer struct {
    29  	Host    string `toml:"host" yaml:"host"`       // env: DD_AGENT_HOST
    30  	Port    string `toml:"port" yaml:"port"`       // env: DD_TRACE_AGENT_PORT
    31  	Service string `toml:"service" yaml:"service"` // env: DD_SERVICE
    32  	Version string `toml:"version" yaml:"version"` // env: DD_VERSION
    33  	Env     string `toml:"env" yaml:"env"`         // env: DD_ENV
    34  
    35  	agentAddr string
    36  
    37  	LogsStartup  bool                   `toml:"logs_startup" yaml:"logs_startup"`   // env: DD_TRACE_STARTUP_LOGS
    38  	Debug        bool                   `toml:"debug" yaml:"debug"`                 // env: DD_TRACE_DEBUG
    39  	TraceEnabled bool                   `toml:"trace_enabled" yaml:"trace_enabled"` // env: DD_TRACE_ENABLED
    40  	Tags         map[string]interface{} `toml:"tags" yaml:"tags"`                   // env: DD_TAGS
    41  }
    42  
    43  func (t *Tracer) GetStartOptions(opts ...StartOption) []tracer.StartOption {
    44  	if t.Tags == nil {
    45  		t.Tags = make(map[string]interface{})
    46  	}
    47  
    48  	for i := range opts {
    49  		opts[i](t)
    50  	}
    51  
    52  	if t.Host == "" {
    53  		t.Host = "127.0.0.1"
    54  	}
    55  	if t.Port == "" {
    56  		t.Port = "9529"
    57  	}
    58  	t.agentAddr = net.JoinHostPort(t.Host, t.Port)
    59  
    60  	startOpts := []tracer.StartOption{
    61  		tracer.WithAgentAddr(t.agentAddr),
    62  		tracer.WithService(t.Service),
    63  		tracer.WithServiceVersion(t.Version),
    64  		tracer.WithLogStartup(t.LogsStartup),
    65  		tracer.WithDebugMode(t.Debug),
    66  		tracer.WithEnv(t.Env),
    67  	}
    68  	for k, v := range t.Tags {
    69  		startOpts = append(startOpts, tracer.WithGlobalTag(k, v))
    70  	}
    71  
    72  	return startOpts
    73  }