feat(sources/postgres): add application_name (#1504)

## Description

---
> Should include a concise description of the changes (bug or feature),
it's
> impact, along with a summary of the solution

## 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-09-25 14:12:07 -07:00
committed by GitHub
parent 9501ebbdbc
commit 72a2366b28
4 changed files with 28 additions and 8 deletions

View File

@@ -117,11 +117,15 @@ func getOpts(ipType, userAgent string, useIAM bool) ([]alloydbconn.Option, error
}
func getConnectionConfig(ctx context.Context, user, pass, dbname string) (string, bool, error) {
userAgent, err := util.UserAgentFromContext(ctx)
if err != nil {
userAgent = "genai-toolbox"
}
useIAM := true
// If username and password both provided, use password authentication
if user != "" && pass != "" {
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, pass, dbname)
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable application_name=%s", user, pass, dbname, userAgent)
useIAM = false
return dsn, useIAM, nil
}
@@ -141,7 +145,7 @@ func getConnectionConfig(ctx context.Context, user, pass, dbname string) (string
}
// Construct IAM connection string with username
dsn := fmt.Sprintf("user=%s dbname=%s sslmode=disable", user, dbname)
dsn := fmt.Sprintf("user=%s dbname=%s sslmode=disable application_name=%s", user, dbname, userAgent)
return dsn, useIAM, nil
}

View File

@@ -36,7 +36,7 @@ func TestParseFromYamlCassandra(t *testing.T) {
sources:
my-cassandra-instance:
kind: cassandra
hosts:
hosts:
- "my-host1"
- "my-host2"
`,
@@ -62,7 +62,7 @@ func TestParseFromYamlCassandra(t *testing.T) {
sources:
my-cassandra-instance:
kind: cassandra
hosts:
hosts:
- "my-host1"
- "my-host2"
username: "user"
@@ -121,11 +121,11 @@ func TestFailParseFromYaml(t *testing.T) {
sources:
my-cassandra-instance:
kind: cassandra
host:
hosts:
- "my-host"
foo: bar
`,
err: "unable to parse source \"my-cassandra-instance\" as \"cassandra\": [1:1] unknown field \"foo\"\n> 1 | foo: bar\n ^\n 2 | host:\n 3 | - my-host\n 4 | kind: cassandra",
err: "unable to parse source \"my-cassandra-instance\" as \"cassandra\": [1:1] unknown field \"foo\"\n> 1 | foo: bar\n ^\n 2 | hosts:\n 3 | - my-host\n 4 | kind: cassandra",
},
{
desc: "missing required field",

View File

@@ -98,11 +98,15 @@ func (s *Source) PostgresPool() *pgxpool.Pool {
}
func getConnectionConfig(ctx context.Context, user, pass, dbname string) (string, bool, error) {
userAgent, err := util.UserAgentFromContext(ctx)
if err != nil {
userAgent = "genai-toolbox"
}
useIAM := true
// If username and password both provided, use password authentication
if user != "" && pass != "" {
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, pass, dbname)
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable application_name=%s", user, pass, dbname, userAgent)
useIAM = false
return dsn, useIAM, nil
}
@@ -122,7 +126,7 @@ func getConnectionConfig(ctx context.Context, user, pass, dbname string) (string
}
// Construct IAM connection string with username
dsn := fmt.Sprintf("user=%s dbname=%s sslmode=disable", user, dbname)
dsn := fmt.Sprintf("user=%s dbname=%s sslmode=disable application_name=%s", user, dbname, userAgent)
return dsn, useIAM, nil
}

View File

@@ -22,6 +22,7 @@ import (
"github.com/goccy/go-yaml"
"github.com/googleapis/genai-toolbox/internal/sources"
"github.com/googleapis/genai-toolbox/internal/util"
"github.com/jackc/pgx/v5/pgxpool"
"go.opentelemetry.io/otel/trace"
)
@@ -99,6 +100,17 @@ func initPostgresConnectionPool(ctx context.Context, tracer trace.Tracer, name,
//nolint:all // Reassigned ctx
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name)
defer span.End()
userAgent, err := util.UserAgentFromContext(ctx)
if err != nil {
userAgent = "genai-toolbox"
}
if queryParams == nil {
// Initialize the map before using it
queryParams = make(map[string]string)
}
if _, ok := queryParams["application_name"]; !ok {
queryParams["application_name"] = userAgent
}
// urlExample := "postgres:dd//username:password@localhost:5432/database_name"
url := &url.URL{