Skip to content

Commit 3ea9739

Browse files
Configure service.instance.id if missing
Configure a default value for the `service.instance.id` resource attribute if not specified by the user.
1 parent 6ff80b4 commit 3ea9739

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h
1717
- Support for file-based configuration of the IL rewrite for
1818
SqlClient instrumentation setting
1919
- Add support for `StackExchange.Redis` for applications targeting .NET Framework.
20+
- Automatically set the `service.instance.id` resource attribute if not provided.
2021

2122
### Changed
2223

src/OpenTelemetry.AutoInstrumentation/Configurations/ResourceConfigurator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public static ResourceBuilder CreateResourceBuilder(ResourceSettings resourceSet
4848
resourceBuilder.AddAttributes([new(Constants.ResourceAttributes.AttributeServiceName, ServiceNameConfigurator.GetFallbackServiceName())]);
4949
}
5050

51+
if (resource.Attributes.All(kvp => kvp.Key != Constants.ResourceAttributes.AttributeServiceInstanceId))
52+
{
53+
// service.instance.id was not configured yet use the fallback.
54+
resourceBuilder.AddAttributes([new(Constants.ResourceAttributes.AttributeServiceInstanceId, Guid.NewGuid().ToString())]);
55+
}
56+
5157
var pluginManager = Instrumentation.PluginManager;
5258
if (pluginManager != null)
5359
{

src/OpenTelemetry.AutoInstrumentation/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static class HttpSpanAttributes
2727
public static class ResourceAttributes
2828
{
2929
public const string AttributeServiceName = "service.name";
30+
public const string AttributeServiceInstanceId = "service.instance.id";
3031
}
3132

3233
public static class ConfigurationValues

test/IntegrationTests/AspNetTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace IntegrationTests;
1010

11-
public class AspNetTests
11+
public class AspNetTests(ITestOutputHelper output)
1212
{
1313
private const string ServiceName = "TestApplication.AspNet.NetFramework";
1414

@@ -22,11 +22,6 @@ public class AspNetTests
2222
}
2323
};
2424

25-
public AspNetTests(ITestOutputHelper output)
26-
{
27-
Output = output;
28-
}
29-
3025
public enum Gac
3126
{
3227
/// <summary>
@@ -53,7 +48,7 @@ public enum AppPoolMode
5348
Integrated
5449
}
5550

56-
private ITestOutputHelper Output { get; }
51+
private ITestOutputHelper Output { get; } = output;
5752

5853
[Theory]
5954
[Trait("Category", "EndToEnd")]
@@ -179,8 +174,9 @@ public async Task TracesResource()
179174
// on the firewall.
180175
using var collector = new MockSpansCollector(Output, host: "*");
181176
using var fwPort = FirewallHelper.OpenWinPort(collector.Port, Output);
182-
collector.ResourceExpector.Expect("service.name", ServiceName); // this is set via env var in Dockerfile and Wep.config, but env var has precedence
183-
collector.ResourceExpector.Expect("deployment.environment.name", "test"); // this is set via Wep.config
177+
collector.ResourceExpector.Expect("service.name", ServiceName); // this is set via env var in Dockerfile and Web.config, but env var has precedence
178+
collector.ResourceExpector.Expect("deployment.environment.name", "test"); // this is set via Web.config
179+
collector.ResourceExpector.Exist("service.instance.id"); // automatically generated
184180

185181
var collectorUrl = $"http://{DockerNetworkHelper.IntegrationTestsGateway}:{collector.Port}";
186182

test/IntegrationTests/Helpers/OtlpResourceExpectorExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ public static void ExpectStandardResources(this OtlpResourceExpector resourceExp
4040
resourceExpector.Exist("os.description");
4141
resourceExpector.Exist("os.name");
4242
resourceExpector.Exist("os.version");
43+
resourceExpector.Exist("service.instance.id");
4344
}
4445
}

test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/ServiceNameConfiguratorTests.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ namespace OpenTelemetry.AutoInstrumentation.Tests.Configurations;
99
[Collection("Non-Parallel Collection")]
1010
public class ServiceNameConfiguratorTests
1111
{
12+
private const string ServiceInstanceId = "service.instance.id";
1213
private const string ServiceName = "service.name";
1314
private const string OtelServiceVariable = "OTEL_SERVICE_NAME";
15+
private const string OtelResourceAttributeVariable = "OTEL_RESOURCE_ATTRIBUTES";
1416

1517
[Fact]
1618
public void GetFallbackServiceName()
@@ -22,24 +24,30 @@ public void GetFallbackServiceName()
2224
Assert.Matches("testhost|ReSharperTestRunner", serviceName);
2325
}
2426

25-
[Fact]
26-
public void ServiceName_Retained_EnvVarSet()
27+
[Theory]
28+
[InlineData(ServiceName, OtelServiceVariable, "TestApplication", "TestApplication")]
29+
[InlineData(ServiceName, OtelResourceAttributeVariable, $"{ServiceName}=TestApplication", "TestApplication")]
30+
[InlineData(ServiceInstanceId, OtelResourceAttributeVariable, $"{ServiceInstanceId}=c8de43ab-0121-4a4a-84ff-2177e1613304", "c8de43ab-0121-4a4a-84ff-2177e1613304")]
31+
public void Environment_Variable_Not_Overwritten(
32+
string attributeName,
33+
string variableName,
34+
string variableValue,
35+
string expectedValue)
2736
{
28-
const string setServiceName = "TestApplication";
2937
try
3038
{
31-
Environment.SetEnvironmentVariable(OtelServiceVariable, setServiceName);
39+
Environment.SetEnvironmentVariable(variableName, variableValue);
3240

3341
var resourceBuilder = ResourceConfigurator.CreateResourceBuilder(new ResourceSettings());
3442
var resource = resourceBuilder.Build();
3543

36-
var serviceName = resource.Attributes.FirstOrDefault(a => a.Key == ServiceName).Value as string;
44+
var actualVaue = resource.Attributes.FirstOrDefault(a => a.Key == attributeName).Value as string;
3745

38-
Assert.Equal(setServiceName, serviceName);
46+
Assert.Equal(expectedValue, actualVaue);
3947
}
4048
finally
4149
{
42-
Environment.SetEnvironmentVariable(OtelServiceVariable, null);
50+
Environment.SetEnvironmentVariable(variableName, null);
4351
}
4452
}
4553
}

0 commit comments

Comments
 (0)