diff --git a/.github/workflows/plugin-tests.yaml b/.github/workflows/plugin-tests.yaml index 94293a55..c061238d 100644 --- a/.github/workflows/plugin-tests.yaml +++ b/.github/workflows/plugin-tests.yaml @@ -104,6 +104,7 @@ jobs: - segmentio-kafka - go-elasticsearchv8 - goframe + - so11y steps: - uses: actions/checkout@v2 with: diff --git a/agent/core/compile.go b/agent/core/compile.go index 5f139418..7b5105c5 100644 --- a/agent/core/compile.go +++ b/agent/core/compile.go @@ -39,6 +39,7 @@ import ( _ "unsafe" //go:nolint + _ "github.com/apache/skywalking-go/agent/core/metrics" _ "github.com/apache/skywalking-go/agent/core/operator" _ "github.com/apache/skywalking-go/agent/core/tracing" _ "github.com/apache/skywalking-go/agent/reporter" diff --git a/agent/core/metrics/compile.go b/agent/core/metrics/compile.go new file mode 100644 index 00000000..428ada19 --- /dev/null +++ b/agent/core/metrics/compile.go @@ -0,0 +1,27 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package metrics + +import ( + //go:nolint + _ "fmt" + _ "sync" + + //go:nolint + _ "github.com/apache/skywalking-go/agent/core/operator" +) diff --git a/go.work b/go.work index 1dfe2c19..29287ae1 100644 --- a/go.work +++ b/go.work @@ -67,6 +67,7 @@ use ( ./test/plugins/scenarios/logging-activation ./test/plugins/scenarios/metric-activation ./test/plugins/scenarios/goframe + ./test/plugins/scenarios/so11y ./tools/go-agent diff --git a/plugins/core/metrics/bridge.go b/plugins/core/metrics/bridge.go index 4b4342cf..b9444fa5 100644 --- a/plugins/core/metrics/bridge.go +++ b/plugins/core/metrics/bridge.go @@ -18,11 +18,11 @@ package metrics func newMeterOpts() *Opts { - return &Opts{labels: make(map[string]string)} + return &Opts{Labels: make(map[string]string)} } func (b *Opts) GetLabels() map[string]string { - return b.labels + return b.Labels } type counterImpl struct { diff --git a/plugins/core/metrics/metrics.go b/plugins/core/metrics/metrics.go index 24ffadec..492cdd47 100644 --- a/plugins/core/metrics/metrics.go +++ b/plugins/core/metrics/metrics.go @@ -22,13 +22,13 @@ import "github.com/apache/skywalking-go/plugins/core/operator" type Opt func(opts *Opts) type Opts struct { - labels map[string]string + Labels map[string]string } // WithLabel adds a label to the metrics. func WithLabel(key, value string) Opt { return func(meter *Opts) { - meter.labels[key] = value + meter.Labels[key] = value } } diff --git a/plugins/core/operator/common.go b/plugins/core/operator/common.go index dbe7684e..e53f006f 100644 --- a/plugins/core/operator/common.go +++ b/plugins/core/operator/common.go @@ -30,6 +30,7 @@ type Operator interface { Entity() interface{} // Get the entity of the service Metrics() interface{} // to MetricsOperator LogReporter() interface{} // to LogReporter + So11y() interface{} // to So11yOperator } type Entity interface { diff --git a/plugins/core/operator/so11y.go b/plugins/core/operator/so11y.go new file mode 100644 index 00000000..66bc1b87 --- /dev/null +++ b/plugins/core/operator/so11y.go @@ -0,0 +1,48 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package operator + +type So11yOperator interface { + CollectErrorOfPlugin(pluginName string) + GenNanoTime() int64 + CollectDurationOfInterceptor(costTime int64) +} + +func ErrorOfPlugin(pluginName string) { + op := GetOperator() + if op == nil { + return + } + op.(So11yOperator).CollectErrorOfPlugin(pluginName) +} + +func NanoTime() int64 { + op := GetOperator() + if op == nil { + return 0 + } + return op.(So11yOperator).GenNanoTime() +} + +func DurationOfInterceptor(costTime int64) { + op := GetOperator() + if op == nil { + return + } + op.(So11yOperator).CollectDurationOfInterceptor(costTime) +} diff --git a/plugins/core/so11y.go b/plugins/core/so11y.go new file mode 100644 index 00000000..8670d8de --- /dev/null +++ b/plugins/core/so11y.go @@ -0,0 +1,162 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package core + +import ( + "sync" + "time" + + "github.com/apache/skywalking-go/plugins/core/metrics" +) + +var ( + instance *So11y + once sync.Once +) + +type So11y struct { + propagatedContextCounter metrics.Counter + propagatedIgnoreContextCounter metrics.Counter + + samplerContextCounter metrics.Counter + samplerIgnoreContextCounter metrics.Counter + + finishContextCounter metrics.Counter + finishIgnoreContextCounter metrics.Counter + + leakedContextCounter metrics.Counter + leakedIgnoreContextCounter metrics.Counter + + errorCounterMap sync.Map + interceptorTimeCost metrics.Histogram +} + +func GetSo11y(t *Tracer) *So11y { + once.Do(func() { + instance = &So11y{ + propagatedIgnoreContextCounter: t.NewCounter("sw_go_created_ignored_context_counter", + &metrics.Opts{ + Labels: map[string]string{"created_by": "propagated"}, + }).(metrics.Counter), + propagatedContextCounter: t.NewCounter("sw_go_created_tracing_context_counter", + &metrics.Opts{ + Labels: map[string]string{"created_by": "propagated"}, + }).(metrics.Counter), + + samplerIgnoreContextCounter: t.NewCounter("sw_go_created_ignored_context_counter", + &metrics.Opts{ + Labels: map[string]string{"created_by": "sampler"}, + }).(metrics.Counter), + samplerContextCounter: t.NewCounter("sw_go_created_tracing_context_counter", &metrics.Opts{ + Labels: map[string]string{"created_by": "sampler"}, + }).(metrics.Counter), + + finishIgnoreContextCounter: t.NewCounter( + "sw_go_finished_ignored_context_counter", nil).(metrics.Counter), + finishContextCounter: t.NewCounter( + "sw_go_finished_tracing_context_counter", nil).(metrics.Counter), + + leakedIgnoreContextCounter: t.NewCounter("sw_go_possible_leaked_context_counter", + &metrics.Opts{ + Labels: map[string]string{"source": "ignore"}, + }).(metrics.Counter), + leakedContextCounter: t.NewCounter("sw_go_possible_leaked_context_counter", + &metrics.Opts{ + Labels: map[string]string{"created_by": "tracing"}, + }).(metrics.Counter), + + interceptorTimeCost: t.NewHistogram("sw_go_tracing_context_performance", 0, + []float64{ + 1000, 10000, 50000, 100000, 300000, 500000, + 1000000, 5000000, 10000000, 20000000, 50000000, 100000000, + }, nil).(metrics.Histogram), + } + }) + return instance +} + +func (s *So11y) MeasureTracingContextCreation(isForceSample, isIgnored bool) { + if isForceSample { + if isIgnored { + s.propagatedIgnoreContextCounter.Inc(1) + } else { + s.propagatedContextCounter.Inc(1) + } + } else { + if isIgnored { + s.samplerIgnoreContextCounter.Inc(1) + } else { + s.samplerContextCounter.Inc(1) + } + } +} + +func (s *So11y) MeasureTracingContextCompletion(isIgnored bool) { + if isIgnored { + s.finishIgnoreContextCounter.Inc(1) + } else { + s.finishContextCounter.Inc(1) + } +} + +func (s *So11y) MeasureLeakedTracingContext(isIgnored bool) { + if isIgnored { + s.leakedIgnoreContextCounter.Inc(1) + } else { + s.leakedContextCounter.Inc(1) + } +} + +func (t *Tracer) So11y() interface{} { + return t +} + +func (t *Tracer) CollectErrorOfPlugin(pluginName string) { + if counter, ok := GetSo11y(t).errorCounterMap.Load(pluginName); ok { + if c, ok := counter.(metrics.Counter); ok { + c.Inc(1) + return + } + } + + t.mu.Lock() + defer t.mu.Unlock() + + if counter, ok := GetSo11y(t).errorCounterMap.Load(pluginName); ok { + if c, ok := counter.(metrics.Counter); ok { + c.Inc(1) + return + } + } + + if counter, ok := t.NewCounter( + "sw_go_interceptor_error_counter", &metrics.Opts{ + Labels: map[string]string{"plugin_name": pluginName}, + }).(metrics.Counter); ok { + GetSo11y(t).errorCounterMap.Store(pluginName, counter) + counter.Inc(1) + } +} + +func (t *Tracer) GenNanoTime() int64 { + return time.Now().UnixNano() +} + +func (t *Tracer) CollectDurationOfInterceptor(costTime int64) { + GetSo11y(t).interceptorTimeCost.Observe(float64(costTime)) +} diff --git a/plugins/core/span_default.go b/plugins/core/span_default.go index e3c2becc..5d006850 100644 --- a/plugins/core/span_default.go +++ b/plugins/core/span_default.go @@ -158,6 +158,7 @@ func (ds *DefaultSpan) ErrorOccured() { func (ds *DefaultSpan) End(changeParent bool) { ds.EndTime = time.Now() + GetSo11y(ds.tracer).MeasureTracingContextCompletion(false) if changeParent { if ctx := getTracingContext(); ctx != nil { ctx.SaveActiveSpan(ds.Parent) diff --git a/plugins/core/span_noop.go b/plugins/core/span_noop.go index 27612874..3591445a 100644 --- a/plugins/core/span_noop.go +++ b/plugins/core/span_noop.go @@ -25,6 +25,7 @@ const noopContextValue = "N/A" type NoopSpan struct { stackCount int + tracer *Tracer } func newSnapshotNoopSpan() *NoopSpan { @@ -34,9 +35,10 @@ func newSnapshotNoopSpan() *NoopSpan { } } -func newNoopSpan() *NoopSpan { +func newNoopSpan(tracer *Tracer) *NoopSpan { return &NoopSpan{ stackCount: 1, + tracer: tracer, } } @@ -99,6 +101,7 @@ func (n *NoopSpan) enterNoSpan() { func (n *NoopSpan) End() { n.stackCount-- if n.stackCount == 0 { + GetSo11y(n.tracer).MeasureTracingContextCompletion(true) if ctx := getTracingContext(); ctx != nil { ctx.SaveActiveSpan(nil) } diff --git a/plugins/core/tracer.go b/plugins/core/tracer.go index 53a5eff1..38ae4312 100644 --- a/plugins/core/tracer.go +++ b/plugins/core/tracer.go @@ -55,6 +55,7 @@ type Tracer struct { meterCollectListeners []func() ignoreSuffix []string traceIgnorePath []string + mu sync.Mutex } func (t *Tracer) Init(entity *reporter.Entity, rep reporter.Reporter, samp Sampler, logger operator.LogOperator, diff --git a/plugins/core/tracing.go b/plugins/core/tracing.go index 8c12c988..789682ce 100644 --- a/plugins/core/tracing.go +++ b/plugins/core/tracing.go @@ -233,10 +233,12 @@ func (s *ContextSnapshot) IsValid() bool { func (t *Tracer) createNoop(operationName string) (*TracingContext, TracingSpan, bool) { if !t.InitSuccess() || t.Reporter.ConnectionStatus() == reporter.ConnectionStatusDisconnect { - return nil, newNoopSpan(), true + GetSo11y(t).MeasureTracingContextCreation(false, true) + return nil, newNoopSpan(t), true } if tracerIgnore(operationName, t.ignoreSuffix, t.traceIgnorePath) { - return nil, newNoopSpan(), true + GetSo11y(t).MeasureTracingContextCreation(false, true) + return nil, newNoopSpan(t), true } ctx := getTracingContext() if ctx != nil { @@ -265,11 +267,12 @@ func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts isForceSample := len(ds.Refs) > 0 // Try to sample when it is not force sample if parentSpan == nil && !isForceSample { - // Force sample - sampled := t.Sampler.IsSampled(ds.OperationName) - if !sampled { + isSampled := t.Sampler.IsSampled(ds.OperationName) + if !isSampled { + GetSo11y(t).MeasureTracingContextCreation(false, true) + GetSo11y(t).MeasureLeakedTracingContext(true) // Filter by sample just return noop span - return newNoopSpan(), true, nil + return newNoopSpan(t), true, nil } } // process the opts from agent core for prepare building segment span @@ -284,6 +287,7 @@ func (t *Tracer) createSpan0(ctx *TracingContext, parent TracingSpan, pluginOpts for _, opt := range pluginOpts { opt.(tracing.SpanOption).Apply(s) } + GetSo11y(t).MeasureTracingContextCreation(isForceSample, false) return s, false, nil } diff --git a/test/plugins/scenarios/so11y/bin/startup.sh b/test/plugins/scenarios/so11y/bin/startup.sh new file mode 100755 index 00000000..c0a00a60 --- /dev/null +++ b/test/plugins/scenarios/so11y/bin/startup.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +home="$(cd "$(dirname $0)"; pwd)" +export SW_AGENT_PLUGIN_EXCLUDES=runtimemetrics +export SW_AGENT_METER_COLLECT_INTERVAL=1 +go build ${GO_BUILD_OPTS} -o so11y + +./so11y \ No newline at end of file diff --git a/test/plugins/scenarios/so11y/config/excepted.yml b/test/plugins/scenarios/so11y/config/excepted.yml new file mode 100644 index 00000000..c60ca7eb --- /dev/null +++ b/test/plugins/scenarios/so11y/config/excepted.yml @@ -0,0 +1,120 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +segmentItems: + - serviceName: so11y + segmentSize: gt 1 + segments: + - segmentId: not null + spans: + - operationName: GET:/propagated + parentSpanId: -1 + spanId: 0 + spanLayer: Http + startTime: nq 0 + endTime: nq 0 + componentId: 5004 + isError: false + spanType: Entry + peer: '' + skipAnalysis: false + tags: + - { key: http.method, value: GET } + - { key: url, value: 'localhost:8080/propagated' } + - { key: status_code, value: '200' } + refs: + - { parentEndpoint: 'GET:/so11y', networkAddress: 'localhost:8080', refType: CrossProcess, + parentSpanId: 1, parentTraceSegmentId: not null, parentServiceInstance: not null, + parentService: so11y, traceId: not null } + - segmentId: not null + spans: + - operationName: GET:/propagated + parentSpanId: 0 + spanId: 1 + spanLayer: Http + startTime: nq 0 + endTime: nq 0 + componentId: 5005 + isError: false + spanType: Exit + peer: localhost:8080 + skipAnalysis: false + tags: + - { key: http.method, value: GET } + - { key: url, value: 'localhost:8080/propagated' } + - { key: status_code, value: '200' } + - operationName: GET:/so11y + parentSpanId: -1 + spanId: 0 + spanLayer: Http + startTime: gt 0 + endTime: gt 0 + componentId: 5004 + isError: false + spanType: Entry + peer: '' + skipAnalysis: false + tags: + - { key: http.method, value: GET } + - { key: url, value: 'service:8080/so11y' } + - { key: status_code, value: '200' } +meterItems: + - serviceName: so11y + meterSize: 9 + meters: + - meterId: + name: sw_go_possible_leaked_context_counter + tags: + - {name: source, value: ignore} + singleValue: 0.0 + - meterId: + name: sw_go_finished_tracing_context_counter + tags: [] + singleValue: 4.0 + - meterId: + name: sw_go_finished_ignored_context_counter + tags: [] + singleValue: 2.0 + - meterId: + name: sw_go_tracing_context_performance + tags: [] + histogramBuckets: [0.0, 1000.0, 10000.0, 50000.0, 100000.0, 300000.0, 500000.0, 1000000.0, 5000000.0, 1.0E7, 2.0E7, 5.0E7, 1.0E8] + - meterId: + name: sw_go_created_ignored_context_counter + tags: + - {name: created_by, value: propagated} + singleValue: 0.0 + - meterId: + name: sw_go_created_tracing_context_counter + tags: + - {name: created_by, value: propagated} + singleValue: 0.0 + - meterId: + name: sw_go_created_ignored_context_counter + tags: + - {name: created_by, value: sampler} + singleValue: 2.0 + - meterId: + name: sw_go_possible_leaked_context_counter + tags: + - {name: created_by, value: tracing} + singleValue: 0.0 + - meterId: + name: sw_go_created_tracing_context_counter + tags: + - {name: created_by, value: sampler} + singleValue: 4.0 +logItems: [ ] diff --git a/test/plugins/scenarios/so11y/go.mod b/test/plugins/scenarios/so11y/go.mod new file mode 100644 index 00000000..a96ef41b --- /dev/null +++ b/test/plugins/scenarios/so11y/go.mod @@ -0,0 +1,3 @@ +module test/plugins/scenarios/so11y + +go 1.19 diff --git a/test/plugins/scenarios/so11y/go.sum b/test/plugins/scenarios/so11y/go.sum new file mode 100644 index 00000000..76bc04ec --- /dev/null +++ b/test/plugins/scenarios/so11y/go.sum @@ -0,0 +1,86 @@ +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/test/plugins/scenarios/so11y/main.go b/test/plugins/scenarios/so11y/main.go new file mode 100644 index 00000000..eaf7456f --- /dev/null +++ b/test/plugins/scenarios/so11y/main.go @@ -0,0 +1,79 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package main + +import ( + "io" + "log" + "net/http" + "time" + + _ "github.com/apache/skywalking-go" +) + +func main() { + http.HandleFunc("/so11y", so11yHandler) + http.HandleFunc("/propagated", propagatedHandler) + http.HandleFunc("/ignored.html", ignoredHandler) + + http.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusOK) + }) + + _ = http.ListenAndServe(":8080", nil) +} + +func so11yHandler(w http.ResponseWriter, r *http.Request) { + resp, err := http.Get("http://localhost:8080/propagated") + if err != nil { + log.Printf("request propagated error: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Print(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + resp, err = http.Get("http://localhost:8080/ignored.html") + if err != nil { + log.Printf("request ignored.html error: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + body, err = io.ReadAll(resp.Body) + if err != nil { + log.Print(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + _, _ = w.Write(body) + time.Sleep(2 * time.Second) // make sure the meter already uploaded +} + +func propagatedHandler(w http.ResponseWriter, r *http.Request) { + time.Sleep(2 * time.Second) // make sure the meter already uploaded + _, _ = w.Write([]byte("success")) +} + +func ignoredHandler(w http.ResponseWriter, r *http.Request) { + time.Sleep(2 * time.Second) // make sure the meter already uploaded + _, _ = w.Write([]byte("Nobody cares me.")) +} diff --git a/test/plugins/scenarios/so11y/plugin.yml b/test/plugins/scenarios/so11y/plugin.yml new file mode 100644 index 00000000..fefe9268 --- /dev/null +++ b/test/plugins/scenarios/so11y/plugin.yml @@ -0,0 +1,37 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +entry-service: http://${HTTP_HOST}:${HTTP_PORT}/so11y +health-checker: http://${HTTP_HOST}:${HTTP_PORT}/health +start-script: ./bin/startup.sh +framework: github.com/gin-gonic/gin +export-port: 8080 +support-version: + - go: 1.19 + framework: + - v1.9.0 + - go: 1.20 + framework: + - v1.9.0 + - go: 1.21 + framework: + - v1.9.0 + - go: 1.22 + framework: + - v1.9.0 + - go: 1.23 + framework: + - v1.9.0 \ No newline at end of file diff --git a/tools/go-agent/instrument/agentcore/instrument.go b/tools/go-agent/instrument/agentcore/instrument.go index bca365f0..41358839 100644 --- a/tools/go-agent/instrument/agentcore/instrument.go +++ b/tools/go-agent/instrument/agentcore/instrument.go @@ -44,7 +44,7 @@ var ( ReporterBasePackage = "agent/reporter" CopiedBasePackage = `skywalking-go(@[\d\w\.\-]+)?\/agent\/core` - CopiedSubPackages = []string{"", "tracing", "operator"} + CopiedSubPackages = []string{"", "tracing", "operator", "metrics"} ) type Instrument struct { diff --git a/tools/go-agent/instrument/plugins/enhance_method.go b/tools/go-agent/instrument/plugins/enhance_method.go index 8ebd85a3..19604392 100644 --- a/tools/go-agent/instrument/plugins/enhance_method.go +++ b/tools/go-agent/instrument/plugins/enhance_method.go @@ -35,7 +35,7 @@ import ( var methodEnhanceAdapterFiles = make(map[string]bool) var methodImportAgentCorePackages = []string{ - "log", "tracing", "operator", + "log", "tracing", "operator", "metrics", } type MethodEnhance struct { @@ -144,6 +144,8 @@ 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{ diff --git a/tools/go-agent/instrument/plugins/rewrite/context.go b/tools/go-agent/instrument/plugins/rewrite/context.go index a5136bad..f7413344 100644 --- a/tools/go-agent/instrument/plugins/rewrite/context.go +++ b/tools/go-agent/instrument/plugins/rewrite/context.go @@ -45,6 +45,7 @@ var OperatePrefix = GenerateCommonPrefix + "operator" var TypePrefix = OperatePrefix + "Type" var VarPrefix = OperatePrefix + "Var" var StaticMethodPrefix = OperatePrefix + "StaticMethod" +var BeforeInterStart = "beforeInterStart" type Context struct { pkgFullPath string @@ -192,7 +193,9 @@ func (c *Context) enhanceTypeNameWhenRewrite(fieldType dst.Expr, parent dst.Node if _, ok := parent.(*dst.CallExpr); ok { t.Name = fmt.Sprintf("%s%s%s", StaticMethodPrefix, c.currentPackageTitle, name) } else { - t.Name = fmt.Sprintf("%s%s%s", TypePrefix, c.currentPackageTitle, name) + if !strings.Contains(name, BeforeInterStart) { + t.Name = fmt.Sprintf("%s%s%s", TypePrefix, c.currentPackageTitle, name) + } } return name, t.Name case *dst.SelectorExpr: diff --git a/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl b/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl index ed278d3a..fefe85e4 100644 --- a/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl +++ b/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl @@ -1,8 +1,11 @@ defer func() { if r := recover(); r != nil { + operator.ErrorOfPlugin("{{.InstrumentName}}") // 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) } }() @@ -24,4 +27,6 @@ if (invocation.isContinue) { *ret_{{$index}} = tmp_{{$index}} } {{- end }} -} \ No newline at end of file +} +{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart +operator.DurationOfInterceptor({{.FuncID}}_interTimeCost) \ No newline at end of file diff --git a/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl b/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl index e29f34b7..6398dccc 100644 --- a/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl +++ b/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl @@ -1,8 +1,12 @@ +{{.FuncID}}_interTimeCost = int64(0) +{{.FuncID}}_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 = &operator.realInvocation{} @@ -33,6 +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 return {{ range $index, $value := .Results -}} def_res_{{$index}}, @@ -44,10 +49,14 @@ if (invocation.isContinue) { def_res_{{$index}} = (invocation.returnValues[{{$index}}]).({{$value.PackagedTypeName}}) } {{- end}} + {{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart + return {{ range $index, $value := .Results -}} def_res_{{$index}}, {{- end}}invocation, true } +{{.FuncID}}_interTimeCost += operator.NanoTime() - {{.FuncID}}_beforeInterStart + return {{ range $index, $value := .Results -}} def_res_{{$index}}, {{- end}}invocation, false \ No newline at end of file