|
| 1 | +# Kevsoft.AWSXRayRecorder.Handlers.MongoDB [](https://github.com/kevbite/Kevsoft.AWSXRayRecorder.Handlers.MongoDB/actions/workflows/continuous-integration-workflow.yml) [](https://www.nuget.org/packages/Kevsoft.AWSXRayRecorder.Handlers.MongoDB) [](https://www.nuget.org/packages/Kevsoft.AWSXRayRecorder.Handlers.MongoDB) |
| 2 | + |
| 3 | +.NET library to trace MongoDB queries with AWS XRay |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +### Installing Package |
| 10 | + |
| 11 | +**Kevsoft.AWSXRayRecorder.Handlers.MongoDB** can be installed directly via the package manager console by executing the following commandlet: |
| 12 | + |
| 13 | +```powershell |
| 14 | +Install-Package Kevsoft.AWSXRayRecorder.Handlers.MongoDB |
| 15 | +``` |
| 16 | + |
| 17 | +alternative you can use the dotnet CLI. |
| 18 | + |
| 19 | +```bash |
| 20 | +dotnet add package Kevsoft.AWSXRayRecorder.Handlers.MongoDB |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### Configuring MongoDB Client |
| 26 | + |
| 27 | +When creating a MongoDB client you'll need to add some extra configuration with the `ConfigureXRay` extension method: |
| 28 | +```csharp |
| 29 | +var settings = new MongoClientSettings |
| 30 | + { |
| 31 | + Server = new MongoServerAddress("localhost") |
| 32 | + }.ConfigureXRay(); |
| 33 | +var mongoClient = new MongoClient(settings); |
| 34 | +``` |
| 35 | + |
| 36 | +You can also configure it straight from the MongoUrl: |
| 37 | + |
| 38 | +```csharp |
| 39 | +var mongoUrl = MongoUrl.Create("mongodb://localhost"); |
| 40 | +var settings = MongoClientSettings.FromUrl(mongoUrl) |
| 41 | + .ConfigureXRay(); |
| 42 | +var mongoClient = new MongoClient(settings); |
| 43 | +``` |
| 44 | + |
| 45 | +Once configured then the queries to MongoDB will be sampled based on your [XRay sampling configuration](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-dotnet-configuration.html#xray-sdk-dotnet-configuration-sampling). |
| 46 | + |
| 47 | +### Mongo XRay Options |
| 48 | + |
| 49 | +The `ConfigureXRay` extension method can accept an additional `MongoXRayOptions` object that allows you to configure what is traced to AWS XRay. |
| 50 | + |
| 51 | +#### Filtering Commands |
| 52 | + |
| 53 | +By default the library filters out the following MongoDB commands sent to the server: |
| 54 | +- buildInfo |
| 55 | +- getLastError |
| 56 | +- isMaster |
| 57 | +- ping |
| 58 | +- saslStart |
| 59 | +- saslContinue |
| 60 | + |
| 61 | +If you require these to commands to be traced, you can clear (or remove) the filtered commands: |
| 62 | +```csharp |
| 63 | +var mongoXRayOptions = new MongoXRayOptions(); |
| 64 | +mongoXRayOptions.FilteredCommands.Clear(); |
| 65 | + |
| 66 | +var mongoClientSettings = new MongoClientSettings() |
| 67 | + .ConfigureXRay(mongoXRayOptions); |
| 68 | +``` |
| 69 | + |
| 70 | +Additional if you want to omit a command when tracing you can add an extra command, for example if we want to omit the `find` command. |
| 71 | + |
| 72 | +```csharp |
| 73 | +var mongoXRayOptions = new MongoXRayOptions(); |
| 74 | +mongoXRayOptions.FilteredCommands.Add("find"); |
| 75 | + |
| 76 | +var mongoClientSettings = new MongoClientSettings() |
| 77 | + .ConfigureXRay(mongoXRayOptions); |
| 78 | +``` |
| 79 | + |
| 80 | +#### Disabling Command Text |
| 81 | + |
| 82 | +By default the library add the command to the AWS XRay annotations, however, if you're sending across secret information you might want to disable this. |
| 83 | +```csharp |
| 84 | +var mongoXRayOptions = new MongoXRayOptions |
| 85 | +{ |
| 86 | + EnableMongoCommandTextInstrumentation = false |
| 87 | +}; |
| 88 | + |
| 89 | +var mongoClientSettings = new MongoClientSettings() |
| 90 | + .ConfigureXRay(mongoXRayOptions); |
| 91 | +``` |
| 92 | + |
| 93 | +#### Adjusting Max Query Time |
| 94 | + |
| 95 | +The library has to keep trace of currently inflight queries, however, this could be come problematic if no query every finishes or spans for days. The library sets a sensible default of 4 hours, but you can change this by settings the `MaxQueryTime` on the settings: |
| 96 | +```csharp |
| 97 | +var mongoXRayOptions = new MongoXRayOptions |
| 98 | +{ |
| 99 | + MaxQueryTime = TimeSpan.FromSeconds(30) |
| 100 | +}; |
| 101 | + |
| 102 | +var mongoClientSettings = new MongoClientSettings() |
| 103 | + .ConfigureXRay(mongoXRayOptions); |
| 104 | +``` |
| 105 | + |
| 106 | +## Samples |
| 107 | + |
| 108 | +The [samples](samples/) folder containers examples of how you could use the Library. |
| 109 | + |
| 110 | +## Contributing |
| 111 | + |
| 112 | +1. Issue |
| 113 | +1. Fork |
| 114 | +1. Hack! |
| 115 | +1. Pull Request |
| 116 | + |
0 commit comments