Skip to content

Commit e88d487

Browse files
committed
adding options pattern to hypersql and hyperpgx
1 parent 67017c2 commit e88d487

File tree

9 files changed

+156
-8
lines changed

9 files changed

+156
-8
lines changed

examples/sql-query/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/hypertrace/goagent/instrumentation/hypertrace"
1717
"github.com/hypertrace/goagent/instrumentation/hypertrace/database/hypersql"
1818
"github.com/hypertrace/goagent/instrumentation/hypertrace/net/hyperhttp"
19-
sdkSQL "github.com/hypertrace/goagent/sdk/instrumentation/database/sql"
2019
)
2120

2221
const mysqlLoopCount int = 5
@@ -74,7 +73,7 @@ func fooHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
7473

7574
func dbConn() (db *sql.DB) {
7675
// Explicitly wrap the MySQLDriver driver with hypersql.
77-
driver := hypersql.Wrap(&mysql.MySQLDriver{}, &sdkSQL.Options{})
76+
driver := hypersql.Wrap(&mysql.MySQLDriver{})
7877

7978
// Register our hypersql wrapper as a database driver.
8079
sql.Register("ht-mysql", driver)

instrumentation/hypertrace/database/hypersql/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,42 @@ sql.Register("ht-mysql", driver)
3939
// Connect to a MySQL database using the hypersql driver wrapper
4040
db, err = sql.Open("ht-mysql", "user:password@/dbname")
4141
```
42+
43+
For adding a filter implementation to the instrumentation, there's an option to use hypersql.WithFilter to add filters to the instrumentation.
44+
```go
45+
46+
import (
47+
"github.com/go-sql-driver/mysql"
48+
"github.com/hypertrace/goagent/instrumentation/hypertrace/database/hypersql"
49+
"github.com/hypertrace/goagent/sdk/filter"
50+
)
51+
52+
// Explicitly wrap the MySQL driver with hypersql
53+
driver := hypersql.Wrap(&mysql.MySQLDriver{}, hypersql.WithFilter(filter.NoopFilter{}))
54+
55+
// Register our hypersql wrapper as a database driver
56+
sql.Register("ht-mysql", driver)
57+
58+
// Connect to a MySQL database using the hypersql driver wrapper
59+
db, err = sql.Open("ht-mysql", "user:password@/dbname")
60+
```
61+
62+
OR
63+
64+
```go
65+
import (
66+
"database/sql"
67+
"github.com/hypertrace/goagent/instrumentation/hypertrace/database/hypersql"
68+
"github.com/hypertrace/goagent/sdk/filter"
69+
)
70+
71+
// Register our hypersql wrapper for the provided MySQL driver.
72+
driverName, err = hypersql.Register("mysql", hypersql.WithFilter(filter.NoopFilter{}))
73+
if err != nil {
74+
log.Fatalf("unable to register goagent driver: %v\n", err)
75+
}
76+
77+
// Connect to a MySQL database using the hypersql driver wrapper.
78+
db, err = sql.Open(driverName, "user:password@/dbname")
79+
80+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package hypersql // import "github.com/hypertrace/goagent/instrumentation/hypertrace/database/hypersql"
2+
3+
import (
4+
"github.com/hypertrace/goagent/sdk/filter"
5+
sdkSQL "github.com/hypertrace/goagent/sdk/instrumentation/database/sql"
6+
)
7+
8+
type options struct {
9+
Filter filter.Filter
10+
}
11+
12+
func (o *options) toSDKOptions() *sdkSQL.Options {
13+
opts := (sdkSQL.Options)(*o)
14+
return &opts
15+
}
16+
17+
type Option func(o *options)
18+
19+
// WithFilter adds a filter to the GRPC option.
20+
func WithFilter(f filter.Filter) Option {
21+
return func(o *options) {
22+
o.Filter = f
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package hypersql // import "github.com/hypertrace/goagent/instrumentation/hypertrace/database/hypersql"
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hypertrace/goagent/sdk/filter"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestOptionsToSDK(t *testing.T) {
11+
o := &options{
12+
Filter: filter.NoopFilter{},
13+
}
14+
assert.Equal(t, filter.NoopFilter{}, o.toSDKOptions().Filter)
15+
}
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
package hypersql // import "github.com/hypertrace/goagent/instrumentation/hypertrace/database/hypersql"
22

33
import (
4+
"database/sql/driver"
45
otelsql "github.com/hypertrace/goagent/instrumentation/opentelemetry/database/hypersql"
56
)
67

78
// Wrap takes a SQL driver and wraps it with Hypertrace instrumentation.
8-
var Wrap = otelsql.Wrap
9+
func Wrap(d driver.Driver, opts ...Option) driver.Driver {
10+
o := &options{}
11+
for _, opt := range opts {
12+
opt(o)
13+
}
14+
15+
return otelsql.Wrap(d, o.toSDKOptions())
16+
}
917

1018
// Register initializes and registers the hypersql wrapped database driver
1119
// identified by its driverName. On success it
1220
// returns the generated driverName to use when calling sql.Open.
13-
var Register = otelsql.Register
21+
func Register(driverName string, opts ...Option) (string, error) {
22+
o := &options{}
23+
for _, opt := range opts {
24+
opt(o)
25+
}
26+
return otelsql.Register(driverName, o.toSDKOptions())
27+
28+
}

instrumentation/hypertrace/github.com/jackc/hyperpgx/go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ replace github.com/hypertrace/goagent => ../../../../..
88

99
replace github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx => ../../../../../instrumentation/opentelemetry/github.com/jackc/hyperpgx
1010

11-
require github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx v0.0.0-00010101000000-000000000000
11+
require (
12+
github.com/hypertrace/goagent v0.0.0-00010101000000-000000000000
13+
github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx v0.0.0-00010101000000-000000000000
14+
github.com/stretchr/testify v1.10.0
15+
)
1216

1317
require (
1418
github.com/cenkalti/backoff/v5 v5.0.2 // indirect
19+
github.com/davecgh/go-spew v1.1.1 // indirect
1520
github.com/felixge/httpsnoop v1.0.4 // indirect
1621
github.com/ghodss/yaml v1.0.0 // indirect
1722
github.com/go-logr/logr v1.4.3 // indirect
@@ -20,7 +25,6 @@ require (
2025
github.com/google/uuid v1.6.0 // indirect
2126
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
2227
github.com/hypertrace/agent-config/gen/go v0.0.0-20240523214336-1259231da906 // indirect
23-
github.com/hypertrace/goagent v0.0.0-00010101000000-000000000000 // indirect
2428
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
2529
github.com/jackc/pgconn v1.8.1 // indirect
2630
github.com/jackc/pgio v1.0.0 // indirect
@@ -30,6 +34,7 @@ require (
3034
github.com/jackc/pgtype v1.7.0 // indirect
3135
github.com/jackc/pgx/v4 v4.11.0 // indirect
3236
github.com/openzipkin/zipkin-go v0.4.3 // indirect
37+
github.com/pmezard/go-difflib v1.0.0 // indirect
3338
github.com/tklauser/go-sysconf v0.3.14 // indirect
3439
github.com/tklauser/numcpus v0.8.0 // indirect
3540
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
@@ -59,4 +64,5 @@ require (
5964
google.golang.org/grpc v1.72.2 // indirect
6065
google.golang.org/protobuf v1.36.6 // indirect
6166
gopkg.in/yaml.v2 v2.4.0 // indirect
67+
gopkg.in/yaml.v3 v3.0.1 // indirect
6268
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package hyperpgx // import "github.com/hypertrace/goagent/instrumentation/hypertrace/github.com/jackc/hyperpgx"
2+
3+
import (
4+
otelpgx "github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx"
5+
"github.com/hypertrace/goagent/sdk/filter"
6+
)
7+
8+
type options struct {
9+
Filter filter.Filter
10+
}
11+
12+
func (o *options) toSDKOptions() *otelpgx.Options {
13+
opts := (otelpgx.Options)(*o)
14+
return &opts
15+
}
16+
17+
type Option func(o *options)
18+
19+
// WithFilter adds a filter to the GRPC option.
20+
func WithFilter(f filter.Filter) Option {
21+
return func(o *options) {
22+
o.Filter = f
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package hyperpgx // import "github.com/hypertrace/goagent/instrumentation/hypertrace/github.com/jackc/hyperpgx"
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hypertrace/goagent/sdk/filter"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestOptionsToSDK(t *testing.T) {
11+
o := &options{
12+
Filter: filter.NoopFilter{},
13+
}
14+
assert.Equal(t, filter.NoopFilter{}, o.toSDKOptions().Filter)
15+
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package hyperpgx // import "github.com/hypertrace/goagent/instrumentation/hypertrace/github.com/jackc/hyperpgx"
22

3-
import otelpgx "github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx"
3+
import (
4+
"context"
45

5-
var Connect = otelpgx.Connect
6+
otelpgx "github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx"
7+
)
8+
9+
func Connect(ctx context.Context, connString string, opts ...Option) (otelpgx.PGXConn, error) {
10+
o := &options{}
11+
for _, opt := range opts {
12+
opt(o)
13+
}
14+
15+
return otelpgx.Connect(ctx, connString, o.toSDKOptions())
16+
}

0 commit comments

Comments
 (0)