Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Release Notes.
* Fix cannot find file when exec build in test/plugins.
* Fix not set span error when http status code >= 400
* Fix http plugin cannot provide peer name when optional Host is empty.
* Fix Correctly instrument newproc1 for Go 1.23+ parameter counts

#### Issues and PR
- All issues are [here](https://github.com/apache/skywalking/milestone/219?closed=1)
Expand Down
42 changes: 41 additions & 1 deletion tools/go-agent/instrument/api/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package api

import "path/filepath"
import (
"path/filepath"
"strconv"
"strings"
)

type CompileOptions struct {
Package string `swflag:"-p"`
Expand All @@ -35,3 +39,39 @@ func (c *CompileOptions) IsValid() bool {
func (c *CompileOptions) CompileBaseDir() string {
return filepath.Dir(filepath.Dir(c.Output))
}

func (c *CompileOptions) CheckGoVersionGreaterOrEqual(requiredMajor, requiredMinor int) bool {
if c.Lang == "" {
return false
}
if !strings.HasPrefix(c.Lang, "go") {
return false
}
versionStr := strings.TrimPrefix(c.Lang, "go")
parts := strings.SplitN(versionStr, ".", 3)
if len(parts) < 2 {
return false
}

majorStr := parts[0]
currentMajor64, err := strconv.ParseInt(majorStr, 10, 64)
if err != nil {
return false
}
currentMajor := int(currentMajor64)

minorStr := parts[1]
currentMinor64, err := strconv.ParseInt(minorStr, 10, 64)
if err != nil {
return false
}
currentMinor := int(currentMinor64)

if currentMajor > requiredMinor {
Copy link

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison should be with requiredMajor, not requiredMinor, to correctly determine if the current major version exceeds the required major version.

Suggested change
if currentMajor > requiredMinor {
if currentMajor > requiredMajor {

Copilot uses AI. Check for mistakes.
return true
}
if currentMajor == requiredMajor && currentMinor >= requiredMajor {
Copy link

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition incorrectly compares currentMinor with requiredMajor; it should compare currentMinor with requiredMinor for an accurate version check.

Suggested change
if currentMajor == requiredMajor && currentMinor >= requiredMajor {
if currentMajor == requiredMajor && currentMinor >= requiredMinor {

Copilot uses AI. Check for mistakes.
return true
}
return false
}
19 changes: 7 additions & 12 deletions tools/go-agent/instrument/runtime/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
package runtime

import (
"strconv"
"strings"

"github.com/dave/dst"
"github.com/dave/dst/dstutil"

Expand Down Expand Up @@ -73,7 +70,11 @@ func (r *Instrument) FilterAndEdit(path string, curFile *dst.File, cursor *dstut
if len(n.Type.Results.List) != 1 {
return false
}
if len(n.Type.Params.List) != 3 {
expectedParamCount := 3
if r.opts.CheckGoVersionGreaterOrEqual(1, 23) {
expectedParamCount = 5
Copy link

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the PR description, for Go 1.23 and later, runtime.newproc1 should expect 2 parameters rather than 5. Please update the logic to set the expected parameter count to 2 for Go 1.23+.

Suggested change
expectedParamCount = 5
expectedParamCount = 2

Copilot uses AI. Check for mistakes.
}
if len(n.Type.Params.List) != expectedParamCount {
return false
}
parameters := tools.EnhanceParameterNames(n.Type.Params, tools.FieldListTypeParam)
Expand Down Expand Up @@ -106,14 +107,8 @@ func (r *Instrument) AfterEnhanceFile(fromPath, newPath string) error {
}

func (r *Instrument) parseInternalAtomicPath() string {
if strings.HasPrefix(r.opts.Lang, "go1.") {
_, after, found := strings.Cut(r.opts.Lang, ".")
if found {
i, err := strconv.ParseInt(after, 10, 64)
if err == nil && i >= 23 {
return "internal/runtime/atomic"
}
}
if r.opts.CheckGoVersionGreaterOrEqual(1, 23) {
return "internal/runtime/atomic"
}
return defaultInternalAtomicPath
}
Expand Down