Skip to content

Commit f25b01f

Browse files
authored
Remove workaround for stable service.instance.id across signals (#108)
1 parent f48c449 commit f25b01f

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Remove workaround for stable `service.instance.id` across signals
6+
([#108](https://github.com/grafana/grafana-opentelemetry-dotnet/pull/108))
7+
38
## 0.9.0-beta.1
49

510
### BREAKING CHANGES

src/Grafana.OpenTelemetry.Base/GrafanaOpenTelemetrySettings.cs

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ public class GrafanaOpenTelemetrySettings
1919
internal const string DisableInstrumentationsEnvVarName = "GRAFANA_DOTNET_DISABLE_INSTRUMENTATIONS";
2020
internal const string ServiceNameEnvVarName = "OTEL_SERVICE_NAME";
2121

22-
// As a workaround, a static random service instance id is initialized and used as default.
23-
// This is to avoid different instance ids to be created by provider builders for different
24-
// signals.
25-
//
26-
// This can be removed once the related issue is resolved:
27-
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4871
28-
internal static string DefaultServiceInstanceId = Guid.NewGuid().ToString();
29-
3022
/// <summary>
3123
/// Gets or sets the exporter settings for sending telemetry data to Grafana.
3224
///
@@ -66,7 +58,7 @@ public class GrafanaOpenTelemetrySettings
6658
///
6759
/// This corresponds to the `service.instance.id` resource attribute.
6860
/// </summary>
69-
public string ServiceInstanceId { get; set; } = DefaultServiceInstanceId;
61+
public string ServiceInstanceId { get; set; }
7062

7163
/// <summary>
7264
/// Gets or sets the name of the deployment environment ("staging" or "production").

src/Grafana.OpenTelemetry.Base/ResourceBuilderExtension.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ internal static class ResourceBuilderExtension
1111
{
1212
public static ResourceBuilder AddGrafanaResource(this ResourceBuilder resourceBuilder, GrafanaOpenTelemetrySettings settings)
1313
{
14+
var serviceInstanceIdProvided = !string.IsNullOrEmpty(settings.ServiceInstanceId);
15+
1416
return resourceBuilder
1517
.AddDetector(new GrafanaOpenTelemetryResourceDetector(settings))
1618
.AddService(
1719
serviceName: settings.ServiceName,
1820
serviceVersion: settings.ServiceVersion,
19-
serviceInstanceId: settings.ServiceInstanceId);
21+
serviceInstanceId: serviceInstanceIdProvided ? settings.ServiceInstanceId : null,
22+
autoGenerateServiceInstanceId: serviceInstanceIdProvided == false);
2023

2124
}
2225
}

tests/Grafana.OpenTelemetry.Tests/GrafanaOpenTelemetrySettingsTest.cs

-9
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ public void DisableInstrumentationsColon()
6565
Assert.DoesNotContain(Instrumentation.NetRuntime, settings.Instrumentations);
6666
}
6767

68-
[Fact]
69-
public void StableServiceInstanceId()
70-
{
71-
var settings1 = new GrafanaOpenTelemetrySettings();
72-
var settings2 = new GrafanaOpenTelemetrySettings();
73-
74-
Assert.Equal(settings1.ServiceInstanceId, settings2.ServiceInstanceId);
75-
}
76-
7768
[Fact]
7869
public void DefaultServiceName()
7970
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright Grafana Labs
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
using System.Linq;
7+
using OpenTelemetry.Resources;
8+
using Xunit;
9+
10+
namespace Grafana.OpenTelemetry.Tests
11+
{
12+
public class ResourceBuilderExtensionTest
13+
{
14+
[Fact]
15+
public void StableServiceInstanceId()
16+
{
17+
var resource1 = ResourceBuilder
18+
.CreateEmpty()
19+
.AddGrafanaResource(new GrafanaOpenTelemetrySettings())
20+
.Build()
21+
.Attributes
22+
.ToDictionary(x => x.Key, x => x.Value);
23+
var resource2 = ResourceBuilder
24+
.CreateEmpty()
25+
.AddGrafanaResource(new GrafanaOpenTelemetrySettings())
26+
.Build()
27+
.Attributes
28+
.ToDictionary(x => x.Key, x => x.Value);
29+
30+
Assert.NotNull(resource1["service.instance.id"]);
31+
Assert.NotNull(resource2["service.instance.id"]);
32+
Assert.Equal(resource1["service.instance.id"], resource2["service.instance.id"]);
33+
}
34+
35+
[Fact]
36+
public void OverrideServiceInstanceId()
37+
{
38+
var settings = new GrafanaOpenTelemetrySettings
39+
{
40+
ServiceInstanceId = "test-id"
41+
};
42+
var resource1 = ResourceBuilder
43+
.CreateEmpty()
44+
.AddGrafanaResource(settings)
45+
.Build()
46+
.Attributes
47+
.ToDictionary(x => x.Key, x => x.Value);
48+
49+
Assert.NotNull(resource1["service.instance.id"]);
50+
Assert.Equal(resource1["service.instance.id"], "test-id");
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)