-
Notifications
You must be signed in to change notification settings - Fork 783
/
Copy pathPeerServiceResolver.cs
93 lines (79 loc) · 2.99 KB
/
PeerServiceResolver.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Runtime.CompilerServices;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Exporter;
internal static class PeerServiceResolver
{
private static readonly Dictionary<string, int> PeerServiceKeyResolutionDictionary = new(StringComparer.OrdinalIgnoreCase)
{
[SemanticConventions.AttributePeerService] = 0, // priority 0 (highest).
["peer.hostname"] = 1,
["peer.address"] = 1,
[SemanticConventions.AttributeHttpHost] = 2, // peer.service for Http.
[SemanticConventions.AttributeDbInstance] = 2, // peer.service for Redis.
};
public interface IPeerServiceState
{
string? PeerService { get; set; }
int? PeerServicePriority { get; set; }
string? HostName { get; set; }
string? IpAddress { get; set; }
long Port { get; set; }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InspectTag<T>(ref T state, string key, string? value)
where T : struct, IPeerServiceState
{
if (PeerServiceKeyResolutionDictionary.TryGetValue(key, out int priority)
&& (state.PeerService == null || priority < state.PeerServicePriority))
{
state.PeerService = value;
state.PeerServicePriority = priority;
}
else if (key == SemanticConventions.AttributeNetPeerName)
{
state.HostName = value;
}
else if (key == SemanticConventions.AttributeNetPeerIp)
{
state.IpAddress = value;
}
else if (key == SemanticConventions.AttributeNetPeerPort && long.TryParse(value, out var port))
{
state.Port = port;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InspectTag<T>(ref T state, string key, long value)
where T : struct, IPeerServiceState
{
if (key == SemanticConventions.AttributeNetPeerPort)
{
state.Port = value;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Resolve<T>(ref T state, out string? peerServiceName, out bool addAsTag)
where T : struct, IPeerServiceState
{
peerServiceName = state.PeerService;
// If priority = 0 that means peer.service was included in tags
addAsTag = state.PeerServicePriority != 0;
if (addAsTag)
{
var hostNameOrIpAddress = state.HostName ?? state.IpAddress;
// peer.service has not already been included, but net.peer.name/ip and optionally net.peer.port are present
if (hostNameOrIpAddress != null)
{
peerServiceName = state.Port == default
? hostNameOrIpAddress
: $"{hostNameOrIpAddress}:{state.Port}";
}
else if (state.PeerService != null)
{
peerServiceName = state.PeerService;
}
}
}
}