Skip to content

Commit 1596837

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #29 from bmermet/globaltracer
Move tracer factory to Tracer class and add global Tracer object
2 parents 30edab5 + 3757c33 commit 1596837

File tree

6 files changed

+56
-51
lines changed

6 files changed

+56
-51
lines changed

src/Datadog.Trace.IntegrationTests.Net45/OpenTracingSendTracesToAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class OpenTracingSendTracesToAgent
1414
public OpenTracingSendTracesToAgent()
1515
{
1616
_httpRecorder = new RecordHttpHandler();
17-
_tracer = OpenTracingTracerFactory.GetTracer(new Uri("http://localhost:8126"), null, null, _httpRecorder);
17+
_tracer = OpenTracingTracerFactory.CreateTracer(new Uri("http://localhost:8126"), null, null, _httpRecorder);
1818
}
1919

2020
[Fact]
@@ -42,7 +42,7 @@ public async void CustomServiceName()
4242
const string ServiceName = "MyService";
4343
var serviceList = new List<ServiceInfo> { new ServiceInfo { App = App, AppType = AppType, ServiceName = ServiceName } };
4444
_httpRecorder = new RecordHttpHandler();
45-
_tracer = OpenTracingTracerFactory.GetTracer(new Uri("http://localhost:8126"), serviceList, null, _httpRecorder);
45+
_tracer = OpenTracingTracerFactory.CreateTracer(new Uri("http://localhost:8126"), serviceList, null, _httpRecorder);
4646

4747
var span = (OpenTracingSpan)_tracer.BuildSpan("Operation")
4848
.WithTag(DDTags.ResourceName, "This is a resource")
@@ -89,7 +89,7 @@ public async void Utf8Everywhere()
8989
public void WithDefaultFactory()
9090
{
9191
// This test does not check anything it validates that this codepath runs without exceptions
92-
var tracer = OpenTracingTracerFactory.GetTracer();
92+
var tracer = OpenTracingTracerFactory.CreateTracer();
9393
tracer.BuildSpan("Operation")
9494
.Start()
9595
.Finish();

src/Datadog.Trace.IntegrationTests/OpenTracingSendTracesToAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class OpenTracingSendTracesToAgent
1414
public OpenTracingSendTracesToAgent()
1515
{
1616
_httpRecorder = new RecordHttpHandler();
17-
_tracer = OpenTracingTracerFactory.GetTracer(new Uri("http://localhost:8126"), null, null, _httpRecorder);
17+
_tracer = OpenTracingTracerFactory.CreateTracer(new Uri("http://localhost:8126"), null, null, _httpRecorder);
1818
}
1919

2020
[Fact]
@@ -42,7 +42,7 @@ public async void CustomServiceName()
4242
const string ServiceName = "MyService";
4343
var serviceList = new List<ServiceInfo> { new ServiceInfo { App = App, AppType = AppType, ServiceName = ServiceName } };
4444
_httpRecorder = new RecordHttpHandler();
45-
_tracer = OpenTracingTracerFactory.GetTracer(new Uri("http://localhost:8126"), serviceList, null, _httpRecorder);
45+
_tracer = OpenTracingTracerFactory.CreateTracer(new Uri("http://localhost:8126"), serviceList, null, _httpRecorder);
4646

4747
var span = (OpenTracingSpan)_tracer.BuildSpan("Operation")
4848
.WithTag(DDTags.ResourceName, "This is a resource")
@@ -89,7 +89,7 @@ public async void Utf8Everywhere()
8989
public void WithDefaultFactory()
9090
{
9191
// This test does not check anything it validates that this codepath runs without exceptions
92-
var tracer = OpenTracingTracerFactory.GetTracer();
92+
var tracer = OpenTracingTracerFactory.CreateTracer();
9393
tracer.BuildSpan("Operation")
9494
.Start()
9595
.Finish();

src/Datadog.Trace.IntegrationTests/SendTracesToAgent.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class SendTracesToAgent
1414
public SendTracesToAgent()
1515
{
1616
_httpRecorder = new RecordHttpHandler();
17-
_tracer = TracerFactory.GetTracer(new Uri("http://localhost:8126"), null, null, _httpRecorder);
17+
_tracer = Tracer.Create(new Uri("http://localhost:8126"), null, null, _httpRecorder);
1818
}
1919

2020
[Fact]
@@ -41,7 +41,7 @@ public async void CustomServiceName()
4141
const string ServiceName = "MyService";
4242
var serviceList = new List<ServiceInfo> { new ServiceInfo { App = App, AppType = AppType, ServiceName = ServiceName } };
4343
_httpRecorder = new RecordHttpHandler();
44-
_tracer = TracerFactory.GetTracer(new Uri("http://localhost:8126"), serviceList, null, _httpRecorder);
44+
_tracer = Tracer.Create(new Uri("http://localhost:8126"), serviceList, null, _httpRecorder);
4545

4646
var scope = _tracer.StartActive("Operation", serviceName: ServiceName);
4747
scope.Span.ResourceName = "This is a resource";
@@ -84,10 +84,17 @@ public async void Utf8Everywhere()
8484
public void WithDefaultFactory()
8585
{
8686
// This test does not check anything it validates that this codepath runs without exceptions
87-
var tracer = OpenTracingTracerFactory.GetTracer();
88-
tracer.BuildSpan("Operation")
89-
.Start()
90-
.Finish();
87+
var tracer = Tracer.Create();
88+
tracer.StartActive("Operation")
89+
.Dispose();
90+
}
91+
92+
[Fact]
93+
public void WithGlobalTracer()
94+
{
95+
// This test does not check anything it validates that this codepath runs without exceptions
96+
Tracer.Instance.StartActive("Operation")
97+
.Dispose();
9198
}
9299
}
93100
}

src/Datadog.Trace/Implementations/Tracer.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Net.Http;
34
using Datadog.Trace.Logging;
45

56
namespace Datadog.Trace
@@ -10,13 +11,19 @@ namespace Datadog.Trace
1011
public class Tracer : IDatadogTracer
1112
{
1213
private static readonly ILog _log = LogProvider.For<Tracer>();
14+
private static Uri _defaultUri = new Uri("http://localhost:8126");
1315

1416
private AsyncLocalScopeManager _scopeManager;
1517
private string _defaultServiceName;
1618
private Dictionary<string, ServiceInfo> _services = new Dictionary<string, ServiceInfo>();
1719
private IAgentWriter _agentWriter;
1820
private bool _isDebugEnabled;
1921

22+
static Tracer()
23+
{
24+
Instance = Create();
25+
}
26+
2027
internal Tracer(IAgentWriter agentWriter, List<ServiceInfo> serviceInfo = null, string defaultServiceName = null, bool isDebugEnabled = false)
2128
{
2229
_isDebugEnabled = isDebugEnabled;
@@ -42,6 +49,11 @@ internal Tracer(IAgentWriter agentWriter, List<ServiceInfo> serviceInfo = null,
4249
_scopeManager = new AsyncLocalScopeManager();
4350
}
4451

52+
/// <summary>
53+
/// Gets or sets the global tracer object
54+
/// </summary>
55+
public static Tracer Instance { get; set; }
56+
4557
/// <summary>
4658
/// Gets the active scope
4759
/// </summary>
@@ -53,6 +65,20 @@ internal Tracer(IAgentWriter agentWriter, List<ServiceInfo> serviceInfo = null,
5365

5466
AsyncLocalScopeManager IDatadogTracer.ScopeManager => _scopeManager;
5567

68+
/// <summary>
69+
/// Create a new Tracer with the given parameters
70+
/// </summary>
71+
/// <param name="agentEndpoint">The agent endpoint where the traces will be sent (default is http://localhost:8126).</param>
72+
/// <param name="serviceInfoList">The service information list.</param>
73+
/// <param name="defaultServiceName">Default name of the service (default is the name of the executing assembly).</param>
74+
/// <param name="isDebugEnabled">Turns on all debug logging (this may have an impact on application performance).</param>
75+
/// <returns>The newly created tracer</returns>
76+
public static Tracer Create(Uri agentEndpoint = null, List<ServiceInfo> serviceInfoList = null, string defaultServiceName = null, bool isDebugEnabled = false)
77+
{
78+
agentEndpoint = agentEndpoint ?? _defaultUri;
79+
return Create(agentEndpoint, serviceInfoList, defaultServiceName, null, isDebugEnabled);
80+
}
81+
5682
/// <summary>
5783
/// Make a span active and return a scope that can be disposed to desactivate the span
5884
/// </summary>
@@ -106,6 +132,14 @@ void IDatadogTracer.Write(List<Span> trace)
106132
_agentWriter.WriteTrace(trace);
107133
}
108134

135+
internal static Tracer Create(Uri agentEndpoint, List<ServiceInfo> serviceInfoList = null, string defaultServiceName = null, DelegatingHandler delegatingHandler = null, bool isDebugEnabled = false)
136+
{
137+
var api = new Api(agentEndpoint, delegatingHandler);
138+
var agentWriter = new AgentWriter(api);
139+
var tracer = new Tracer(agentWriter, serviceInfoList, defaultServiceName, isDebugEnabled);
140+
return tracer;
141+
}
142+
109143
private void CurrentDomain_ProcessExit(object sender, EventArgs e)
110144
{
111145
_agentWriter.FlushAndCloseAsync().Wait();

src/Datadog.Trace/Implementations/TracerFactory.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Datadog.Trace/OpentracingImplementations/OpenTracingTracerFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public static class OpenTracingTracerFactory
2020
/// <param name="defaultServiceName">Default name of the service (default is the name of the executing assembly).</param>
2121
/// <param name="isDebugEnabled">Turns on all debug logging (this may have an impact on application performance).</param>
2222
/// <returns>A Datadog compatible ITracer implementation</returns>
23-
public static ITracer GetTracer(Uri agentEndpoint = null, List<ServiceInfo> serviceInfoList = null, string defaultServiceName = null, bool isDebugEnabled = false)
23+
public static ITracer CreateTracer(Uri agentEndpoint = null, List<ServiceInfo> serviceInfoList = null, string defaultServiceName = null, bool isDebugEnabled = false)
2424
{
2525
agentEndpoint = agentEndpoint ?? _defaultUri;
26-
return GetTracer(agentEndpoint, serviceInfoList, defaultServiceName, null, isDebugEnabled);
26+
return CreateTracer(agentEndpoint, serviceInfoList, defaultServiceName, null, isDebugEnabled);
2727
}
2828

29-
internal static OpenTracingTracer GetTracer(Uri agentEndpoint, List<ServiceInfo> serviceInfoList = null, string defaultServiceName = null, DelegatingHandler delegatingHandler = null, bool isDebugEnabled = false)
29+
internal static OpenTracingTracer CreateTracer(Uri agentEndpoint, List<ServiceInfo> serviceInfoList = null, string defaultServiceName = null, DelegatingHandler delegatingHandler = null, bool isDebugEnabled = false)
3030
{
3131
var api = new Api(agentEndpoint, delegatingHandler);
3232
var agentWriter = new AgentWriter(api);

0 commit comments

Comments
 (0)