Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: toolkit span add event impl #200

Merged
merged 5 commits into from
Sep 2, 2024
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: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Release Notes.
------------------
#### Features

* support attaching events to span in the toolkit.

#### Plugins

#### Bug Fixes
Expand Down
8 changes: 8 additions & 0 deletions plugins/trace-activation/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func (i *Instrument) Points() []*instrument.Point {
PackagePath: "trace", At: instrument.NewMethodEnhance("*SpanRef", "AddLog"),
Interceptor: "AsyncLogInterceptor",
},
{
PackagePath: "trace", At: instrument.NewMethodEnhance("*SpanRef", "AddEvent"),
Interceptor: "AsyncAddEventInterceptor",
},
{
PackagePath: "trace", At: instrument.NewStaticMethodEnhance("AddEvent"),
Interceptor: "AddEventInterceptor",
},
{
PackagePath: "trace", At: instrument.NewStaticMethodEnhance("AddLog"),
Interceptor: "AddLogInterceptor",
Expand Down
44 changes: 44 additions & 0 deletions plugins/trace-activation/trace/add_event_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 traceactivation

import (
"github.com/apache/skywalking-go/plugins/core/operator"
"github.com/apache/skywalking-go/plugins/core/tracing"
"github.com/apache/skywalking-go/toolkit/trace"
)

type AddEventInterceptor struct {
}

func (h *AddEventInterceptor) BeforeInvoke(invocation operator.Invocation) error {
span := tracing.ActiveSpan()
if span != nil {
et := invocation.Args()[0].(trace.EventType)
event := invocation.Args()[1].(string)
if event == "" {
event = defaultEventMsg
}
span.Log(string(et), event)
}
return nil
}

func (h *AddEventInterceptor) AfterInvoke(_ operator.Invocation, _ ...interface{}) error {
return nil
}
47 changes: 47 additions & 0 deletions plugins/trace-activation/trace/async_add_event_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 traceactivation

import (
"github.com/apache/skywalking-go/plugins/core/operator"
"github.com/apache/skywalking-go/plugins/core/tracing"
"github.com/apache/skywalking-go/toolkit/trace"
)

type AsyncAddEventInterceptor struct {
}

func (h *AsyncAddEventInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *AsyncAddEventInterceptor) AfterInvoke(invocation operator.Invocation, _ ...interface{}) error {
enhanced, ok := invocation.CallerInstance().(operator.EnhancedInstance)
if !ok {
return nil
}
s := enhanced.GetSkyWalkingDynamicField().(tracing.Span)
et := invocation.Args()[0].(trace.EventType)
event := invocation.Args()[1].(string)
if event == "" {
event = defaultEventMsg
}
s.Log(string(et), event)
enhanced.SetSkyWalkingDynamicField(s)
return nil
}
22 changes: 22 additions & 0 deletions plugins/trace-activation/trace/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 traceactivation
ShyunnY marked this conversation as resolved.
Show resolved Hide resolved

const (
defaultEventMsg = "unsetEvent"
)
16 changes: 16 additions & 0 deletions test/plugins/scenarios/trace-activation/config/excepted.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ segmentItems:
logs:
- logEvent:
- { key: testAsyncLog, value: success }
- logEvent:
- { key: warn, value: bar }
- operationName: testAddEvent
parentSpanId: 8
spanId: 14
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
isError: false
spanType: Local
peer: ''
skipAnalysis: false
logs:
- logEvent:
- { key: debug, value: foo }
- operationName: GET:/consumer
parentSpanId: -1
spanId: 0
Expand Down
1 change: 1 addition & 0 deletions test/plugins/scenarios/trace-activation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func consumerHandler(w http.ResponseWriter, r *http.Request) {
testContextCarrier()
testComponent()
testAsyncInCrossGoroutine()
testEvent()
ShyunnY marked this conversation as resolved.
Show resolved Hide resolved
}

func main() {
Expand Down
7 changes: 7 additions & 0 deletions test/plugins/scenarios/trace-activation/test_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func testTag() {
trace.StopSpan()
}

func testEvent() {
trace.CreateLocalSpan("testAddEvent")
trace.AddEvent(trace.DebugEventType, "foo")
trace.StopSpan()
}

func testLog() {
trace.CreateLocalSpan("testAddLog")
trace.AddLog("AddLog", "success")
Expand Down Expand Up @@ -112,6 +118,7 @@ func testAsyncInCrossGoroutine() {
go func() {
s.SetTag("testAsyncTag", "success")
s.AddLog("testAsyncLog", "success")
s.AddEvent(trace.WarnEventType, "bar")
ShyunnY marked this conversation as resolved.
Show resolved Hide resolved
s.AsyncFinish()
ch <- ""
}()
Expand Down
4 changes: 4 additions & 0 deletions toolkit/trace/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func SetTag(key string, value string) {
func AddLog(...string) {
}

func AddEvent(et EventType, event string) {

}

func GetCorrelation(key string) string {
return ""
}
Expand Down
1 change: 1 addition & 0 deletions toolkit/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package trace

// EventType Defines the type of Span event
type EventType string

const (
Expand Down
2 changes: 1 addition & 1 deletion tools/go-agent/instrument/plugins/rewrite/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *Context) rewriteTopLevelNames(files map[*FileInfo]*dst.File) {
case *dst.ImportSpec:
case *dst.TypeSpec:
case *dst.GenDecl:
if n.Tok == token.VAR && cursor.Parent() == f {
if (n.Tok == token.VAR || n.Tok == token.CONST) && cursor.Parent() == f {
for _, spec := range n.Specs {
if valueSpec, ok := spec.(*dst.ValueSpec); ok {
c.Var(valueSpec, true)
Expand Down
Loading