Debian Codegraph @

postgres/postgres.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package postgres provides functions to open PostgreSQL databases with OpenTelemetry instrumentation.
16 package postgres
17
18 import (
19 "context"
20 "database/sql"
21 "database/sql/driver"
22 "net/url"
23
24 "github.com/XSAM/otelsql"
25 "github.com/lib/pq"
26 "gocloud.dev/internal/openurl"
27 )
28
29 // Scheme is the URL scheme this package registers its URLOpener under on
30 // DefaultMux.
31 const Scheme = "postgres"
32
33 func init() {
34 DefaultURLMux().RegisterPostgres(Scheme, &URLOpener{})
35 }
36
37 // URLOpener opens URLs like "postgres://" by using the underlying PostgreSQL driver.
38 // See https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters for details.
39 type URLOpener struct {
40 TraceOpts []otelsql.Option
41 }
42
43 // OpenPostgresURL opens a new database connection wrapped with OpenTelemetry instrumentation.
44 func (uo *URLOpener) OpenPostgresURL(ctx context.Context, u *url.URL) (*sql.DB, error) {
45 u2 := new(url.URL)
46 *u2 = *u
47 u2.Scheme = "postgres"
48 return sql.OpenDB(connector{dsn: u2.String(), traceOpts: append([]otelsql.Option(nil), uo.TraceOpts...)}), nil
49 }
50
51 type connector struct {
52 dsn string
53 traceOpts []otelsql.Option
54 }
55
56 func (c connector) Connect(ctx context.Context) (driver.Conn, error) {
57 return c.Driver().Open(c.dsn)
58 }
59
60 func (c connector) Driver() driver.Driver {
61 return otelsql.WrapDriver(&pq.Driver{}, c.traceOpts...)
62 }
63
64 // PostgresURLOpener can open Postgres connections based on a URL.
65 // The opener must not modify the URL argument. OpenPostgresURL must be safe to
66 // call from multiple goroutines.
67 //
68 // This interface is generally implemented by types in driver packages.
69 type PostgresURLOpener interface {
70 // OpenPostgresURL opens a Postgres connection based on the URL.
71 OpenPostgresURL(ctx context.Context, u *url.URL) (*sql.DB, error)
72 }
73
74 // URLMux is a URL opener multiplexer. It matches the scheme of the URLs
75 // against a set of registered schemes and calls the opener that matches the
76 // URL's scheme.
77 //
78 // The zero value is a multiplexer with no registered schemes.
79 type URLMux struct {
80 schemes openurl.SchemeMap
81 }
82
83 // RegisterPostgres registers the opener with the given scheme. If an opener
84 // already exists for the scheme, RegisterPostgres panics.
85 func (mux *URLMux) RegisterPostgres(scheme string, opener PostgresURLOpener) {
86 mux.schemes.Register("postgres", "DB", scheme, opener)
87 }
88
89 // OpenPostgres calls OpenPostgresURL with the URL parsed from urlstr.
90 // OpenPostgres is safe to call from multiple goroutines.
91 func (mux *URLMux) OpenPostgres(ctx context.Context, urlstr string) (*sql.DB, error) {
92 opener, u, err := mux.schemes.FromString("DB", urlstr)
93 if err != nil {
94 return nil, err
95 }
96 return opener.(PostgresURLOpener).OpenPostgresURL(ctx, u)
97 }
98
99 // OpenPostgresURL dispatches the URL to the opener that is registered with the
100 // URL's scheme. OpenPostgresURL is safe to call from multiple goroutines.
101 func (mux *URLMux) OpenPostgresURL(ctx context.Context, u *url.URL) (*sql.DB, error) {
102 opener, err := mux.schemes.FromURL("DB", u)
103 if err != nil {
104 return nil, err
105 }
106 return opener.(PostgresURLOpener).OpenPostgresURL(ctx, u)
107 }
108
109 var defaultURLMux = new(URLMux)
110
111 // DefaultURLMux returns the URLMux used by OpenPostgres.
112 //
113 // Driver packages can use this to register their PostgresURLOpener on the mux.
114 func DefaultURLMux() *URLMux {
115 return defaultURLMux
116 }
117
118 // Open opens the bucket identified by the URL given. URL openers must be
119 // registered in the DefaultURLMux, which is typically done in driver
120 // packages' initialization.
121 //
122 // See the URLOpener documentation in driver subpackages for more
123 // details on supported scheme(s) and URL parameter(s).
124 func Open(ctx context.Context, urlstr string) (*sql.DB, error) {
125 return defaultURLMux.OpenPostgres(ctx, urlstr)
126 }