Skip to content
Next Next commit
Added nullable annotations for the trace folder in the api project.
  • Loading branch information
CodeBlanch committed Sep 21, 2023
commit 7a6549411a4a753476c3306ab483ff6bffc15b48
102 changes: 51 additions & 51 deletions src/OpenTelemetry.Api/.publicApi/Stable/net462/PublicAPI.Shipped.txt

Large diffs are not rendered by default.

102 changes: 51 additions & 51 deletions src/OpenTelemetry.Api/.publicApi/Stable/net6.0/PublicAPI.Shipped.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions src/OpenTelemetry.Api/Trace/ActivityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Diagnostics;
using System.Runtime.CompilerServices;
using OpenTelemetry.Internal;
Expand Down Expand Up @@ -41,10 +43,11 @@ public static class ActivityExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetStatus(this Activity activity, Status status)
{
Debug.Assert(activity != null, "Activity should not be null");

activity.SetTag(SpanAttributeConstants.StatusCodeKey, StatusHelper.GetTagValueForStatusCode(status.StatusCode));
activity.SetTag(SpanAttributeConstants.StatusDescriptionKey, status.Description);
if (activity != null)
{
activity.SetTag(SpanAttributeConstants.StatusCodeKey, StatusHelper.GetTagValueForStatusCode(status.StatusCode));
activity.SetTag(SpanAttributeConstants.StatusDescriptionKey, status.Description);
}
}

/// <summary>
Expand All @@ -57,7 +60,8 @@ public static void SetStatus(this Activity activity, Status status)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Status GetStatus(this Activity activity)
{
if (!activity.TryGetStatus(out StatusCode statusCode, out string statusDescription))
if (activity == null
|| !activity.TryGetStatus(out var statusCode, out var statusDescription))
{
return Status.Unset;
}
Expand All @@ -71,10 +75,8 @@ public static Status GetStatus(this Activity activity)
/// <param name="activity">Activity instance.</param>
/// <param name="ex">Exception to be recorded.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RecordException(this Activity activity, Exception ex)
{
activity?.RecordException(ex, default);
}
public static void RecordException(this Activity activity, Exception? ex)
=> RecordException(activity, ex, default);

/// <summary>
/// Adds an activity event containing information from the specified exception and additional tags.
Expand All @@ -83,7 +85,7 @@ public static void RecordException(this Activity activity, Exception ex)
/// <param name="ex">Exception to be recorded.</param>
/// <param name="tags">Additional tags to record on the event.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RecordException(this Activity activity, Exception ex, in TagList tags)
public static void RecordException(this Activity activity, Exception? ex, in TagList tags)
{
if (ex == null || activity == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

namespace OpenTelemetry.Trace;

/// <summary>
Expand Down
42 changes: 15 additions & 27 deletions src/OpenTelemetry.Api/Trace/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Diagnostics;

namespace OpenTelemetry.Trace;
Expand All @@ -30,16 +32,16 @@ namespace OpenTelemetry.Trace;
/// </summary>
/// <param name="spanContext">Span context of a linked span.</param>
public Link(in SpanContext spanContext)
: this(in spanContext, attributes: null)
{
this.ActivityLink = new ActivityLink(spanContext.ActivityContext);
}

/// <summary>
/// Initializes a new instance of the <see cref="Link"/> struct.
/// </summary>
/// <param name="spanContext">Span context of a linked span.</param>
/// <param name="attributes">Link attributes.</param>
public Link(in SpanContext spanContext, SpanAttributes attributes)
public Link(in SpanContext spanContext, SpanAttributes? attributes)
{
this.ActivityLink = new ActivityLink(spanContext.ActivityContext, attributes?.Attributes);
}
Expand All @@ -48,53 +50,39 @@ public Link(in SpanContext spanContext, SpanAttributes attributes)
/// Gets the span context of a linked span.
/// </summary>
public SpanContext Context
{
get
{
return new SpanContext(this.ActivityLink.Context);
}
}
=> new(this.ActivityLink.Context);

/// <summary>
/// Gets the collection of attributes associated with the link.
/// </summary>
public IEnumerable<KeyValuePair<string, object>> Attributes
{
get
{
return this.ActivityLink.Tags;
}
}
public IEnumerable<KeyValuePair<string, object?>>? Attributes
=> this.ActivityLink.Tags;

/// <summary>
/// Compare two <see cref="Link"/> for equality.
/// </summary>
/// <param name="link1">First link to compare.</param>
/// <param name="link2">Second link to compare.</param>
public static bool operator ==(Link link1, Link link2) => link1.Equals(link2);
public static bool operator ==(Link link1, Link link2)
=> link1.Equals(link2);

/// <summary>
/// Compare two <see cref="Link"/> for not equality.
/// </summary>
/// <param name="link1">First link to compare.</param>
/// <param name="link2">Second link to compare.</param>
public static bool operator !=(Link link1, Link link2) => !link1.Equals(link2);
public static bool operator !=(Link link1, Link link2)
=> !link1.Equals(link2);

/// <inheritdoc />
public override bool Equals(object obj)
{
return (obj is Link link) && this.ActivityLink.Equals(link.ActivityLink);
}
public override bool Equals(object? obj)
=> obj is Link link && this.ActivityLink.Equals(link.ActivityLink);

/// <inheritdoc />
public override int GetHashCode()
{
return this.ActivityLink.GetHashCode();
}
=> this.ActivityLink.GetHashCode();

/// <inheritdoc/>
public bool Equals(Link other)
{
return this.ActivityLink.Equals(other.ActivityLink);
}
=> this.ActivityLink.Equals(other.ActivityLink);
}
18 changes: 10 additions & 8 deletions src/OpenTelemetry.Api/Trace/SpanAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Diagnostics;
using OpenTelemetry.Internal;

Expand All @@ -37,12 +39,12 @@ public SpanAttributes()
/// Initializes a new instance of the <see cref="SpanAttributes"/> class.
/// </summary>
/// <param name="attributes">Initial attributes to store in the collection.</param>
public SpanAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
public SpanAttributes(IEnumerable<KeyValuePair<string, object?>> attributes)
: this()
{
Guard.ThrowIfNull(attributes);

foreach (KeyValuePair<string, object> kvp in attributes)
foreach (KeyValuePair<string, object?> kvp in attributes)
{
this.AddInternal(kvp.Key, kvp.Value);
}
Expand All @@ -65,7 +67,7 @@ public void Add(string key, long value)
/// </summary>
/// <param name="key">Entry key.</param>
/// <param name="value">Entry value.</param>
public void Add(string key, string value)
public void Add(string key, string? value)
{
this.AddInternal(key, value);
}
Expand Down Expand Up @@ -95,7 +97,7 @@ public void Add(string key, double value)
/// </summary>
/// <param name="key">Entry key.</param>
/// <param name="values">Entry value.</param>
public void Add(string key, long[] values)
public void Add(string key, long[]? values)
{
this.AddInternal(key, values);
}
Expand All @@ -105,7 +107,7 @@ public void Add(string key, long[] values)
/// </summary>
/// <param name="key">Entry key.</param>
/// <param name="values">Entry value.</param>
public void Add(string key, string[] values)
public void Add(string key, string?[]? values)
{
this.AddInternal(key, values);
}
Expand All @@ -115,7 +117,7 @@ public void Add(string key, string[] values)
/// </summary>
/// <param name="key">Entry key.</param>
/// <param name="values">Entry value.</param>
public void Add(string key, bool[] values)
public void Add(string key, bool[]? values)
{
this.AddInternal(key, values);
}
Expand All @@ -125,12 +127,12 @@ public void Add(string key, bool[] values)
/// </summary>
/// <param name="key">Entry key.</param>
/// <param name="values">Entry value.</param>
public void Add(string key, double[] values)
public void Add(string key, double[]? values)
{
this.AddInternal(key, values);
}

private void AddInternal(string key, object value)
private void AddInternal(string key, object? value)
{
Guard.ThrowIfNull(key);

Expand Down
69 changes: 25 additions & 44 deletions src/OpenTelemetry.Api/Trace/SpanContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#nullable enable

using System.Diagnostics;
using OpenTelemetry.Context.Propagation;

Expand All @@ -39,7 +42,12 @@ namespace OpenTelemetry.Trace;
/// associate with the <see cref="SpanContext"/>.</param>
/// <param name="isRemote">The value indicating whether this <see cref="SpanContext"/> was propagated from the remote parent.</param>
/// <param name="traceState">The traceState to associate with the <see cref="SpanContext"/>.</param>
public SpanContext(in ActivityTraceId traceId, in ActivitySpanId spanId, ActivityTraceFlags traceFlags, bool isRemote = false, IEnumerable<KeyValuePair<string, string>> traceState = null)
public SpanContext(
in ActivityTraceId traceId,
in ActivitySpanId spanId,
ActivityTraceFlags traceFlags,
bool isRemote = false,
IEnumerable<KeyValuePair<string, string>>? traceState = null)
{
this.ActivityContext = new ActivityContext(traceId, spanId, traceFlags, TraceStateUtilsNew.GetString(traceState), isRemote);
}
Expand All @@ -57,46 +65,26 @@ public SpanContext(in ActivityContext activityContext)
/// Gets the <see cref="ActivityTraceId"/> associated with this <see cref="SpanContext"/>.
/// </summary>
public ActivityTraceId TraceId
{
get
{
return this.ActivityContext.TraceId;
}
}
=> this.ActivityContext.TraceId;

/// <summary>
/// Gets the <see cref="ActivitySpanId"/> associated with this <see cref="SpanContext"/>.
/// </summary>
public ActivitySpanId SpanId
{
get
{
return this.ActivityContext.SpanId;
}
}
=> this.ActivityContext.SpanId;

/// <summary>
/// Gets the <see cref="ActivityTraceFlags"/> associated with this <see cref="SpanContext"/>.
/// </summary>
public ActivityTraceFlags TraceFlags
{
get
{
return this.ActivityContext.TraceFlags;
}
}
=> this.ActivityContext.TraceFlags;

/// <summary>
/// Gets a value indicating whether this <see cref="SpanContext" />
/// was propagated from a remote parent.
/// </summary>
public bool IsRemote
{
get
{
return this.ActivityContext.IsRemote;
}
}
=> this.ActivityContext.IsRemote;

/// <summary>
/// Gets a value indicating whether this <see cref="SpanContext"/> is valid.
Expand Down Expand Up @@ -125,47 +113,40 @@ public IEnumerable<KeyValuePair<string, string>> TraceState
/// Converts a <see cref="SpanContext"/> into an <see cref="ActivityContext"/>.
/// </summary>
/// <param name="spanContext"><see cref="SpanContext"/> source.</param>
public static implicit operator ActivityContext(SpanContext spanContext) => spanContext.ActivityContext;
public static implicit operator ActivityContext(SpanContext spanContext)
=> spanContext.ActivityContext;

/// <summary>
/// Compare two <see cref="SpanContext"/> for equality.
/// </summary>
/// <param name="spanContext1">First SpanContext to compare.</param>
/// <param name="spanContext2">Second SpanContext to compare.</param>
public static bool operator ==(SpanContext spanContext1, SpanContext spanContext2) => spanContext1.Equals(spanContext2);
public static bool operator ==(SpanContext spanContext1, SpanContext spanContext2)
=> spanContext1.Equals(spanContext2);

/// <summary>
/// Compare two <see cref="SpanContext"/> for not equality.
/// </summary>
/// <param name="spanContext1">First SpanContext to compare.</param>
/// <param name="spanContext2">Second SpanContext to compare.</param>
public static bool operator !=(SpanContext spanContext1, SpanContext spanContext2) => !spanContext1.Equals(spanContext2);
public static bool operator !=(SpanContext spanContext1, SpanContext spanContext2)
=> !spanContext1.Equals(spanContext2);

/// <inheritdoc/>
public override int GetHashCode()
{
return this.ActivityContext.GetHashCode();
}
=> this.ActivityContext.GetHashCode();

/// <inheritdoc/>
public override bool Equals(object obj)
{
return (obj is SpanContext ctx) && this.ActivityContext.Equals(ctx.ActivityContext);
}
public override bool Equals(object? obj)
=> obj is SpanContext ctx && this.Equals(ctx);

/// <inheritdoc/>
public bool Equals(SpanContext other)
{
return this.ActivityContext.Equals(other.ActivityContext);
}
=> this.ActivityContext.Equals(other.ActivityContext);

private static bool IsTraceIdValid(ActivityTraceId traceId)
{
return traceId != default;
}
=> traceId != default;

private static bool IsSpanIdValid(ActivitySpanId spanId)
{
return spanId != default;
}
=> spanId != default;
}
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Api/Trace/SpanKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

namespace OpenTelemetry.Trace;

/// <summary>
Expand Down
Loading