-
Notifications
You must be signed in to change notification settings - Fork 889
Expand file tree
/
Copy pathSamplingResult.cs
More file actions
108 lines (96 loc) · 4.41 KB
/
SamplingResult.cs
File metadata and controls
108 lines (96 loc) · 4.41 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
// <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 decision.
/// </summary>
public readonly struct 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>>();
}
/// <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.RecordAndSampled : SamplingDecision.NotRecord;
this.Attributes = Enumerable.Empty<KeyValuePair<string, object>>();
}
/// <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;
}
/// <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>
/// 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 SamplingResult))
{
return false;
}
var that = (SamplingResult)obj;
return this.Decision == that.Decision && this.Attributes == that.Attributes;
}
/// <inheritdoc/>
public override int GetHashCode()
{
var result = 1;
result = (31 * result) + this.Decision.GetHashCode();
result = (31 * result) + this.Attributes.GetHashCode();
return result;
}
}
}