github.com/pinpoint-apm/pinpoint-go-agent@v1.4.1-0.20240110120318-a50c2eb18c8c/noop.go (about)

     1  package pinpoint
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"time"
     7  )
     8  
     9  type noopAgent struct {
    10  	config *Config
    11  }
    12  
    13  var defaultNoopAgent = &noopAgent{
    14  	config: defaultConfig(),
    15  }
    16  
    17  // NoopAgent returns a Agent that doesn't collect tracing data.
    18  func NoopAgent() Agent {
    19  	return defaultNoopAgent
    20  }
    21  
    22  func (agent *noopAgent) NewSpanTracer(operation string, rpcName string) Tracer {
    23  	return NoopTracer()
    24  }
    25  
    26  func (agent *noopAgent) NewSpanTracerWithReader(operation string, rpcName string, reader DistributedTracingContextReader) Tracer {
    27  	return NoopTracer()
    28  }
    29  
    30  func (agent *noopAgent) Enable() bool {
    31  	return false
    32  }
    33  
    34  func (agent *noopAgent) Config() *Config {
    35  	return agent.config
    36  }
    37  
    38  func (agent *noopAgent) Shutdown() {
    39  }
    40  
    41  type noopSpan struct {
    42  	agent       *agent
    43  	spanId      int64
    44  	startTime   time.Time
    45  	rpcName     string
    46  	goroutineId int64
    47  	withStats   bool
    48  	urlStat     *UrlStatEntry
    49  
    50  	noopSe      noopSpanEvent
    51  	annotations noopAnnotation
    52  }
    53  
    54  var defaultNoopSpan = noopSpan{}
    55  
    56  // NoopTracer returns a Tracer that doesn't collect tracing data.
    57  func NoopTracer() Tracer {
    58  	return &defaultNoopSpan
    59  }
    60  
    61  func newUnSampledSpan(agent *agent, rpcName string) *noopSpan {
    62  	span := noopSpan{}
    63  	span.agent = agent
    64  	span.spanId = generateSpanId()
    65  	span.startTime = time.Now()
    66  	span.rpcName = rpcName
    67  	span.withStats = true
    68  
    69  	addUnSampledActiveSpan(&span)
    70  
    71  	return &span
    72  }
    73  
    74  func (span *noopSpan) EndSpan() {
    75  	if span.withStats {
    76  		dropUnSampledActiveSpan(span)
    77  		endTime := time.Now()
    78  		elapsed := endTime.Sub(span.startTime).Milliseconds()
    79  		collectResponseTime(elapsed)
    80  		if span.urlStat != nil {
    81  			span.agent.enqueueUrlStat(&urlStat{entry: span.urlStat, endTime: endTime, elapsed: elapsed})
    82  		}
    83  	}
    84  }
    85  
    86  func (span *noopSpan) NewSpanEvent(operationName string) Tracer {
    87  	return span
    88  }
    89  
    90  func (span *noopSpan) NewAsyncSpan() Tracer {
    91  	return &noopSpan{}
    92  }
    93  
    94  func (span *noopSpan) NewGoroutineTracer() Tracer {
    95  	return &noopSpan{}
    96  }
    97  
    98  func (span *noopSpan) WrapGoroutine(goroutineName string, goroutine func(context.Context), ctx context.Context) func() {
    99  	asyncSpan := span.NewGoroutineTracer()
   100  
   101  	var newCtx context.Context
   102  	if ctx == nil {
   103  		newCtx = NewContext(context.Background(), asyncSpan)
   104  	} else {
   105  		newCtx = NewContext(ctx, asyncSpan)
   106  	}
   107  
   108  	return func() {
   109  		goroutine(newCtx)
   110  	}
   111  }
   112  
   113  func (span *noopSpan) EndSpanEvent() {}
   114  
   115  func (span *noopSpan) TransactionId() TransactionId {
   116  	return TransactionId{"Noop", 0, 0}
   117  }
   118  
   119  func (span *noopSpan) SpanId() int64 {
   120  	return span.spanId
   121  }
   122  
   123  func (span *noopSpan) Span() SpanRecorder {
   124  	return span
   125  }
   126  
   127  func (span *noopSpan) SpanEvent() SpanEventRecorder {
   128  	return &span.noopSe
   129  }
   130  
   131  func (span *noopSpan) SetError(e error) {}
   132  
   133  func (span *noopSpan) SetFailure() {}
   134  
   135  func (span *noopSpan) SetServiceType(typ int32) {}
   136  
   137  func (span *noopSpan) SetRpcName(rpc string) {}
   138  
   139  func (span *noopSpan) SetRemoteAddress(remoteAddress string) {}
   140  
   141  func (span *noopSpan) SetEndPoint(endPoint string) {}
   142  
   143  func (span *noopSpan) SetAcceptorHost(host string) {}
   144  
   145  func (span *noopSpan) Inject(writer DistributedTracingContextWriter) {
   146  	if writer != nil {
   147  		writer.Set(HeaderSampled, "s0")
   148  	}
   149  }
   150  
   151  func (span *noopSpan) Extract(reader DistributedTracingContextReader) {}
   152  
   153  func (span *noopSpan) Annotations() Annotation {
   154  	return &span.annotations
   155  }
   156  
   157  func (span *noopSpan) SetLogging(logInfo int32) {}
   158  
   159  func (span *noopSpan) IsSampled() bool {
   160  	return false
   161  }
   162  
   163  func (span *noopSpan) collectUrlStat(stat *UrlStatEntry) {
   164  	if span.withStats && span.agent.config.collectUrlStat {
   165  		if stat.Url == "" {
   166  			stat.Url = "UNKNOWN_URL"
   167  		}
   168  
   169  		span.urlStat = stat
   170  	}
   171  }
   172  
   173  func (span *noopSpan) AddMetric(metric string, value interface{}) {
   174  	if metric == MetricURLStat {
   175  		span.collectUrlStat(value.(*UrlStatEntry))
   176  	}
   177  }
   178  
   179  func (span *noopSpan) JsonString() []byte {
   180  	b, _ := json.Marshal(span)
   181  	return b
   182  }
   183  
   184  type noopSpanEvent struct {
   185  	annotations noopAnnotation
   186  }
   187  
   188  var defaultNoopSpanEvent = noopSpanEvent{}
   189  
   190  func (se *noopSpanEvent) SetError(e error, errorName ...string) {}
   191  
   192  func (se *noopSpanEvent) SetServiceType(typ int32) {}
   193  
   194  func (se *noopSpanEvent) SetDestination(id string) {}
   195  
   196  func (se *noopSpanEvent) SetEndPoint(endPoint string) {}
   197  
   198  func (se *noopSpanEvent) SetSQL(sql string, args string) {}
   199  
   200  func (se *noopSpanEvent) Annotations() Annotation {
   201  	return &se.annotations
   202  }
   203  
   204  func (se *noopSpanEvent) FixDuration(start time.Time, end time.Time) {}
   205  
   206  type noopAnnotation struct{}
   207  
   208  func (a *noopAnnotation) AppendInt(key int32, i int32) {}
   209  
   210  func (a *noopAnnotation) AppendLong(key int32, l int64) {}
   211  
   212  func (a *noopAnnotation) AppendString(key int32, s string) {}
   213  
   214  func (a *noopAnnotation) AppendStringString(key int32, s1 string, s2 string) {}
   215  
   216  func (a *noopAnnotation) AppendIntStringString(key int32, i int32, s1 string, s2 string) {}
   217  
   218  func (a *noopAnnotation) AppendBytesStringString(key int32, b []byte, s1 string, s2 string) {}
   219  
   220  func (a *noopAnnotation) AppendLongIntIntByteByteString(key int32, l int64, i1 int32, i2 int32, b1 int32, b2 int32, s string) {
   221  }
   222  
   223  type noopDistributedTracingContextReader struct{}
   224  
   225  func (r *noopDistributedTracingContextReader) Get(key string) string {
   226  	return ""
   227  }