Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"

import (
"go.mongodb.org/mongo-driver/event"

Check failure on line 7 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/config.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (goimports)
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

Expand All @@ -13,11 +15,13 @@

// config is used to configure the mongo tracer.
type config struct {
TracerProvider trace.TracerProvider

Tracer trace.Tracer

TracerProvider trace.TracerProvider
Tracer trace.Tracer
CommandAttributeDisabled bool

// CommandAttributesFn is a function that returns a list of attributes using
// the CommandStartedEvent. This is used to add custom attributes to the span.
CommandAttributesFn func(event *event.CommandStartedEvent) []attribute.KeyValue
}

// newConfig returns a config with all Options set.
Expand Down Expand Up @@ -66,3 +70,12 @@
cfg.CommandAttributeDisabled = disabled
})
}

// WithCommandAttributesFn specifies a function that returns a list of
// attributes using the CommandStartedEvent. This is used to add custom
// attributes to the span.
func WithCommandAttributesFn(fn func(event *event.CommandStartedEvent) []attribute.KeyValue) Option {

Check failure on line 77 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/config.go

View workflow job for this annotation

GitHub Actions / lint

import-shadowing: The name 'event' shadows an import name (revive)
return optionFunc(func(cfg *config) {
cfg.CommandAttributesFn = fn
})

Check warning on line 80 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/config.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/config.go#L77-L80

Added lines #L77 - L80 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
type AttributeOptions struct {
collectionName string
commandAttributeDisabled bool
commandAttributes []attribute.KeyValue
}

// AttributeOption is a function type that modifies AttributeOptions.
Expand All @@ -65,6 +66,13 @@
}
}

// WithCommandAttributes is a functional option to set command attributes.
func WithCommandAttributes(attrs ...attribute.KeyValue) AttributeOption {
return func(opts *AttributeOptions) {
opts.commandAttributes = attrs
}

Check warning on line 73 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go#L70-L73

Added lines #L70 - L73 were not covered by tests
}

// CommandStartedTraceAttrs generates trace attributes for a CommandStartedEvent
// based on the EventMonitor version.
func (m EventMonitor) CommandStartedTraceAttrs(
Expand Down Expand Up @@ -128,7 +136,13 @@
attrs = append(attrs, semconv1260.NetworkPeerAddress(net.JoinHostPort(hostname, strconv.Itoa(port))))

if !opts.commandAttributeDisabled {
attrs = append(attrs, semconv1260.DBQueryText(sanitizeCommand(evt.Command)))
if opts.commandAttributes != nil {
for _, attr := range opts.commandAttributes {

Check failure on line 140 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go

View workflow job for this annotation

GitHub Actions / lint

S1011: should replace loop with attrs = append(attrs, opts.commandAttributes...) (staticcheck)
attrs = append(attrs, attr)
}

Check warning on line 142 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go#L140-L142

Added lines #L140 - L142 were not covered by tests
} else {
attrs = append(attrs, semconv1260.DBQueryText(sanitizeCommand(evt.Command)))
}
}

if opts.collectionName != "" {
Expand Down Expand Up @@ -157,7 +171,13 @@
attrs = append(attrs, semconv1210.NetPeerName(hostname))

if !opts.commandAttributeDisabled {
attrs = append(attrs, semconv1210.DBStatement(sanitizeCommand(evt.Command)))
if opts.commandAttributes != nil {
for _, attr := range opts.commandAttributes {

Check failure on line 175 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go

View workflow job for this annotation

GitHub Actions / lint

S1011: should replace loop with attrs = append(attrs, opts.commandAttributes...) (staticcheck)
attrs = append(attrs, attr)
}

Check warning on line 177 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go#L175-L177

Added lines #L175 - L177 were not covered by tests
} else {
attrs = append(attrs, semconv1210.DBStatement(sanitizeCommand(evt.Command)))
}
}

if opts.collectionName != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
semconv.WithCommandAttributeDisabled(m.cfg.CommandAttributeDisabled),
}

if m.cfg.CommandAttributesFn != nil {
attrOptions = append(attrOptions, semconv.WithCommandAttributes(m.cfg.CommandAttributesFn(evt)...))
}

Check warning on line 40 in instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/mongo.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/mongo.go#L39-L40

Added lines #L39 - L40 were not covered by tests

var spanName string
if collection, err := extractCollection(evt); err == nil && collection != "" {
spanName = collection + "."
Expand Down
Loading