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 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 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
e9173c7
Resolve merge conflicts
prestonvasquez Dec 30, 2024
9d062b7
otelmongo#6171 Migrate to semconv pkg pattern
prestonvasquez Dec 30, 2024
94788c2
otelmongo#6171 Resolve merge conflicts
prestonvasquez Dec 30, 2024
3b7f9a2
Update CHANGELOG
prestonvasquez Dec 30, 2024
6dd8e88
Fix typos and wrap comments at 80
prestonvasquez Dec 30, 2024
6a7d4ce
Add licenses to semconv files
prestonvasquez Jan 2, 2025
cc503cb
Resolve linting issues
prestonvasquez Jan 3, 2025
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 @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Generate server metrics with semantic conventions v1.26 in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` when `OTEL_SEMCONV_STABILITY_OPT_IN` is set to `http/dup`. (#6411)

### 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)

## [1.33.0/0.58.0/0.27.0/0.13.0/0.8.0/0.6.0/0.5.0] - 2024-12-12

### Added
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
@@ -0,0 +1,166 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"net"
"os"
"strconv"
"strings"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/event"

"go.opentelemetry.io/otel/attribute"

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

// Constants for environment variable keys and versions.
const (
semconvOptIn = "OTEL_SEMCONV_STABILITY_OPT_IN"
semconvOptInDup = "mongo/dup"
semconvOptIn1260 = "mongo"
)

// EventMonitor is responsible for monitoring events with a specified semantic
// version.
type EventMonitor struct {
version string
}

// NewEventMonitor creates an EventMonitor with the version set based on the
// OTEL_SEMCONV_STABILITY_OPT_IN environment variable.
func NewEventMonitor() EventMonitor {
return EventMonitor{
version: strings.ToLower(os.Getenv(semconvOptIn)),
}
}

// AttributeOptions represents options for tracing attributes.
type AttributeOptions struct {
collectionName string
commandAttributeDisabled bool
}

// AttributeOption is a function type that modifies AttributeOptions.
type AttributeOption func(*AttributeOptions)

// WithCollectionName is a functional option to set the collection name in
// AttributeOptions.
func WithCollectionName(collName string) AttributeOption {
return func(opts *AttributeOptions) {
opts.collectionName = collName
}
}

// WithCommandAttributeDisabled is a functional option to enable or disable
// command attributes.
func WithCommandAttributeDisabled(disabled bool) AttributeOption {
return func(opts *AttributeOptions) {
opts.commandAttributeDisabled = disabled
}
}

// CommandStartedTraceAttrs generates trace attributes for a CommandStartedEvent
// based on the EventMonitor version.
func (m EventMonitor) CommandStartedTraceAttrs(
evt *event.CommandStartedEvent,
opts ...AttributeOption,
) []attribute.KeyValue {
switch m.version {
case semconvOptIn1260:
return commandStartedTraceAttrsV1260(evt, opts...)
case semconvOptInDup:
return append(commandStartedTraceAttrsV1260(evt, opts...), commandStartedTraceAttrsV1210(evt, opts...)...)
default:
return commandStartedTraceAttrsV1210(evt, opts...)
}
}

// peerInfo extracts the hostname and port from a CommandStartedEvent.
func peerInfo(evt *event.CommandStartedEvent) (hostname string, port int) {
hostname = evt.ConnectionID
port = 27017 // Default MongoDB port

host, portStr, err := net.SplitHostPort(hostname)
if err != nil {
// If there's an error (likely because there's no port), assume default port
// and use ConnectionID as hostname
return hostname, port
}

if parsedPort, err := strconv.Atoi(portStr); err == nil {
port = parsedPort
}

return host, port
}

// sanitizeCommand converts a BSON command to a sanitized JSON string.
// TODO: Sanitize values where possible and limit maximum size.
Copy link
Member

Choose a reason for hiding this comment

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

Having this as two different TODOs made it clearer that it's two different things that will be tackled separately.

func sanitizeCommand(command bson.Raw) string {
b, _ := bson.MarshalExtJSON(command, false, false)

return string(b)
}

// commandStartedTraceAttrsV1260 generates trace attributes for semantic version
// 1.26.0.
func commandStartedTraceAttrsV1260(evt *event.CommandStartedEvent, setters ...AttributeOption) []attribute.KeyValue {
opts := &AttributeOptions{}
for _, set := range setters {
set(opts)
}

attrs := []attribute.KeyValue{semconv1260.DBSystemMongoDB}

attrs = append(attrs, semconv1260.DBOperationName(evt.CommandName))
attrs = append(attrs, semconv1260.DBNamespace(evt.DatabaseName))
attrs = append(attrs, semconv1260.NetworkTransportTCP)

hostname, port := peerInfo(evt)
attrs = append(attrs, semconv1260.NetworkPeerPort(port))
attrs = append(attrs, semconv1260.NetworkPeerAddress(net.JoinHostPort(hostname, strconv.Itoa(port))))

if !opts.commandAttributeDisabled {
attrs = append(attrs, semconv1260.DBQueryText(sanitizeCommand(evt.Command)))
}

if opts.collectionName != "" {
attrs = append(attrs, semconv1260.DBCollectionName(opts.collectionName))
}

return attrs
}

// commandStartedTraceAttrsV1210 generates trace attributes for semantic version
// 1.21.0.
func commandStartedTraceAttrsV1210(evt *event.CommandStartedEvent, setters ...AttributeOption) []attribute.KeyValue {
opts := &AttributeOptions{}
for _, set := range setters {
set(opts)
}

attrs := []attribute.KeyValue{semconv1210.DBSystemMongoDB}

attrs = append(attrs, semconv1210.DBOperation(evt.CommandName))
attrs = append(attrs, semconv1210.DBName(evt.DatabaseName))
attrs = append(attrs, semconv1210.NetTransportTCP)

hostname, port := peerInfo(evt)
attrs = append(attrs, semconv1210.NetPeerPort(port))
attrs = append(attrs, semconv1210.NetPeerName(hostname))

if !opts.commandAttributeDisabled {
attrs = append(attrs, semconv1210.DBStatement(sanitizeCommand(evt.Command)))
}

if opts.collectionName != "" {
attrs = append(attrs, semconv1210.DBMongoDBCollection(opts.collectionName))
}

return attrs
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package semconv

import (
"net"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/event"

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

func TestNewEventMonitor(t *testing.T) {
tests := []struct {
name string
version string
want string
}{
{
name: "Default Version",
version: "",
want: "",
},
{
name: "Version 1260",
version: semconvOptIn1260,
want: "mongo",
},
{
name: "Duplicate Version",
version: semconvOptInDup,
want: "mongo/dup",
},
}

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

monitor := NewEventMonitor()
assert.Equal(t, test.want, monitor.version, "Expected version does not match")
})
}
}

func TestPeerInfo(t *testing.T) {
// Test cases for peerInfo
tests := []struct {
name string
connectionID string
wantHostname string
wantPort int
}{
{"No Port", "localhost", "localhost", 27017},
{"With Port", "localhost:12345", "localhost", 12345},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
evt := &event.CommandStartedEvent{ConnectionID: tt.connectionID}
hostname, port := peerInfo(evt)
assert.Equal(t, tt.wantHostname, hostname, "Hostname does not match")
assert.Equal(t, tt.wantPort, port, "Port does not match")
})
}
}

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

v1210 := []attribute.KeyValue{
semconv1210.DBSystemMongoDB,
{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{
semconv1260.DBSystemMongoDB,
{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: v1210,
},
{
name: "unsupported version",
initAttrs: []attribute.KeyValue{},
version: "mongo/foo",
want: v1210,
},
{
name: "mongo",
initAttrs: []attribute.KeyValue{},
version: "mongo",
want: v1260,
},
{
name: "mongo/dup",
initAttrs: []attribute.KeyValue{},
version: "mongo/dup",
want: append(v1210, v1260...),
},
}

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

stmtBytes, err := bson.Marshal(bson.D{{Key: "insert", Value: "users"}})
assert.NoError(t, err)

monitor := NewEventMonitor()
attrs := monitor.CommandStartedTraceAttrs(&event.CommandStartedEvent{
DatabaseName: dbNamespace,
CommandName: opName,
Command: bson.Raw(stmtBytes),
ConnectionID: net.JoinHostPort(host, strconv.FormatInt(int64(port), 10)),
}, WithCollectionName(coll))

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