-
Notifications
You must be signed in to change notification settings - Fork 582
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
prestonvasquez
wants to merge
32
commits into
open-telemetry:main
Choose a base branch
from
prestonvasquez:otelmongo#6171
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+452
−57
Open
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 f7449ef
Merge branch 'main' into otelmongo#6171
prestonvasquez e075e01
otelmongo#6171 update changelog
prestonvasquez c863b33
Merge branch 'otelmongo#6171' of github.com:prestonvasquez/openteleme…
prestonvasquez 6b5056e
Merge branch 'main' into otelmongo#6171
prestonvasquez 3db421e
otelmongo#6171 Add semconv registry
prestonvasquez a2bc652
Merge branch 'otelmongo#6171' of github.com:prestonvasquez/openteleme…
prestonvasquez 2eef3fe
otelmongo#6171 Specify generic types
prestonvasquez aaa6129
otelmongo#6171 Remove profiling files
prestonvasquez bf676a1
Update CHANGELOG.md
prestonvasquez c5b3f3c
otelmongo#6171 clean up tests
prestonvasquez d0ec780
Merge branch 'otelmongo#6171' of github.com:prestonvasquez/openteleme…
prestonvasquez b42dac7
otelmongo#6171 Resolve merge conflicts
prestonvasquez 9aed9c9
otelmongo#6171 udpate changelog
prestonvasquez 00ad843
otelmongo#6171 use goimports to format testfile
prestonvasquez 6ebd6d2
otelmongo#6171 Run precommit
prestonvasquez 91a4e29
Remove registry pattern
prestonvasquez d7f792f
Update docs and changelog
prestonvasquez fd6c1f6
Remove version
prestonvasquez b140c55
Add license
prestonvasquez 3fd3835
Merge main
prestonvasquez 3777228
Use switch statement for tcp
prestonvasquez e9b22bd
Merge branch 'main' into otelmongo#6171
prestonvasquez 39b5893
Move CHANGELOG
prestonvasquez 6de6ad5
run precommit
prestonvasquez e9173c7
Resolve merge conflicts
prestonvasquez 9d062b7
otelmongo#6171 Migrate to semconv pkg pattern
prestonvasquez 94788c2
otelmongo#6171 Resolve merge conflicts
prestonvasquez 3b7f9a2
Update CHANGELOG
prestonvasquez 6dd8e88
Fix typos and wrap comments at 80
prestonvasquez 6a7d4ce
Add licenses to semconv files
prestonvasquez cc503cb
Resolve linting issues
prestonvasquez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
166 changes: 166 additions & 0 deletions
166
...rumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.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,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. | ||
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 | ||
} |
158 changes: 158 additions & 0 deletions
158
...tation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor_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,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) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.