Skip to content

Commit

Permalink
Remove registry pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Nov 13, 2024
2 parents 64d6d83 + 6ebd6d2 commit 91a4e29
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- The function signature of `NewLogProcessor` in `go.opentelemetry.io/contrib/processors/minsev` has changed to accept the added `Severitier` interface instead of a `log.Severity`. (#6116)
<<<<<<< HEAD
- Updated `go.opentelemetry.io/contrib/config` to use the [v0.3.0](https://github.com/open-telemetry/opentelemetry-configuration/releases/tag/v0.3.0) release of schema which includes backwards incompatible changes. (#6126)
=======
- Upgrade the `go.opentelemetry.io/otel/semconv/v1.17.0` dependency to `go.opentelemetry.io/otel/semconv/v1.26.0` in `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo`. (#6172)
- Rename attributes `db.operation` to `db.operation.name`, `db.name` to `db.namespace`, `net.peer.port` to `network.peer.port`, `net.transport` to `network.transport` and `db.statement` to `db.query.text` in `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo`. (#6172)
>>>>>>> 6ebd6d2b67e08bc6950647fe5efe45a55f5cba32
- `NewSDK` in `go.opentelemetry.io/contrib/config` now returns a no-op SDK if `disabled` is set to `true`. (#6185)
- The deprecated `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho` package has found a Code Owner.
The package is no longer deprecated. (#6207)
Expand Down
19 changes: 16 additions & 3 deletions instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@
// 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
// 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
// - 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
//
// The "OTEL_SEMCONV_STABILITY_OPT_IN" environment variable can be used to opt
// into semconv/v1.26.0:
// - "mongo/v1.26.0": emit v1.26.0 semantic conventions
// - "mongo/v1.17.0": emit v1.17.0 (default) semantic conventions
// - "mongo/dup": emit the stable version (v1.17.0) and all other supported semantic conventions
//
// "mongo/dup" takes precedence over "mongo/v*". By default, otelmongo only emits v1.17.0.
//
// For example, the following will use v1.26.0 for otelmongo and duplicate
// attributes (old + new) for otelhttp:
//
// export OTEL_SEMCONV_STABILITY_OPT_IN="mongo/v1.26.0,http/dup"
package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ module go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/m
go 1.22

require (
github.com/stretchr/testify v1.9.0
go.mongodb.org/mongo-driver v1.17.1
go.opentelemetry.io/otel v1.31.0
go.opentelemetry.io/otel/trace v1.31.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/montanaflynn/stats v0.7.1 // 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
Expand All @@ -22,4 +25,5 @@ require (
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mong
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace"

"go.mongodb.org/mongo-driver/bson"
Expand All @@ -31,25 +32,28 @@ type monitor struct {
}

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,
}
attrs := []attribute.KeyValue{semconv.DBSystemMongoDB}

attrs = appendOpNameAttrs(attrs, evt.CommandName)
attrs = appendDBNamespace(attrs, evt.DatabaseName)
attrs = appendNetworkPort(attrs, port)
attrs = appendNetworkHost(attrs, hostname)
attrs = appendNetworkAddress(attrs, net.JoinHostPort(hostname, strconv.Itoa(port)))
attrs = appendNetworkTransport(attrs)

if !m.cfg.CommandAttributeDisabled {
attrs = append(attrs, semconv.DBStatement(sanitizeCommand(evt.Command)))
attrs = appendDBStatement(attrs, sanitizeCommand(evt.Command))
}

var spanName string
if collection, err := extractCollection(evt); err == nil && collection != "" {
spanName = collection + "."
attrs = append(attrs, semconv.DBMongoDBCollection(collection))

attrs = appendCollection(attrs, collection)
}

spanName += evt.CommandName
opts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"

import (
"os"

"go.opentelemetry.io/otel/attribute"
semconv1170 "go.opentelemetry.io/otel/semconv/v1.17.0"
semconv1260 "go.opentelemetry.io/otel/semconv/v1.26.0"
)

const (
semconvOptIn = "OTEL_SEMCONV_STABILITY_OPT_IN"
semconvOptIn1260 = "mongo/v1.26.0"
semconvOptInDup = "mongo/dup"
)

func appendAttrs[T string | int](
attrs []attribute.KeyValue,
semconvMap1170 func(T) attribute.KeyValue,
semconvMap1260 func(T) attribute.KeyValue,
val T,
) []attribute.KeyValue {
switch os.Getenv(semconvOptIn) {
case semconvOptIn1260:
if semconvMap1260 != nil {
attrs = append(attrs, semconvMap1260(val))
}
case semconvOptInDup:
if semconvMap1170 != nil {
attrs = append(attrs, semconvMap1170(val))
}

if semconvMap1260 != nil {
attrs = append(attrs, semconvMap1260(val))
}
default:
if semconvMap1170 != nil {
attrs = append(attrs, semconvMap1170(val))
}
}

return attrs
}

func appendOpNameAttrs(attrs []attribute.KeyValue, op string) []attribute.KeyValue {
return appendAttrs(attrs, semconv1170.DBOperation, semconv1260.DBOperationName, op)
}

func appendDBNamespace(attrs []attribute.KeyValue, ns string) []attribute.KeyValue {
return appendAttrs(attrs, semconv1170.DBName, semconv1260.DBNamespace, ns)
}

func appendDBStatement(attrs []attribute.KeyValue, stmt string) []attribute.KeyValue {
return appendAttrs(attrs, semconv1170.DBStatement, semconv1260.DBQueryText, stmt)
}

func appendNetworkPort(attrs []attribute.KeyValue, p int) []attribute.KeyValue {
return appendAttrs(attrs, semconv1170.NetPeerPort, semconv1260.NetworkPeerPort, p)
}

func appendNetworkHost(attrs []attribute.KeyValue, h string) []attribute.KeyValue {
return appendAttrs(attrs, semconv1170.NetPeerName, nil, h)
}

func appendNetworkAddress(attrs []attribute.KeyValue, addr string) []attribute.KeyValue {
return appendAttrs(attrs, nil, semconv1260.NetworkPeerAddress, addr)
}

func appendNetworkTransport(attrs []attribute.KeyValue) []attribute.KeyValue {
optIn := os.Getenv(semconvOptIn)
useSemconv1260 := optIn == semconvOptIn1260
useSemconvDup := optIn == semconvOptInDup

if useSemconv1260 || useSemconvDup {
attrs = append(attrs, semconv1260.NetworkTransportTCP)
}

if !useSemconv1260 || useSemconvDup {
attrs = append(attrs, semconv1170.NetTransportTCP)
}

return attrs
}

func appendCollection(attrs []attribute.KeyValue, coll string) []attribute.KeyValue {
return appendAttrs(attrs, semconv1170.DBMongoDBCollection, semconv1260.DBCollectionName, coll)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelmongo

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/attribute"
)

func Test_appendOpNameAttrs(t *testing.T) {
const (
opName = "opName"
dbNamespace = "dbNamespace"
port = 1
host = "host"
address = "host:1"
stmt = `{insert: "users"}`
coll = "coll"
)

v1170 := []attribute.KeyValue{
{Key: "db.operation", Value: attribute.StringValue(opName)},
{Key: "db.name", Value: attribute.StringValue(dbNamespace)},
{Key: "db.statement", Value: attribute.StringValue(stmt)},
{Key: "net.peer.port", Value: attribute.IntValue(port)},
{Key: "net.peer.name", Value: attribute.StringValue(host)},
{Key: "net.transport", Value: attribute.StringValue("ip_tcp")},
{Key: "db.mongodb.collection", Value: attribute.StringValue("coll")},
}

v1260 := []attribute.KeyValue{
{Key: "db.operation.name", Value: attribute.StringValue(opName)},
{Key: "db.namespace", Value: attribute.StringValue(dbNamespace)},
{Key: "db.query.text", Value: attribute.StringValue(stmt)},
{Key: "network.peer.port", Value: attribute.IntValue(port)},
{Key: "network.peer.address", Value: attribute.StringValue(address)},
{Key: "network.transport", Value: attribute.StringValue("tcp")},
{Key: "db.collection.name", Value: attribute.StringValue("coll")},
}

tests := []struct {
name string
initAttrs []attribute.KeyValue
version string
want []attribute.KeyValue
}{
{
name: "no version",
initAttrs: []attribute.KeyValue{},
version: "",
want: v1170,
},
{
name: "unsupported version",
initAttrs: []attribute.KeyValue{},
version: "mongo/foo",
want: v1170,
},
{
name: "mongo/v1.26.0",
initAttrs: []attribute.KeyValue{},
version: "mongo/v1.26.0",
want: v1260,
},
{
name: "mongo/dup",
initAttrs: []attribute.KeyValue{},
version: "mongo/dup",
want: append(v1170, v1260...),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Setenv(semconvOptIn, test.version)

attrs := appendOpNameAttrs(test.initAttrs, opName)
attrs = appendDBNamespace(attrs, dbNamespace)
attrs = appendDBStatement(attrs, stmt)
attrs = appendNetworkPort(attrs, port)
attrs = appendNetworkHost(attrs, host)
attrs = appendNetworkAddress(attrs, address)
attrs = appendNetworkTransport(attrs)
attrs = appendCollection(attrs, coll)

assert.ElementsMatch(t, test.want, attrs)
})
}
}

func Benchmark_appendAttrs(b *testing.B) {
ini := []attribute.KeyValue{}

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
ini = appendOpNameAttrs(ini, "opName")
ini = appendDBNamespace(ini, "dbNamespace")
ini = appendDBStatement(ini, `{insert: "users"}`)
ini = appendNetworkPort(ini, 1)
ini = appendNetworkHost(ini, "host")
ini = appendNetworkAddress(ini, "host:1")
ini = appendNetworkTransport(ini)
ini = appendCollection(ini, "coll")
}
}
Loading

0 comments on commit 91a4e29

Please sign in to comment.