Skip to content

Commit 0931d85

Browse files
committed
Adding readme
1 parent 792f226 commit 0931d85

File tree

5 files changed

+130
-6
lines changed

5 files changed

+130
-6
lines changed

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Kevsoft.AWSXRayRecorder.Handlers.MongoDB [![Continuous Integration Workflow](https://github.com/kevbite/Kevsoft.AWSXRayRecorder.Handlers.MongoDB/actions/workflows/continuous-integration-workflow.yml/badge.svg)](https://github.com/kevbite/Kevsoft.AWSXRayRecorder.Handlers.MongoDB/actions/workflows/continuous-integration-workflow.yml) [![install from nuget](http://img.shields.io/nuget/v/Kevsoft.AWSXRayRecorder.Handlers.MongoDB.svg?style=flat-square)](https://www.nuget.org/packages/Kevsoft.AWSXRayRecorder.Handlers.MongoDB) [![downloads](http://img.shields.io/nuget/dt/Kevsoft.AWSXRayRecorder.Handlers.MongoDB.svg?style=flat-square)](https://www.nuget.org/packages/Kevsoft.AWSXRayRecorder.Handlers.MongoDB)
2+
3+
.NET library to trace MongoDB queries with AWS XRay
4+
5+
![XRay Trace Map](assets/xray-trace-map.png "XRay Trace Map")
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+

assets/xray-trace-map.png

14 KB
Loading

samples/AspNetCoreWebAPI/Controllers/ExampleController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
22
using Kevsoft.AWSXRayRecorder.Handlers.MongoDB;
33
using Microsoft.AspNetCore.Mvc;
44
using MongoDB.Bson;
@@ -14,7 +14,7 @@ public class ExampleController : ControllerBase
1414
[HttpGet]
1515
public async Task<string> Get()
1616
{
17-
var settings = XRayMongoClientSettingsConfigurator.Configure(new MongoClientSettings { }, new MongoXRayOptions());
17+
var settings = XRayMongoClientSettingsConfigurator.ConfigureXRay(new MongoClientSettings { }, new MongoXRayOptions());
1818

1919
var mongoClient = new MongoClient(settings);
2020

src/Kevsoft.AWSXRayRecorder.Handlers.MongoDB/XRayMongoClientSettingsConfigurator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@ public static class XRayMongoClientSettingsConfigurator
2121

2222
private static MongoXRayOptions _settings = new();
2323

24+
/// <summary>
25+
/// Configures the mongo client settings to trace with XRay
26+
/// </summary>
27+
/// <param name="mongoDbSettings"></param>
28+
/// <returns></returns>
29+
public static MongoClientSettings ConfigureXRay(this MongoClientSettings mongoDbSettings)
30+
{
31+
return ConfigureXRay(mongoDbSettings, new MongoXRayOptions());
32+
}
2433
/// <summary>
2534
/// Configures the mongo client settings to trace with XRay
2635
/// </summary>
2736
/// <param name="mongoDbSettings"></param>
2837
/// <param name="settings"></param>
2938
/// <returns></returns>
30-
public static MongoClientSettings Configure(MongoClientSettings mongoDbSettings, MongoXRayOptions settings)
39+
public static MongoClientSettings ConfigureXRay(this MongoClientSettings mongoDbSettings, MongoXRayOptions settings)
3140
{
3241
_settings = settings;
3342
_nextPruneTimeTicks = DateTime.UtcNow.Add(_settings.MaxQueryTime).Ticks;

test/Kevsoft.AWSXRayRecorder.Handlers.MongoDB.Tests/MongoDbXRayTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ public async Task ShouldEmitMongoDBSubSegment()
1919
{
2020
Amazon.XRay.Recorder.Core.AWSXRayRecorder.InitializeInstance();
2121
Amazon.XRay.Recorder.Core.AWSXRayRecorder.Instance.Emitter = this;
22-
var settings =
23-
XRayMongoClientSettingsConfigurator.Configure(new MongoClientSettings(), new MongoXRayOptions());
2422

23+
var settings = new MongoClientSettings().ConfigureXRay();
2524
var mongoClient = new MongoClient(settings);
2625

2726
var database = mongoClient.GetDatabase("test");
@@ -52,7 +51,7 @@ public async Task ShouldEmitMongoDBSubSegmentOnFailedCommand()
5251
Amazon.XRay.Recorder.Core.AWSXRayRecorder.InitializeInstance();
5352
Amazon.XRay.Recorder.Core.AWSXRayRecorder.Instance.Emitter = this;
5453

55-
var settings = XRayMongoClientSettingsConfigurator.Configure(new MongoClientSettings(), new MongoXRayOptions());
54+
var settings = new MongoClientSettings().ConfigureXRay();
5655

5756
var mongoClient = new MongoClient(settings);
5857

0 commit comments

Comments
 (0)