[sdk] Generate a consistent instanceId for the lifetime of a process (#4988)
This commit is contained in:
parent
c0a55775b4
commit
cd8de07562
|
|
@ -6,6 +6,11 @@
|
|||
`8.0.0-rc.2.23479.6`.
|
||||
([#4959](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4959))
|
||||
|
||||
* The `AddService` `ResourceBuilder` extension method will now generate the same
|
||||
`service.instance.id` for the lifetime of a process when
|
||||
`autoGenerateServiceInstanceId` is `true`.
|
||||
([#4988](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4988))
|
||||
|
||||
## 1.7.0-alpha.1
|
||||
|
||||
Released 2023-Oct-16
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ namespace OpenTelemetry.Resources;
|
|||
/// </summary>
|
||||
public static class ResourceBuilderExtensions
|
||||
{
|
||||
private static readonly string InstanceId = Guid.NewGuid().ToString();
|
||||
|
||||
private static Resource TelemetryResource { get; } = new Resource(new Dictionary<string, object>
|
||||
{
|
||||
[ResourceSemanticConventions.AttributeTelemetrySdkName] = "opentelemetry",
|
||||
|
|
@ -71,7 +73,7 @@ public static class ResourceBuilderExtensions
|
|||
|
||||
if (serviceInstanceId == null && autoGenerateServiceInstanceId)
|
||||
{
|
||||
serviceInstanceId = Guid.NewGuid().ToString();
|
||||
serviceInstanceId = InstanceId;
|
||||
}
|
||||
|
||||
if (serviceInstanceId != null)
|
||||
|
|
|
|||
|
|
@ -68,6 +68,24 @@ public class ResourceBuilderTests
|
|||
Assert.Contains(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, "my-service"), resource.Attributes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ServiceResourceGeneratesConsistentInstanceId()
|
||||
{
|
||||
var firstResource = ResourceBuilder.CreateEmpty().AddService("my-service").Build();
|
||||
|
||||
var firstInstanceIdAttribute = firstResource.Attributes.FirstOrDefault(kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceInstance);
|
||||
|
||||
Assert.NotNull(firstInstanceIdAttribute.Value);
|
||||
|
||||
var secondResource = ResourceBuilder.CreateEmpty().AddService("other-service").Build();
|
||||
|
||||
var secondInstanceIdAttribute = secondResource.Attributes.FirstOrDefault(kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceInstance);
|
||||
|
||||
Assert.NotNull(secondInstanceIdAttribute.Value);
|
||||
|
||||
Assert.Equal(firstInstanceIdAttribute.Value, secondInstanceIdAttribute.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClearTest()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue