diff --git a/CHANGES.md b/CHANGES.md index a6ef9405..88dc93ae 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ Release Notes. 0.4.0 ------------------ #### Features +* Add support ignore suffix. #### Plugins * Support setting a discard type of reporter. diff --git a/docs/en/agent/tracing-metrics-logging.md b/docs/en/agent/tracing-metrics-logging.md index 4d540ebb..07ccf8da 100644 --- a/docs/en/agent/tracing-metrics-logging.md +++ b/docs/en/agent/tracing-metrics-logging.md @@ -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 diff --git a/plugins/core/tracer.go b/plugins/core/tracer.go index a0b797c8..e10a56b3 100644 --- a/plugins/core/tracer.go +++ b/plugins/core/tracer.go @@ -22,6 +22,7 @@ import ( defLog "log" "os" "reflect" + "strings" "sync" "github.com/apache/skywalking-go/plugins/core/operator" @@ -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 @@ -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() { diff --git a/plugins/core/tracing.go b/plugins/core/tracing.go index fb7b1322..8fac32f8 100644 --- a/plugins/core/tracing.go +++ b/plugins/core/tracing.go @@ -20,6 +20,7 @@ package core import ( "reflect" "runtime/debug" + "strings" "github.com/pkg/errors" @@ -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 } @@ -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 } @@ -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 } @@ -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() @@ -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 +} diff --git a/tools/go-agent/config/agent.default.yaml b/tools/go-agent/config/agent.default.yaml index 14835ee9..0af4207c 100644 --- a/tools/go-agent/config/agent.default.yaml +++ b/tools/go-agent/config/agent.default.yaml @@ -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} diff --git a/tools/go-agent/config/loader.go b/tools/go-agent/config/loader.go index fa9c56bf..e42420da 100644 --- a/tools/go-agent/config/loader.go +++ b/tools/go-agent/config/loader.go @@ -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 { diff --git a/tools/go-agent/instrument/agentcore/instrument.go b/tools/go-agent/instrument/agentcore/instrument.go index 04af9376..83dcbb65 100644 --- a/tools/go-agent/instrument/agentcore/instrument.go +++ b/tools/go-agent/instrument/agentcore/instrument.go @@ -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 {