feat: add program name to MySQL connections (#1617)

## Description

---
Audit your applications by using the following sql to track program name
```
SELECT
    session_connect_attrs.ATTR_VALUE AS program_name,
    processlist.*
FROM
    information_schema.processlist
LEFT JOIN
    performance_schema.session_connect_attrs
ON
    (processlist.ID = session_connect_attrs.PROCESSLIST_ID AND session_connect_attrs.ATTR_NAME = "program_name");
```


## PR Checklist

---
> Thank you for opening a Pull Request! Before submitting your PR, there
are a
> few things you can do to make sure it goes smoothly:

- [ ] Make sure you reviewed

[CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md)
- [ ] Make sure to open an issue as a

[bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose)
before writing your code! That way we can discuss the change, evaluate
  designs, and agree on the general idea
- [ ] Ensure the tests and linter pass
- [ ] Code coverage does not decrease (if any source code was changed)
- [ ] Appropriate docs were updated (if necessary)
- [ ] Make sure to add `!` if this involve a breaking change

🛠️ Fixes #<issue_number_goes_here>
This commit is contained in:
Averi Kitsch
2025-10-02 15:41:31 -07:00
committed by GitHub
parent af72637009
commit c4a22b8d3b
2 changed files with 7 additions and 3 deletions

View File

@@ -118,9 +118,8 @@ func initCloudSQLMySQLConnectionPool(ctx context.Context, tracer trace.Tracer, n
return nil, fmt.Errorf("unable to register driver: %w", err)
}
}
// Tell the driver to use the Cloud SQL Go Connector to create connections
dsn := fmt.Sprintf("%s:%s@cloudsql-mysql(%s:%s:%s)/%s", user, pass, project, region, instance, dbname)
dsn := fmt.Sprintf("%s:%s@cloudsql-mysql(%s:%s:%s)/%s?connectionAttributes=program_name:%s", user, pass, project, region, instance, dbname, userAgent)
db, err := sql.Open(
"cloudsql-mysql",
dsn,

View File

@@ -24,6 +24,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/goccy/go-yaml"
"github.com/googleapis/genai-toolbox/internal/sources"
"github.com/googleapis/genai-toolbox/internal/util"
"go.opentelemetry.io/otel/trace"
)
@@ -122,7 +123,11 @@ func initMySQLConnectionPool(ctx context.Context, tracer trace.Tracer, name, hos
values.Set(k, v)
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", user, pass, host, port, dbname)
userAgent, err := util.UserAgentFromContext(ctx)
if err != nil {
return nil, err
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&connectionAttributes=program_name:%s", user, pass, host, port, dbname, userAgent)
if enc := values.Encode(); enc != "" {
dsn += "&" + enc
}