Skip to content

Commit

Permalink
❤️ skip if there is segment call already
Browse files Browse the repository at this point in the history
  • Loading branch information
budougumi0617 committed Jan 11, 2021
1 parent ea1539c commit 729f73a
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion process.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func Process(filename string, src []byte) ([]byte, error) {
return true
}

fd.Body.List = append([]ast.Stmt{ds}, fd.Body.List...)
if !existFromContext(pkg, fd.Body.List[0]) {
fd.Body.List = append([]ast.Stmt{ds}, fd.Body.List...)
}
return false
}
}
return true
Expand Down Expand Up @@ -92,6 +95,20 @@ func addImport(fs *token.FileSet, f *ast.File) (string, error) {
return "", nil
}

func existFromContext(pn string, s ast.Stmt) bool {
var result bool
ast.Inspect(s, func(n ast.Node) bool {
if se, ok := n.(*ast.SelectorExpr); ok {
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == pn && se.Sel.Name == "FromContext" {
result = true
return false
}
}
return true
})
return result
}

// buildDeferStmt builds the defer statement with args.
// ex:
// defer newrelic.FromContext(ctx).StartSegment("slow").End()
Expand Down

0 comments on commit 729f73a

Please sign in to comment.