codegen

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

Codegen

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. After a while, it gets very hard to interpret what they do.
  3. Unit tests can be very 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.

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

View Source
const (
	NrginImportPath = "github.com/newrelic/go-agent/v3/integrations/nrgin"
	GinImportPath   = "github.com/gin-gonic/gin"
)
View Source
const (
	NrgrpcImportPath = "github.com/newrelic/go-agent/v3/integrations/nrgrpc"
	GrpcImportPath   = "google.golang.org/grpc"
)
View Source
const DefaultContextParameter = "ctx"
View Source
const (
	DefaultTransactionVariable = "nrTxn"
)
View Source
const (
	HttpImportPath = "net/http"
)
View Source
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

func CreateStatementBlock(spacingBefore bool, stmts ...dst.Stmt)

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 DeferSegment(segmentName string, transactionVariable dst.Expr) *dst.DeferStmt

func EndExternalSegment

func EndExternalSegment(segmentName string, nodeDecs *dst.NodeDecs) *dst.ExprStmt

func EndTransaction

func EndTransaction(transactionVariableName string) *dst.ExprStmt

func GetApplication

func GetApplication(transactionVariableExpression dst.Expr) dst.Expr

func GrpcStreamContext

func GrpcStreamContext(streamServerObject *dst.Ident) *dst.CallExpr

func HttpRequestContext

func HttpRequestContext() dst.Expr

func IfErrorNotNilNoticeError

func IfErrorNotNilNoticeError(errorVariable, transactionVariable dst.Expr) *dst.IfStmt

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 InitializeAgent(AppName, AgentVariableName string) []dst.Stmt

func NewContextExpression

func NewContextExpression(context dst.Expr, transaction dst.Expr) dst.Expr

NewContextExpression creates an expression that creates a new context this is protected from using the same object, and will always clone inputs

func NewContextParameter

func NewContextParameter(name string) *dst.Field

ContextParameter creates a field for a context parameter

func NewTransactionParameter

func NewTransactionParameter(txnName string) *dst.Field

NewTransactionParameter returns a field definition for a transaction parameter

func NoticeError

func NoticeError(errExpr, transactionVariable dst.Expr, stmtBlock dst.Stmt) *dst.ExprStmt

NoticeError Generates a statement that calls txn.NoticeError(err)

func NrGinMiddleware

func NrGinMiddleware(routerName string, agentVariableName dst.Expr) (*dst.ExprStmt, string)

GinMiddlewareCall returns a new relic gin middleware call, and a string representing the import path of the library that contains the middleware function

func NrGrpcStreamClientInterceptor

func NrGrpcStreamClientInterceptor(call *dst.CallExpr) *dst.CallExpr

func NrGrpcStreamServerInterceptor

func NrGrpcStreamServerInterceptor(agentVariable dst.Expr, call *dst.CallExpr) *dst.CallExpr

func NrGrpcUnaryClientInterceptor

func NrGrpcUnaryClientInterceptor(call *dst.CallExpr) *dst.CallExpr

GrpcUnaryInterceptor generates a dst Call Expression for a newrelic nrgrpc unary interceptor

func NrGrpcUnaryServerInterceptor

func NrGrpcUnaryServerInterceptor(agentVariable dst.Expr, call *dst.CallExpr) *dst.CallExpr

func PrependStatementToFunctionDecl

func PrependStatementToFunctionDecl(fn *dst.FuncDecl, stmt dst.Stmt)

func PrependStatementToFunctionLit

func PrependStatementToFunctionLit(fn *dst.FuncLit, stmt dst.Stmt)

func RoundTripper

func RoundTripper(clientVariable dst.Expr, spacingAfter dst.SpaceType) *dst.AssignStmt

func ShutdownAgent

func ShutdownAgent(AgentVariableName string) *dst.ExprStmt

func StartExternalSegment

func StartExternalSegment(request, txnVariable dst.Expr, segmentVar string, nodeDecs *dst.NodeDecs) *dst.AssignStmt

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

func TxnFromContextExpression(contextObject dst.Expr) dst.Expr

TxnFromContextExpression returns a call to `newrelic.FromContext(contextObject)`

func TxnFromGinContext

func TxnFromGinContext(txnVariable string, ctxName string) *dst.AssignStmt

func TxnNewGoroutine

func TxnNewGoroutine(transaction dst.Expr) *dst.CallExpr

TxnNewGoroutine returns a call to txn.NewGoroutine()

func WrapContextExpression

func WrapContextExpression(context dst.Expr, transaction string, async bool) dst.Expr

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

func WrapHttpHandle(agentVariable dst.Expr, handle *dst.CallExpr)

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

func WrapHttpHandleFunc(agentVariable dst.Expr, handle *dst.CallExpr)

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

func WrapStatements(first, wrapped, last dst.Stmt)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL