Skip to content

Commit

Permalink
✨ add buildDeferStmtWithHttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
budougumi0617 committed Jan 4, 2021
1 parent 15ba02f commit 7432aa8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions nrseg.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,36 @@ func buildDeferStmt(pkgName, ctxName, segName string) *ast.DeferStmt {
},
}
}

// buildDeferStmt builds the defer statement with *http.Request.
// ex:
// defer newrelic.FromContext(req.Context()).StartSegment("slow").End()
func buildDeferStmtWithHttpRequest(pkgName, reqName, segName string) *ast.DeferStmt {
return &ast.DeferStmt{
Call: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: pkgName},
Sel: &ast.Ident{Name: "FromContext"},
},
Args: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: reqName},
Sel: &ast.Ident{Name: "Context"},
},
},
},
},
Sel: &ast.Ident{Name: "StartSegment"},
},
Args: []ast.Expr{&ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(segName)}},
},
Sel: &ast.Ident{Name: "End"},
},
},
}
}

0 comments on commit 7432aa8

Please sign in to comment.