Skip to content

Commit 757121a

Browse files
Add Logging via LibLog (#44)
* add logging via liblog * update example app w/serilog to demonstrate logging levels
1 parent 0f78521 commit 757121a

File tree

19 files changed

+109
-28
lines changed

19 files changed

+109
-28
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ var overrideTags = new Dictionary<string, object>
6565
};
6666
var tracerOptions = new Options("TEST_TOKEN").WithSatellite(satelliteOptions).WithTags(overrideTags);
6767
var tracer = new Tracer(tracerOptions);
68-
```
68+
```
69+
70+
## Logging
71+
This tracer uses [LibLog](https://github.com/damianh/LibLog), a transparent logging abstraction that provides built-in support for NLog, Log4Net, Serilog, and Loupe.
72+
If you use a logging provider that isn't identified by LibLog, see [this gist](https://gist.github.com/damianh/fa529b8346a83f7f49a9) on how to implement a custom logging provider.

examples/LightStep.CSharpAspectTestApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +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).WithStatellite(lsSettings);
20+
var lsOptions = new Options(lsKey).WithSatellite(lsSettings);
2121
var tracer = new Tracer(lsOptions);
2222

2323
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").WithStatellite(new SatelliteOptions("localhost", 9996, true));
15+
var tracerOptions = new Options("TEST_TOKEN").WithSatellite(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/LightStep.CSharpTestApp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</PropertyGroup>
88
<ItemGroup>
99
<PackageReference Include="OpenTracing" Version="0.12.0" />
10+
<PackageReference Include="Serilog" Version="2.7.1" />
11+
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
1012
</ItemGroup>
1113
<ItemGroup>
1214
<ProjectReference Include="..\..\src\LightStep\LightStep.csproj" />

examples/LightStep.CSharpTestApp/Program.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
using System.Threading;
44
using Google.Protobuf.WellKnownTypes;
55
using OpenTracing.Util;
6+
using global::Serilog;
7+
using Serilog.Events;
8+
using Serilog.Sinks.SystemConsole;
69

710
namespace LightStep.CSharpTestApp
811
{
912
internal class Program
1013
{
1114
private static void Main(string[] args)
1215
{
16+
Log.Logger = new LoggerConfiguration()
17+
.MinimumLevel.Debug()
18+
.WriteTo.Console()
19+
.CreateLogger();
20+
1321
// substitute your own LS API Key here
1422
var lightStepSatellite = new SatelliteOptions("localhost", 9996, true);
15-
var lightStepOptions = new Options("TEST_TOKEN").WithStatellite(lightStepSatellite);
23+
var lightStepOptions = new Options("TEST_TOKEN").WithSatellite(lightStepSatellite);
1624
var tracer = new Tracer(lightStepOptions);
1725
GlobalTracer.Register(tracer);
1826

@@ -21,16 +29,17 @@ private static void Main(string[] args)
2129
{
2230
scope.Span.Log("test");
2331
tracer.ActiveSpan.Log($"iteration {i}");
24-
Console.WriteLine("sleeping for a bit");
32+
2533
Thread.Sleep(new Random().Next(5, 10));
2634
var innerSpan = tracer.BuildSpan("childSpan").Start();
2735
innerSpan.SetTag("innerTestTag", "true");
28-
Console.WriteLine("sleeping more...");
36+
2937
Thread.Sleep(new Random().Next(10, 20));
3038
innerSpan.Finish();
3139
}
3240

3341
tracer.Flush();
42+
Console.ReadKey();
3443
}
3544

3645
}

src/LightStep/Collector/LightStepHttpClient.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
3+
using System.Diagnostics;
44
using System.Linq;
55
using System.Net;
66
using System.Net.Http;
77
using System.Net.Http.Headers;
88
using System.Threading.Tasks;
99
using Google.Protobuf;
10+
using LightStep.Logging;
1011

1112
namespace LightStep.Collector
1213
{
@@ -18,6 +19,7 @@ public class LightStepHttpClient
1819
private readonly Options _options;
1920
private HttpClient _client;
2021
private readonly string _url;
22+
private static readonly ILog _logger = LogProvider.GetCurrentClassLogger();
2123

2224
/// <summary>
2325
/// Create a new client.
@@ -61,15 +63,15 @@ public async Task<ReportResponse> SendReport(ReportRequest report)
6163
response.EnsureSuccessStatusCode();
6264
var responseData = await response.Content.ReadAsStreamAsync();
6365
responseValue = ReportResponse.Parser.ParseFrom(responseData);
66+
_logger.Debug($"Report HTTP Response {response.StatusCode}");
6467
}
6568
catch (HttpRequestException ex)
6669
{
67-
Console.WriteLine(ex);
68-
Console.WriteLine("resetting httpclient");
70+
_logger.WarnException("Exception caught while sending report, resetting HttpClient", ex);
6971
_client.Dispose();
7072
_client = new HttpClient();
7173
}
72-
74+
7375
return responseValue;
7476
}
7577

@@ -80,6 +82,10 @@ public async Task<ReportResponse> SendReport(ReportRequest report)
8082
/// <returns>A <see cref="ReportRequest" /></returns>
8183
public ReportRequest Translate(IEnumerable<SpanData> spans)
8284
{
85+
_logger.Debug($"Serializing spans to proto.");
86+
var timer = new Stopwatch();
87+
timer.Start();
88+
8389
var request = new ReportRequest
8490
{
8591
Reporter = new Reporter
@@ -90,6 +96,9 @@ public ReportRequest Translate(IEnumerable<SpanData> spans)
9096
};
9197
_options.Tags.ToList().ForEach(t => request.Reporter.Tags.Add(new KeyValue().MakeKeyValueFromKvp(t)));
9298
spans.ToList().ForEach(span => request.Spans.Add(new Span().MakeSpanFromSpanData(span)));
99+
100+
timer.Stop();
101+
_logger.Debug($"Serialization complete in {timer.ElapsedMilliseconds}ms. Request size: {request.CalculateSize()}b.");
93102
return request;
94103
}
95104
}

src/LightStep/LightStep.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
<ItemGroup>
99
<PackageReference Include="Google.Api.CommonProtos" Version="1.3.0" />
1010
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
11+
<PackageReference Include="LibLog" Version="5.0.3">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
1116
<PackageReference Include="OpenTracing" Version="0.12.0" />
1217
<PackageReference Include="System.Json" Version="4.5.0" />
1318
</ItemGroup>

src/LightStep/LightStepSpanRecorder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using LightStep.Logging;
45

56
namespace LightStep
67
{
78
/// <inheritdoc />
89
public sealed class LightStepSpanRecorder : ISpanRecorder
910
{
1011
private List<SpanData> Spans { get; } = new List<SpanData>();
12+
private static readonly ILog _logger = LogProvider.GetCurrentClassLogger();
1113

1214
/// <inheritdoc />
1315
public void RecordSpan(SpanData span)
1416
{
17+
_logger.Trace($"Waiting for lock in SpanRecorder.");
1518
lock (Spans)
1619
{
20+
_logger.Trace($"Lock freed, adding new span: {span}");
1721
Spans.Add(span);
1822
}
1923

@@ -22,8 +26,10 @@ public void RecordSpan(SpanData span)
2226
/// <inheritdoc />
2327
public List<SpanData> GetSpanBuffer()
2428
{
29+
_logger.Trace("Waiting for lock in SpanRecorder");
2530
lock (Spans)
2631
{
32+
_logger.Trace("Lock freed, getting span buffer.");
2733
var currentSpans = Spans.ToList();
2834
Spans.Clear();
2935
return currentSpans;
@@ -34,8 +40,10 @@ public List<SpanData> GetSpanBuffer()
3440
/// <inheritdoc />
3541
public void ClearSpanBuffer()
3642
{
43+
_logger.Trace("Waiting for lock in SpanRecorder.");
3744
lock (Spans)
3845
{
46+
_logger.Trace("Lock freed, clearing span buffer.");
3947
Spans.Clear();
4048
}
4149

src/LightStep/Options.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Runtime.Versioning;
7+
using LightStep.Logging;
78

89
namespace LightStep
910
{
@@ -12,6 +13,8 @@ namespace LightStep
1213
/// </summary>
1314
public class Options
1415
{
16+
private static readonly ILog _logger = LogProvider.GetCurrentClassLogger();
17+
1518
/// <summary>
1619
/// An identifier for the Tracer.
1720
/// </summary>
@@ -54,42 +57,49 @@ public class Options
5457

5558
public Options WithToken(string token)
5659
{
60+
_logger.Debug($"Setting access token to {token}");
5761
AccessToken = token;
5862
return this;
5963
}
6064

6165
public Options WithHttp2()
6266
{
67+
_logger.Debug("Enabling HTTP/2 support.");
6368
UseHttp2 = true;
6469
return this;
6570
}
6671

67-
public Options WithStatellite(SatelliteOptions options)
72+
public Options WithSatellite(SatelliteOptions options)
6873
{
74+
_logger.Debug($"Setting satellite to {options}");
6975
Satellite = options;
7076
return this;
7177
}
7278

7379
public Options WithReportPeriod(TimeSpan period)
7480
{
81+
_logger.Debug($"Setting reporting period to {period}");
7582
ReportPeriod = period;
7683
return this;
7784
}
7885

7986
public Options WithReportTimeout(TimeSpan timeout)
8087
{
88+
_logger.Debug($"Setting report timeout to {timeout}");
8189
ReportTimeout = timeout;
8290
return this;
8391
}
8492

8593
public Options WithTags(IDictionary<string, object> tags)
8694
{
95+
_logger.Debug($"Setting default tags to: {tags.Select(kvp => $"{kvp.Key}:{kvp.Value},")}");
8796
Tags = MergeTags(tags);
8897
return this;
8998
}
9099

91100
public Options WithAutomaticReporting(bool shouldRun)
92101
{
102+
_logger.Debug($"Setting automatic reporting to {shouldRun}");
93103
Run = shouldRun;
94104
return this;
95105
}
@@ -119,7 +129,7 @@ private IDictionary<string, object> MergeTags(IDictionary<string, object> input)
119129
foreach (var item in attributes)
120130
{
121131
if (!mergedAttributes.ContainsKey(item.Key))
122-
{
132+
{
123133
mergedAttributes.Add(item.Key, item.Value);
124134
}
125135
}

src/LightStep/Propagation/PropagatorStack.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
using System;
22
using System.Collections.Generic;
3+
using LightStep.Logging;
34
using OpenTracing.Propagation;
45

56
namespace LightStep.Propagation
67
{
78
/// <inheritdoc />
89
public class PropagatorStack : IPropagator
910
{
11+
private static readonly ILog _logger = LogProvider.GetCurrentClassLogger();
12+
1013
/// <inheritdoc />
1114
public PropagatorStack(IFormat<ITextMap> format)
1215
{
1316
if (format == null) throw new ArgumentNullException(nameof(format));
14-
17+
_logger.Trace($"Creating new PropagatorStack with format {format}");
1518
Format = format;
1619
Propagators = new List<IPropagator>();
1720
}
@@ -30,7 +33,10 @@ public PropagatorStack(IFormat<ITextMap> format)
3033
public void Inject<TCarrier>(SpanContext context, IFormat<TCarrier> format, TCarrier carrier)
3134
{
3235
Propagators.ForEach(propagator =>
33-
propagator.Inject(context, format, carrier)
36+
{
37+
_logger.Debug($"Injecting context to {propagator.GetType()}");
38+
propagator.Inject(context, format, carrier);
39+
}
3440
);
3541
}
3642

@@ -39,6 +45,7 @@ public SpanContext Extract<TCarrier>(IFormat<TCarrier> format, TCarrier carrier)
3945
{
4046
for (var i = Propagators.Count - 1; i >= 0; i--)
4147
{
48+
_logger.Debug($"Trying to extract from {Propagators[i].GetType()}");
4249
var context = Propagators[i].Extract(format, carrier);
4350
if (context != null) return context;
4451
}
@@ -55,7 +62,7 @@ public SpanContext Extract<TCarrier>(IFormat<TCarrier> format, TCarrier carrier)
5562
public PropagatorStack AddPropagator(IPropagator propagator)
5663
{
5764
if (propagator == null) throw new ArgumentNullException(nameof(propagator));
58-
65+
_logger.Trace($"Adding {propagator.GetType()} to PropagatorStack.");
5966
Propagators.Add(propagator);
6067
return this;
6168
}

0 commit comments

Comments
 (0)