Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/plugin-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
- go-redisv9
- go-restfulv3
- gorm
- kratosv2
# - kratosv2 temporary disable because it's not stable
- microv4
- mongo
- mysql
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release Notes.
* Support Windows plugin test.
* Support Kafka reporter.
* Add recover to goroutine to prevent unexpected panics.
* Add mutex to fix some data race.

#### Plugins

Expand Down
18 changes: 15 additions & 3 deletions plugins/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package core

import "reflect"
import (
"reflect"
"sync"
)

var (
GetGLS = func() interface{} { return nil }
Expand All @@ -29,16 +32,21 @@ var (
)

type ContextSnapshoter interface {
TakeSnapShot(val interface{}) interface{}
TakeSnapShot() interface{}
}

type TracingContext struct {
activeSpan TracingSpan
Runtime *RuntimeContext
ID *IDContext

activeSpanLock sync.RWMutex
}

func (t *TracingContext) TakeSnapShot(val interface{}) interface{} {
func (t *TracingContext) TakeSnapShot() interface{} {
if t == nil {
return nil
}
snapshot := newSnapshotSpan(t.ActiveSpan())
return &TracingContext{
activeSpan: snapshot,
Expand All @@ -48,13 +56,17 @@ func (t *TracingContext) TakeSnapShot(val interface{}) interface{} {
}

func (t *TracingContext) ActiveSpan() TracingSpan {
t.activeSpanLock.RLock()
defer t.activeSpanLock.RUnlock()
if t.activeSpan == nil || reflect.ValueOf(t.activeSpan).IsZero() {
return nil
}
return t.activeSpan
}

func (t *TracingContext) SaveActiveSpan(s TracingSpan) {
t.activeSpanLock.Lock()
defer t.activeSpanLock.Unlock()
t.activeSpan = s
}

Expand Down
14 changes: 12 additions & 2 deletions plugins/core/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (t *Tracer) reachNotInitMetrics() {
func (t *Tracer) sendMetrics() {
meters := make([]reporter.ReportedMeter, 0)
// call collect hook
for _, hook := range t.meterCollectListeners {
for _, hook := range t.allMeterCollectListeners() {
hook()
}
t.meterMap.Range(func(key, value interface{}) bool {
Expand All @@ -90,6 +90,14 @@ func (t *Tracer) sendMetrics() {
t.Reporter.SendMetrics(meters)
}

func (t *Tracer) allMeterCollectListeners() []func() {
t.meterCollectListenersLock.RLock()
defer t.meterCollectListenersLock.RUnlock()
listeners := make([]func(), 0, len(t.meterCollectListeners))
listeners = append(listeners, t.meterCollectListeners...)
return listeners
}

func (t *Tracer) NewCounter(name string, opt interface{}) interface{} {
counter := newCounter(name, nil, 0)
if o, ok := opt.(meterOpts); ok && o != nil {
Expand Down Expand Up @@ -118,6 +126,8 @@ func (t *Tracer) NewHistogram(name string, minValue float64, steps []float64, op
}

func (t *Tracer) AddCollectHook(f func()) {
t.meterCollectListenersLock.Lock()
defer t.meterCollectListenersLock.Unlock()
t.meterCollectListeners = append(t.meterCollectListeners, f)
}

Expand Down Expand Up @@ -347,7 +357,7 @@ func (h *histogramBucket) Bucket() float64 {
}

func (h *histogramBucket) Count() int64 {
return *h.value
return atomic.LoadInt64(h.value)
}

func (h *histogramBucket) IsNegativeInfinity() bool {
Expand Down
4 changes: 4 additions & 0 deletions plugins/core/operator/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type realInvocation struct {
returnValues []interface{}

context interface{}

// self obs
interTimeCost int64
beforeInterStart int64
}

func (i *realInvocation) CallerInstance() interface{} {
Expand Down
7 changes: 7 additions & 0 deletions plugins/core/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ type DynamicSampler struct {
currentRate float64
defaultRate float64
sampler Sampler
locker sync.RWMutex
}

// IsSampled implements IsSampled() of Sampler.
func (s *DynamicSampler) IsSampled(operation string) bool {
s.locker.RLock()
defer s.locker.RUnlock()
return s.sampler.IsSampled(operation)
}

Expand All @@ -136,11 +139,15 @@ func (s *DynamicSampler) Notify(eventType reporter.AgentConfigEventType, newValu
} else {
sampler = NewRandomSampler(samplingRate)
}
s.locker.Lock()
defer s.locker.Unlock()
s.sampler = sampler
s.currentRate = samplingRate
}

func (s *DynamicSampler) Value() string {
s.locker.RLock()
defer s.locker.RUnlock()
return fmt.Sprintf("%f", s.currentRate)
}

Expand Down
10 changes: 5 additions & 5 deletions plugins/core/span_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ func (rs *RootSegmentSpan) AsyncFinish() {
}

func (rs *RootSegmentSpan) end0() {
go func() {
defer func() {
_ = recover()
}()
rs.doneCh <- atomic.SwapInt32(rs.SegmentContext.refNum, -1)
defer func() {
_ = recover()
}()
if rs != nil && rs.doneCh != nil && rs.SegmentContext.refNum != nil {
rs.doneCh <- atomic.SwapInt32(rs.SegmentContext.refNum, -1)
}
}

func (rs *RootSegmentSpan) createRootSegmentContext(ctx *TracingContext, _ SegmentSpan) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/test_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func SetAsNewGoroutine() {
return
}
if e := gls.(ContextSnapshoter); e != nil {
SetGLS(e.TakeSnapShot(GetGLS()))
SetGLS(e.TakeSnapShot())
}
}

Expand Down
11 changes: 6 additions & 5 deletions plugins/core/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ type Tracer struct {
// for plugin tools
tools *TracerTools
// for all metrics
meterMap *sync.Map
meterCollectListeners []func()
ignoreSuffix []string
traceIgnorePath []string
mu sync.Mutex
meterMap *sync.Map
meterCollectListeners []func()
meterCollectListenersLock sync.RWMutex
ignoreSuffix []string
traceIgnorePath []string
mu sync.Mutex
}

func (t *Tracer) Init(entity *reporter.Entity, rep reporter.Reporter, samp Sampler, logger operator.LogOperator,
Expand Down
2 changes: 2 additions & 0 deletions plugins/core/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ func (t *Tracer) ContinueContext(snapshot interface{}) {
ctx = NewTracingContext()
SetGLS(ctx)
}
ctx.activeSpanLock.Lock()
defer ctx.activeSpanLock.Unlock()
ctx.activeSpan = snap.activeSpan
ctx.Runtime = snap.runtime
}
Expand Down
2 changes: 0 additions & 2 deletions tools/go-agent/instrument/plugins/enhance_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ func (m *MethodEnhance) BuildForDelegator() []dst.Decl {
result := make([]dst.Decl, 0)

result = append(result, tools.GoStringToDecls(fmt.Sprintf(`var %s = &%s{}`, m.InterceptorVarName, m.InterceptorGeneratedName))...)
result = append(result, tools.GoStringToDecls(fmt.Sprintf(`var %s_interTimeCost int64`, m.FuncID))...)
result = append(result, tools.GoStringToDecls(fmt.Sprintf(`var %s_beforeInterStart int64`, m.FuncID))...)
preFunc := &dst.FuncDecl{
Name: &dst.Ident{Name: m.AdapterPreFuncName},
Type: &dst.FuncType{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defer func() {
// log error
log.Errorf("execute interceptor after invoke error, instrument name: %s, interceptor name: %s, function ID: %s, error: %v, stack: %s",
"{{.InstrumentName}}", "{{.InterceptorDefineName}}", "{{.FuncID}}", r, tracing.DebugStack())
{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart
operator.DurationOfInterceptor({{.FuncID}}_interTimeCost)
invocation.interTimeCost += operator.NanoTime() - invocation.beforeInterStart
operator.DurationOfInterceptor(invocation.interTimeCost)
}
}()

Expand All @@ -28,5 +28,5 @@ if (invocation.isContinue) {
}
{{- end }}
}
{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart
operator.DurationOfInterceptor({{.FuncID}}_interTimeCost)
invocation.interTimeCost += operator.NanoTime() - invocation.beforeInterStart
operator.DurationOfInterceptor(invocation.interTimeCost)
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{{.FuncID}}_interTimeCost = int64(0)
{{.FuncID}}_beforeInterStart = operator.NanoTime()
invocation = &operator.realInvocation{}
invocation.interTimeCost = int64(0)
invocation.beforeInterStart = operator.NanoTime()
defer func() {
if err := recover(); err != nil {
operator.ErrorOfPlugin("{{.InstrumentName}}")
// log error
log.Errorf("execute interceptor before invoke error, instrument name: %s, interceptor name: %s, function ID: %s, error: %v, stack: %s",
"{{.InstrumentName}}", "{{.InterceptorDefineName}}", "{{.FuncID}}", err, tracing.DebugStack())
{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart
invocation.interTimeCost += operator.NanoTime() - invocation.beforeInterStart
}
}()
invocation = &operator.realInvocation{}
{{if .Recvs -}}
invocation.callerInstance = *recv_0 // for caller if exist
{{- end}}
Expand Down Expand Up @@ -37,7 +37,7 @@ if err := {{.InterceptorVarName}}.BeforeInvoke(invocation); err != nil {
// using go2sky log error
log.Warnf("execute interceptor before invoke error, instrument name: %s, interceptor name: %s, function ID: %s, error: %v",
"{{.InstrumentName}}", "{{.InterceptorDefineName}}", "{{.FuncID}}", err)
{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart
invocation.interTimeCost += operator.NanoTime() - invocation.beforeInterStart

return {{ range $index, $value := .Results -}}
def_res_{{$index}},
Expand All @@ -49,13 +49,13 @@ if (invocation.isContinue) {
def_res_{{$index}} = (invocation.returnValues[{{$index}}]).({{$value.PackagedTypeName}})
}
{{- end}}
{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart
invocation.interTimeCost += operator.NanoTime() - invocation.beforeInterStart

return {{ range $index, $value := .Results -}}
def_res_{{$index}},
{{- end}}invocation, true
}
{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart
invocation.interTimeCost += operator.NanoTime() - invocation.beforeInterStart

return {{ range $index, $value := .Results -}}
def_res_{{$index}},
Expand Down
6 changes: 3 additions & 3 deletions tools/go-agent/instrument/runtime/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ func _skywalking_metrics_hook_append_impl(f func()) {
}

type ContextSnapshoter interface {
TakeSnapShot(val interface{}) interface{}
TakeSnapShot() interface{}
}

func goroutineChange(tls interface{}) interface{} {
if tls == nil {
return nil
}
if taker, ok := tls.(ContextSnapshoter); ok {
return taker.TakeSnapShot(tls)
if taker, ok := tls.(ContextSnapshoter); ok && taker != nil {
return taker.TakeSnapShot()
}
return tls
}
Expand Down
Loading