Skip to content

Commit fce590f

Browse files
authored
Merge pull request #5 from bmermet/betterservicename
Set smarter default service name
2 parents 8e19573 + 864c0b3 commit fce590f

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

src/Datadog.Tracer.IntegrationTests/SendTracesToAgent.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,13 @@ public async void MinimalSpan()
5050
span.Finish();
5151

5252
// Check that the HTTP calls went as expected
53-
await _httpRecorder.WaitForCompletion(2);
54-
Assert.Equal(2, _httpRecorder.Requests.Count);
55-
Assert.Equal(2, _httpRecorder.Responses.Count);
53+
await _httpRecorder.WaitForCompletion(1);
54+
Assert.Equal(1, _httpRecorder.Requests.Count);
55+
Assert.Equal(1, _httpRecorder.Responses.Count);
5656
Assert.All(_httpRecorder.Responses, (x) => Assert.Equal(HttpStatusCode.OK, x.StatusCode));
5757

5858
var trace = _httpRecorder.Traces.Single();
5959
AssertSpanEqual(span, trace.Single());
60-
61-
var serviceInfo = _httpRecorder.Services.Single().ServiceInfos().Single();
62-
Assert.Equal(Constants.UnkownService, serviceInfo.ServiceName);
63-
Assert.Equal(Constants.UnkownApp, serviceInfo.App);
64-
Assert.Equal(Constants.WebAppType, serviceInfo.AppType);
6560
}
6661

6762
[Fact]
@@ -81,17 +76,15 @@ public async void CustomServiceName()
8176
span.Finish();
8277

8378
// Check that the HTTP calls went as expected
84-
await _httpRecorder.WaitForCompletion(3);
85-
Assert.Equal(3, _httpRecorder.Requests.Count);
86-
Assert.Equal(3, _httpRecorder.Responses.Count);
79+
await _httpRecorder.WaitForCompletion(2);
80+
Assert.Equal(2, _httpRecorder.Requests.Count);
81+
Assert.Equal(2, _httpRecorder.Responses.Count);
8782
Assert.All(_httpRecorder.Responses, (x) => Assert.Equal(HttpStatusCode.OK, x.StatusCode));
8883

8984
var trace = _httpRecorder.Traces.Single();
9085
AssertSpanEqual(span, trace.Single());
9186

92-
var serviceInfos = _httpRecorder.Services.Select(x => x.ServiceInfos().Single()).ToList();
93-
Assert.Equal(2, serviceInfos.Count);
94-
var serviceInfo = serviceInfos.Single(x => x.ServiceName == ServiceName);
87+
var serviceInfo = _httpRecorder.Services.Select(x => x.ServiceInfos().Single()).Single();
9588
Assert.Equal(ServiceName, serviceInfo.ServiceName);
9689
Assert.Equal(App, serviceInfo.App);
9790
Assert.Equal(AppType, serviceInfo.AppType);
@@ -108,9 +101,9 @@ public async void Utf8Everywhere()
108101
span.Finish();
109102

110103
// Check that the HTTP calls went as expected
111-
await _httpRecorder.WaitForCompletion(2);
112-
Assert.Equal(2, _httpRecorder.Requests.Count);
113-
Assert.Equal(2, _httpRecorder.Responses.Count);
104+
await _httpRecorder.WaitForCompletion(1);
105+
Assert.Equal(1, _httpRecorder.Requests.Count);
106+
Assert.Equal(1, _httpRecorder.Responses.Count);
114107
Assert.All(_httpRecorder.Responses, (x) => Assert.Equal(HttpStatusCode.OK, x.StatusCode));
115108

116109
var trace = _httpRecorder.Traces.Single();

src/Datadog.Tracer.Tests/TracerTests.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ public class TracerTests
1010
{
1111
private Mock<IAgentWriter> _agentWriter = new Mock<IAgentWriter>();
1212

13-
[Fact]
14-
public void Ctor_DefaultValues_ShouldSendDefaultServiceInfo()
15-
{
16-
var tracer = new Tracer(_agentWriter.Object);
17-
_agentWriter.Verify(x => x.WriteServiceInfo(It.Is<ServiceInfo>(y => y.ServiceName == Constants.UnkownService && y.AppType == Constants.WebAppType && y.App == Constants.UnkownApp)), Times.Once);
18-
}
19-
2013
[Fact]
2114
public void BuildSpan_NoParameter_DefaultParameters()
2215
{
@@ -25,7 +18,7 @@ public void BuildSpan_NoParameter_DefaultParameters()
2518
var builder = tracer.BuildSpan("Op1");
2619
var span = (Span)builder.Start();
2720

28-
Assert.Equal(Constants.UnkownService, span.ServiceName);
21+
Assert.Equal("Datadog.Tracer", span.ServiceName);
2922
Assert.Equal("Op1", span.OperationName);
3023
}
3124

src/Datadog.Tracer/Tracer.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using OpenTracing.Propagation;
33
using System;
44
using System.Collections.Generic;
5+
using System.Reflection;
56
using System.Threading;
67

78
namespace Datadog.Tracer
@@ -18,17 +19,7 @@ internal class Tracer : ITracer, IDatadogTracer
1819
public Tracer(IAgentWriter agentWriter, List<ServiceInfo> serviceInfo = null, string defaultServiceName = null)
1920
{
2021
_agentWriter = agentWriter;
21-
// TODO:bertrand be smarter about the service name
22-
_defaultServiceName = defaultServiceName ?? Constants.UnkownService;
23-
if (_defaultServiceName == Constants.UnkownService)
24-
{
25-
_services[Constants.UnkownService] = new ServiceInfo
26-
{
27-
ServiceName = Constants.UnkownService,
28-
App = Constants.UnkownApp,
29-
AppType = Constants.WebAppType,
30-
};
31-
}
22+
_defaultServiceName = GetExecutingAssemblyName() ?? Constants.UnkownService;
3223
if (serviceInfo != null)
3324
{
3425
foreach(var service in serviceInfo)
@@ -42,6 +33,19 @@ public Tracer(IAgentWriter agentWriter, List<ServiceInfo> serviceInfo = null, st
4233
}
4334
}
4435

36+
private string GetExecutingAssemblyName()
37+
{
38+
try
39+
{
40+
var name = Assembly.GetExecutingAssembly().GetName();
41+
return name.Name;
42+
}
43+
catch
44+
{
45+
return null;
46+
}
47+
}
48+
4549
public ISpanBuilder BuildSpan(string operationName)
4650
{
4751
return new SpanBuilder(this, operationName);

0 commit comments

Comments
 (0)