forked from DataDog/dd-trace-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib/database/sql: add in ddh, dddb propagation (DataDog#2550)
contrib/database/sql: add in ddh, dddb propagation
- Loading branch information
Showing
6 changed files
with
100 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,11 @@ package internal // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql | |
|
||
import ( | ||
"net" | ||
"net/url" | ||
"strings" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/log" | ||
) | ||
|
||
// ParseDSN parses various supported DSN types into a map of key/value pairs which can be used as valid tags. | ||
|
@@ -19,20 +21,36 @@ func ParseDSN(driverName, dsn string) (meta map[string]string, err error) { | |
case "mysql": | ||
meta, err = parseMySQLDSN(dsn) | ||
if err != nil { | ||
log.Debug("Error parsing DSN for mysql: %v", err) | ||
return | ||
} | ||
case "postgres", "pgx": | ||
meta, err = parsePostgresDSN(dsn) | ||
if err != nil { | ||
log.Debug("Error parsing DSN for postgres: %v", err) | ||
return | ||
} | ||
case "sqlserver": | ||
meta, err = parseSQLServerDSN(dsn) | ||
if err != nil { | ||
log.Debug("Error parsing DSN for sqlserver: %v", err) | ||
return | ||
} | ||
default: | ||
// not supported | ||
// Try to parse the DSN and see if the scheme contains a known driver name. | ||
u, e := url.Parse(dsn) | ||
if e != nil { | ||
// dsn is not a valid URL, so just ignore | ||
log.Debug("Error parsing driver name from DSN: %v", e) | ||
return | ||
} | ||
if driverName != u.Scheme { | ||
// In some cases the driver is registered under a non-official name. | ||
// For example, "Test" may be the registered name with a DSN of "postgres://postgres:[email protected]:5432/fakepreparedb" | ||
// for the purposes of testing/mocking. | ||
// In these cases, we try to parse the DSN based upon the DSN itself, instead of the registered driver name | ||
return ParseDSN(u.Scheme, dsn) | ||
} | ||
} | ||
return reduceKeys(meta), nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ func TestDBMPropagation(t *testing.T) { | |
opts []RegisterOption | ||
callDB func(ctx context.Context, db *sql.DB) error | ||
prepared []string | ||
dsn string | ||
executed []*regexp.Regexp | ||
}{ | ||
{ | ||
|
@@ -73,7 +74,8 @@ func TestDBMPropagation(t *testing.T) { | |
_, err := db.PrepareContext(ctx, "SELECT 1 from DUAL") | ||
return err | ||
}, | ||
prepared: []string{"/*dddbs='test.db',dde='test-env',ddps='test-service',ddpv='1.0.0'*/ SELECT 1 from DUAL"}, | ||
dsn: "postgres://postgres:[email protected]:5432/fakepreparedb?sslmode=disable", | ||
prepared: []string{"/*dddbs='test.db',dde='test-env',ddps='test-service',ddpv='1.0.0',ddh='127.0.0.1',dddb='fakepreparedb'*/ SELECT 1 from DUAL"}, | ||
}, | ||
{ | ||
name: "query", | ||
|
@@ -109,7 +111,8 @@ func TestDBMPropagation(t *testing.T) { | |
_, err := db.QueryContext(ctx, "SELECT 1 from DUAL") | ||
return err | ||
}, | ||
executed: []*regexp.Regexp{regexp.MustCompile("/\\*dddbs='test.db',dde='test-env',ddps='test-service',ddpv='1.0.0',traceparent='00-00000000000000000000000000000001-[\\da-f]{16}-01'\\*/ SELECT 1 from DUAL")}, | ||
dsn: "postgres://postgres:[email protected]:5432/fakequerydb?sslmode=disable", | ||
executed: []*regexp.Regexp{regexp.MustCompile("/\\*dddbs='test.db',dde='test-env',ddps='test-service',ddpv='1.0.0',traceparent='00-00000000000000000000000000000001-[\\da-f]{16}-01',ddh='127.0.0.1',dddb='fakequerydb'\\*/ SELECT 1 from DUAL")}, | ||
}, | ||
{ | ||
name: "exec", | ||
|
@@ -145,7 +148,8 @@ func TestDBMPropagation(t *testing.T) { | |
_, err := db.ExecContext(ctx, "SELECT 1 from DUAL") | ||
return err | ||
}, | ||
executed: []*regexp.Regexp{regexp.MustCompile("/\\*dddbs='test.db',dde='test-env',ddps='test-service',ddpv='1.0.0',traceparent='00-00000000000000000000000000000001-[\\da-f]{16}-01'\\*/ SELECT 1 from DUAL")}, | ||
dsn: "postgres://postgres:[email protected]:5432/fakeexecdb?sslmode=disable", | ||
executed: []*regexp.Regexp{regexp.MustCompile("/\\*dddbs='test.db',dde='test-env',ddps='test-service',ddpv='1.0.0',traceparent='00-00000000000000000000000000000001-[\\da-f]{16}-01',ddh='127.0.0.1',dddb='fakeexecdb'\\*/ SELECT 1 from DUAL")}, | ||
}, | ||
} | ||
|
||
|
@@ -163,9 +167,12 @@ func TestDBMPropagation(t *testing.T) { | |
Register("test", d, tc.opts...) | ||
defer unregister("test") | ||
|
||
db, err := Open("test", "dn") | ||
dsn := "dn" | ||
if tc.dsn != "" { | ||
dsn = tc.dsn | ||
} | ||
db, err := Open("test", dsn) | ||
require.NoError(t, err) | ||
|
||
s, ctx := tracer.StartSpanFromContext(context.Background(), "test.call", tracer.WithSpanID(1)) | ||
err = tc.callDB(ctx, db) | ||
s.Finish() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters