Skip to content

Commit

Permalink
Update Azure Core Shared Codes 2023-11-09_01:16:40 (Azure#3917)
Browse files Browse the repository at this point in the history
  • Loading branch information
azure-sdk authored and live1206 committed Dec 7, 2023
1 parent 6497255 commit 7f4732e
Showing 1 changed file with 27 additions and 56 deletions.
83 changes: 27 additions & 56 deletions src/assets/Azure.Core.Shared/DiagnosticScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -63,28 +66,22 @@ 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)
{
_activityAdapter?.AddTag(name, value);
}

public void AddAttribute<T>(string name,
#if AZURE_NULLABLE
[AllowNull]
#endif
T value)
{
AddAttribute(name, value, static v => Convert.ToString(v, CultureInfo.InvariantCulture) ?? string.Empty);
}

public void AddAttribute<T>(string name, T value, Func<T, string> format)
{
if (_activityAdapter != null)
if (_activityAdapter != null && value != null)
{
var formattedValue = format(value);
_activityAdapter.AddTag(name, formattedValue);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -424,11 +417,7 @@ public void AddLink(string traceparent, string? tracestate, IDictionary<string,
{
foreach (var tag in _tagCollection)
{
#if NETCOREAPP2_1
_currentActivity.AddObjectTag(tag.Key, tag.Value);
#else
_currentActivity.AddTag(tag.Key, tag.Value);
#endif
AddObjectTag(tag.Key, tag.Value!);
}
}

Expand Down Expand Up @@ -554,6 +543,22 @@ public void SetTraceContext(string traceparent, string? tracestate)
_tracestate = tracestate;
}

private void AddObjectTag(string name, object value)
{
#if NETCOREAPP2_1
_currentActivity?.AddTag(name, value.ToString());
#else
if (_activitySource?.HasListeners() == true)
{
_currentActivity?.AddTag(name, value);
}
else
{
_currentActivity?.AddTag(name, value.ToString());
}
#endif
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The class constructor is marked with RequiresUnreferencedCode.")]
public void Dispose()
{
Expand Down Expand Up @@ -828,40 +833,6 @@ public static void SetErrorStatus(this Activity activity, string? errorDescripti
SetErrorStatusMethod(activity, 2 /* Error */, errorDescription);
}

public static void AddObjectTag(this Activity activity, string name, object value)
{
if (ActivityAddTagMethod == null)
{
var method = typeof(Activity).GetMethod("AddTag", BindingFlags.Instance | BindingFlags.Public, null, new Type[]
{
typeof(string),
typeof(object)
}, null);

if (method == null)
{
// If the object overload is not available, fall back to the string overload. The assumption is that the object overload
// not being available means that we cannot be using activity source, so the string cast should never fail because we will always
// be passing a string value.
ActivityAddTagMethod = (activityParameter, nameParameter, valueParameter) => 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<Action<Activity, string, object?>>(
Expression.Call(ActivityParameter, method, nameParameter, valueParameter),
ActivityParameter, nameParameter, valueParameter).Compile();
}
}

ActivityAddTagMethod(activity, name, value);
}

public static bool SupportsActivitySource()
{
return SupportsActivitySourceSwitch && ActivitySourceType != null;
Expand Down

0 comments on commit 7f4732e

Please sign in to comment.