OpenPostgresURL opens a new GCP database connection wrapped with OpenTelemetry instrumentation.
(ctx context.Context, u *url.URL)
| 98 | |
| 99 | // OpenPostgresURL opens a new GCP database connection wrapped with OpenTelemetry instrumentation. |
| 100 | func (uo *URLOpener) OpenPostgresURL(ctx context.Context, u *url.URL) (*sql.DB, error) { |
| 101 | if uo.CertSource == nil { |
| 102 | return nil, fmt.Errorf("gcppostgres: URLOpener CertSource is nil") |
| 103 | } |
| 104 | instance, dbName, err := instanceFromURL(u) |
| 105 | if err != nil { |
| 106 | return nil, fmt.Errorf("gcppostgres: open %v: %v", u, err) |
| 107 | } |
| 108 | |
| 109 | query := u.Query() |
| 110 | for k := range query { |
| 111 | // Only permit parameters that do not conflict with other behavior. |
| 112 | if k == "sslmode" || k == "sslcert" || k == "sslkey" || k == "sslrootcert" { |
| 113 | return nil, fmt.Errorf("gcppostgres: open: extra parameter %s not allowed", k) |
| 114 | } |
| 115 | } |
| 116 | query.Set("sslmode", "disable") |
| 117 | |
| 118 | u2 := new(url.URL) |
| 119 | *u2 = *u |
| 120 | u2.Scheme = "postgres" |
| 121 | u2.Host = "cloudsql" |
| 122 | u2.Path = "/" + dbName |
| 123 | u2.RawQuery = query.Encode() |
| 124 | db := sql.OpenDB(connector{ |
| 125 | client: &proxy.Client{ |
| 126 | Port: 3307, |
| 127 | Certs: uo.CertSource, |
| 128 | }, |
| 129 | instance: instance, |
| 130 | pqConn: u2.String(), |
| 131 | traceOpts: append([]otelsql.Option(nil), uo.TraceOpts...), |
| 132 | }) |
| 133 | return db, nil |
| 134 | } |
| 135 | |
| 136 | func instanceFromURL(u *url.URL) (instance, db string, _ error) { |
| 137 | path := u.Host + u.Path // everything after scheme but before query or fragment |
nothing calls this directly
no test coverage detected