-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDBInstrumentation.cs
168 lines (138 loc) · 5.12 KB
/
MongoDBInstrumentation.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using OpenTelemetry.AutoInstrumentation.DuckTyping;
using OpenTelemetry.AutoInstrumentation.Instrumentations.MongoDB.DuckTypes;
namespace OpenTelemetry.AutoInstrumentation.Instrumentations.MongoDB;
internal static class MongoDBInstrumentation
{
private static readonly ActivitySource Source = new("OpenTelemetry.AutoInstrumentation.MongoDB");
public static Activity? StartDatabaseActivity(
object? instance,
IConnection connection)
{
if (instance == null)
{
return null;
}
if (!TryGetDatabaseName(instance, out var database))
{
return null;
}
if (!TryGetQueryDetails(instance, out var collection, out var operationName))
{
return null;
}
if (!TryGetNetworkAttributes(connection, out var serverAddress, out var serverName, out var serverPort))
{
return null;
}
var spanName = $"{operationName} {collection}";
var activity = Source.StartActivity(name: spanName, kind: ActivityKind.Client);
if (activity is { IsAllDataRequested: true })
{
SetCommonAttributes(activity, operationName, collection, database, serverAddress, serverName, serverPort);
}
return activity;
}
private static void SetCommonAttributes(
Activity activity,
string? operationName,
string? collection,
string? database,
string? serverAddress,
string? serverName,
int? serverPort)
{
activity.SetTag(DatabaseAttributes.Keys.DbSystem, DatabaseAttributes.Values.MongoDB.MongoDbSystem);
activity.SetTag(DatabaseAttributes.Keys.DbCollectionName, collection);
activity.SetTag(DatabaseAttributes.Keys.DbNamespace, database);
activity.SetTag(DatabaseAttributes.Keys.DbOperationName, operationName);
if (!string.IsNullOrEmpty(serverAddress))
{
activity.SetTag(NetworkAttributes.Keys.NetworkPeerAddress, serverAddress);
}
if (!string.IsNullOrEmpty(serverName))
{
activity.SetTag(NetworkAttributes.Keys.ServerAddress, serverName);
}
if (serverPort is not null)
{
activity.SetTag(NetworkAttributes.Keys.ServerPort, serverPort);
activity.SetTag(NetworkAttributes.Keys.NetworkPeerPort, serverPort);
}
}
private static bool TryGetDatabaseName(object instance, out string? database)
{
database = null;
if (instance.TryDuckCast<IWireProtocolWithDatabaseNamespaceStruct>(out var protocolWithDatabaseNamespace)
&& protocolWithDatabaseNamespace.DatabaseNamespace is not null
&& protocolWithDatabaseNamespace.DatabaseNamespace.TryDuckCast<DatabaseNamespaceStruct>(out var databaseNamespace))
{
database = databaseNamespace.DatabaseName;
}
if (string.IsNullOrEmpty(database))
{
return false;
}
return true;
}
private static bool TryGetQueryDetails(object instance, out string? collection, out string? operationName)
{
collection = null;
operationName = null;
if (instance.TryDuckCast<IWireProtocolWithCommandStruct>(out var protocolWithCommand)
&& protocolWithCommand.Command is not null
&& protocolWithCommand.Command.TryDuckCast<IBsonDocumentProxy>(out var bsonDocument))
{
var firstElement = bsonDocument.GetElement(0);
var mongoOperationName = firstElement.Name;
if (mongoOperationName is "isMaster" or "hello")
{
return false;
}
collection = firstElement.Value?.ToString();
operationName = mongoOperationName;
}
if (string.IsNullOrEmpty(collection) || string.IsNullOrEmpty(operationName))
{
return false;
}
return true;
}
private static bool TryGetNetworkAttributes(IConnection connection, out string? serverAddress, out string? serverName, out int? serverPort)
{
serverAddress = null;
serverName = null;
serverPort = null;
if (connection.EndPoint == null)
{
return false;
}
if (connection.EndPoint is IPEndPoint ipEndPoint)
{
serverAddress = ipEndPoint.Address.ToString();
serverPort = ipEndPoint.Port;
serverName = Dns.GetHostEntry(serverAddress).ToString();
}
else if (connection.EndPoint is DnsEndPoint dnsEndPoint)
{
serverName = dnsEndPoint.Host;
serverPort = dnsEndPoint.Port;
var ipAddresses = Dns.GetHostAddresses(serverName);
if (ipAddresses.Length > 0)
{
serverAddress = ipAddresses[0].ToString();
}
}
return true;
}
}