From 8295e19c277357e0c1b7aa577553937a95c6d5af Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 7 Feb 2023 14:16:26 -0800 Subject: [PATCH 1/8] Add testing for Go 1.20 --- .github/workflows/ci.yml | 2 +- CHANGELOG.md | 3 +++ README.md | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54a108c5276..f8c40acb2e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -109,7 +109,7 @@ jobs: compatibility-test: strategy: matrix: - go-version: [1.19, 1.18] + go-version: ["1.20", 1.19, 1.18] os: [ubuntu-latest, macos-latest, windows-latest] # GitHub Actions does not support arm* architectures on default # runners. It is possible to acomplish this with a self-hosted runner diff --git a/CHANGELOG.md b/CHANGELOG.md index 540bc79fa4f..cfdc19f0c80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068) +- Support [Go 1.20]. (#TBD) ## [1.14.0/0.39.0/0.8.0] - 2023-02-07 @@ -680,3 +681,5 @@ First official tagged release of `contrib` repository. [0.8.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.8.0 [0.7.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.7.0 [0.6.1]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.6.1 + +[Go 1.20]: https://go.dev/doc/go1.20 diff --git a/README.md b/README.md index 7a8ee9adf1d..87b5104320d 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,19 @@ This project is tested on the following systems. | OS | Go Version | Architecture | | ------- | ---------- | ------------ | +| Ubuntu | 1.20 | amd64 | | Ubuntu | 1.19 | amd64 | | Ubuntu | 1.18 | amd64 | +| Ubuntu | 1.20 | 386 | | Ubuntu | 1.19 | 386 | | Ubuntu | 1.18 | 386 | +| MacOS | 1.20 | amd64 | | MacOS | 1.19 | amd64 | | MacOS | 1.18 | amd64 | +| Windows | 1.20 | amd64 | | Windows | 1.19 | amd64 | | Windows | 1.18 | amd64 | +| Windows | 1.20 | 386 | | Windows | 1.19 | 386 | | Windows | 1.18 | 386 | From a08e89debc8803b2cabbdb101f7359349ddf2b96 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 7 Feb 2023 14:17:06 -0800 Subject: [PATCH 2/8] Update changelog PR number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfdc19f0c80..c5b539cbe6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068) -- Support [Go 1.20]. (#TBD) +- Support [Go 1.20]. (#3372) ## [1.14.0/0.39.0/0.8.0] - 2023-02-07 From f5bf363aac88c5899ec2be112c57e0e23c3100ce Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 8 Feb 2023 10:13:10 -0800 Subject: [PATCH 3/8] Cleanup instead of defer in beego test --- .../github.com/astaxie/beego/otelbeego/test/beego_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go index b0a8684da12..c69f2ec2f43 100644 --- a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go +++ b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go @@ -240,7 +240,7 @@ func TestRender(t *testing.T) { // Create the view file, err := os.CreateTemp(dir, "*index.tpl") require.NoError(t, err) - defer file.Close() + t.Cleanup(func() { require.NoError(t, file.Close()) }) _, err = file.WriteString(htmlStr) require.NoError(t, err) // Add path to view path From 54bb8065ccec370a53ee6d907967e2f592b32e26 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 8 Feb 2023 11:26:08 -0800 Subject: [PATCH 4/8] Use os.TempDir instead of t.TempDir --- .../astaxie/beego/otelbeego/test/beego_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go index c69f2ec2f43..09f6b070887 100644 --- a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go +++ b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go @@ -234,19 +234,24 @@ func TestRender(t *testing.T) { "Hello World" + "This is a template test. Hello {{.name}}" - // Create a temp directory to hold a view - dir := t.TempDir() + // Create a temp directory to hold a view. Do not use t.TempDir because we + // cannot close the references to the directory once the templates are + // added to beego and Windows errors when the testing system tries to + // remove the directory (2023-02-08 windows-2022 (20230129.1) Go 1.20.0). + dir := filepath.Join(os.TempDir(), "TestRender") + require.NoError(t, os.MkdirAll(dir, 0777)) + t.Cleanup(func() { require.NoError(t, os.RemoveAll(dir)) }) // Create the view - file, err := os.CreateTemp(dir, "*index.tpl") + f, err := os.CreateTemp(dir, "*index.tpl") require.NoError(t, err) - t.Cleanup(func() { require.NoError(t, file.Close()) }) - _, err = file.WriteString(htmlStr) + t.Cleanup(func() { require.NoError(t, f.Close()) }) + _, err = f.WriteString(htmlStr) require.NoError(t, err) // Add path to view path require.NoError(t, beego.AddViewPath(dir)) beego.SetViewsPath(dir) - _, tplName = filepath.Split(file.Name()) + _, tplName = filepath.Split(f.Name()) sr := tracetest.NewSpanRecorder() tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) From e60e25822606935a48114e7922cc831128177756 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 8 Feb 2023 13:53:03 -0800 Subject: [PATCH 5/8] Remove temp dir after replaceBeego --- .../beego/otelbeego/test/beego_test.go | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go index 09f6b070887..027e1923260 100644 --- a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go +++ b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go @@ -225,25 +225,23 @@ func TestStatic(t *testing.T) { assertSpan(t, spans[0], tc) } +var htmlStr = ` + + + + Hello World + + This is a template test. Hello {{.name}} + +` + func TestRender(t *testing.T) { - // Disable autorender to enable traced render - beego.BConfig.WebConfig.AutoRender = false - addTestRoutes(t) - defer replaceBeego() - htmlStr := "" + - "Hello World" + - "This is a template test. Hello {{.name}}" - - // Create a temp directory to hold a view. Do not use t.TempDir because we - // cannot close the references to the directory once the templates are - // added to beego and Windows errors when the testing system tries to - // remove the directory (2023-02-08 windows-2022 (20230129.1) Go 1.20.0). - dir := filepath.Join(os.TempDir(), "TestRender") - require.NoError(t, os.MkdirAll(dir, 0777)) - t.Cleanup(func() { require.NoError(t, os.RemoveAll(dir)) }) + // Create a temp directory to hold a view. + dir := t.TempDir() // Create the view - f, err := os.CreateTemp(dir, "*index.tpl") + tplName = "index.tpl" + f, err := os.Create(filepath.Join(dir, tplName)) require.NoError(t, err) t.Cleanup(func() { require.NoError(t, f.Close()) }) _, err = f.WriteString(htmlStr) @@ -251,7 +249,11 @@ func TestRender(t *testing.T) { // Add path to view path require.NoError(t, beego.AddViewPath(dir)) beego.SetViewsPath(dir) - _, tplName = filepath.Split(f.Name()) + + // Disable autorender to enable traced render + beego.BConfig.WebConfig.AutoRender = false + addTestRoutes(t) + t.Cleanup(replaceBeego) sr := tracetest.NewSpanRecorder() tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) From 216accd0352b556e6d2fe5389ad11d69f45c9aeb Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 8 Feb 2023 14:04:51 -0800 Subject: [PATCH 6/8] Unset the views path --- .../github.com/astaxie/beego/otelbeego/test/beego_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go index 027e1923260..5db6d8d5ff5 100644 --- a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go +++ b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go @@ -249,6 +249,7 @@ func TestRender(t *testing.T) { // Add path to view path require.NoError(t, beego.AddViewPath(dir)) beego.SetViewsPath(dir) + t.Cleanup(func() { beego.SetViewsPath("") }) // Disable autorender to enable traced render beego.BConfig.WebConfig.AutoRender = false From 505f19f59fc1ec5b6eebb73307073206bf4250a0 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 8 Feb 2023 17:24:18 -0800 Subject: [PATCH 7/8] Use custom filesystem for testing tmpls --- .../beego/otelbeego/test/beego_test.go | 69 ++++++++++++------- .../astaxie/beego/otelbeego/test/go.mod | 1 + 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go index 5db6d8d5ff5..d466206d412 100644 --- a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go +++ b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go @@ -21,9 +21,9 @@ import ( "net/http" "net/http/httptest" "os" - "path/filepath" "strings" "testing" + "time" "go.opentelemetry.io/contrib/instrumentation/github.com/astaxie/beego/otelbeego" "go.opentelemetry.io/contrib/instrumentation/github.com/astaxie/beego/otelbeego/internal" @@ -36,6 +36,7 @@ import ( "github.com/astaxie/beego" beegoCtx "github.com/astaxie/beego/context" + assetfs "github.com/elazarl/go-bindata-assetfs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -236,25 +237,47 @@ var htmlStr = ` ` func TestRender(t *testing.T) { - // Create a temp directory to hold a view. - dir := t.TempDir() - - // Create the view tplName = "index.tpl" - f, err := os.Create(filepath.Join(dir, tplName)) - require.NoError(t, err) - t.Cleanup(func() { require.NoError(t, f.Close()) }) - _, err = f.WriteString(htmlStr) - require.NoError(t, err) - // Add path to view path - require.NoError(t, beego.AddViewPath(dir)) - beego.SetViewsPath(dir) - t.Cleanup(func() { beego.SetViewsPath("") }) - - // Disable autorender to enable traced render - beego.BConfig.WebConfig.AutoRender = false - addTestRoutes(t) - t.Cleanup(replaceBeego) + beego.SetTemplateFSFunc(func() http.FileSystem { + return &assetfs.AssetFS{ + Asset: func(path string) ([]byte, error) { + if path == tplName { + return []byte(htmlStr), nil + } + return nil, os.ErrNotExist + }, + AssetDir: func(path string) ([]string, error) { + if path == "" { + return []string{tplName}, nil + } + return nil, os.ErrNotExist + }, + AssetInfo: func(path string) (os.FileInfo, error) { + if path == tplName { + return &assetfs.FakeFile{ + Path: path, + Len: int64(len(htmlStr)), + Timestamp: time.Now(), + }, nil + } + return nil, os.ErrNotExist + }, + } + }) + viewPath := "/" + require.NoError(t, beego.AddViewPath(viewPath)) + + ctrl := &testController{ + Controller: beego.Controller{ + ViewPath: viewPath, + EnableRender: true, + }, + T: t, + } + app := beego.NewApp() + app.Handlers.Add("/template/render", ctrl, "get:TemplateRender") + app.Handlers.Add("/template/renderstring", ctrl, "get:TemplateRenderString") + app.Handlers.Add("/template/renderbytes", ctrl, "get:TemplateRenderBytes") sr := tracetest.NewSpanRecorder() tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) @@ -267,7 +290,7 @@ func TestRender(t *testing.T) { rr := httptest.NewRecorder() req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost/template%s", str), nil) require.NoError(t, err) - mw(beego.BeeApp.Handlers).ServeHTTP(rr, req) + mw(app.Handlers).ServeHTTP(rr, req) body, err := io.ReadAll(rr.Result().Body) require.Equal(t, strings.Replace(htmlStr, "{{.name}}", "test", 1), string(body)) require.NoError(t, err) @@ -277,16 +300,14 @@ func TestRender(t *testing.T) { require.Len(t, spans, 6) // 3 HTTP requests, each creating 2 spans for _, span := range spans { switch span.Name() { - case "/template/render": - case "/template/renderstring": - case "/template/renderbytes": + case "GET": continue case internal.RenderTemplateSpanName, internal.RenderStringSpanName, internal.RenderBytesSpanName: assert.Contains(t, span.Attributes(), internal.TemplateKey.String(tplName)) default: - t.Fatal("unexpected span name") + t.Fatal("unexpected span name", span.Name()) } } } diff --git a/instrumentation/github.com/astaxie/beego/otelbeego/test/go.mod b/instrumentation/github.com/astaxie/beego/otelbeego/test/go.mod index 898a438cf4b..6879eee3f96 100644 --- a/instrumentation/github.com/astaxie/beego/otelbeego/test/go.mod +++ b/instrumentation/github.com/astaxie/beego/otelbeego/test/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/astaxie/beego v1.12.3 + github.com/elazarl/go-bindata-assetfs v1.0.0 github.com/stretchr/testify v1.8.1 go.opentelemetry.io/contrib/instrumentation/github.com/astaxie/beego/otelbeego v0.39.0 go.opentelemetry.io/contrib/propagators/b3 v1.14.0 From f9441ec3e69e697ac7ec9a81082af1767d220b7d Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 9 Feb 2023 07:56:44 -0800 Subject: [PATCH 8/8] Support windows filepath --- .../github.com/astaxie/beego/otelbeego/test/beego_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go index d466206d412..9c089f95ee2 100644 --- a/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go +++ b/instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go @@ -21,6 +21,7 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "testing" "time" @@ -241,19 +242,20 @@ func TestRender(t *testing.T) { beego.SetTemplateFSFunc(func() http.FileSystem { return &assetfs.AssetFS{ Asset: func(path string) ([]byte, error) { - if path == tplName { + if _, f := filepath.Split(path); f == tplName { return []byte(htmlStr), nil } return nil, os.ErrNotExist }, AssetDir: func(path string) ([]string, error) { - if path == "" { + switch path { + case "", `\`: return []string{tplName}, nil } return nil, os.ErrNotExist }, AssetInfo: func(path string) (os.FileInfo, error) { - if path == tplName { + if _, f := filepath.Split(path); f == tplName { return &assetfs.FakeFile{ Path: path, Len: int64(len(htmlStr)),