func()

in tools/go-agent/instrument/plugins/instrument.go [456:510]


func (i *Instrument) writeDelegatorFile(ctx *rewrite.Context, basePath string) ([]string, error) {
	file := &dst.File{
		Name: dst.NewIdent("delegator"), // write to adapter temporary, it will be rewritten later
	}

	// append header
	importsHeader := &dst.GenDecl{Tok: token.IMPORT}
	for _, e := range i.enhancements {
		e.BuildImports(importsHeader)
	}
	file.Decls = append(file.Decls, importsHeader)

	// append init function
	initFunc := &dst.FuncDecl{
		Name: dst.NewIdent("init"),
		Type: &dst.FuncType{},
		Body: &dst.BlockStmt{},
	}
	for _, enhance := range i.enhancements {
		funcs := enhance.InitFunctions()
		for _, fun := range funcs {
			if fun.AfterCoreInit {
				initFunc.Body.List = append(initFunc.Body.List, &dst.ExprStmt{X: &dst.CallExpr{
					Fun:  dst.NewIdent(rewrite.GlobalOperatorRealAppendTracerInitNotify),
					Args: []dst.Expr{dst.NewIdent(fun.Name)},
				}})
			} else {
				initFunc.Body.List = append(initFunc.Body.List, &dst.ExprStmt{X: &dst.CallExpr{Fun: dst.NewIdent(fun.Name)}})
			}
		}
	}
	file.Decls = append(file.Decls, initFunc)

	// append other decls
	for _, enhance := range i.enhancements {
		file.Decls = append(file.Decls, enhance.BuildForDelegator()...)
	}

	ctx.SingleFile(file)

	if len(ctx.InitFuncDetector) > 0 {
		for _, fun := range ctx.InitFuncDetector {
			initFunc.Body.List = append(initFunc.Body.List, &dst.ExprStmt{X: &dst.CallExpr{
				Fun:  dst.NewIdent(rewrite.GlobalOperatorRealAppendTracerInitNotify),
				Args: []dst.Expr{dst.NewIdent(fun)},
			}})
		}
	}

	adapterFile := filepath.Join(basePath, "skywalking_delegator.go")
	if err := tools.WriteDSTFile(adapterFile, file, nil); err != nil {
		return nil, err
	}
	return []string{adapterFile}, nil
}