Skip to content

Instantly share code, notes, and snippets.

@sarathsp06
Created February 13, 2024 08:00
Show Gist options
  • Save sarathsp06/e711b0a4c4f7086823eff32c01d426e1 to your computer and use it in GitHub Desktop.
Save sarathsp06/e711b0a4c4f7086823eff32c01d426e1 to your computer and use it in GitHub Desktop.
newrelic code generator template
import (
"context"
"github.com/newrelic/go-agent/v3/newrelic"
pb "github.com/shellagilehub/donut-clients/donuts/go"
"github.com/doug-martin/goqu/v9"
)
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithTracing" .Interface.Name)) }}
// {{$decorator}} implements {{.Interface.Type}} interface instrumented with opentracing spans
type {{$decorator}} struct {
{{.Interface.Type}}
{{if .Vars.Transaction}}
app *newrelic.App
{{end}}
}
// New{{$decorator}} returns {{$decorator}}
func New{{$decorator}} (base {{.Interface.Type}},app *newrelic.App) {{$decorator}} {
return {{$decorator}} {base,app}
}
{{range $method := .Interface.Methods}}
{{if $method.AcceptsContext}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
{{if $.Vars.Transaction}}
tx := _d.app.StartTransaction("GQL/QUERY/{{$method.Name}}")
tx.SetUserID(auth.UserEmailFromContext(ctx))
tx.SetWebRequestHTTP(nil)
defer tx.End()
{{else}}
txn := newrelic.FromContext(ctx)
segment := txn.StartSegment("{{$.Interface.Type}}.{{$method.Name}}")
defer func() {
{{- if $method.ReturnsError -}}
if err != nil {
txn.NoticeError(err)
}
{{- end }}
segment.End()
}()
{{end}}
{{$method.Pass (printf "_d.%s." $.Interface.Name) }}
}
{{end}}
{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment