Skip to content

Commit 9e56b19

Browse files
authored
ENGTAI-63552: adding filter eval for client spans (#250)
* adding filter call to grpc * add code for http client * add support for sql client * adding sql client in options wrapper * adding pgx support * adding options pattern to hypersql and hyperpgx
1 parent e15303b commit 9e56b19

File tree

24 files changed

+640
-51
lines changed

24 files changed

+640
-51
lines changed

examples/postgres-query/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ toolchain go1.24.2
66

77
replace github.com/hypertrace/goagent => ../..
88

9-
replace github.com/hypertrace/goagent/instrumentation/hypertrace/github.com/jackc/hyperpgx => ../../instrumentation/opentelemetry/github.com/jackc/hyperpgx
9+
replace github.com/hypertrace/goagent/instrumentation/hypertrace/github.com/jackc/hyperpgx => ../../instrumentation/hypertrace/github.com/jackc/hyperpgx
10+
11+
replace github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx => ../../instrumentation/opentelemetry/github.com/jackc/hyperpgx
1012

1113
require github.com/hypertrace/goagent/instrumentation/hypertrace/github.com/jackc/hyperpgx v0.0.0-00010101000000-000000000000
1214

@@ -21,6 +23,7 @@ require (
2123
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
2224
github.com/hypertrace/agent-config/gen/go v0.0.0-20240523214336-1259231da906 // indirect
2325
github.com/hypertrace/goagent v0.0.0-00010101000000-000000000000 // indirect
26+
github.com/hypertrace/goagent/instrumentation/opentelemetry/github.com/jackc/hyperpgx v0.0.0-00010101000000-000000000000 // indirect
2427
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
2528
github.com/jackc/pgconn v1.8.1 // indirect
2629
github.com/jackc/pgio v1.0.0 // indirect

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+
}

instrumentation/hypertrace/google.golang.org/hypergrpc/client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ import (
1111
// for use in a grpc.Dial call.
1212
// Interceptor format will be replaced with the stats.Handler since instrumentation has moved to the stats.Handler.
1313
// See: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/v1.36.0/instrumentation/google.golang.org/grpc/otelgrpc/example_test.go
14-
func UnaryClientInterceptor() grpc.UnaryClientInterceptor {
14+
func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
15+
o := &options{}
16+
for _, opt := range opts {
17+
opt(o)
18+
}
19+
1520
return sdkgrpc.WrapUnaryClientInterceptor(
1621
grpcunaryinterceptors.UnaryClientInterceptor(),
1722
opentelemetry.SpanFromContext,
23+
o.toSDKOptions(),
1824
map[string]string{},
1925
)
2026
}

0 commit comments

Comments
 (0)