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

GODRIVER-2572 Add log messages to Server selection spec #1325

Merged
merged 56 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
7b46fe0
update windows distro to vsCurrent-latest
prestonvasquez Jun 2, 2023
7bde5ae
Merge branch 'master' of github.com:mongodb/mongo-go-driver
prestonvasquez Jun 5, 2023
f7abd3c
GODRIVER-2742 set implementation
prestonvasquez Jun 8, 2023
92cb1bf
GODRIVER-2743 Add CompareUint32Ptr function
prestonvasquez Jun 13, 2023
4c95947
GODRIVER-2585 Initialize SDAM logging
prestonvasquez Jun 15, 2023
faca1cc
GODRIVER-2585 initialize heartbeat logging
prestonvasquez Jun 18, 2023
d7005b5
GODRIVER-2585 merge master
prestonvasquez Jun 21, 2023
5f4f342
GODRIVER-2585 Add remaining SDAM logging USTs
prestonvasquez Jun 22, 2023
9a9122d
GODRIVER-2585 Seperate server and topology component structs
prestonvasquez Jun 22, 2023
a0dfef3
GODRIER-2585 Revert changes to the internal logger
prestonvasquez Jun 22, 2023
c0c767f
GODRIVER-2585 Extend reduced HB frequency logic to operational clients
prestonvasquez Jun 23, 2023
85dbb56
GODRIVER-2585 Clean up comments
prestonvasquez Jun 23, 2023
2f73b7c
Merge branch 'master' into GODRIVER-2585
prestonvasquez Jun 23, 2023
1c05011
GODRIVER-2585 Remove empty space from comment
prestonvasquez Jun 23, 2023
4d75016
GODRIVER-2585 Fix static analysis failures
prestonvasquez Jun 23, 2023
d8eaf6c
GODRIVER-2585 add missing licenses
prestonvasquez Jun 28, 2023
27d61c0
Merge branch 'master' into GODRIVER-2742
prestonvasquez Jun 28, 2023
a7d6685
GODRIVER-2742 Add mongodcryptd process runner
prestonvasquez Jun 29, 2023
66bec31
GODRIVER-2742 Use internal compare function in server desc equal
prestonvasquez Jun 29, 2023
4299250
GODRIVER-2742 fix failures with session timout selection
prestonvasquez Jun 30, 2023
af250a1
GODRIVER-2742 Fix topology test failures
prestonvasquez Jun 30, 2023
5848ad5
GODRIVER-2742 More test failure fixes
prestonvasquez Jun 30, 2023
a333d3e
GODRIVER-2742 Convert mongocryptd prose test to mtest
prestonvasquez Jun 30, 2023
0696575
GODRIVER-2742 Use mtest and fix typos
prestonvasquez Jul 5, 2023
dcb8380
GODRIVER-2742 update mtest usage
prestonvasquez Jul 5, 2023
bdd4e8c
GODRIVER-2585 Add component tests
prestonvasquez Jul 6, 2023
8d4cd4c
GODRIVER-2585 Add missing licenses
prestonvasquez Jul 6, 2023
02b7657
GODRIVER-2585 Add comment to Print ack the message duplication
prestonvasquez Jul 7, 2023
8d91a00
Merge branch 'GODRIVER-2742' into GODRIVER-2572
prestonvasquez Jul 7, 2023
e291f7c
GODRIVER-2572 Add UST
prestonvasquez Jul 7, 2023
b2bff57
GODRIVER-2572 Merge SDAM logging for schema update
prestonvasquez Jul 7, 2023
f2e8a83
GODRIVER-2572 Initialize server selection logging
prestonvasquez Jul 10, 2023
fa90017
GODRIVER-2572 First round infrastructure changes
prestonvasquez Jul 15, 2023
5d96f7e
GODRIVER-2572 Start updating UST runner to include event matching
prestonvasquez Jul 18, 2023
46b62be
GODRIVER-2742 Review changes
prestonvasquez Jul 19, 2023
ccc4072
Update x/mongo/driver/topology/sdam_spec_test.go
prestonvasquez Jul 19, 2023
990f508
Update x/mongo/driver/topology/sdam_spec_test.go
prestonvasquez Jul 19, 2023
6ae5e38
GODRIVER-2572 Merge master
prestonvasquez Jul 19, 2023
1541fee
GODRIVER-2572 Remove safety guards that mask errors
prestonvasquez Jul 19, 2023
4a4bb38
GODRIVER-2572 Merge 2572
prestonvasquez Jul 19, 2023
735b497
GODRIVER-2572 Convert port when logging closed event
prestonvasquez Jul 19, 2023
43e4c3a
GODRIVER-2572 Clean up test use-case
prestonvasquez Jul 20, 2023
7e86550
GODRIVER-2572 Add the opereration names
prestonvasquez Jul 21, 2023
b8b84a9
GODRIVER-2572 Merge master
prestonvasquez Jul 26, 2023
ebf07b4
GODRIVER-2572 Remove uint32 comparison logic
prestonvasquez Jul 26, 2023
0fd270a
GODRIVER-2572 Clean up comments
prestonvasquez Jul 26, 2023
eaad735
GODRIVER-2572 Resolve merge conflicts
prestonvasquez Aug 2, 2023
2a1771b
GODRIVER-2572 Fix static analysis errors
prestonvasquez Aug 4, 2023
a474c8c
GODRIVER-2572 PR requests
prestonvasquez Aug 8, 2023
2bc53da
GODRIVER-2572 Resolve misaligned server selectors
prestonvasquez Aug 8, 2023
adf86c7
GODRIVER-2572 Fix static analysis errors
prestonvasquez Aug 8, 2023
cb497c0
GODRIVER-2572 Add licenses
prestonvasquez Aug 8, 2023
3da9902
GODRIVER-2572 Clean up code per review
prestonvasquez Aug 14, 2023
79894b8
GODRIVER-2572 Resolve merge conflict
prestonvasquez Aug 14, 2023
906d534
GODRIVER-2572 Update topology options to default load empty log options
prestonvasquez Aug 15, 2023
ffbfe15
GODRIVER-2572 Add correct options to log constructor
prestonvasquez Aug 15, 2023
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
31 changes: 31 additions & 0 deletions internal/driverutil/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package driverutil

// Operation Names should be sourced from the command reference documentation:
// https://www.mongodb.com/docs/manual/reference/command/
const (
AbortTransactionOp = "abortTransaction" // AbortTransactionOp is the name for aborting a transaction
AggregateOp = "aggregate" // AggregateOp is the name for aggregating
CommitTransactionOp = "commitTransaction" // CommitTransactionOp is the name for committing a transaction
CountOp = "count" // CountOp is the name for counting
CreateOp = "create" // CreateOp is the name for creating
CreateIndexesOp = "createIndexes" // CreateIndexesOp is the name for creating indexes
DeleteOp = "delete" // DeleteOp is the name for deleting
DistinctOp = "distinct" // DistinctOp is the name for distinct
DropOp = "drop" // DropOp is the name for dropping
DropDatabaseOp = "dropDatabase" // DropDatabaseOp is the name for dropping a database
DropIndexesOp = "dropIndexes" // DropIndexesOp is the name for dropping indexes
EndSessionsOp = "endSessions" // EndSessionsOp is the name for ending sessions
FindAndModifyOp = "findAndModify" // FindAndModifyOp is the name for finding and modifying
FindOp = "find" // FindOp is the name for finding
InsertOp = "insert" // InsertOp is the name for inserting
ListCollectionsOp = "listCollections" // ListCollectionsOp is the name for listing collections
ListIndexesOp = "listIndexes" // ListIndexesOp is the name for listing indexes
ListDatabasesOp = "listDatabases" // ListDatabasesOp is the name for listing databases
UpdateOp = "update" // UpdateOp is the name for updating
)
40 changes: 40 additions & 0 deletions internal/logger/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const (
ConnectionCheckoutFailed = "Connection checkout failed"
ConnectionCheckedOut = "Connection checked out"
ConnectionCheckedIn = "Connection checked in"
ServerSelectionFailed = "Server selection failed"
ServerSelectionStarted = "Server selection started"
ServerSelectionSucceeded = "Server selection succeeded"
ServerSelectionWaiting = "Waiting for suitable server to become available"
TopologyClosed = "Stopped topology monitoring"
TopologyDescriptionChanged = "Topology description changed"
TopologyOpening = "Starting topology monitoring"
Expand All @@ -53,21 +57,27 @@ const (
KeyMessage = "message"
KeyMinPoolSize = "minPoolSize"
KeyNewDescription = "newDescription"
KeyOperation = "operation"
KeyOperationID = "operationId"
KeyPreviousDescription = "previousDescription"
KeyRemainingTimeMS = "remainingTimeMS"
KeyReason = "reason"
KeyReply = "reply"
KeyRequestID = "requestId"
KeySelector = "selector"
KeyServerConnectionID = "serverConnectionId"
KeyServerHost = "serverHost"
KeyServerPort = "serverPort"
KeyServiceID = "serviceId"
KeyTimestamp = "timestamp"
KeyTopologyDescription = "topologyDescription"
KeyTopologyID = "topologyId"
)

// KeyValues is a list of key-value pairs.
type KeyValues []interface{}

// Add adds a key-value pair to an instance of a KeyValues list.
func (kvs *KeyValues) Add(key string, value interface{}) {
*kvs = append(*kvs, key, value)
}
Expand Down Expand Up @@ -252,6 +262,36 @@ func SerializeServer(srv Server, extraKV ...interface{}) KeyValues {
return keysAndValues
}

// ServerSelection contains data that all server selection messages MUST
// contain.
type ServerSelection struct {
Selector string
OperationID *int32
Operation string
TopologyDescription string
}

// SerializeServerSelection serializes a Topology message into a slice of keys
// and values that can be passed to a logger.
func SerializeServerSelection(srvSelection ServerSelection, extraKV ...interface{}) KeyValues {
keysAndValues := KeyValues{
KeySelector, srvSelection.Selector,
KeyOperation, srvSelection.Operation,
KeyTopologyDescription, srvSelection.TopologyDescription,
}

if srvSelection.OperationID != nil {
keysAndValues.Add(KeyOperationID, *srvSelection.OperationID)
}

// Add the optional keys and values.
for i := 0; i < len(extraKV); i += 2 {
keysAndValues.Add(extraKV[i].(string), extraKV[i+1])
}

return keysAndValues
}

// Topology contains data that all topology messages MAY contain.
type Topology struct {
ID primitive.ObjectID // Driver's unique ID for this topology
Expand Down
48 changes: 48 additions & 0 deletions internal/logger/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package logger

import "context"

// contextKey is a custom type used to prevent key collisions when using the
// context package.
type contextKey string

const (
contextKeyOperation contextKey = "operation"
contextKeyOperationID contextKey = "operationID"
)

// WithOperationName adds the operation name to the context.
func WithOperationName(ctx context.Context, operation string) context.Context {
return context.WithValue(ctx, contextKeyOperation, operation)
}

// WithOperationID adds the operation ID to the context.
func WithOperationID(ctx context.Context, operationID int32) context.Context {
return context.WithValue(ctx, contextKeyOperationID, operationID)
}

// OperationName returns the operation name from the context.
func OperationName(ctx context.Context) (string, bool) {
operationName := ctx.Value(contextKeyOperation)
if operationName == nil {
return "", false
}

return operationName.(string), true
}

// OperationID returns the operation ID from the context.
func OperationID(ctx context.Context) (int32, bool) {
operationID := ctx.Value(contextKeyOperationID)
if operationID == nil {
return 0, false
}

return operationID.(int32), true
}
187 changes: 187 additions & 0 deletions internal/logger/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package logger_test

import (
"context"
"testing"

"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/logger"
)

func TestContext_WithOperationName(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctx context.Context
opName string
ok bool
}{
{
name: "simple",
ctx: context.Background(),
opName: "foo",
ok: true,
},
}

for _, tt := range tests {
tt := tt // Capture the range variable.

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctx := logger.WithOperationName(tt.ctx, tt.opName)

opName, ok := logger.OperationName(ctx)
assert.Equal(t, tt.ok, ok)

if ok {
assert.Equal(t, tt.opName, opName)
}
})
}
}

func TestContext_OperationName(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctx context.Context
opName interface{}
ok bool
}{
{
name: "nil",
ctx: context.Background(),
opName: nil,
ok: false,
},
{
name: "string type",
ctx: context.Background(),
opName: "foo",
ok: true,
},
{
name: "non-string type",
ctx: context.Background(),
opName: int32(1),
ok: false,
},
}

for _, tt := range tests {
tt := tt // Capture the range variable.

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()

if opNameStr, ok := tt.opName.(string); ok {
ctx = logger.WithOperationName(tt.ctx, opNameStr)
}

opName, ok := logger.OperationName(ctx)
assert.Equal(t, tt.ok, ok)

if ok {
assert.Equal(t, tt.opName, opName)
}
})
}
}

func TestContext_WithOperationID(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctx context.Context
opID int32
ok bool
}{
{
name: "non-zero",
ctx: context.Background(),
opID: 1,
ok: true,
},
}

for _, tt := range tests {
tt := tt // Capture the range variable.

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctx := logger.WithOperationID(tt.ctx, tt.opID)

opID, ok := logger.OperationID(ctx)
assert.Equal(t, tt.ok, ok)

if ok {
assert.Equal(t, tt.opID, opID)
}
})
}
}

func TestContext_OperationID(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctx context.Context
opID interface{}
ok bool
}{
{
name: "nil",
ctx: context.Background(),
opID: nil,
ok: false,
},
{
name: "i32 type",
ctx: context.Background(),
opID: int32(1),
ok: true,
},
{
name: "non-i32 type",
ctx: context.Background(),
opID: "foo",
ok: false,
},
}

for _, tt := range tests {
tt := tt // Capture the range variable.

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()

if opIDI32, ok := tt.opID.(int32); ok {
ctx = logger.WithOperationID(tt.ctx, opIDI32)
}

opName, ok := logger.OperationID(ctx)
assert.Equal(t, tt.ok, ok)

if ok {
assert.Equal(t, tt.opID, opName)
}
})
}
}
2 changes: 2 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

// Package logger provides the internal logging solution for the MongoDB Go
// Driver.
package logger

import (
Expand Down
Loading
Loading