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

ci: Windows build pipelines #221

Merged
merged 8 commits into from
Nov 25, 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
3 changes: 2 additions & 1 deletion .github/workflows/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
# If you want to matrix build , you can append the following list.
matrix:
Expand All @@ -23,6 +23,7 @@ jobs:
- 1.23
os:
- ubuntu-latest
- windows-latest
steps:
- uses: actions/checkout@v4

Expand Down
55 changes: 35 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Get the current operating system and CPU architecture of the system
#-------------------------------------------------------------------------------
# General build options
CURRENT_OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
CURRENT_ARCH := $(shell uname -m)

MOD_NAME := github.com/alibaba/opentelemetry-go-auto-instrumentation
VERSION := $(MAIN_VERSION)_$(COMMIT_ID)
XVERSION := -X=$(MOD_NAME)/tool/config.ToolVersion=$(VERSION)
STRIP_DEBUG := -s -w
LDFLAGS := $(XVERSION) $(STRIP_DEBUG)
BUILD_CMD = CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -a -ldflags="$(LDFLAGS)" -o $(3) ./tool/cmd

OUTPUT_BASE = otel
OUTPUT_DARWIN_AMD64 = $(OUTPUT_BASE)-darwin-amd64
OUTPUT_LINUX_AMD64 = $(OUTPUT_BASE)-linux-amd64
OUTPUT_WINDOWS_AMD64 = $(OUTPUT_BASE)-windows-amd64.exe
OUTPUT_DARWIN_ARM64 = $(OUTPUT_BASE)-darwin-arm64
OUTPUT_LINUX_ARM64 = $(OUTPUT_BASE)-linux-arm64

#-------------------------------------------------------------------------------
# Multiple OS and ARCH support
ifeq ($(CURRENT_ARCH), x86_64)
CURRENT_ARCH := amd64
endif

# Check if current os contains "MINGW" or "MSYS" to determine if it is Windows
ifeq ($(findstring mingw,$(CURRENT_OS)),mingw)
CURRENT_OS := windows
endif

ifeq ($(findstring msys,$(CURRENT_OS)),msys)
CURRENT_OS := windows
endif

# Get the current Git commit ID
CHECK_GIT_DIRECTORY := $(if $(wildcard .git),true,false)
ifeq ($(CHECK_GIT_DIRECTORY),true)
Expand All @@ -36,27 +62,16 @@ else
COMMIT_ID := default
endif

# General build options
MOD_NAME := github.com/alibaba/opentelemetry-go-auto-instrumentation
TOOL_REL_NAME := otel

VERSION := $(MAIN_VERSION)_$(COMMIT_ID)
XVERSION := -X=$(MOD_NAME)/tool/config.ToolVersion=$(VERSION)
STRIP_DEBUG := -s -w
LDFLAGS := $(XVERSION) $(STRIP_DEBUG)
BUILD_CMD = CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -a -ldflags="$(LDFLAGS)" -o $(3) ./tool/cmd

OUTPUT_BASE = $(TOOL_REL_NAME)
OUTPUT_DARWIN_AMD64 = $(OUTPUT_BASE)-darwin-amd64
OUTPUT_LINUX_AMD64 = $(OUTPUT_BASE)-linux-amd64
OUTPUT_WINDOWS_AMD64 = $(OUTPUT_BASE)-windows-amd64.exe
OUTPUT_DARWIN_ARM64 = $(OUTPUT_BASE)-darwin-arm64
OUTPUT_LINUX_ARM64 = $(OUTPUT_BASE)-linux-arm64

#-------------------------------------------------------------------------------
# Build targets
.PHONY: build
build:
go mod tidy
$(call BUILD_CMD,$(CURRENT_OS),$(CURRENT_ARCH),$(OUTPUT_BASE))
$(eval OUTPUT_BIN=$(OUTPUT_BASE))
ifeq ($(CURRENT_OS),windows)
$(eval OUTPUT_BIN=$(OUTPUT_BASE).exe)
endif
$(call BUILD_CMD,$(CURRENT_OS),$(CURRENT_ARCH),$(OUTPUT_BIN))

.PHONY: all test clean

Expand Down Expand Up @@ -87,7 +102,7 @@ clean:
go clean

test:
go test -timeout 50m -v github.com/alibaba/opentelemetry-go-auto-instrumentation/test
go test -timeout 50m -v $(MOD_NAME)/test

install: build
@echo "Running install process..."
Expand Down
8 changes: 4 additions & 4 deletions test/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func TestBuildProject5(t *testing.T) {
RunGoBuild(t, "go", "build", "m1")
// both test_fmt.json and default.json rules should be available
// because we always append new -rule to the default.json by default
ExpecPreprocessContains(t, shared.DebugLogFile, "fmt")
ExpecPreprocessContains(t, shared.DebugLogFile, "database/sql")
ExpectPreprocessContains(t, shared.DebugLogFile, "fmt")
ExpectPreprocessContains(t, shared.DebugLogFile, "database/sql")
}

func TestBuildProject6(t *testing.T) {
Expand All @@ -82,6 +82,6 @@ func TestBuildProject6(t *testing.T) {
RunSet(t, "-disabledefault=true", "-rule=../../pkg/data/test_fmt.json", "-verbose")
RunGoBuild(t, "go", "build", "m1")
// only test_fmt.json should be available because -disabledefault is set
ExpecPreprocessContains(t, shared.DebugLogFile, "fmt")
ExpecPreprocessNotContains(t, shared.DebugLogFile, "database/sql")
ExpectPreprocessContains(t, shared.DebugLogFile, "fmt")
ExpectPreprocessNotContains(t, shared.DebugLogFile, "database/sql")
}
4 changes: 2 additions & 2 deletions test/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func TestFlags(t *testing.T) {
UseApp(AppName)

RunGoBuildFallible(t, "go", "build", "-thisisnotvalid")
ExpecPreprocessContains(t, shared.DebugLogFile, "failed to")
ExpectPreprocessContains(t, shared.DebugLogFile, "failed to")

RunVersion(t)
ExpectStdoutContains(t, "version")

RunGoBuildFallible(t, "go", "build", "notevenaflag")
ExpecPreprocessContains(t, shared.DebugLogFile, "failed to")
ExpectPreprocessContains(t, shared.DebugLogFile, "failed to")

RunSet(t, "-verbose")
RunGoBuild(t, "go", "build", `-ldflags=-X main.Placeholder=replaced`)
Expand Down
3 changes: 0 additions & 3 deletions test/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,4 @@ func TestRunHttpclient(t *testing.T) {
ExpectContains(t, stderr, "debug.Stack()") // during recover()
ExpectContains(t, stderr, "4008208820")
ExpectContains(t, stderr, "Prince of Qin Smashing the Battle line")

//ExpecPreprocessContains(t, "debug.log", "go.opentelemetry.io/otel@v1.31.0")
//ExpectInstrumentContains(t, "debug.log", "go.opentelemetry.io/otel@v1.31.0")
}
26 changes: 15 additions & 11 deletions test/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"

"github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/test/version"

"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/shared"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util"
)

const (
ExecName = "otel"
)
func getExecName() string {
execName := "otel"
if runtime.GOOS == "windows" {
return execName + ".exe"
}
return execName
}

func runCmd(args []string) *exec.Cmd {
path := args[0]
Expand Down Expand Up @@ -78,8 +82,8 @@ func readStderrLog(t *testing.T) string {

func RunVersion(t *testing.T) {
util.Assert(pwd != "", "pwd is empty")
path := filepath.Join(filepath.Dir(pwd), ExecName)
cmd := runCmd(append([]string{path, "version"}))
path := filepath.Join(filepath.Dir(pwd), getExecName())
cmd := runCmd([]string{path, "version"})
err := cmd.Run()
if err != nil {
t.Fatal(err)
Expand All @@ -88,7 +92,7 @@ func RunVersion(t *testing.T) {

func RunSet(t *testing.T, args ...string) {
util.Assert(pwd != "", "pwd is empty")
path := filepath.Join(filepath.Dir(pwd), ExecName)
path := filepath.Join(filepath.Dir(pwd), getExecName())
cmd := runCmd(append([]string{path, "set"}, args...))
err := cmd.Run()
if err != nil {
Expand All @@ -99,7 +103,7 @@ func RunSet(t *testing.T, args ...string) {
func RunGoBuild(t *testing.T, args ...string) {
util.Assert(pwd != "", "pwd is empty")
RunSet(t, "-debuglog")
path := filepath.Join(filepath.Dir(pwd), ExecName)
path := filepath.Join(filepath.Dir(pwd), getExecName())
cmd := runCmd(append([]string{path}, args...))
err := cmd.Run()
if err != nil {
Expand All @@ -120,7 +124,7 @@ func RunGoBuild(t *testing.T, args ...string) {
func RunGoBuildFallible(t *testing.T, args ...string) {
util.Assert(pwd != "", "pwd is empty")
RunSet(t, "-debuglog")
path := filepath.Join(filepath.Dir(pwd), ExecName)
path := filepath.Join(filepath.Dir(pwd), getExecName())
cmd := runCmd(append([]string{path}, args...))
err := cmd.Run()
if err == nil {
Expand Down Expand Up @@ -204,13 +208,13 @@ func ExpectInstrumentNotContains(t *testing.T, log string, rule string) {
ExpectNotContains(t, content, rule)
}

func ExpecPreprocessContains(t *testing.T, log string, rule string) {
func ExpectPreprocessContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, log)
content := readLog(t, path)
ExpectContains(t, content, rule)
}

func ExpecPreprocessNotContains(t *testing.T, log string, rule string) {
func ExpectPreprocessNotContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, log)
content := readLog(t, path)
ExpectNotContains(t, content, rule)
Expand Down
4 changes: 2 additions & 2 deletions tool/preprocess/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (dp *DepProcessor) fetchEmbed(path string) (string, error) {
if err != nil {
return err
}
target := shared.GePreprocessLogPath(filepath.Join(OtelRuleCache, p))
target := shared.GetPreprocessLogPath(filepath.Join(OtelRuleCache, p))
err = os.MkdirAll(filepath.Dir(target), 0777)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
Expand All @@ -111,7 +111,7 @@ func (dp *DepProcessor) fetchEmbed(path string) (string, error) {
}
// Now all rule files are copied to the local file system, we can return
// the path to corresponding local file system
dir := shared.GePreprocessLogPath(filepath.Join(OtelRuleCache, path))
dir := shared.GetPreprocessLogPath(filepath.Join(OtelRuleCache, path))
return dir, nil
}

Expand Down
25 changes: 15 additions & 10 deletions tool/preprocess/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func initRuleDir() (err error) {
return nil
}

func (dp *DepProcessor) copyRules(target string) (err error) {
func (dp *DepProcessor) copyRules(pkgName string) (err error) {
if len(dp.bundles) == 0 {
return nil
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (dp *DepProcessor) copyRules(target string) (err error) {
continue
}

ruleDir := filepath.Join(target, dir)
ruleDir := filepath.Join(pkgName, dir)
err = os.MkdirAll(ruleDir, 0777)
if err != nil {
return fmt.Errorf("failed to create dir %v: %w",
Expand Down Expand Up @@ -241,7 +241,7 @@ func (dp *DepProcessor) copyRule(path, target string,
return nil
}

func (dp *DepProcessor) initRules(pkgName, target string) (err error) {
func (dp *DepProcessor) initRules(pkgName string) (err error) {
c := fmt.Sprintf("package %s\n", pkgName)
imports := make(map[string]string)

Expand Down Expand Up @@ -272,7 +272,10 @@ func (dp *DepProcessor) initRules(pkgName, target string) (err error) {
aliasPkg = imports[bundle.ImportPath]
}
if rule.OnEnter != "" {
rd := filepath.Join(OtelRules, dp.rule2Dir[rule])
// @@Dont use filepath.Join here, because this is import
// path presented in Go source code, which should always
// use forward slash
rd := fmt.Sprintf("%s/%s", OtelRules, dp.rule2Dir[rule])
path, err := dp.getImportPathOf(rd)
if err != nil {
return fmt.Errorf("failed to get import path: %w",
Expand All @@ -289,7 +292,7 @@ func (dp *DepProcessor) initRules(pkgName, target string) (err error) {
)
}
if rule.OnExit != "" {
rd := filepath.Join(OtelRules, dp.rule2Dir[rule])
rd := fmt.Sprintf("%s/%s", OtelRules, dp.rule2Dir[rule])
path, err := dp.getImportPathOf(rd)
if err != nil {
return fmt.Errorf("failed to get import path: %w",
Expand Down Expand Up @@ -339,7 +342,8 @@ func (dp *DepProcessor) initRules(pkgName, target string) (err error) {
}
c += "}\n"

_, err = util.WriteFile(target, c)
initTarget := filepath.Join(OtelRules, OtelSetupInst)
_, err = util.WriteFile(initTarget, c)
if err != nil {
return err
}
Expand All @@ -358,8 +362,9 @@ func (dp *DepProcessor) addRuleImport() error {
return nil
}

func (dp *DepProcessor) setupOtelSDK(pkgName, target string) error {
_, err := resource.CopyOtelSetupTo(pkgName, target)
func (dp *DepProcessor) setupOtelSDK(pkgName string) error {
setupTarget := filepath.Join(OtelRules, OtelSetupSDK)
_, err := resource.CopyOtelSetupTo(pkgName, setupTarget)
if err != nil {
return fmt.Errorf("failed to copy otel setup sdk: %w", err)
}
Expand All @@ -376,11 +381,11 @@ func (dp *DepProcessor) setupRules() (err error) {
if err != nil {
return fmt.Errorf("failed to setup rules: %w", err)
}
err = dp.initRules(OtelRules, filepath.Join(OtelRules, OtelSetupInst))
err = dp.initRules(OtelRules)
if err != nil {
return fmt.Errorf("failed to setup initiator: %w", err)
}
err = dp.setupOtelSDK(OtelRules, filepath.Join(OtelRules, OtelSetupSDK))
err = dp.setupOtelSDK(OtelRules)
if err != nil {
return fmt.Errorf("failed to setup otel sdk: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tool/resource/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func FindRuleFiles(rule InstRule) ([]string, error) {

func StoreRuleBundles(bundles []*RuleBundle) error {
shared.GuaranteeInPreprocess()
ruleFile := shared.GePreprocessLogPath(RuleBundleJsonFile)
ruleFile := shared.GetPreprocessLogPath(RuleBundleJsonFile)
bs, err := json.Marshal(bundles)
if err != nil {
return fmt.Errorf("failed to marshal bundles: %w", err)
Expand All @@ -156,7 +156,7 @@ func StoreRuleBundles(bundles []*RuleBundle) error {
func LoadRuleBundles() ([]*RuleBundle, error) {
shared.GuaranteeInInstrument()

ruleFile := shared.GePreprocessLogPath(RuleBundleJsonFile)
ruleFile := shared.GetPreprocessLogPath(RuleBundleJsonFile)
data, err := util.ReadFile(ruleFile)
if err != nil {
return nil, fmt.Errorf("failed to read used rules: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion tool/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func GetInstrumentLogPath(name string) string {
return filepath.Join(TempBuildDir, PInstrument, name)
}

func GePreprocessLogPath(name string) string {
func GetPreprocessLogPath(name string) string {
return filepath.Join(TempBuildDir, PPreprocess, name)
}

Expand Down
Loading