Documentation
¶
Overview ¶
codegen is a library that creates, decorates or modifies DST objects in specific repeatable ways. This library is a common place for logic around how new nodes in a DST tree get created, as well as how to handle the whitespace and comments related to those nodes and the elements around them. Any function that creates a new node for insertion into the tree should be added here. When implementing functions for this library, the following rules should apply:
1. Any DST objects (expressions, statements, nodes, etc.) that are consumed as inputs should be defensively cloned before returning them as part of an output. There is a small execution cost to this, but if an object is duplicated anywhere in the tree, a runtime panic will occur. 2. Please add a comment header about what the output of your function is and what it does. All exported functions MUST be documented in way that is compatible with `godoc`. 3. Unit tests can be basic since the generated objects are going to be covered in many ways by the end to end tests. However, if a node gets returns that is invalid, it will fail to reneder and may result in a panic, which is not an acceptable outcome. A test to verify that the output is what we expect is a good safegard.
Index ¶
- Constants
- func CaptureErrorReturnCallExpression(pkg *decorator.Package, call *dst.CallExpr, transactionVariable dst.Expr) ([]dst.Stmt, []dst.Expr)
- func CaptureHttpResponse(segmentVariable string, responseVariable dst.Expr) *dst.AssignStmt
- func CreateStatementBlock(spacingBefore bool, stmts ...dst.Stmt)
- func DeferSegment(segmentName string, transactionVariable dst.Expr) *dst.DeferStmt
- func EndExternalSegment(segmentName string, nodeDecs *dst.NodeDecs) *dst.ExprStmt
- func EndTransaction(transactionVariableName string) *dst.ExprStmt
- func GetApplication(transactionVariableExpression dst.Expr) dst.Expr
- func GrpcStreamContext(streamServerObject *dst.Ident) *dst.CallExpr
- func HttpRequestContext() dst.Expr
- func IfErrorNotNilNoticeError(errorVariable, transactionVariable dst.Expr) *dst.IfStmt
- func InitializeAgent(AppName, AgentVariableName string) []dst.Stmt
- func NewContextExpression(context dst.Expr, transaction dst.Expr) dst.Expr
- func NewContextParameter(name string) *dst.Field
- func NewTransactionParameter(txnName string) *dst.Field
- func NoticeError(errExpr, transactionVariable dst.Expr, stmtBlock dst.Stmt) *dst.ExprStmt
- func NrGinMiddleware(routerName string, agentVariableName dst.Expr) (*dst.ExprStmt, string)
- func NrGrpcStreamClientInterceptor(call *dst.CallExpr) *dst.CallExpr
- func NrGrpcStreamServerInterceptor(agentVariable dst.Expr, call *dst.CallExpr) *dst.CallExpr
- func NrGrpcUnaryClientInterceptor(call *dst.CallExpr) *dst.CallExpr
- func NrGrpcUnaryServerInterceptor(agentVariable dst.Expr, call *dst.CallExpr) *dst.CallExpr
- func PrependStatementToFunctionDecl(fn *dst.FuncDecl, stmt dst.Stmt)
- func PrependStatementToFunctionLit(fn *dst.FuncLit, stmt dst.Stmt)
- func RoundTripper(clientVariable dst.Expr, spacingAfter dst.SpaceType) *dst.AssignStmt
- func ShutdownAgent(AgentVariableName string) *dst.ExprStmt
- func StartExternalSegment(request, txnVariable dst.Expr, segmentVar string, nodeDecs *dst.NodeDecs) *dst.AssignStmt
- func StartTransaction(appVariableName, transactionVariableName, transactionName string, ...) *dst.AssignStmt
- func TxnFromContext(txnVariable string, contextObject dst.Expr) *dst.AssignStmt
- func TxnFromContextExpression(contextObject dst.Expr) dst.Expr
- func TxnFromGinContext(txnVariable string, ctxName string) *dst.AssignStmt
- func TxnNewGoroutine(transaction dst.Expr) *dst.CallExpr
- func WrapContextExpression(context dst.Expr, transaction string, async bool) dst.Expr
- func WrapHttpHandle(agentVariable dst.Expr, handle *dst.CallExpr)
- func WrapHttpHandleFunc(agentVariable dst.Expr, handle *dst.CallExpr)
- func WrapRequestContext(request dst.Expr, txnVariable dst.Expr, nodeDecs *dst.NodeDecs) *dst.AssignStmt
- func WrapStatements(first, wrapped, last dst.Stmt)
Constants ¶
const ( NrginImportPath = "github.com/newrelic/go-agent/v3/integrations/nrgin" GinImportPath = "github.com/gin-gonic/gin" )
const ( NrgrpcImportPath = "github.com/newrelic/go-agent/v3/integrations/nrgrpc" GrpcImportPath = "google.golang.org/grpc" )
const DefaultContextParameter = "ctx"
const (
DefaultTransactionVariable = "nrTxn"
)
const (
HttpImportPath = "net/http"
)
const ( // the import path for the newrelic package NewRelicAgentImportPath string = "github.com/newrelic/go-agent/v3/newrelic" )
Variables ¶
This section is empty.
Functions ¶
func CaptureErrorReturnCallExpression ¶
func CaptureErrorReturnCallExpression(pkg *decorator.Package, call *dst.CallExpr, transactionVariable dst.Expr) ([]dst.Stmt, []dst.Expr)
CaptureErrorReturnCallExpression checks if the return values of a function call is an error, and generates code to assign the return values to variables, check if the error is not nil, and call txn.NoticeError(err) if the error is not nil. It returns the statements that need to be added to the tree, and the expressions that are assigned to the return values of the function call. The list of expressions can be used to replace the expression in the return statement.
func CaptureHttpResponse ¶
func CaptureHttpResponse(segmentVariable string, responseVariable dst.Expr) *dst.AssignStmt
func CreateStatementBlock ¶
CreateStatementBlock modifies the formatting of a set of statements to all be on separate lines, without any additional spacing between them.
White space is always added after the block.
If spacingBefore == true, an emptyline is added before the block.
func DeferSegment ¶
func EndExternalSegment ¶
func EndTransaction ¶
func HttpRequestContext ¶
func IfErrorNotNilNoticeError ¶
IfErrorNotNilNoticeError creates an if statement that checks if the errorVariable is not nil, and calls notice error if its not nil
Example:
if err != nil { txn.NoticeError(err) }
func InitializeAgent ¶
func NewContextExpression ¶
NewContextExpression creates an expression that creates a new context this is protected from using the same object, and will always clone inputs
func NewContextParameter ¶
ContextParameter creates a field for a context parameter
func NewTransactionParameter ¶
NewTransactionParameter returns a field definition for a transaction parameter
func NoticeError ¶
NoticeError Generates a statement that calls txn.NoticeError(err)
func NrGinMiddleware ¶
GinMiddlewareCall returns a new relic gin middleware call, and a string representing the import path of the library that contains the middleware function
func NrGrpcUnaryClientInterceptor ¶
GrpcUnaryInterceptor generates a dst Call Expression for a newrelic nrgrpc unary interceptor
func RoundTripper ¶
func ShutdownAgent ¶
func StartExternalSegment ¶
func StartTransaction ¶
func StartTransaction(appVariableName, transactionVariableName, transactionName string, overwriteVariable bool) *dst.AssignStmt
starts a NewRelic transaction if overwireVariable is true, the transaction variable will be overwritten by variable assignment, otherwise it will be defined
func TxnFromContext ¶
func TxnFromContext(txnVariable string, contextObject dst.Expr) *dst.AssignStmt
func TxnFromContextExpression ¶
TxnFromContextExpression returns a call to `newrelic.FromContext(contextObject)`
func TxnFromGinContext ¶
func TxnFromGinContext(txnVariable string, ctxName string) *dst.AssignStmt
func TxnNewGoroutine ¶
TxnNewGoroutine returns a call to txn.NewGoroutine()
func WrapContextExpression ¶
WrapContextExpression creates an expression that injects a context with a transaction if async is true, the transaction will be cloned by calling NewGoroutine() this is protected from using the same object, and will always clone inputs
func WrapHttpHandle ¶
WrapHttpHandle does an in place edit of a call expression to http.Handle replacing it with a call to newrelic.WrapHandle
agentVariable should be passed from tracestate.State and WILL NOT BE CLONED
func WrapHttpHandleFunc ¶
WrapHttpHandleFunc does an in place edit of a call expression to http.HandleFunc replacing it with a call to newrelic.WrapHandleFunc
agentVariable should be passed from tracestate.State and WILL NOT BE CLONED
func WrapRequestContext ¶
func WrapRequestContext(request dst.Expr, txnVariable dst.Expr, nodeDecs *dst.NodeDecs) *dst.AssignStmt
adds a transaction to the HTTP request context object by creating a line of code that injects it equal to calling: newrelic.RequestWithTransactionContext()
func WrapStatements ¶
Types ¶
This section is empty.