diff --git a/src/assets/Azure.Core.Shared/DiagnosticScope.cs b/src/assets/Azure.Core.Shared/DiagnosticScope.cs index 62f5389b513..b24a4546534 100644 --- a/src/assets/Azure.Core.Shared/DiagnosticScope.cs +++ b/src/assets/Azure.Core.Shared/DiagnosticScope.cs @@ -19,6 +19,9 @@ namespace Azure.Core.Pipeline { private const string AzureSdkScopeLabel = "az.sdk.scope"; internal const string OpenTelemetrySchemaAttribute = "az.schema_url"; + + // we follow OpenTelemtery Semantic Conventions 1.23.0 + // https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0 internal const string OpenTelemetrySchemaVersion = "https://opentelemetry.io/schemas/1.23.0"; private static readonly object AzureSdkScopeValue = bool.TrueString; @@ -63,9 +66,12 @@ internal DiagnosticScope(string scopeName, DiagnosticListener source, object? di public bool IsEnabled { get; } - public void AddAttribute(string name, string value) + public void AddAttribute(string name, string? value) { - _activityAdapter?.AddTag(name, value); + if (value != null) + { + _activityAdapter?.AddTag(name, value); + } } public void AddIntegerAttribute(string name, int value) @@ -73,18 +79,9 @@ public void AddIntegerAttribute(string name, int value) _activityAdapter?.AddTag(name, value); } - public void AddAttribute(string name, -#if AZURE_NULLABLE - [AllowNull] -#endif - T value) - { - AddAttribute(name, value, static v => Convert.ToString(v, CultureInfo.InvariantCulture) ?? string.Empty); - } - public void AddAttribute(string name, T value, Func format) { - if (_activityAdapter != null) + if (_activityAdapter != null && value != null) { var formattedValue = format(value); _activityAdapter.AddTag(name, formattedValue); @@ -269,11 +266,7 @@ public void AddTag(string name, object value) } else { -#if NETCOREAPP2_1 - _currentActivity?.AddObjectTag(name, value); -#else - _currentActivity?.AddTag(name, value); -#endif + AddObjectTag(name, value); } } @@ -424,11 +417,7 @@ public void AddLink(string traceparent, string? tracestate, IDictionary activityParameter.AddTag( - nameParameter, - // null check is required to keep nullable reference compilation happy - valueParameter == null ? null : (string)valueParameter); - } - else - { - var nameParameter = Expression.Parameter(typeof(string)); - var valueParameter = Expression.Parameter(typeof(object)); - - ActivityAddTagMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method, nameParameter, valueParameter), - ActivityParameter, nameParameter, valueParameter).Compile(); - } - } - - ActivityAddTagMethod(activity, name, value); - } - public static bool SupportsActivitySource() { return SupportsActivitySourceSwitch && ActivitySourceType != null;