Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added support to enable_open_tracing
  • Loading branch information
harishb2k committed Apr 14, 2024
commit 95f8e88d006504defd75372c7509e54d09e37416
1 change: 1 addition & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type tmplCtx struct {
UsesBatch bool
OmitSqlcVersion bool
BuildTags string
EnableOpenTracing bool
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down
7 changes: 7 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func sortedImports(std map[string]struct{}, pkg map[ImportSpec]struct{}) fileImp
func (i *importer) queryImports(filename string) fileImports {
var gq []Query
anyNonCopyFrom := false
openTrackingImport := false
for _, query := range i.Queries {
if usesBatch([]Query{query}) {
continue
Expand All @@ -309,6 +310,9 @@ func (i *importer) queryImports(filename string) fileImports {
anyNonCopyFrom = true
}
}
if query.EnableOpenTracing {
openTrackingImport = true
}
}

std, pkg := buildImports(i.Options, gq, func(name string) bool {
Expand Down Expand Up @@ -393,6 +397,9 @@ func (i *importer) queryImports(filename string) fileImports {
if anyNonCopyFrom {
std["context"] = struct{}{}
}
if openTrackingImport {
pkg[ImportSpec{Path: "github.com/opentracing/opentracing-go"}] = struct{}{}
}

sqlpkg := parseDriver(i.Options.SqlPackage)
if sqlcSliceScan() && !sqlpkg.IsPGX() {
Expand Down
1 change: 1 addition & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

type Options struct {
EnableOpenTracing bool `json:"enable_open_tracing" yaml:"enable_open_tracing"`
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
EmitJsonTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
JsonTagsIdUppercase bool `json:"json_tags_id_uppercase" yaml:"json_tags_id_uppercase"`
Expand Down
19 changes: 10 additions & 9 deletions internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,16 @@ func (v QueryValue) VariableForField(f Field) string {

// A struct used to generate methods and fields on the Queries struct
type Query struct {
Cmd string
Comments []string
MethodName string
FieldName string
ConstantName string
SQL string
SourceName string
Ret QueryValue
Arg QueryValue
Cmd string
Comments []string
MethodName string
FieldName string
ConstantName string
SQL string
SourceName string
EnableOpenTracing bool
Ret QueryValue
Arg QueryValue
// Used for :copyfrom
Table *plugin.Identifier
}
Expand Down
17 changes: 9 additions & 8 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,15 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
}

gq := Query{
Cmd: query.Cmd,
ConstantName: constantName,
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
MethodName: query.Name,
SourceName: query.Filename,
SQL: query.Text,
Comments: comments,
Table: query.InsertIntoTable,
Cmd: query.Cmd,
ConstantName: constantName,
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
MethodName: query.Name,
SourceName: query.Filename,
SQL: query.Text,
Comments: comments,
Table: query.InsertIntoTable,
EnableOpenTracing: options.EnableOpenTracing,
}
sqlpkg := parseDriver(options.SqlPackage)

Expand Down
24 changes: 24 additions & 0 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
{{if eq .EnableOpenTracing true}}
span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}")
defer span.Finish()
{{end}}
{{- template "queryCodeStdExec" . }}
{{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }}
var {{.Ret.Name}} {{.Ret.Type}}
Expand All @@ -36,6 +40,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
{{if eq .EnableOpenTracing true}}
span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}")
defer span.Finish()
{{end}}
{{- template "queryCodeStdExec" . }}
if err != nil {
return nil, err
Expand Down Expand Up @@ -67,6 +75,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) error {
{{if eq .EnableOpenTracing true}}
span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}")
defer span.Finish()
{{end}}
{{- template "queryCodeStdExec" . }}
return err
}
Expand All @@ -76,6 +88,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) (int64, error) {
{{if eq .EnableOpenTracing true}}
span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}")
defer span.Finish()
{{end}}
{{- template "queryCodeStdExec" . }}
if err != nil {
return 0, err
Expand All @@ -88,6 +104,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) (int64, error) {
{{if eq .EnableOpenTracing true}}
span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}")
defer span.Finish()
{{end}}
{{- template "queryCodeStdExec" . }}
if err != nil {
return 0, err
Expand All @@ -100,6 +120,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) (sql.Result, error) {
{{if eq .EnableOpenTracing true}}
span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}")
defer span.Finish()
{{end}}
{{- template "queryCodeStdExec" . }}
}
{{end}}
Expand Down