Skip to content

Commit

Permalink
fix: OtelRules initialized to wrong path
Browse files Browse the repository at this point in the history
  • Loading branch information
y1yang0 committed Jan 23, 2025
1 parent 32923f4 commit 16cc1be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
22 changes: 14 additions & 8 deletions tool/preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func newDepProcessor() *DepProcessor {
}

func (dp *DepProcessor) getGoModPath() string {
util.Assert(dp.modulePath != "", "modulePath is empty")
util.Assert(filepath.IsAbs(dp.modulePath), "modulePath is not absolute")
return dp.modulePath
}

Expand All @@ -137,6 +139,10 @@ func (dp *DepProcessor) getGoModName() string {
return dp.moduleName
}

func (dp *DepProcessor) generatedOf(dir string) string {
return filepath.Join(dp.getGoModDir(), dir)
}

// runCmdOutput runs the command and returns its standard output. dir specifies
// the working directory of the command. If dir is the empty string, run runs
// the command in the calling process's current directory.
Expand Down Expand Up @@ -284,10 +290,10 @@ func (dp *DepProcessor) postProcess() {
}

// rm -rf otel_rules
_ = os.RemoveAll(OtelRules)
_ = os.RemoveAll(dp.generatedOf(OtelRules))

// rm -rf otel_pkgdep
_ = os.RemoveAll(OtelPkgDep)
_ = os.RemoveAll(dp.generatedOf(OtelPkgDep))

// Restore everything we have modified during instrumentation
_ = dp.restoreBackupFiles()
Expand Down Expand Up @@ -623,11 +629,11 @@ func (dp *DepProcessor) preclean() {
}
}
// Clean otel_rules/otel_pkgdep directory
if util.PathExists(OtelRules) {
_ = os.RemoveAll(OtelRules)
if util.PathExists(dp.generatedOf(OtelRules)) {
_ = os.RemoveAll(dp.generatedOf(OtelRules))
}
if util.PathExists(OtelPkgDep) {
_ = os.RemoveAll(OtelPkgDep)
if util.PathExists(dp.generatedOf(OtelPkgDep)) {
_ = os.RemoveAll(dp.generatedOf(OtelPkgDep))
}
}

Expand Down Expand Up @@ -841,7 +847,7 @@ func (dp *DepProcessor) saveDebugFiles() {
dir := filepath.Join(util.GetTempBuildDir(), OtelRules)
err := os.MkdirAll(dir, os.ModePerm)
if err == nil {
util.CopyDir(OtelRules, dir)
util.CopyDir(dp.generatedOf(OtelRules), dir)
}
dir = filepath.Join(util.GetTempBuildDir(), OtelUser)
err = os.MkdirAll(dir, os.ModePerm)
Expand All @@ -867,7 +873,7 @@ func (dp *DepProcessor) setupDeps() error {
return err
}

// Run go mod tidy first to fetch all dependencies
// Run go mod t idy first to fetch all dependencies
err = dp.runModTidy()
if err != nil {
return err
Expand Down
24 changes: 3 additions & 21 deletions tool/preprocess/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,12 @@ const (
ApiCallContext = "CallContext"
)

func initRuleDir() (err error) {
if util.PathExists(OtelRules) {
err = os.RemoveAll(OtelRules)
if err != nil {
return errc.New(errc.ErrRemoveAll, OtelRules)
}
}
err = os.MkdirAll(OtelRules, os.ModePerm)
if err != nil {
return errc.New(errc.ErrMkdirAll, OtelRules)
}
return nil
}

func (dp *DepProcessor) copyRules() (err error) {
if len(dp.bundles) == 0 {
return nil
}
// Copy matched rules to ${GOMOD.DIR}/otel_rules
targetDir := filepath.Join(dp.getGoModDir(), OtelRules)
targetDir := dp.generatedOf(OtelRules)
// Find out which resource files we should add to project
for _, bundle := range dp.bundles {
for _, funcRules := range bundle.File2FuncRules {
Expand Down Expand Up @@ -310,7 +296,7 @@ func (dp *DepProcessor) initRules() (err error) {
c += "}\n"

// Write to ${GOMOD.DIR}/otel_rules/otel_setup_inst.go
initTarget := filepath.Join(dp.getGoModDir(), OtelRules, OtelSetupInst)
initTarget := dp.generatedOf(filepath.Join(OtelRules, OtelSetupInst))
_, err = util.WriteFile(initTarget, c)
if err != nil {
return err
Expand Down Expand Up @@ -381,7 +367,7 @@ func (dp *DepProcessor) rewriteRules() error {

func (dp *DepProcessor) setupOtelSDK() error {
// Copy otel_setup_sdk.go to ${GOMOD.DIR}/otel_rules
setupTarget := filepath.Join(dp.getGoModDir(), OtelRules, OtelSetupSDK)
setupTarget := dp.generatedOf(filepath.Join(OtelRules, OtelSetupSDK))
_, err := resource.CopyOtelSetupTo(OtelRules, setupTarget)
if err != nil {
return err
Expand All @@ -391,10 +377,6 @@ func (dp *DepProcessor) setupOtelSDK() error {

func (dp *DepProcessor) setupRules() (err error) {
defer util.PhaseTimer("Setup")()
err = initRuleDir()
if err != nil {
return err
}
err = dp.copyRules()
if err != nil {
return err
Expand Down

0 comments on commit 16cc1be

Please sign in to comment.