Skip to content

Commit

Permalink
Remove workaround for stable service.instance.id across signals (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-hensley authored Aug 12, 2024
1 parent f48c449 commit f25b01f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

* Remove workaround for stable `service.instance.id` across signals
([#108](https://github.com/grafana/grafana-opentelemetry-dotnet/pull/108))

## 0.9.0-beta.1

### BREAKING CHANGES
Expand Down
10 changes: 1 addition & 9 deletions src/Grafana.OpenTelemetry.Base/GrafanaOpenTelemetrySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ public class GrafanaOpenTelemetrySettings
internal const string DisableInstrumentationsEnvVarName = "GRAFANA_DOTNET_DISABLE_INSTRUMENTATIONS";
internal const string ServiceNameEnvVarName = "OTEL_SERVICE_NAME";

// As a workaround, a static random service instance id is initialized and used as default.
// This is to avoid different instance ids to be created by provider builders for different
// signals.
//
// This can be removed once the related issue is resolved:
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4871
internal static string DefaultServiceInstanceId = Guid.NewGuid().ToString();

/// <summary>
/// Gets or sets the exporter settings for sending telemetry data to Grafana.
///
Expand Down Expand Up @@ -66,7 +58,7 @@ public class GrafanaOpenTelemetrySettings
///
/// This corresponds to the `service.instance.id` resource attribute.
/// </summary>
public string ServiceInstanceId { get; set; } = DefaultServiceInstanceId;
public string ServiceInstanceId { get; set; }

/// <summary>
/// Gets or sets the name of the deployment environment ("staging" or "production").
Expand Down
5 changes: 4 additions & 1 deletion src/Grafana.OpenTelemetry.Base/ResourceBuilderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ internal static class ResourceBuilderExtension
{
public static ResourceBuilder AddGrafanaResource(this ResourceBuilder resourceBuilder, GrafanaOpenTelemetrySettings settings)
{
var serviceInstanceIdProvided = !string.IsNullOrEmpty(settings.ServiceInstanceId);

return resourceBuilder
.AddDetector(new GrafanaOpenTelemetryResourceDetector(settings))
.AddService(
serviceName: settings.ServiceName,
serviceVersion: settings.ServiceVersion,
serviceInstanceId: settings.ServiceInstanceId);
serviceInstanceId: serviceInstanceIdProvided ? settings.ServiceInstanceId : null,
autoGenerateServiceInstanceId: serviceInstanceIdProvided == false);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@ public void DisableInstrumentationsColon()
Assert.DoesNotContain(Instrumentation.NetRuntime, settings.Instrumentations);
}

[Fact]
public void StableServiceInstanceId()
{
var settings1 = new GrafanaOpenTelemetrySettings();
var settings2 = new GrafanaOpenTelemetrySettings();

Assert.Equal(settings1.ServiceInstanceId, settings2.ServiceInstanceId);
}

[Fact]
public void DefaultServiceName()
{
Expand Down
53 changes: 53 additions & 0 deletions tests/Grafana.OpenTelemetry.Tests/ResourceBuilderExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Copyright Grafana Labs
// SPDX-License-Identifier: Apache-2.0
//

using System.Linq;
using OpenTelemetry.Resources;
using Xunit;

namespace Grafana.OpenTelemetry.Tests
{
public class ResourceBuilderExtensionTest
{
[Fact]
public void StableServiceInstanceId()
{
var resource1 = ResourceBuilder
.CreateEmpty()
.AddGrafanaResource(new GrafanaOpenTelemetrySettings())
.Build()
.Attributes
.ToDictionary(x => x.Key, x => x.Value);
var resource2 = ResourceBuilder
.CreateEmpty()
.AddGrafanaResource(new GrafanaOpenTelemetrySettings())
.Build()
.Attributes
.ToDictionary(x => x.Key, x => x.Value);

Assert.NotNull(resource1["service.instance.id"]);
Assert.NotNull(resource2["service.instance.id"]);
Assert.Equal(resource1["service.instance.id"], resource2["service.instance.id"]);
}

[Fact]
public void OverrideServiceInstanceId()
{
var settings = new GrafanaOpenTelemetrySettings
{
ServiceInstanceId = "test-id"
};
var resource1 = ResourceBuilder
.CreateEmpty()
.AddGrafanaResource(settings)
.Build()
.Attributes
.ToDictionary(x => x.Key, x => x.Value);

Assert.NotNull(resource1["service.instance.id"]);
Assert.Equal(resource1["service.instance.id"], "test-id");
}
}
}

0 comments on commit f25b01f

Please sign in to comment.