Skip to content

Commit 1536d1f

Browse files
authored
feat(sources/mssql): add app name to MSSQL (#1620)
## Description --- Add auditable application name to mssql sources. Query the db with: ``` SELECT session_id, program_name FROM sys.dm_exec_sessions; ``` ## 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>
1 parent 36c6584 commit 1536d1f

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

internal/sources/cloudsqlmssql/cloud_sql_mssql.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,25 @@ func initCloudSQLMssqlConnection(ctx context.Context, tracer trace.Tracer, name,
111111
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name)
112112
defer span.End()
113113

114+
userAgent, err := util.UserAgentFromContext(ctx)
115+
if err != nil {
116+
return nil, err
117+
}
118+
114119
// Create dsn
115-
query := fmt.Sprintf("database=%s&cloudsql=%s:%s:%s", dbname, project, region, instance)
120+
query := url.Values{}
121+
query.Add("app name", userAgent)
122+
query.Add("database", dbname)
123+
query.Add("cloudsql", fmt.Sprintf("%s:%s:%s", project, region, instance))
124+
116125
url := &url.URL{
117126
Scheme: "sqlserver",
118127
User: url.UserPassword(user, pass),
119128
Host: ipAddress,
120-
RawQuery: query,
129+
RawQuery: query.Encode(),
121130
}
122131

123132
// Get dial options
124-
userAgent, err := util.UserAgentFromContext(ctx)
125-
if err != nil {
126-
return nil, err
127-
}
128133
opts, err := sources.GetCloudSQLOpts(ipType, userAgent, false)
129134
if err != nil {
130135
return nil, err

internal/sources/mssql/mssql.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/goccy/go-yaml"
2424
"github.com/googleapis/genai-toolbox/internal/sources"
25+
"github.com/googleapis/genai-toolbox/internal/util"
2526
_ "github.com/microsoft/go-mssqldb"
2627
"go.opentelemetry.io/otel/trace"
2728
)
@@ -114,8 +115,13 @@ func initMssqlConnection(
114115
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name)
115116
defer span.End()
116117

118+
userAgent, err := util.UserAgentFromContext(ctx)
119+
if err != nil {
120+
userAgent = "genai-toolbox"
121+
}
117122
// Create dsn
118123
query := url.Values{}
124+
query.Add("app name", userAgent)
119125
query.Add("database", dbname)
120126
if encrypt != "" {
121127
query.Add("encrypt", encrypt)

0 commit comments

Comments
 (0)