-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding instrumentation support for mongo-driver/v2
- Loading branch information
1 parent
00786cc
commit 95dcf65
Showing
16 changed files
with
855 additions
and
0 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
68 changes: 68 additions & 0 deletions
68
instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/config.go
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo" | ||
|
||
import ( | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// ScopeName is the instrumentation scope name. | ||
const ScopeName = "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo" | ||
|
||
// config is used to configure the mongo tracer. | ||
type config struct { | ||
TracerProvider trace.TracerProvider | ||
|
||
Tracer trace.Tracer | ||
|
||
CommandAttributeDisabled bool | ||
} | ||
|
||
// newConfig returns a config with all Options set. | ||
func newConfig(opts ...Option) config { | ||
cfg := config{ | ||
TracerProvider: otel.GetTracerProvider(), | ||
CommandAttributeDisabled: true, | ||
} | ||
for _, opt := range opts { | ||
opt.apply(&cfg) | ||
} | ||
|
||
cfg.Tracer = cfg.TracerProvider.Tracer( | ||
ScopeName, | ||
trace.WithInstrumentationVersion(Version()), | ||
) | ||
return cfg | ||
} | ||
|
||
// Option specifies instrumentation configuration options. | ||
type Option interface { | ||
apply(*config) | ||
} | ||
|
||
type optionFunc func(*config) | ||
|
||
func (o optionFunc) apply(c *config) { | ||
o(c) | ||
} | ||
|
||
// WithTracerProvider specifies a tracer provider to use for creating a tracer. | ||
// If none is specified, the global provider is used. | ||
func WithTracerProvider(provider trace.TracerProvider) Option { | ||
return optionFunc(func(cfg *config) { | ||
if provider != nil { | ||
cfg.TracerProvider = provider | ||
} | ||
}) | ||
} | ||
|
||
// WithCommandAttributeDisabled specifies if the MongoDB command is added as an attribute to Spans or not. | ||
// This is disabled by default and the MongoDB command will not be added as an attribute | ||
// to Spans if this option is not provided. | ||
func WithCommandAttributeDisabled(disabled bool) Option { | ||
return optionFunc(func(cfg *config) { | ||
cfg.CommandAttributeDisabled = disabled | ||
}) | ||
} |
15 changes: 15 additions & 0 deletions
15
instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/doc.go
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package otelmongo instruments go.mongodb.org/mongo-driver/mongo. | ||
// | ||
// This package is compatible with v0.2.0 of | ||
// go.mongodb.org/mongo-driver/mongo. | ||
// | ||
// `NewMonitor` will return an event.CommandMonitor which is used to trace | ||
// requests. | ||
// | ||
// This code was originally based on the following: | ||
// - https://github.com/DataDog/dd-trace-go/tree/02f0449efa3cb382d499fadc873957385dcb2192/contrib/go.mongodb.org/mongo-driver/mongo | ||
// - https://github.com/DataDog/dd-trace-go/tree/v1.23.3/ddtrace/ext | ||
package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo" |
41 changes: 41 additions & 0 deletions
41
instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/example_test.go
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelmongo_test | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/v2/bson" | ||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
|
||
"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo" | ||
) | ||
|
||
func Example() { | ||
// connect to MongoDB | ||
opts := options.Client() | ||
opts.Monitor = otelmongo.NewMonitor() | ||
opts.ApplyURI("mongodb://localhost:27017") | ||
client, err := mongo.Connect(opts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
db := client.Database("example") | ||
inventory := db.Collection("inventory") | ||
|
||
_, err = inventory.InsertOne(context.Background(), bson.D{ | ||
{Key: "item", Value: "canvas"}, | ||
{Key: "qty", Value: 100}, | ||
{Key: "attributes", Value: bson.A{"cotton"}}, | ||
{Key: "size", Value: bson.D{ | ||
{Key: "h", Value: 28}, | ||
{Key: "w", Value: 35.5}, | ||
{Key: "uom", Value: "cm"}, | ||
}}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/go.mod
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo | ||
|
||
go 1.22.0 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.10.0 | ||
go.mongodb.org/mongo-driver/v2 v2.0.0 | ||
go.opentelemetry.io/otel v1.33.0 | ||
go.opentelemetry.io/otel/trace v1.33.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-logr/logr v1.4.2 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/golang/snappy v0.0.4 // indirect | ||
github.com/klauspost/compress v1.17.11 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect | ||
github.com/xdg-go/scram v1.1.2 // indirect | ||
github.com/xdg-go/stringprep v1.0.4 // indirect | ||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect | ||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.33.0 // indirect | ||
golang.org/x/crypto v0.31.0 // indirect | ||
golang.org/x/sync v0.10.0 // indirect | ||
golang.org/x/text v0.21.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
76 changes: 76 additions & 0 deletions
76
instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/go.sum
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= | ||
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= | ||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= | ||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= | ||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= | ||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= | ||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= | ||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= | ||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= | ||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= | ||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= | ||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= | ||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= | ||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= | ||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= | ||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= | ||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= | ||
go.mongodb.org/mongo-driver/v2 v2.0.0 h1:Jfd7XpdZa9yk3eY774bO7SWVb30noLSirL9nKTpavhI= | ||
go.mongodb.org/mongo-driver/v2 v2.0.0/go.mod h1:nSjmNq4JUstE8IRZKTktLgMHM4F1fccL6HGX1yh+8RA= | ||
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= | ||
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= | ||
go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw= | ||
go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I= | ||
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ= | ||
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M= | ||
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s= | ||
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= | ||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= | ||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= | ||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= | ||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= | ||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
150 changes: 150 additions & 0 deletions
150
instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/mongo.go
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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo" | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/codes" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.21.0" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"go.mongodb.org/mongo-driver/v2/bson" | ||
"go.mongodb.org/mongo-driver/v2/event" | ||
) | ||
|
||
type spanKey struct { | ||
ConnectionID string | ||
RequestID int64 | ||
} | ||
|
||
type monitor struct { | ||
sync.Mutex | ||
spans map[spanKey]trace.Span | ||
cfg config | ||
} | ||
|
||
func (m *monitor) Started(ctx context.Context, evt *event.CommandStartedEvent) { | ||
var spanName string | ||
|
||
hostname, port := peerInfo(evt) | ||
|
||
attrs := []attribute.KeyValue{ | ||
semconv.DBSystemMongoDB, | ||
semconv.DBOperation(evt.CommandName), | ||
semconv.DBName(evt.DatabaseName), | ||
semconv.NetPeerName(hostname), | ||
semconv.NetPeerPort(port), | ||
semconv.NetTransportTCP, | ||
} | ||
if !m.cfg.CommandAttributeDisabled { | ||
attrs = append(attrs, semconv.DBStatement(sanitizeCommand(evt.Command))) | ||
} | ||
if collection, err := extractCollection(evt); err == nil && collection != "" { | ||
spanName = collection + "." | ||
attrs = append(attrs, semconv.DBMongoDBCollection(collection)) | ||
} | ||
spanName += evt.CommandName | ||
opts := []trace.SpanStartOption{ | ||
trace.WithSpanKind(trace.SpanKindClient), | ||
trace.WithAttributes(attrs...), | ||
} | ||
_, span := m.cfg.Tracer.Start(ctx, spanName, opts...) | ||
key := spanKey{ | ||
ConnectionID: evt.ConnectionID, | ||
RequestID: evt.RequestID, | ||
} | ||
m.Lock() | ||
m.spans[key] = span | ||
m.Unlock() | ||
} | ||
|
||
func (m *monitor) Succeeded(ctx context.Context, evt *event.CommandSucceededEvent) { | ||
m.Finished(&evt.CommandFinishedEvent, nil) | ||
} | ||
|
||
func (m *monitor) Failed(ctx context.Context, evt *event.CommandFailedEvent) { | ||
m.Finished(&evt.CommandFinishedEvent, fmt.Errorf("%s", evt.Failure)) | ||
} | ||
|
||
func (m *monitor) Finished(evt *event.CommandFinishedEvent, err error) { | ||
key := spanKey{ | ||
ConnectionID: evt.ConnectionID, | ||
RequestID: evt.RequestID, | ||
} | ||
m.Lock() | ||
span, ok := m.spans[key] | ||
if ok { | ||
delete(m.spans, key) | ||
} | ||
m.Unlock() | ||
if !ok { | ||
return | ||
} | ||
|
||
if err != nil { | ||
span.SetStatus(codes.Error, err.Error()) | ||
} | ||
|
||
span.End() | ||
} | ||
|
||
// TODO sanitize values where possible, then re-enable `db.statement` span attributes default. | ||
// TODO limit maximum size. | ||
func sanitizeCommand(command bson.Raw) string { | ||
b, _ := bson.MarshalExtJSON(command, false, false) | ||
return string(b) | ||
} | ||
|
||
// extractCollection extracts the collection for the given mongodb command event. | ||
// For CRUD operations, this is the first key/value string pair in the bson | ||
// document where key == "<operation>" (e.g. key == "insert"). | ||
// For database meta-level operations, such a key may not exist. | ||
func extractCollection(evt *event.CommandStartedEvent) (string, error) { | ||
elt, err := evt.Command.IndexErr(0) | ||
if err != nil { | ||
return "", err | ||
} | ||
if key, err := elt.KeyErr(); err == nil && key == evt.CommandName { | ||
var v bson.RawValue | ||
if v, err = elt.ValueErr(); err != nil || v.Type != bson.TypeString { | ||
return "", err | ||
} | ||
return v.StringValue(), nil | ||
} | ||
return "", errors.New("collection name not found") | ||
} | ||
|
||
// NewMonitor creates a new mongodb event CommandMonitor. | ||
func NewMonitor(opts ...Option) *event.CommandMonitor { | ||
cfg := newConfig(opts...) | ||
m := &monitor{ | ||
spans: make(map[spanKey]trace.Span), | ||
cfg: cfg, | ||
} | ||
return &event.CommandMonitor{ | ||
Started: m.Started, | ||
Succeeded: m.Succeeded, | ||
Failed: m.Failed, | ||
} | ||
} | ||
|
||
func peerInfo(evt *event.CommandStartedEvent) (hostname string, port int) { | ||
hostname = evt.ConnectionID | ||
port = 27017 | ||
if idx := strings.IndexByte(hostname, '['); idx >= 0 { | ||
hostname = hostname[:idx] | ||
} | ||
if idx := strings.IndexByte(hostname, ':'); idx >= 0 { | ||
port = func(p int, e error) int { return p }(strconv.Atoi(hostname[idx+1:])) | ||
hostname = hostname[:idx] | ||
} | ||
return hostname, port | ||
} |
Oops, something went wrong.