Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(attributes) bump semconv to 1.26.0 #6172

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
95348f2
otelmongo#6171 bump semconv to 1.26.0
prestonvasquez Oct 1, 2024
f7449ef
Merge branch 'main' into otelmongo#6171
prestonvasquez Oct 1, 2024
e075e01
otelmongo#6171 update changelog
prestonvasquez Oct 1, 2024
c863b33
Merge branch 'otelmongo#6171' of github.com:prestonvasquez/openteleme…
prestonvasquez Oct 1, 2024
6b5056e
Merge branch 'main' into otelmongo#6171
prestonvasquez Oct 2, 2024
3db421e
otelmongo#6171 Add semconv registry
prestonvasquez Oct 3, 2024
a2bc652
Merge branch 'otelmongo#6171' of github.com:prestonvasquez/openteleme…
prestonvasquez Oct 3, 2024
2eef3fe
otelmongo#6171 Specify generic types
prestonvasquez Oct 4, 2024
aaa6129
otelmongo#6171 Remove profiling files
prestonvasquez Oct 4, 2024
bf676a1
Update CHANGELOG.md
prestonvasquez Oct 4, 2024
c5b3f3c
otelmongo#6171 clean up tests
prestonvasquez Oct 4, 2024
d0ec780
Merge branch 'otelmongo#6171' of github.com:prestonvasquez/openteleme…
prestonvasquez Oct 4, 2024
b42dac7
otelmongo#6171 Resolve merge conflicts
prestonvasquez Oct 4, 2024
9aed9c9
otelmongo#6171 udpate changelog
prestonvasquez Oct 4, 2024
00ad843
otelmongo#6171 use goimports to format testfile
prestonvasquez Oct 4, 2024
6ebd6d2
otelmongo#6171 Run precommit
prestonvasquez Oct 4, 2024
91a4e29
Remove registry pattern
prestonvasquez Nov 13, 2024
d7f792f
Update docs and changelog
prestonvasquez Nov 13, 2024
fd6c1f6
Remove version
prestonvasquez Nov 13, 2024
b140c55
Add license
prestonvasquez Nov 13, 2024
3fd3835
Merge main
prestonvasquez Nov 13, 2024
3777228
Use switch statement for tcp
prestonvasquez Nov 13, 2024
e9b22bd
Merge branch 'main' into otelmongo#6171
prestonvasquez Nov 19, 2024
39b5893
Move CHANGELOG
prestonvasquez Nov 19, 2024
6de6ad5
run precommit
prestonvasquez Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added support for providing `endpoint`, `pollingIntervalMs` and `initialSamplingRate` using environment variable `OTEL_TRACES_SAMPLER_ARG` in `go.opentelemetry.io/contrib/samples/jaegerremote`. (#6310)
- Added support exporting logs via OTLP over gRPC in `go.opentelemetry.io/contrib/config`. (#6340)

### Changed

- Support both `go.opentelemetry.io/otel/semconv/v1.21.0` (default) and `go.opentelemetry.io/otel/semconv/v1.26.0` (opt-in) in `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo` per the [Database semantic convention stability migration guide](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/db-migration.md#database-semantic-convention-stability-migration-guide) (#6172)

### Fixed

- Fixed the value for configuring the OTLP exporter to use `grpc` instead of `grpc/protobuf` in `go.opentelemetry.io/contrib/config`. (#6338)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
// 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": emit v1.26.0 semantic conventions
// - "": emit v1.21.0 (default) semantic conventions
// - "mongo/dup": emit v1.21.0 (default) and v1.26.0
//
// By default, otelmongo only emits v1.21.0.
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.32.0
go.opentelemetry.io/otel/trace v1.32.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.29.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/text v0.20.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 @@ -7,13 +7,14 @@ import (
"context"
"errors"
"fmt"
"net"
"strconv"
"strings"
"sync"

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

"go.mongodb.org/mongo-driver/bson"
Expand All @@ -32,25 +33,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than creating one method for each attribute, how about one method which returns all attributes, and can decide their key name.
Kind of like what the otelhttp library does, where we define two structs, one for the old and another for the new version.
https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/instrumentation/net/http/otelhttp/internal/semconv

I would also move the logic into an internal package, for a better separation of concerns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting idea. If I understand correctly, you're suggesting we define two separate command monitors specifically for the semantic conventions. Could you explain the benefits of this pattern over the more atomic append* solution proposed in the PR? The goal of the existing solution is to minimize code changes when advancing the library to a desired version, while also avoiding duplication in the monitoring logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your solution works if some values are renamed. But if a new value is added, or one is removed then, it becomes trickier to handle since the root method has to know about everything.

With one struct for each semconv version, that struct knows exactly what to do for that version, and that's it. There aren't two versions mixed.
It would also avoid having to define one method for each attribute.

Finally, doing the separation this way also makes it easier to add a third version if we someday need it (I really hope we don't).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At last using the same patterns in multiple modules help in maintenance of the repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmathieu Here are my arguments for the factory pattern over struct duplication:

if a new value is added, or one is removed then, it becomes trickier to handle since the root method has to know about everything.

In this case you would just add nil to the unused version, networkHost for example:

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

It would also avoid having to define one method for each attribute.

This is pretty low complexity, exchanging 1 line of code for 3 to avoid refactoring the library.

Finally, doing the separation this way also makes it easier to add a third version if we someday need it (I really hope we don't).

By design, there can never be a third version. The clarification of this point can be found here: open-telemetry/semantic-conventions#1502. Additionally, this logic should be rolled back entirely once we release a stable version:

we only considered using OTEL_SEMCONV_STABILITY_OPT_IN for the initial unstable to stable migration, and not subsequent major version bumps from stable -> (breaking) stable

Copy link
Member

@pellared pellared Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prestonvasquez, please first create a PR which refactors otelhttp. We need to try using similar patterns across the codebase to make it easier to maintain and more readable. Ideally the pattern should be described in https://github.com/open-telemetry/opentelemetry-go/blob/main/CONTRIBUTING.md#style-guide.


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,88 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"

import (
"os"

"go.opentelemetry.io/otel/attribute"
semconv1210 "go.opentelemetry.io/otel/semconv/v1.21.0"
semconv1260 "go.opentelemetry.io/otel/semconv/v1.26.0"
)

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

func appendAttrs[T string | int](
attrs []attribute.KeyValue,
semconvMap1210 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 semconvMap1210 != nil {
attrs = append(attrs, semconvMap1210(val))
}

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

return attrs
}

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

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

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

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

func appendNetworkHost(attrs []attribute.KeyValue, h string) []attribute.KeyValue {
return appendAttrs(attrs, semconv1210.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 {
switch os.Getenv(semconvOptIn) {
case semconvOptIn1260:
attrs = append(attrs, semconv1260.NetworkTransportTCP)
case semconvOptInDup:
attrs = append(attrs, semconv1260.NetworkTransportTCP)
fallthrough
default:
attrs = append(attrs, semconv1210.NetTransportTCP)
}

return attrs
}

func appendCollection(attrs []attribute.KeyValue, coll string) []attribute.KeyValue {
return appendAttrs(attrs, semconv1210.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",
initAttrs: []attribute.KeyValue{},
version: "mongo",
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
Loading