Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Commit 7b13f78

Browse files
author
Julio Guerra
committed
v0.10.1
Fix: - Fix the instrumentation tool ignoring vendored packages and leading to missing hook points in the agent.
2 parents dc7c33e + f8d6a42 commit 7b13f78

File tree

7 files changed

+104
-6
lines changed

7 files changed

+104
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# v0.10.1
2+
3+
## Fix
4+
5+
- (#116) Fix the instrumentation tool ignoring vendored packages, leading to
6+
missing hook points in the agent.
7+
18
# v0.10.0
29

310
## New Features

internal/app/runtime.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,15 @@ func executable(logger *plog.Logger) string {
153153

154154
func VendorPrefix() string {
155155
type t struct{}
156-
pkg := reflect.TypeOf(t{}).PkgPath()
157-
vendor := "vendor/"
158-
i := strings.Index(pkg, vendor)
156+
pkgPath := reflect.TypeOf(t{}).PkgPath()
157+
return vendorPrefix(pkgPath)
158+
}
159+
160+
func vendorPrefix(pkgPath string) (prefix string) {
161+
vendor := "/vendor/"
162+
i := strings.Index(pkgPath, vendor)
159163
if i == -1 {
160164
return ""
161165
}
162-
return pkg[:i+len(vendor)]
166+
return pkgPath[:i+len(vendor)]
163167
}

internal/app/runtime_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2016 - 2020 Sqreen. All Rights Reserved.
2+
// Please refer to our terms for more information:
3+
// https://www.sqreen.io/terms.html
4+
5+
package app
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestVendorPrefix(t *testing.T) {
14+
for _, tc := range []struct {
15+
PkgPath string
16+
Expected string
17+
}{
18+
{
19+
PkgPath: "github.com/sqreen/go-agent/internal/protection/http",
20+
Expected: "",
21+
},
22+
23+
{
24+
PkgPath: "github.com/my-org/my-app/vendor/github.com/sqreen/go-agent/internal/protection/http",
25+
Expected: "github.com/my-org/my-app/vendor/",
26+
},
27+
28+
{
29+
PkgPath: "my-app/vendor/github.com/sqreen/go-agent/internal/protection/http",
30+
Expected: "my-app/vendor/",
31+
},
32+
} {
33+
tc := tc
34+
t.Run("", func(t *testing.T) {
35+
got := vendorPrefix(tc.PkgPath)
36+
require.Equal(t, tc.Expected, got)
37+
})
38+
}
39+
}

internal/rule/rule.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ func NewEngine(logger Logger, instrumentationEngine InstrumentationFace, metrics
5959
if instrumentationEngine == nil {
6060
instrumentationEngine = defaultInstrumentationEngine
6161
}
62+
63+
vendorPrefix := app.VendorPrefix()
64+
if vendorPrefix != "" {
65+
logger.Debugf("vendor folder detected at `%s`", vendorPrefix)
66+
}
67+
6268
return &Engine{
6369
logger: logger,
6470
metricsEngine: metricsEngine,
6571
publicKey: publicKey,
66-
vendorPrefix: app.VendorPrefix(),
72+
vendorPrefix: vendorPrefix,
6773
instrumentationEngine: instrumentationEngine,
6874
errorMetricsStore: errorMetricsStore,
6975
}

internal/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package version
66

7-
const version = "0.10.0"
7+
const version = "0.10.1"
88

99
func Version() string {
1010
return version

sdk/sqreen-instrumentation-tool/ast.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,21 @@ var limitedInstrumentationPkgPrefixes = []string{
449449
"database/sql",
450450
}
451451

452+
// Given the Go vendoring conventions, return the package prefix of the vendored
453+
// package. For example, given `my-app/vendor/github.com/sqreen/go-agent`,
454+
// the function should return `my-app/vendor/`
455+
func unvendorPackagePath(pkg string) (unvendored string) {
456+
vendorDir := "/vendor/"
457+
i := strings.Index(pkg, vendorDir)
458+
if i == -1 {
459+
return pkg
460+
}
461+
return pkg[i+len(vendorDir):]
462+
}
463+
452464
func isPackageNameIgnored(pkg string, fullInstrumentation bool) bool {
465+
pkg = unvendorPackagePath(pkg)
466+
453467
for _, prefix := range ignoredPkgPrefixes {
454468
if strings.HasPrefix(pkg, prefix) {
455469
return true

sdk/sqreen-instrumentation-tool/main_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,31 @@ func TestParseCommandID(t *testing.T) {
4646
})
4747
}
4848
}
49+
50+
func TestUnvendorPackagePath(t *testing.T) {
51+
for _, tc := range []struct {
52+
PkgPath string
53+
Expected string
54+
}{
55+
{
56+
PkgPath: "github.com/sqreen/go-agent/internal/protection/http",
57+
Expected: "github.com/sqreen/go-agent/internal/protection/http",
58+
},
59+
60+
{
61+
PkgPath: "github.com/my-org/my-app/vendor/github.com/sqreen/go-agent/internal/protection/http",
62+
Expected: "github.com/sqreen/go-agent/internal/protection/http",
63+
},
64+
65+
{
66+
PkgPath: "my-app/vendor/github.com/sqreen/go-agent/internal/protection/http",
67+
Expected: "github.com/sqreen/go-agent/internal/protection/http",
68+
},
69+
} {
70+
tc := tc
71+
t.Run("", func(t *testing.T) {
72+
got := unvendorPackagePath(tc.PkgPath)
73+
require.Equal(t, tc.Expected, got)
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)