-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Instrumentation.EntityFrameworkCore] Update semantic conventions for…
… stable release (#2130) Co-authored-by: joegoldman2 <[email protected]>
- Loading branch information
1 parent
b8fe2cb
commit 5a9c9a8
Showing
10 changed files
with
339 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#nullable enable | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace OpenTelemetry.Internal; | ||
|
||
/// <summary> | ||
/// Helper class for Database Semantic Conventions. | ||
/// </summary> | ||
/// <remarks> | ||
/// Due to a breaking change in the semantic convention, affected instrumentation libraries | ||
/// must inspect an environment variable to determine which attributes to emit. | ||
/// This is expected to be removed when the instrumentation libraries reach Stable. | ||
/// <see href="https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/database/database-spans.md"/>. | ||
/// </remarks> | ||
internal static class DatabaseSemanticConventionHelper | ||
{ | ||
internal const string SemanticConventionOptInKeyName = "OTEL_SEMCONV_STABILITY_OPT_IN"; | ||
internal static readonly char[] Separator = new[] { ',', ' ' }; | ||
|
||
[Flags] | ||
public enum DatabaseSemanticConvention | ||
{ | ||
/// <summary> | ||
/// Instructs an instrumentation library to emit the old experimental Database attributes. | ||
/// </summary> | ||
Old = 0x1, | ||
|
||
/// <summary> | ||
/// Instructs an instrumentation library to emit the new, v1.28.0 Database attributes. | ||
/// </summary> | ||
New = 0x2, | ||
|
||
/// <summary> | ||
/// Instructs an instrumentation library to emit both the old and new attributes. | ||
/// </summary> | ||
Dupe = Old | New, | ||
} | ||
|
||
public static DatabaseSemanticConvention GetSemanticConventionOptIn(IConfiguration configuration) | ||
{ | ||
if (TryGetConfiguredValues(configuration, out var values)) | ||
{ | ||
if (values.Contains("database/dup")) | ||
{ | ||
return DatabaseSemanticConvention.Dupe; | ||
} | ||
else if (values.Contains("database")) | ||
{ | ||
return DatabaseSemanticConvention.New; | ||
} | ||
} | ||
|
||
return DatabaseSemanticConvention.Old; | ||
} | ||
|
||
private static bool TryGetConfiguredValues(IConfiguration configuration, [NotNullWhen(true)] out HashSet<string>? values) | ||
{ | ||
try | ||
{ | ||
var stringValue = configuration[SemanticConventionOptInKeyName]; | ||
|
||
if (string.IsNullOrWhiteSpace(stringValue)) | ||
{ | ||
values = null; | ||
return false; | ||
} | ||
|
||
var stringValues = stringValue!.Split(separator: Separator, options: StringSplitOptions.RemoveEmptyEntries); | ||
values = new HashSet<string>(stringValues, StringComparer.OrdinalIgnoreCase); | ||
return true; | ||
} | ||
catch | ||
{ | ||
values = null; | ||
return false; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
test/OpenTelemetry.Contrib.Shared.Tests/DatabaseSemanticConventionHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Xunit; | ||
using static OpenTelemetry.Internal.DatabaseSemanticConventionHelper; | ||
|
||
namespace OpenTelemetry.Internal.Tests; | ||
|
||
public class DatabaseSemanticConventionHelperTests | ||
{ | ||
public static IEnumerable<object[]> TestCases => new List<object[]> | ||
{ | ||
new object[] { null!, DatabaseSemanticConvention.Old }, | ||
new object[] { string.Empty, DatabaseSemanticConvention.Old }, | ||
new object[] { " ", DatabaseSemanticConvention.Old }, | ||
new object[] { "junk", DatabaseSemanticConvention.Old }, | ||
new object[] { "none", DatabaseSemanticConvention.Old }, | ||
new object[] { "NONE", DatabaseSemanticConvention.Old }, | ||
new object[] { "database", DatabaseSemanticConvention.New }, | ||
new object[] { "DATABASE", DatabaseSemanticConvention.New }, | ||
new object[] { "database/dup", DatabaseSemanticConvention.Dupe }, | ||
new object[] { "DATABASE/DUP", DatabaseSemanticConvention.Dupe }, | ||
new object[] { "junk,,junk", DatabaseSemanticConvention.Old }, | ||
new object[] { "junk,JUNK", DatabaseSemanticConvention.Old }, | ||
new object[] { "junk1,junk2", DatabaseSemanticConvention.Old }, | ||
new object[] { "junk,database", DatabaseSemanticConvention.New }, | ||
new object[] { "junk,database , database ,junk", DatabaseSemanticConvention.New }, | ||
new object[] { "junk,database/dup", DatabaseSemanticConvention.Dupe }, | ||
new object[] { "junk, database/dup ", DatabaseSemanticConvention.Dupe }, | ||
new object[] { "database/dup,database", DatabaseSemanticConvention.Dupe }, | ||
new object[] { "database,database/dup", DatabaseSemanticConvention.Dupe }, | ||
}; | ||
|
||
[Fact] | ||
public void VerifyFlags() | ||
{ | ||
var testValue = DatabaseSemanticConvention.Dupe; | ||
Assert.True(testValue.HasFlag(DatabaseSemanticConvention.Old)); | ||
Assert.True(testValue.HasFlag(DatabaseSemanticConvention.New)); | ||
|
||
testValue = DatabaseSemanticConvention.Old; | ||
Assert.True(testValue.HasFlag(DatabaseSemanticConvention.Old)); | ||
Assert.False(testValue.HasFlag(DatabaseSemanticConvention.New)); | ||
|
||
testValue = DatabaseSemanticConvention.New; | ||
Assert.False(testValue.HasFlag(DatabaseSemanticConvention.Old)); | ||
Assert.True(testValue.HasFlag(DatabaseSemanticConvention.New)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(TestCases))] | ||
public void VerifyGetSemanticConventionOptIn_UsingEnvironmentVariable(string input, string expectedValue) | ||
{ | ||
try | ||
{ | ||
Environment.SetEnvironmentVariable(SemanticConventionOptInKeyName, input); | ||
|
||
var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue); | ||
Assert.Equal(expected, GetSemanticConventionOptIn(new ConfigurationBuilder().AddEnvironmentVariables().Build())); | ||
} | ||
finally | ||
{ | ||
Environment.SetEnvironmentVariable(SemanticConventionOptInKeyName, null); | ||
} | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(TestCases))] | ||
public void VerifyGetSemanticConventionOptIn_UsingIConfiguration(string input, string expectedValue) | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new Dictionary<string, string?> { [SemanticConventionOptInKeyName] = input }) | ||
.Build(); | ||
|
||
var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue); | ||
Assert.Equal(expected, GetSemanticConventionOptIn(configuration)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.