-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
instrument: fix duplicated instrumentation statements on already inst…
…rumented frameworks (#42) * instrument: fix duplicated instrumentation statements on already instrumented frameworks * instrument: add missing license preamble in new test files * instrument: fix test code for Chi v5
- Loading branch information
Showing
12 changed files
with
351 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2023-present Datadog, Inc. | ||
|
||
package instrument | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/datadog/orchestrion/internal/config" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestChiV5(t *testing.T) { | ||
var codeTpl = `package main | ||
import %s | ||
func register() { | ||
%s | ||
} | ||
` | ||
var wantTpl = `package main | ||
import ( | ||
"github.com/datadog/orchestrion/instrument" | ||
%s | ||
) | ||
func register() { | ||
//dd:instrumented | ||
%s | ||
//dd:startinstrument | ||
%s | ||
//dd:endinstrument | ||
} | ||
` | ||
|
||
tests := []struct { | ||
pkg string | ||
stmt string | ||
want string | ||
tmpl string | ||
}{ | ||
{pkg: `"github.com/go-chi/chi/v5"`, stmt: `r := chi.NewRouter()`, want: `r.Use(instrument.ChiV5Middleware())`, tmpl: wantTpl}, | ||
{pkg: `chi "github.com/go-chi/chi/v5"`, stmt: `r := chi.NewRouter()`, want: `r.Use(instrument.ChiV5Middleware())`, tmpl: wantTpl}, | ||
{pkg: `chiv5 "github.com/go-chi/chi/v5"`, stmt: `r := chiv5.NewRouter()`, want: `r.Use(instrument.ChiV5Middleware())`, tmpl: wantTpl}, | ||
} | ||
|
||
for i, tc := range tests { | ||
t.Run(fmt.Sprintf("tc-%d", i), func(t *testing.T) { | ||
code := fmt.Sprintf(codeTpl, tc.pkg, tc.stmt) | ||
reader, err := InstrumentFile("test", strings.NewReader(code), config.Config{}) | ||
require.Nil(t, err) | ||
got, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
want := fmt.Sprintf(tc.tmpl, tc.pkg, tc.stmt, tc.want) | ||
require.Equal(t, want, string(got)) | ||
|
||
reader, err = UninstrumentFile("test", strings.NewReader(want), config.Config{}) | ||
require.Nil(t, err) | ||
orig, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
require.Equal(t, code, string(orig)) | ||
}) | ||
} | ||
} | ||
|
||
func TestChiV5Duplicates(t *testing.T) { | ||
var tpl = `package main | ||
import ( | ||
"github.com/datadog/orchestrion/instrument" | ||
"github.com/go-chi/chi/v5" | ||
) | ||
func chiV5Server() { | ||
//dd:instrumented | ||
r := chi.NewRouter() | ||
//dd:startinstrument | ||
r.Use(instrument.ChiV5Middleware()) | ||
//dd:endinstrument | ||
} | ||
` | ||
|
||
reader, err := InstrumentFile("test", strings.NewReader(tpl), config.Config{}) | ||
require.Nil(t, err) | ||
got, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
require.Equal(t, tpl, string(got)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2023-present Datadog, Inc. | ||
|
||
package instrument | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/datadog/orchestrion/internal/config" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEchoV4(t *testing.T) { | ||
var codeTpl = `package main | ||
import %s | ||
func register() { | ||
%s | ||
} | ||
` | ||
var wantTpl = `package main | ||
import ( | ||
"github.com/datadog/orchestrion/instrument" | ||
%s | ||
) | ||
func register() { | ||
//dd:instrumented | ||
%s | ||
//dd:startinstrument | ||
%s | ||
//dd:endinstrument | ||
} | ||
` | ||
|
||
tests := []struct { | ||
pkg string | ||
stmt string | ||
want string | ||
tmpl string | ||
}{ | ||
{pkg: `"github.com/labstack/echo/v4"`, stmt: `r := echo.New()`, want: `r.Use(instrument.EchoV4Middleware())`, tmpl: wantTpl}, | ||
{pkg: `echo "github.com/labstack/echo/v4"`, stmt: `r := echo.New()`, want: `r.Use(instrument.EchoV4Middleware())`, tmpl: wantTpl}, | ||
{pkg: `echov4 "github.com/labstack/echo/v4"`, stmt: `r := echov4.New()`, want: `r.Use(instrument.EchoV4Middleware())`, tmpl: wantTpl}, | ||
} | ||
|
||
for i, tc := range tests { | ||
t.Run(fmt.Sprintf("tc-%d", i), func(t *testing.T) { | ||
code := fmt.Sprintf(codeTpl, tc.pkg, tc.stmt) | ||
reader, err := InstrumentFile("test", strings.NewReader(code), config.Config{}) | ||
require.Nil(t, err) | ||
got, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
want := fmt.Sprintf(tc.tmpl, tc.pkg, tc.stmt, tc.want) | ||
require.Equal(t, want, string(got)) | ||
|
||
reader, err = UninstrumentFile("test", strings.NewReader(want), config.Config{}) | ||
require.Nil(t, err) | ||
orig, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
require.Equal(t, code, string(orig)) | ||
}) | ||
} | ||
} | ||
|
||
func TestEchoV4Duplicates(t *testing.T) { | ||
var tpl = `package main | ||
import ( | ||
"github.com/datadog/orchestrion/instrument" | ||
"github.com/labstack/echo/v4" | ||
) | ||
func echoV4Server() { | ||
//dd:instrumented | ||
r := echo.New() | ||
//dd:startinstrument | ||
r.Use(instrument.EchoV4Middleware()) | ||
//dd:endinstrument | ||
} | ||
` | ||
|
||
reader, err := InstrumentFile("test", strings.NewReader(tpl), config.Config{}) | ||
require.Nil(t, err) | ||
got, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
require.Equal(t, tpl, string(got)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2023-present Datadog, Inc. | ||
|
||
package instrument | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/datadog/orchestrion/internal/config" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGin(t *testing.T) { | ||
var codeTpl = `package main | ||
import "github.com/gin-gonic/gin" | ||
func register() { | ||
%s | ||
} | ||
` | ||
var wantTpl = `package main | ||
import ( | ||
"github.com/datadog/orchestrion/instrument" | ||
"github.com/gin-gonic/gin" | ||
) | ||
func register() { | ||
//dd:instrumented | ||
%s | ||
//dd:startinstrument | ||
%s | ||
//dd:endinstrument | ||
} | ||
` | ||
|
||
tests := []struct { | ||
in string | ||
want string | ||
tmpl string | ||
}{ | ||
{in: `g := gin.New()`, want: `g.Use(instrument.GinMiddleware())`, tmpl: wantTpl}, | ||
{in: `g := gin.Default()`, want: `g.Use(instrument.GinMiddleware())`, tmpl: wantTpl}, | ||
} | ||
|
||
for i, tc := range tests { | ||
t.Run(fmt.Sprintf("tc-%d", i), func(t *testing.T) { | ||
code := fmt.Sprintf(codeTpl, tc.in) | ||
reader, err := InstrumentFile("test", strings.NewReader(code), config.Config{}) | ||
require.Nil(t, err) | ||
got, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
want := fmt.Sprintf(tc.tmpl, tc.in, tc.want) | ||
require.Equal(t, want, string(got)) | ||
|
||
reader, err = UninstrumentFile("test", strings.NewReader(want), config.Config{}) | ||
require.Nil(t, err) | ||
orig, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
require.Equal(t, code, string(orig)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGinDuplicates(t *testing.T) { | ||
var tpl = `package main | ||
import ( | ||
"net/http" | ||
"github.com/datadog/orchestrion/instrument" | ||
"github.com/gin-gonic/gin" | ||
) | ||
func ginServer() { | ||
//dd:instrumented | ||
r := gin.Default() | ||
//dd:startinstrument | ||
r.Use(instrument.GinMiddleware()) | ||
//dd:endinstrument | ||
r.GET("/ping", func(c *gin.Context) { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "pong", | ||
}) | ||
}) | ||
r.Run() | ||
} | ||
` | ||
|
||
reader, err := InstrumentFile("test", strings.NewReader(tpl), config.Config{}) | ||
require.Nil(t, err) | ||
got, err := io.ReadAll(reader) | ||
require.Nil(t, err) | ||
require.Equal(t, tpl, string(got)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.