-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathActivityExtensions.cs
More file actions
121 lines (113 loc) · 5.73 KB
/
ActivityExtensions.cs
File metadata and controls
121 lines (113 loc) · 5.73 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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Apache.Arrow.Adbc.Tracing
{
public static class ActivityExtensions
{
/// <summary>
/// Add a new <see cref="ActivityEvent"/> object to the <see cref="Activity.Events" /> list.
/// </summary>
/// <param name="eventName">The name of the event.</param>
/// <param name="tags">The optional list of tags to attach to the event.</param>
/// <returns><see langword="this"/> for convenient chaining.</returns>
public static Activity? AddEvent(this Activity? activity, string eventName, IReadOnlyList<KeyValuePair<string, object?>>? tags = default)
{
if (activity == null) return activity;
ActivityTagsCollection? tagsCollection = tags == null ? null : new ActivityTagsCollection(tags);
return activity?.AddEvent(new ActivityEvent(eventName, tags: tagsCollection));
}
/// <summary>
/// Add a new <see cref="ActivityLink"/> to the <see cref="Activity.Links"/> list.
/// </summary>
/// <param name="traceParent">The traceParent id for the associated <see cref="ActivityContext"/>.</param>
/// <param name="tags">The optional list of tags to attach to the event.</param>
/// <returns><see langword="this"/> for convenient chaining.</returns>
public static Activity? AddLink(this Activity? activity, string traceParent, IReadOnlyList<KeyValuePair<string, object?>>? tags = default)
{
if (activity == null) return activity;
ActivityTagsCollection? tagsCollection = tags == null ? null : new ActivityTagsCollection(tags);
return activity?.AddLink(new ActivityLink(ActivityContext.Parse(traceParent, null), tags: tagsCollection));
}
/// <summary>
/// Update the Activity to have a tag with an additional 'key' and value 'value'.
/// This shows up in the <see cref="TagObjects"/> enumeration. It is meant for information that
/// is useful to log but not needed for runtime control (for the latter, <see cref="Baggage"/>)
/// </summary>
/// <returns><see langword="this" /> for convenient chaining.</returns>
/// <param name="key">The tag key name as a function</param>
/// <param name="value">The tag value mapped to the input key as a function</param>
public static Activity? AddTag(this Activity? activity, string key, Func<object?> value)
{
return activity?.AddTag(key, value());
}
/// <summary>
/// Update the Activity to have a tag with an additional 'key' and value 'value'.
/// This shows up in the <see cref="TagObjects"/> enumeration. It is meant for information that
/// is useful to log but not needed for runtime control (for the latter, <see cref="Baggage"/>)
/// </summary>
/// <returns><see langword="this" /> for convenient chaining.</returns>
/// <param name="key">The tag key name as a function</param>
/// <param name="value">The tag value mapped to the input key</param>
/// /// <param name="condition">The condition to check before adding the tag</param>
public static Activity? AddConditionalTag(this Activity? activity, string key, string? value, bool condition)
{
if (condition)
{
return activity?.AddTag(key, value);
}
return activity;
}
/// <summary>
/// Update the Activity to have a tag with an additional 'key' and value 'value'.
/// This shows up in the <see cref="TagObjects"/> enumeration. It is meant for information that
/// is useful to log but not needed for runtime control (for the latter, <see cref="Baggage"/>)
/// </summary>
/// <returns><see langword="this" /> for convenient chaining.</returns>
/// <param name="key">The tag key name</param>
/// <param name="value">The tag value mapped to the input key as a function</param>
/// <param name="guidFormat">The format indicator for 16-byte GUID arrays.</param>
public static Activity? AddTag(this Activity? activity, string key, byte[]? value, string? guidFormat)
{
if (value == null)
{
return activity?.AddTag(key, value);
}
if (value.Length == 16)
{
return activity?.AddTag(key, new Guid(value).ToString(guidFormat));
}
return activity?.AddTag(key, ToHexString(value));
}
#if NET5_0_OR_GREATER
private static string ToHexString(byte[] value) => Convert.ToHexString(value);
#else
private static string ToHexString(byte[] value)
{
StringBuilder hex = new(value.Length * 2);
foreach (byte b in value)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
#endif
}
}