-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathStatus.cs
108 lines (92 loc) · 3.35 KB
/
Status.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
namespace OpenTelemetry.Trace;
/// <summary>
/// Span execution status.
/// </summary>
public readonly struct Status : IEquatable<Status>
{
/// <summary>
/// The operation completed successfully.
/// </summary>
public static readonly Status Ok = new(StatusCode.Ok);
/// <summary>
/// The default status.
/// </summary>
public static readonly Status Unset = new(StatusCode.Unset);
/// <summary>
/// The operation contains an error.
/// </summary>
public static readonly Status Error = new(StatusCode.Error);
internal Status(StatusCode statusCode, string? description = null)
{
this.StatusCode = statusCode;
this.Description = description;
}
/// <summary>
/// Gets the canonical code from this status.
/// </summary>
public StatusCode StatusCode { get; }
/// <summary>
/// Gets the status description.
/// </summary>
public string? Description { get; }
/// <summary>
/// Compare two <see cref="Status"/> for equality.
/// </summary>
/// <param name="status1">First Status to compare.</param>
/// <param name="status2">Second Status to compare.</param>
public static bool operator ==(Status status1, Status status2) => status1.Equals(status2);
/// <summary>
/// Compare two <see cref="Status"/> for not equality.
/// </summary>
/// <param name="status1">First Status to compare.</param>
/// <param name="status2">Second Status to compare.</param>
public static bool operator !=(Status status1, Status status2) => !status1.Equals(status2);
/// <summary>
/// Returns a new instance of a status with the description populated.
/// </summary>
/// <remarks>
/// Note: Status Description is only valid for <see
/// cref="StatusCode.Error"/> Status and will be ignored for all other
/// <see cref="Trace.StatusCode"/> values. See the <a
/// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#set-status">Status
/// API</a> for details.
/// </remarks>
/// <param name="description">Description of the status.</param>
/// <returns>New instance of the status class with the description populated.</returns>
public Status WithDescription(string? description)
{
if (this.StatusCode != StatusCode.Error || this.Description == description)
{
return this;
}
return new Status(this.StatusCode, description);
}
/// <inheritdoc/>
public override bool Equals(object? obj)
=> obj is Status status && this.Equals(status);
/// <inheritdoc/>
public override int GetHashCode()
{
var hash = 17;
unchecked
{
hash = (31 * hash) + this.StatusCode.GetHashCode();
hash = (31 * hash) + (this.Description?.GetHashCode() ?? 0);
}
return hash;
}
/// <inheritdoc/>
public override string ToString()
{
return nameof(Status)
+ "{"
+ nameof(this.StatusCode) + "=" + this.StatusCode + ", "
+ nameof(this.Description) + "=" + this.Description
+ "}";
}
/// <inheritdoc/>
public bool Equals(Status other)
=> this.StatusCode == other.StatusCode && this.Description == other.Description;
}