From 95f8e88d006504defd75372c7509e54d09e37416 Mon Sep 17 00:00:00 2001 From: Harish Bohara Date: Sun, 14 Apr 2024 16:09:50 +0530 Subject: [PATCH 1/3] added support to enable_open_tracing --- internal/codegen/golang/gen.go | 1 + internal/codegen/golang/imports.go | 7 ++++++ internal/codegen/golang/opts/options.go | 1 + internal/codegen/golang/query.go | 19 ++++++++------- internal/codegen/golang/result.go | 17 ++++++------- .../golang/templates/stdlib/queryCode.tmpl | 24 +++++++++++++++++++ 6 files changed, 52 insertions(+), 17 deletions(-) diff --git a/internal/codegen/golang/gen.go b/internal/codegen/golang/gen.go index 5b7977f500..21dfe9538a 100644 --- a/internal/codegen/golang/gen.go +++ b/internal/codegen/golang/gen.go @@ -41,6 +41,7 @@ type tmplCtx struct { UsesBatch bool OmitSqlcVersion bool BuildTags string + EnableOpenTracing bool } func (t *tmplCtx) OutputQuery(sourceName string) bool { diff --git a/internal/codegen/golang/imports.go b/internal/codegen/golang/imports.go index 9e7819e4b1..77a82f7795 100644 --- a/internal/codegen/golang/imports.go +++ b/internal/codegen/golang/imports.go @@ -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 @@ -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 { @@ -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() { diff --git a/internal/codegen/golang/opts/options.go b/internal/codegen/golang/opts/options.go index 30a6c2246c..6e489fd2e7 100644 --- a/internal/codegen/golang/opts/options.go +++ b/internal/codegen/golang/opts/options.go @@ -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"` diff --git a/internal/codegen/golang/query.go b/internal/codegen/golang/query.go index 3b4fb2fa1a..64b978aad7 100644 --- a/internal/codegen/golang/query.go +++ b/internal/codegen/golang/query.go @@ -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 } diff --git a/internal/codegen/golang/result.go b/internal/codegen/golang/result.go index 560e112af2..d96e2e11d7 100644 --- a/internal/codegen/golang/result.go +++ b/internal/codegen/golang/result.go @@ -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) diff --git a/internal/codegen/golang/templates/stdlib/queryCode.tmpl b/internal/codegen/golang/templates/stdlib/queryCode.tmpl index cf56000ec6..662a71ba92 100644 --- a/internal/codegen/golang/templates/stdlib/queryCode.tmpl +++ b/internal/codegen/golang/templates/stdlib/queryCode.tmpl @@ -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}} @@ -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 @@ -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 } @@ -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 @@ -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 @@ -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}} From 61647eacfabb23e5a6674fdb005e64e8b86d6c7a Mon Sep 17 00:00:00 2001 From: Harish Bohara Date: Sun, 14 Apr 2024 16:12:02 +0530 Subject: [PATCH 2/3] added support to enable_open_tracing --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index d8e3b46bb8..ff59ea67f7 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,31 @@ Check out [an interactive example](https://play.sqlc.dev/) to see it in action, - [Downloads](https://downloads.sqlc.dev/) - [Community](https://discord.gg/EcXzGe5SEs) +### Delta on top of main sqlc +Added support for `enable_open_tracing` which adds open tracing to the generated code. +```go +span, ctx := opentracing.StartSpanFromContext(ctx, "MethodName") +defer span.Finish() +``` + +```yaml +version: "2" +sql: + - engine: "mysql" + queries: "query.sql" + schema: "schema.sql" + gen: + go: + emit_prepared_queries: true + emit_interface: true + enable_open_tracing: true + emit_exact_table_names: false + emit_empty_slices: true + emit_json_tags: true + package: "authors" + out: "./authors" +``` + ## Supported languages - [sqlc-gen-go](https://github.com/sqlc-dev/sqlc-gen-go) From 412cb74ee30edb7133ab26e8b7865545d94662a4 Mon Sep 17 00:00:00 2001 From: Harish Bohara Date: Tue, 16 Apr 2024 00:54:43 +0530 Subject: [PATCH 3/3] fixed issue with empty space which failed a test --- .../golang/templates/stdlib/queryCode.tmpl | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/codegen/golang/templates/stdlib/queryCode.tmpl b/internal/codegen/golang/templates/stdlib/queryCode.tmpl index 662a71ba92..cf048c4671 100644 --- a/internal/codegen/golang/templates/stdlib/queryCode.tmpl +++ b/internal/codegen/golang/templates/stdlib/queryCode.tmpl @@ -23,10 +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}} + {{- if eq .EnableOpenTracing true}} span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}") defer span.Finish() - {{end}} + {{end -}} {{- template "queryCodeStdExec" . }} {{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }} var {{.Ret.Name}} {{.Ret.Type}} @@ -40,10 +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}} + {{- if eq .EnableOpenTracing true}} span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}") defer span.Finish() - {{end}} + {{end -}} {{- template "queryCodeStdExec" . }} if err != nil { return nil, err @@ -75,10 +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}} + {{- if eq .EnableOpenTracing true}} span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}") defer span.Finish() - {{end}} + {{end -}} {{- template "queryCodeStdExec" . }} return err } @@ -88,10 +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}} + {{- if eq .EnableOpenTracing true}} span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}") defer span.Finish() - {{end}} + {{end -}} {{- template "queryCodeStdExec" . }} if err != nil { return 0, err @@ -104,10 +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}} + {{- if eq .EnableOpenTracing true}} span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}") defer span.Finish() - {{end}} + {{end -}} {{- template "queryCodeStdExec" . }} if err != nil { return 0, err @@ -120,10 +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}} + {{- if eq .EnableOpenTracing true}} span, ctx := opentracing.StartSpanFromContext(ctx, "{{.MethodName}}") defer span.Finish() - {{end}} + {{end -}} {{- template "queryCodeStdExec" . }} } {{end}}