Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.
This repository was archived by the owner on May 29, 2026. It is now read-only.

bug: Go library does not comment controller #279

Description

@duffn

EnableController is available on the CommenterConfig struct (

EnableController bool
), however, the code that outputs the SQL comment does not output a comment for the controller context key (
func (conn *sqlCommenterConn) withComment(ctx context.Context, query string) string {
var commentsMap = map[string]string{}
query = strings.TrimSpace(query)
config := conn.options.Config
// Sorted alphabetically
if config.EnableAction && (ctx.Value(core.Action) != nil) {
commentsMap[core.Action] = ctx.Value(core.Action).(string)
}
// `driver` information should not be coming from framework.
// So, explicitly adding that here.
if config.EnableDBDriver {
commentsMap[core.Driver] = fmt.Sprintf("database/sql:%s", conn.options.Tags.DriverName)
}
if config.EnableFramework && (ctx.Value(core.Framework) != nil) {
commentsMap[core.Framework] = ctx.Value(core.Framework).(string)
}
if config.EnableRoute && (ctx.Value(core.Route) != nil) {
commentsMap[core.Route] = ctx.Value(core.Route).(string)
}
if config.EnableTraceparent {
carrier := core.ExtractTraceparent(ctx)
if val, ok := carrier["traceparent"]; ok {
commentsMap[core.Traceparent] = val
}
}
if config.EnableApplication {
if !attemptedToAutosetApplication && conn.options.Tags.Application == "" {
attemptedToAutosetApplication = true
bi, ok := debug.ReadBuildInfo()
if ok {
conn.options.Tags.Application = bi.Path
}
}
if conn.options.Tags.Application != "" {
commentsMap[core.Application] = conn.options.Tags.Application
}
}
var commentsString string = ""
if len(commentsMap) > 0 { // Converts comments map to string and appends it to query
commentsString = fmt.Sprintf("/*%s*/", core.ConvertMapToComment(commentsMap))
}
// A semicolon at the end of the SQL statement means the query ends there.
// We need to insert the comment before that to be considered as part of the SQL statemtent.
if query[len(query)-1:] == ";" {
return fmt.Sprintf("%s%s;", strings.TrimSuffix(query, ";"), commentsString)
}
return fmt.Sprintf("%s%s", query, commentsString)
}
).

I will plan to open a PR for this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions