Skip to content

Commit

Permalink
Add support ignore suffix (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodePrometheus authored Jan 24, 2024
1 parent 322daeb commit ffaa359
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Release Notes.
0.4.0
------------------
#### Features
* Add support ignore suffix.

#### Plugins
* Support setting a discard type of reporter.
Expand Down
7 changes: 4 additions & 3 deletions docs/en/agent/tracing-metrics-logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ If you wish to disable a particular plugin to prevent enhancements related to th

The basic configuration is as follows:

| Name | Environment Key | Default Value | Description |
|-------------------------|-------------------|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------|
| agent.sampler | SW_AGENT_SAMPLER | 1 | Sampling rate of tracing data, which is a floating-point value that must be between 0 and 1. |
| Name | Environment Key | Default Value | Description |
|---------------------|------------------------|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|
| agent.sampler | SW_AGENT_SAMPLER | 1 | Sampling rate of tracing data, which is a floating-point value that must be between 0 and 1. |
| agent.ignore_suffix | SW_AGENT_IGNORE_SUFFIX | .jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg | If the operation name of the first span is included in this set, this segment should be ignored.(multiple split by ","). |

## Metrics

Expand Down
5 changes: 4 additions & 1 deletion plugins/core/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
defLog "log"
"os"
"reflect"
"strings"
"sync"

"github.com/apache/skywalking-go/plugins/core/operator"
Expand Down Expand Up @@ -52,10 +53,11 @@ type Tracer struct {
// for all metrics
meterMap *sync.Map
meterCollectListeners []func()
ignoreSuffix []string
}

func (t *Tracer) Init(entity *reporter.Entity, rep reporter.Reporter, samp Sampler, logger operator.LogOperator,
meterCollectSecond int, correlation *CorrelationConfig) error {
meterCollectSecond int, correlation *CorrelationConfig, ignoreSuffixStr string) error {
t.ServiceEntity = entity
t.Reporter = rep
t.Sampler = samp
Expand All @@ -66,6 +68,7 @@ func (t *Tracer) Init(entity *reporter.Entity, rep reporter.Reporter, samp Sampl
t.initFlag = 1
t.initMetricsCollect(meterCollectSecond)
t.correlation = correlation
t.ignoreSuffix = strings.Split(ignoreSuffixStr, ",")
// notify the tracer been init success
if len(GetInitNotify()) > 0 {
for _, fun := range GetInitNotify() {
Expand Down
25 changes: 21 additions & 4 deletions plugins/core/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package core
import (
"reflect"
"runtime/debug"
"strings"

"github.com/pkg/errors"

Expand All @@ -42,7 +43,7 @@ func (t *Tracer) DebugStack() []byte {
}

func (t *Tracer) CreateEntrySpan(operationName string, extractor interface{}, opts ...interface{}) (s interface{}, err error) {
ctx, tracingSpan, noop := t.createNoop()
ctx, tracingSpan, noop := t.createNoop(operationName)
if noop {
return tracingSpan, nil
}
Expand All @@ -66,7 +67,7 @@ func (t *Tracer) CreateEntrySpan(operationName string, extractor interface{}, op
}

func (t *Tracer) CreateLocalSpan(operationName string, opts ...interface{}) (s interface{}, err error) {
ctx, tracingSpan, noop := t.createNoop()
ctx, tracingSpan, noop := t.createNoop(operationName)
if noop {
return tracingSpan, nil
}
Expand All @@ -78,7 +79,7 @@ func (t *Tracer) CreateLocalSpan(operationName string, opts ...interface{}) (s i
}

func (t *Tracer) CreateExitSpan(operationName, peer string, injector interface{}, opts ...interface{}) (s interface{}, err error) {
ctx, tracingSpan, noop := t.createNoop()
ctx, tracingSpan, noop := t.createNoop(operationName)
if noop {
return tracingSpan, nil
}
Expand Down Expand Up @@ -225,10 +226,13 @@ func (s *ContextSnapshot) IsValid() bool {
return s.activeSpan != nil && s.runtime != nil
}

func (t *Tracer) createNoop() (*TracingContext, TracingSpan, bool) {
func (t *Tracer) createNoop(operationName string) (*TracingContext, TracingSpan, bool) {
if !t.InitSuccess() || t.Reporter.ConnectionStatus() == reporter.ConnectionStatusDisconnect {
return nil, newNoopSpan(), true
}
if ignoreSuffixFilter(operationName, t.ignoreSuffix) {
return nil, newNoopSpan(), true
}
ctx := getTracingContext()
if ctx != nil {
span := ctx.ActiveSpan()
Expand Down Expand Up @@ -337,3 +341,16 @@ func saveSpanToActiveIfNotError(ctx *TracingContext, span interface{}, err error
ctx.SaveActiveSpan(span.(TracingSpan))
SetGLS(ctx)
}

func ignoreSuffixFilter(operationName string, ignoreSuffix []string) bool {
suffixIdx := strings.LastIndex(operationName, ".")
if suffixIdx == -1 {
return false
}
for _, suffix := range ignoreSuffix {
if suffix == operationName[suffixIdx:] {
return true
}
}
return false
}
2 changes: 2 additions & 0 deletions tools/go-agent/config/agent.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ agent:
correlation:
max_key_count: ${SW_AGENT_CORRELATION_MAX_KEY_COUNT:3}
max_value_size: ${SW_AGENT_CORRELATION_MAX_VALUE_SIZE:128}
# If the operation name of the first span is included in this set, this segment should be ignored.(multiple split by ",")
ignore_suffix: ${SW_AGENT_IGNORE_SUFFIX:.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg}

reporter:
discard: ${SW_AGENT_REPORTER_DISCARD:false}
Expand Down
1 change: 1 addition & 0 deletions tools/go-agent/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Agent struct {
Sampler StringValue `yaml:"sampler"`
Meter Meter `yaml:"meter"`
Correlation Correlation `yaml:"correlation"`
IgnoreSuffix StringValue `yaml:"ignore_suffix"`
}

type Reporter struct {
Expand Down
3 changes: 2 additions & 1 deletion tools/go-agent/instrument/agentcore/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func (t *Tracer) InitTracer(extend map[string]interface{}) {
MaxKeyCount : {{.Config.Agent.Correlation.MaxKeyCount.ToGoIntValue "loading the agent correlation maxKeyCount error"}},
MaxValueSize : {{.Config.Agent.Correlation.MaxValueSize.ToGoIntValue "loading the agent correlation maxValueSize error"}},
}
if err := t.Init(entity, rep, samp, logger, meterCollectInterval, correlation); err != nil {
ignoreSuffixStr := {{.Config.Agent.IgnoreSuffix.ToGoStringValue}}
if err := t.Init(entity, rep, samp, logger, meterCollectInterval, correlation, ignoreSuffixStr); err != nil {
t.Log.Errorf("cannot initialize the SkyWalking Tracer: %v", err)
}
}`, struct {
Expand Down

0 comments on commit ffaa359

Please sign in to comment.