Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to add open-tracing on go mysql #3335

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
Loading