Skip to content

Commit

Permalink
Add WithCommandLoggingDisabled to otelmongo tracer (#723)
Browse files Browse the repository at this point in the history
* Add WithCommandLoggingDisabled to otelmongo tracer so the user can opt-out of tracing the mongo statements

* Update instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/config.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update CHANGELOG.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/config.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* mr comments

* Update instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/config.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* remove adding CGO_ENABLED=1

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
davidgwcurve and MrAlias authored Apr 8, 2021
1 parent ae2c628 commit 5b82c08
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Changed

- The `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo` instrumentation now accepts a `WithCommandAttributeDisabled`,
so the caller can specify whether to opt-out of tracing the mongo command. (#712)

## [0.19.0] - 2021-03-19

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type config struct {
TracerProvider trace.TracerProvider

Tracer trace.Tracer

CommandAttributeDisabled bool
}

// newConfig returns a config with all Options set.
Expand Down Expand Up @@ -55,3 +57,11 @@ func WithTracerProvider(provider trace.TracerProvider) Option {
cfg.TracerProvider = provider
}
}

// WithCommandAttributeDisabled specifies if the MongoDB command is added as an attribute to Spans or not.
// The MongoDB command will be added as an attribute to Spans by default if this option is not provided.
func WithCommandAttributeDisabled(disabled bool) Option {
return func(cfg *config) {
cfg.CommandAttributeDisabled = disabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ type monitor struct {

func (m *monitor) Started(ctx context.Context, evt *event.CommandStartedEvent) {
hostname, port := peerInfo(evt)
b, _ := bson.MarshalExtJSON(evt.Command, false, false)

attrs := []attribute.KeyValue{
ServiceName(m.serviceName),
DBOperation(evt.CommandName),
DBInstance(evt.DatabaseName),
DBStatement(string(b)),
DBSystem("mongodb"),
PeerHostname(hostname),
PeerPort(port),
}
if !m.cfg.CommandAttributeDisabled {
b, _ := bson.MarshalExtJSON(evt.Command, false, false)
attrs = append(attrs, DBStatement(string(b)))
}

opts := []trace.SpanOption{
trace.WithAttributes(attrs...),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,64 @@ func TestMain(m *testing.M) {
}

func Test(t *testing.T) {
sr := new(oteltest.SpanRecorder)
provider := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
tt := []struct {
title string
commandAttributeDisabled bool
}{
{
title: "should successfully trace spans",
commandAttributeDisabled: false,
},
{
title: "should successfully trace spans without the statement when command logging is disabled",
commandAttributeDisabled: true,
},
}

hostname, port := "localhost", "27017"
for _, tc := range tt {
t.Run(tc.title, func(t *testing.T) {
sr := new(oteltest.SpanRecorder)
provider := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))

ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
hostname, port := "localhost", "27017"

ctx, span := provider.Tracer(defaultTracerName).Start(ctx, "mongodb-test")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

addr := "mongodb://localhost:27017/?connect=direct"
opts := options.Client()
opts.Monitor = NewMonitor("mongo", WithTracerProvider(provider))
opts.ApplyURI(addr)
client, err := mongo.Connect(ctx, opts)
if err != nil {
t.Fatal(err)
}
ctx, span := provider.Tracer(defaultTracerName).Start(ctx, "mongodb-test")

_, err = client.Database("test-database").Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}})
if err != nil {
t.Fatal(err)
}
addr := "mongodb://localhost:27017/?connect=direct"
opts := options.Client()
opts.Monitor = NewMonitor("mongo", WithTracerProvider(provider), WithCommandAttributeDisabled(tc.commandAttributeDisabled))
opts.ApplyURI(addr)
client, err := mongo.Connect(ctx, opts)
if err != nil {
t.Fatal(err)
}

span.End()
_, err = client.Database("test-database").Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}})
if err != nil {
t.Fatal(err)
}

spans := sr.Completed()
assert.Len(t, spans, 2)
assert.Equal(t, spans[0].SpanContext().TraceID, spans[1].SpanContext().TraceID)
span.End()

s := spans[0]
assert.Equal(t, "mongo", s.Attributes()[ServiceNameKey].AsString())
assert.Equal(t, "insert", s.Attributes()[DBOperationKey].AsString())
assert.Equal(t, hostname, s.Attributes()[PeerHostnameKey].AsString())
assert.Equal(t, port, s.Attributes()[PeerPortKey].AsString())
assert.Contains(t, s.Attributes()[DBStatementKey].AsString(), `"test-item":"test-value"`)
assert.Equal(t, "test-database", s.Attributes()[DBInstanceKey].AsString())
assert.Equal(t, "mongodb", s.Attributes()[DBSystemKey].AsString())
spans := sr.Completed()
assert.Len(t, spans, 2)
assert.Equal(t, spans[0].SpanContext().TraceID, spans[1].SpanContext().TraceID)

s := spans[0]
assert.Equal(t, "mongo", s.Attributes()[ServiceNameKey].AsString())
assert.Equal(t, "insert", s.Attributes()[DBOperationKey].AsString())
assert.Equal(t, hostname, s.Attributes()[PeerHostnameKey].AsString())
assert.Equal(t, port, s.Attributes()[PeerPortKey].AsString())
if tc.commandAttributeDisabled {
assert.NotContains(t, s.Attributes()[DBStatementKey].AsString(), `"test-item":"test-value"`)
} else {
assert.Contains(t, s.Attributes()[DBStatementKey].AsString(), `"test-item":"test-value"`)
}
assert.Equal(t, "test-database", s.Attributes()[DBInstanceKey].AsString())
assert.Equal(t, "mongodb", s.Attributes()[DBSystemKey].AsString())
})
}
}

0 comments on commit 5b82c08

Please sign in to comment.