This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/skywalking-go.git
The following commit(s) were added to refs/heads/main by this push: new 74b6886 feat: Add SetComponent() to toolkit-trace (#135) 74b6886 is described below commit 74b68861aed04b4d78fcc5b4bcd925113f7de81d Author: Alipebt <1160756...@qq.com> AuthorDate: Tue Oct 24 21:22:33 2023 +0800 feat: Add SetComponent() to toolkit-trace (#135) --- .../advanced-features/manual-apis/toolkit-trace.md | 27 ++++++++++++++ plugins/trace-activation/instrument.go | 5 +++ .../trace-activation/set_component_interceptor.go | 41 +++++++--------------- .../scenarios/trace-activation/config/excepted.yml | 11 ++++++ test/plugins/scenarios/trace-activation/main.go | 1 + .../scenarios/trace-activation/test_service.go | 6 ++++ toolkit/trace/api.go | 3 ++ 7 files changed, 65 insertions(+), 29 deletions(-) diff --git a/docs/en/advanced-features/manual-apis/toolkit-trace.md b/docs/en/advanced-features/manual-apis/toolkit-trace.md index 1e0073a..6eeb810 100644 --- a/docs/en/advanced-features/manual-apis/toolkit-trace.md +++ b/docs/en/advanced-features/manual-apis/toolkit-trace.md @@ -23,6 +23,21 @@ type ExtractorRef func(headerKey string) (string, error) type InjectorRef func(headerKey, headerValue string) error ``` +The following demo demonstrates how to pass the Context Carrier in the Tracing API: + +```go +// create a new entry span and extract the context carrier from the request +trace.CreateEntrySpan("EntrySpan", func(headerKey string) (string, error) { + return request.Header.Get(headerKey), nil +}) + +// create a new exit span and inject the context carrier into the request +trace.CreateExitSpan("ExitSpan", request.Host, func(headerKey, headerValue string) error { + request.Header.Add(headerKey, headerValue) + return nil +}) +``` + ### Create Span Use `trace.CreateEntrySpan()` API to create entry span, and then use `SpanRef` to contain the reference of created span in agent kernel. @@ -70,6 +85,18 @@ trace.AddLog(...string) trace.SetTag("key","value") ``` +### Set ComponentID + +Use `trace.SetComponent()` to set the component id of the Span + +- the type of parameter is int32. + +```go +trace.SetComponent(ComponentID) +``` + +The Component ID in Span is used to identify the current component, which is declared in the [component libraries YAML](https://github.com/apache/skywalking/blob/master/oap-server/server-starter/src/main/resources/component-libraries.yml) from the OAP server side. + ### Async Prepare/Finish Use `trace.PrepareAsync()` to make current span still alive until `trace.AsyncFinish()` called. diff --git a/plugins/trace-activation/instrument.go b/plugins/trace-activation/instrument.go index 1721d3b..f091226 100644 --- a/plugins/trace-activation/instrument.go +++ b/plugins/trace-activation/instrument.go @@ -128,6 +128,11 @@ func (i *Instrument) Points() []*instrument.Point { At: instrument.NewStaticMethodEnhance("SetCorrelation"), Interceptor: "SetCorrelationInterceptor", }, + { + PackagePath: "", PackageName: "trace", + At: instrument.NewStaticMethodEnhance("SetComponent"), + Interceptor: "SetComponentInterceptor", + }, } } diff --git a/test/plugins/scenarios/trace-activation/main.go b/plugins/trace-activation/set_component_interceptor.go similarity index 51% copy from test/plugins/scenarios/trace-activation/main.go copy to plugins/trace-activation/set_component_interceptor.go index 2ca47ce..1f1d411 100644 --- a/test/plugins/scenarios/trace-activation/main.go +++ b/plugins/trace-activation/set_component_interceptor.go @@ -15,41 +15,24 @@ // specific language governing permissions and limitations // under the License. -package main +package traceactivation import ( - "net/http" - - _ "github.com/apache/skywalking-go" - "github.com/apache/skywalking-go/toolkit/trace" + "github.com/apache/skywalking-go/plugins/core/operator" + "github.com/apache/skywalking-go/plugins/core/tracing" ) -func providerHandler(w http.ResponseWriter, r *http.Request) { - trace.CreateLocalSpan("testSetCorrelation") - trace.SetTag("testCorrelation", trace.GetCorrelation("testCorrelation")) - trace.StopSpan() +type SetComponentInterceptor struct { } -func consumerHandler(w http.ResponseWriter, r *http.Request) { - testTag() - testLog() - testGetSegmentID() - testGetSpanID() - testGetTraceID() - testSetOperationName() - testCorrelation() - testContext() - testContextCarrier() +func (h *SetComponentInterceptor) BeforeInvoke(invocation operator.Invocation) error { + span := tracing.ActiveSpan() + if span != nil { + span.SetComponent(invocation.Args()[0].(int32)) + } + return nil } -func main() { - http.HandleFunc("/provider", providerHandler) - - http.HandleFunc("/consumer", consumerHandler) - - http.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) { - writer.WriteHeader(http.StatusOK) - }) - - _ = http.ListenAndServe(":8080", nil) +func (h *SetComponentInterceptor) AfterInvoke(invocation operator.Invocation, result ...interface{}) error { + return nil } diff --git a/test/plugins/scenarios/trace-activation/config/excepted.yml b/test/plugins/scenarios/trace-activation/config/excepted.yml index fe8abc8..d8a0042 100644 --- a/test/plugins/scenarios/trace-activation/config/excepted.yml +++ b/test/plugins/scenarios/trace-activation/config/excepted.yml @@ -191,6 +191,17 @@ segmentItems: parentSpanId: 10, parentTraceSegmentId: not null, parentServiceInstance: not null, parentService: trace-activation, traceId: not null} + - operationName: testComponent + parentSpanId: 8 + spanId: 12 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 5006 + isError: false + spanType: Local + peer: '' + skipAnalysis: false - operationName: GET:/consumer parentSpanId: -1 spanId: 0 diff --git a/test/plugins/scenarios/trace-activation/main.go b/test/plugins/scenarios/trace-activation/main.go index 2ca47ce..a9da938 100644 --- a/test/plugins/scenarios/trace-activation/main.go +++ b/test/plugins/scenarios/trace-activation/main.go @@ -40,6 +40,7 @@ func consumerHandler(w http.ResponseWriter, r *http.Request) { testCorrelation() testContext() testContextCarrier() + testComponent() } func main() { diff --git a/test/plugins/scenarios/trace-activation/test_service.go b/test/plugins/scenarios/trace-activation/test_service.go index bf521e0..9345194 100644 --- a/test/plugins/scenarios/trace-activation/test_service.go +++ b/test/plugins/scenarios/trace-activation/test_service.go @@ -97,3 +97,9 @@ func testCorrelation() { return } } + +func testComponent() { + trace.CreateLocalSpan("testComponent") + trace.SetComponent(5006) + trace.StopSpan() +} diff --git a/toolkit/trace/api.go b/toolkit/trace/api.go index 66b2beb..00bd491 100644 --- a/toolkit/trace/api.go +++ b/toolkit/trace/api.go @@ -75,3 +75,6 @@ func GetCorrelation(key string) string { // nolint func SetCorrelation(key string, value string) { } + +func SetComponent(componentID int32) { +}