Skip to content

Commit 0f78521

Browse files
fix #36 - refactor options class (#43)
* fix #36 - refactor options class * update changelog/readme * remove some unneccessary whitespace
1 parent b89d939 commit 0f78521

File tree

11 files changed

+97
-83
lines changed

11 files changed

+97
-83
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22

33
_vNext_
4+
- The `Options` class has been refactored to support a more fluent style of configuration. **Breaking Change**
5+
- By default, `SatelliteOptions` are now created when `Options` is created that points to the LightStep public satellite pool.
6+
- Please see the readme for more information on configuring the tracer.
47
- Tags which are passed with an empty value will now set the string value "null" when reporting to LightStep.
58

69
_v0.0.8_

README.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@ Install the package via NuGet into your solution, or use `Install-Package LightS
1717
# Basic Usage
1818
It's recommended to initialize the tracer once at the beginning of your application and assign it as the global tracer, as follows:
1919
```c#
20-
// substitute your own LS API Key here
21-
var lsKey = "TEST_TOKEN";
22-
23-
// substitute your satellite endpoint (host, port) here.
24-
var lsSettings = new SatelliteOptions("localhost", 9996, false);
25-
26-
// create a new tracer and register it
27-
var tracer = new Tracer(new Options(lsKey, lsSettings));
20+
var tracerOptions = new Options("PROJECT_ACCESS_TOKEN");
21+
var tracer = new Tracer(tracerOptions);
2822
GlobalTracer.Register(tracer);
29-
...
3023
```
3124

3225
Once you've initialized a tracer, you can begin to create and emit spans.
@@ -40,15 +33,15 @@ You can also refer to the `examples` folder for sample code and projects.
4033
There's several options that can be adjusted when instantiating a `LightStepTracer`.
4134

4235
## `Options`
43-
| Property | Description |
36+
| Method | Description |
4437
| -------- | ----------- |
45-
| Tags | Default tags to apply to all spans created by the tracer. |
46-
| ReportPeriod | How frequently the Tracer should batch and send Spans to LightStep (30s default) |
47-
| ReportTimeout | Timeout for sending spans to the Satellite |
48-
| AccessToken | The LightStep Project Access Key |
49-
| Tags | Tags that are applied to each span generated by the tracer. Override or add new tags via the constructor. |
50-
| Satellite | A SatelliteOptions object that specifies the host, port, and if we should use HTTPS |
51-
| UseHttp2 | If this is true, we use HTTP/2 to communicate with the Satellite |
38+
| WithTags(IDictionary<string, object>) | Default tags to apply to all spans created by the tracer. |
39+
| WithReportPeriod(TimeSpan) | How frequently the Tracer should batch and send Spans to LightStep (30s default) |
40+
| WithReportTimeout(TimeSpan) | Timeout for sending spans to the Satellite |
41+
| WithToken(string) | The LightStep Project Access Token |
42+
| WithSatellite(SatelliteOptions) | A SatelliteOptions object that specifies the host, port, and if we should use HTTPS |
43+
| WithHttp2(bool) | If this is true, we use HTTP/2 to communicate with the Satellite |
44+
| WithAutomaticReporting(bool) | If false, disables the automatic flushing of buffered spans. |
5245

5346
## `SatelliteOptions`
5447
| Property | Description |
@@ -64,12 +57,12 @@ The C# Tracer will prefer TLS 1.2 when available on all .NET Runtime versions, b
6457
The following is an example of overriding the LightStep Component Name and adding a new custom tag for all spans -
6558

6659
```csharp
67-
var satelliteOptions = newSatelliteOptions("satellite.mydomain.com");
60+
var satelliteOptions = new SatelliteOptions("satellite.mydomain.com");
6861
var overrideTags = new Dictionary<string, object>
6962
{
7063
{LightStepConstants.ComponentNameKey, "test_component"},
7164
{"my_tag", "foobar"}
7265
};
73-
var tracerOptions = new Options("TEST_TOKEN", overrideTags, satelliteOptions);
74-
var tracer = new Tracer(tracerOptions, satelliteOptions);
66+
var tracerOptions = new Options("TEST_TOKEN").WithSatellite(satelliteOptions).WithTags(overrideTags);
67+
var tracer = new Tracer(tracerOptions);
7568
```

examples/LightStep.CSharpAspectTestApp/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ static void Main(string[] args)
1717
// create your tracer options, initialize it, assign it to the GlobalTracer
1818
var lsKey = Environment.GetEnvironmentVariable("LS_KEY");
1919
var lsSettings = new SatelliteOptions("collector.lightstep.com");
20-
var lsOptions = new Options(lsKey, lsSettings);
21-
lsOptions.UseHttp2 = false;
20+
var lsOptions = new Options(lsKey).WithStatellite(lsSettings);
2221
var tracer = new Tracer(lsOptions);
2322

2423
GlobalTracer.Register(tracer);

examples/LightStep.CSharpDITestApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class Program
1212
public static void Main(string[] args)
1313
{
1414
// replace these options with your satellite and api key
15-
var tracerOptions = new Options("TEST_TOKEN", new SatelliteOptions("localhost", 9996, true));
15+
var tracerOptions = new Options("TEST_TOKEN").WithStatellite(new SatelliteOptions("localhost", 9996, true));
1616
var container = new WindsorContainer();
1717
// during registration, pass your options to the concrete LightStep Tracer implementation
1818
container.Register(Component.For<ITracer>().ImplementedBy<Tracer>().DependsOn(Dependency.OnValue("options", tracerOptions)));

examples/LightStep.CSharpTestApp/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
using System;
22
using System.IO;
33
using System.Threading;
4+
using Google.Protobuf.WellKnownTypes;
45
using OpenTracing.Util;
56

6-
namespace LightStep.TestApp
7+
namespace LightStep.CSharpTestApp
78
{
89
internal class Program
910
{
1011
private static void Main(string[] args)
1112
{
1213
// substitute your own LS API Key here
13-
var lsKey = "TEST_TOKEN";
14-
var lsSettings = new SatelliteOptions("localhost", 9996, true);
15-
var tracer = new Tracer(new Options(lsKey, lsSettings));
14+
var lightStepSatellite = new SatelliteOptions("localhost", 9996, true);
15+
var lightStepOptions = new Options("TEST_TOKEN").WithStatellite(lightStepSatellite);
16+
var tracer = new Tracer(lightStepOptions);
1617
GlobalTracer.Register(tracer);
1718

1819
for (var i = 0; i < 500; i++)

src/LightStep/Options.cs

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,71 +17,101 @@ public class Options
1717
/// </summary>
1818
public readonly ulong TracerGuid = new Random().NextUInt64();
1919

20-
/// <summary>
21-
/// Creates a new set of options for the LightStep tracer.
22-
/// </summary>
23-
/// <param name="token">Project API Key.</param>
24-
/// <param name="satelliteOptions">Satellite endpoint configuration.</param>
25-
public Options(string token, SatelliteOptions satelliteOptions) : this(token, new Dictionary<string, object>(), satelliteOptions)
26-
{
27-
28-
}
29-
30-
/// <summary>
31-
/// Creates a new set of options for the LightStep tracer.
32-
/// </summary>
33-
/// <param name="token">Project API key.</param>
34-
/// <param name="tags">Tags to add to every span emitted from this tracer.</param>
35-
/// <param name="satelliteOptions">Satellite endpoint configuration.</param>
36-
/// <exception cref="ArgumentNullException">An API key is required.</exception>
37-
public Options(string token, IDictionary<string, object> tags, SatelliteOptions satelliteOptions)
38-
{
39-
if (string.IsNullOrWhiteSpace(token)) throw new ArgumentNullException(nameof(token));
40-
41-
Tags = tags.Count > 0 ? MergeTags(tags) : InitializeDefaultTags();
42-
ReportPeriod = TimeSpan.FromMilliseconds(5000);
43-
ReportTimeout = TimeSpan.FromSeconds(30);
44-
AccessToken = token;
45-
Satellite = satelliteOptions;
46-
UseHttp2 = false;
47-
Run = true;
48-
}
49-
5020
/// <summary>
5121
/// API key for a LightStep project.
5222
/// </summary>
53-
public string AccessToken { get; set; }
23+
public string AccessToken { get; private set; }
5424

5525
/// <summary>
5626
/// If true, tracer will start reporting
5727
/// </summary>
5828
///
59-
public bool Run { get; set; }
29+
public bool Run { get; private set; }
6030
/// <summary>
6131
/// True if the satellite connection should use HTTP/2, false otherwise.
6232
/// </summary>
63-
public bool UseHttp2;
33+
public bool UseHttp2 { get; private set; }
6434

6535
/// <summary>
6636
/// LightStep Satellite endpoint configuration.
6737
/// </summary>
68-
public SatelliteOptions Satellite { get; set; }
38+
public SatelliteOptions Satellite { get; private set; }
6939

7040
/// <summary>
7141
/// How often the reporter will send spans to a LightStep Satellite.
7242
/// </summary>
73-
public TimeSpan ReportPeriod { get; set; }
43+
public TimeSpan ReportPeriod { get; private set; }
7444

7545
/// <summary>
7646
/// Timeout for sending spans to a LightStep Satellite.
7747
/// </summary>
78-
public TimeSpan ReportTimeout { get; set; }
48+
public TimeSpan ReportTimeout { get; private set; }
7949

8050
/// <summary>
8151
/// Tags that should be applied to each span generated by this tracer.
8252
/// </summary>
83-
public IDictionary<string, object> Tags { get; set; }
53+
public IDictionary<string, object> Tags { get; private set; }
54+
55+
public Options WithToken(string token)
56+
{
57+
AccessToken = token;
58+
return this;
59+
}
8460

61+
public Options WithHttp2()
62+
{
63+
UseHttp2 = true;
64+
return this;
65+
}
66+
67+
public Options WithStatellite(SatelliteOptions options)
68+
{
69+
Satellite = options;
70+
return this;
71+
}
72+
73+
public Options WithReportPeriod(TimeSpan period)
74+
{
75+
ReportPeriod = period;
76+
return this;
77+
}
78+
79+
public Options WithReportTimeout(TimeSpan timeout)
80+
{
81+
ReportTimeout = timeout;
82+
return this;
83+
}
84+
85+
public Options WithTags(IDictionary<string, object> tags)
86+
{
87+
Tags = MergeTags(tags);
88+
return this;
89+
}
90+
91+
public Options WithAutomaticReporting(bool shouldRun)
92+
{
93+
Run = shouldRun;
94+
return this;
95+
}
96+
97+
/// <summary>
98+
/// Creates a new set of options for the LightStep tracer.
99+
/// </summary>
100+
/// <param name="token">Project API key.</param>
101+
/// <exception cref="ArgumentNullException">An API key is required.</exception>
102+
public Options(string token)
103+
{
104+
if (string.IsNullOrWhiteSpace(token)) throw new ArgumentNullException(nameof(token));
105+
106+
Tags = InitializeDefaultTags();
107+
ReportPeriod = TimeSpan.FromMilliseconds(5000);
108+
ReportTimeout = TimeSpan.FromSeconds(30);
109+
AccessToken = token;
110+
Satellite = new SatelliteOptions("collector.lightstep.com");
111+
UseHttp2 = false;
112+
Run = true;
113+
}
114+
85115
private IDictionary<string, object> MergeTags(IDictionary<string, object> input)
86116
{
87117
var attributes = InitializeDefaultTags();

src/LightStep/SatelliteOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace LightStep
22
{
33
/// <summary>
4-
/// Contains information on a LightStep Satellite Endpoint.
4+
/// Options to configure a LightStep satellite.
55
/// </summary>
66
public class SatelliteOptions
77
{

test/LightStep.Tests/LightStepProtoTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ private Tracer GetTracer(ISpanRecorder recorder = null)
1313
{
1414
var spanRecorder = recorder ?? new SimpleMockRecorder();
1515
var satelliteOptions = new SatelliteOptions("localhost", 80, true);
16-
var tracerOptions = new Options("TEST", satelliteOptions);
17-
tracerOptions.Run = false;
18-
16+
var tracerOptions = new Options("TEST").WithStatellite(satelliteOptions).WithAutomaticReporting(false);
1917
return new Tracer(tracerOptions, spanRecorder);
2018
}
2119

2220
private LightStepHttpClient GetClient()
2321
{
2422
var satelliteOptions = new SatelliteOptions("localhost", 80, true);
25-
var tracerOptions = new Options("TEST", satelliteOptions);
26-
tracerOptions.Run = false;
23+
var tracerOptions = new Options("TEST").WithStatellite(satelliteOptions).WithAutomaticReporting(false);
2724
return new LightStepHttpClient("http://localhost:80", tracerOptions);
2825
}
2926

test/LightStep.Tests/PropagatorTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public void PropagatorStackInTracerShouldInjectAndExtract()
1818

1919
var sr = new SimpleMockRecorder();
2020
var satOpts = new SatelliteOptions("localhost", 80, true);
21-
var tracerOpts = new Options("TEST", satOpts);
22-
tracerOpts.Run = false;
21+
var tracerOpts = new Options("TEST").WithStatellite(satOpts).WithAutomaticReporting(false);
2322

2423
var tracer = new Tracer(tracerOpts, sr, ps);
2524

test/LightStep.Tests/SpanTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ private Tracer GetTracer(ISpanRecorder recorder = null)
1313
{
1414
var spanRecorder = recorder ?? new SimpleMockRecorder();
1515
var satelliteOptions = new SatelliteOptions("localhost", 80, true);
16-
var tracerOptions = new Options("TEST", satelliteOptions);
17-
tracerOptions.Run = false;
16+
var tracerOptions = new Options("TEST").WithStatellite(satelliteOptions).WithAutomaticReporting(false);
1817
return new Tracer(tracerOptions, spanRecorder);
1918
}
2019

0 commit comments

Comments
 (0)