forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamplingResult.cs
More file actions
154 lines (136 loc) · 6.82 KB
/
SamplingResult.cs
File metadata and controls
154 lines (136 loc) · 6.82 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// <copyright file="SamplingResult.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Collections.Generic;
using System.Linq;
namespace OpenTelemetry.Trace
{
/// <summary>
/// Sampling result.
/// </summary>
public readonly struct SamplingResult : System.IEquatable<SamplingResult>
{
/// <summary>
/// Initializes a new instance of the <see cref="SamplingResult"/> struct.
/// </summary>
/// <param name="decision"> indicates whether an activity object is recorded and sampled.</param>
public SamplingResult(SamplingDecision decision)
{
this.Decision = decision;
this.Attributes = Enumerable.Empty<KeyValuePair<string, object>>();
this.TraceStateString = null;
}
/// <summary>
/// Initializes a new instance of the <see cref="SamplingResult"/> struct.
/// </summary>
/// <param name="isSampled"> True if sampled, false otherwise.</param>
public SamplingResult(bool isSampled)
{
this.Decision = isSampled ? SamplingDecision.RecordAndSample : SamplingDecision.Drop;
this.Attributes = Enumerable.Empty<KeyValuePair<string, object>>();
this.TraceStateString = null;
}
/// <summary>
/// Initializes a new instance of the <see cref="SamplingResult"/> struct.
/// </summary>
/// <param name="decision">indicates whether an activity object is recorded and sampled.</param>
/// <param name="attributes">Attributes associated with the sampling decision. Attributes list passed to
/// this method must be immutable. Mutations of the collection and/or attribute values may lead to unexpected behavior.</param>
public SamplingResult(SamplingDecision decision, IEnumerable<KeyValuePair<string, object>> attributes)
{
this.Decision = decision;
// Note: Decision object takes ownership of the collection.
// Current implementation has no means to ensure the collection will not be modified by the caller.
// If this behavior will be abused we must switch to cloning of the collection.
this.Attributes = attributes;
this.TraceStateString = null;
}
/// <summary>
/// Initializes a new instance of the <see cref="SamplingResult"/> struct.
/// </summary>
/// <param name="decision">indicates whether an activity object is recorded and sampled.</param>
/// <param name="traceStateString">traceStateString associated with the created Activity.</param>
public SamplingResult(SamplingDecision decision, string traceStateString)
{
this.Decision = decision;
this.Attributes = Enumerable.Empty<KeyValuePair<string, object>>();
this.TraceStateString = traceStateString;
}
/// <summary>
/// Initializes a new instance of the <see cref="SamplingResult"/> struct.
/// </summary>
/// <param name="decision">indicates whether an activity object is recorded and sampled.</param>
/// <param name="attributes">Attributes associated with the sampling decision. Attributes list passed to
/// this method must be immutable. Mutations of the collection and/or attribute values may lead to unexpected behavior.</param>
/// <param name="traceStateString">traceStateString associated with the created Activity.</param>
public SamplingResult(SamplingDecision decision, IEnumerable<KeyValuePair<string, object>> attributes, string traceStateString)
{
this.Decision = decision;
// Note: Decision object takes ownership of the collection.
// Current implementation has no means to ensure the collection will not be modified by the caller.
// If this behavior will be abused we must switch to cloning of the collection.
this.Attributes = attributes;
this.TraceStateString = traceStateString;
}
/// <summary>
/// Gets a value indicating indicates whether an activity object is recorded and sampled.
/// </summary>
public SamplingDecision Decision { get; }
/// <summary>
/// Gets a map of attributes associated with the sampling decision.
/// </summary>
public IEnumerable<KeyValuePair<string, object>> Attributes { get; }
/// <summary>
/// Gets the Tracestate.
/// </summary>
public string TraceStateString { get; }
/// <summary>
/// Compare two <see cref="SamplingResult"/> for equality.
/// </summary>
/// <param name="decision1">First Decision to compare.</param>
/// <param name="decision2">Second Decision to compare.</param>
public static bool operator ==(SamplingResult decision1, SamplingResult decision2) => decision1.Equals(decision2);
/// <summary>
/// Compare two <see cref="SamplingResult"/> for not equality.
/// </summary>
/// <param name="decision1">First Decision to compare.</param>
/// <param name="decision2">Second Decision to compare.</param>
public static bool operator !=(SamplingResult decision1, SamplingResult decision2) => !decision1.Equals(decision2);
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj is not SamplingResult)
{
return false;
}
var that = (SamplingResult)obj;
return this.Decision == that.Decision && this.Attributes == that.Attributes && this.TraceStateString == that.TraceStateString;
}
/// <inheritdoc/>
public override int GetHashCode()
{
var result = 1;
result = (31 * result) + this.Decision.GetHashCode();
result = (31 * result) + this.Attributes.GetHashCode();
result = this.TraceStateString == null ? result : (result * 31) + this.TraceStateString.GetHashCode();
return result;
}
/// <inheritdoc/>
public bool Equals(SamplingResult other)
{
return this.Decision == other.Decision && this.Attributes.SequenceEqual(other.Attributes) && this.TraceStateString == other.TraceStateString;
}
}
}