Added Sql connection-level attributes (from spec) as an opt-in feature. (#782)

* Added Sql connection-level attributes (from spec) as an opt-in feature.

* Code review feedback.
This commit is contained in:
Mikel Blanchard 2020-07-07 22:37:08 -07:00 committed by GitHub
parent 8f0cf0bd1a
commit 049f72c0b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 243 additions and 33 deletions

View File

@ -25,12 +25,13 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
{
private static readonly Dictionary<string, int> PeerServiceKeyResolutionDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
[SpanAttributeConstants.PeerServiceKey] = 0, // peer.service primary.
["net.peer.name"] = 1, // peer.service first alternative.
["peer.hostname"] = 2, // peer.service second alternative.
["peer.address"] = 2, // peer.service second alternative.
[SpanAttributeConstants.PeerServiceKey] = 0, // priority 0 (highest).
[SpanAttributeConstants.NetPeerName] = 1,
[SpanAttributeConstants.NetPeerIp] = 2,
["peer.hostname"] = 2,
["peer.address"] = 2,
["http.host"] = 3, // peer.service for Http.
["db.instance"] = 4, // peer.service for Redis.
["db.instance"] = 3, // peer.service for Redis.
};
private static readonly DictionaryEnumerator<string, string, TagState>.ForEachDelegate ProcessActivityTagRef = ProcessActivityTag;

View File

@ -45,12 +45,13 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
private static readonly Dictionary<string, int> PeerServiceKeyResolutionDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
[SpanAttributeConstants.PeerServiceKey] = 0, // peer.service primary.
["net.peer.name"] = 1, // peer.service first alternative.
["peer.hostname"] = 2, // peer.service second alternative.
["peer.address"] = 2, // peer.service second alternative.
[SpanAttributeConstants.PeerServiceKey] = 0, // priority 0 (highest).
[SpanAttributeConstants.NetPeerName] = 1,
[SpanAttributeConstants.NetPeerIp] = 2,
["peer.hostname"] = 2,
["peer.address"] = 2,
["http.host"] = 3, // peer.service for Http.
["db.instance"] = 4, // peer.service for Redis.
["db.instance"] = 3, // peer.service for Redis.
};
private static readonly DictionaryEnumerator<string, object, TagState>.ForEachDelegate ProcessAttributeRef = ProcessAttribute;

View File

@ -32,12 +32,13 @@ namespace OpenTelemetry.Exporter.Zipkin.Implementation
private static readonly Dictionary<string, int> RemoteEndpointServiceNameKeyResolutionDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
[SpanAttributeConstants.PeerServiceKey] = 0, // RemoteEndpoint.ServiceName primary.
["net.peer.name"] = 1, // RemoteEndpoint.ServiceName first alternative.
["peer.hostname"] = 2, // RemoteEndpoint.ServiceName second alternative.
["peer.address"] = 2, // RemoteEndpoint.ServiceName second alternative.
[SpanAttributeConstants.PeerServiceKey] = 0, // priority 0 (highest).
[SpanAttributeConstants.NetPeerName] = 1,
[SpanAttributeConstants.NetPeerIp] = 2,
["peer.hostname"] = 2,
["peer.address"] = 2,
["http.host"] = 3, // RemoteEndpoint.ServiceName for Http.
["db.instance"] = 4, // RemoteEndpoint.ServiceName for Redis.
["db.instance"] = 3, // RemoteEndpoint.ServiceName for Redis.
};
private static readonly string InvalidSpanId = default(ActivitySpanId).ToHexString();

View File

@ -28,12 +28,13 @@ namespace OpenTelemetry.Exporter.Zipkin.Implementation
{
private static readonly Dictionary<string, int> RemoteEndpointServiceNameKeyResolutionDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
[SpanAttributeConstants.PeerServiceKey] = 0, // RemoteEndpoint.ServiceName primary.
["net.peer.name"] = 1, // RemoteEndpoint.ServiceName first alternative.
["peer.hostname"] = 2, // RemoteEndpoint.ServiceName second alternative.
["peer.address"] = 2, // RemoteEndpoint.ServiceName second alternative.
[SpanAttributeConstants.PeerServiceKey] = 0, // priority 0 (highest).
[SpanAttributeConstants.NetPeerName] = 1,
[SpanAttributeConstants.NetPeerIp] = 2,
["peer.hostname"] = 2,
["peer.address"] = 2,
["http.host"] = 3, // RemoteEndpoint.ServiceName for Http.
["db.instance"] = 4, // RemoteEndpoint.ServiceName for Redis.
["db.instance"] = 3, // RemoteEndpoint.ServiceName for Redis.
};
private static readonly ConcurrentDictionary<string, ZipkinEndpoint> LocalEndpointCache = new ConcurrentDictionary<string, ZipkinEndpoint>();

View File

@ -85,9 +85,10 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
activity.AddTag(SpanAttributeConstants.ComponentKey, "sql");
activity.AddTag(SpanAttributeConstants.DatabaseSystemKey, MicrosoftSqlServerDatabaseSystemName);
activity.AddTag(SpanAttributeConstants.PeerServiceKey, (string)dataSource);
activity.AddTag(SpanAttributeConstants.DatabaseNameKey, (string)database);
this.options.AddConnectionLevelDetailsToActivity((string)dataSource, activity);
if (this.commandTypeFetcher.Fetch(command) is CommandType commandType)
{
switch (commandType)

View File

@ -117,9 +117,10 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
activity.AddTag(SpanAttributeConstants.ComponentKey, "sql");
activity.AddTag(SpanAttributeConstants.DatabaseSystemKey, SqlClientDiagnosticListener.MicrosoftSqlServerDatabaseSystemName);
activity.AddTag(SpanAttributeConstants.PeerServiceKey, (string)eventData.Payload[1]);
activity.AddTag(SpanAttributeConstants.DatabaseNameKey, databaseName);
this.options.AddConnectionLevelDetailsToActivity((string)eventData.Payload[1], activity);
string commandText = (string)eventData.Payload[3];
if (string.IsNullOrEmpty(commandText))
{

View File

@ -13,7 +13,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Concurrent;
using System.Data;
using System.Diagnostics;
using System.Text.RegularExpressions;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.Dependencies
{
@ -22,12 +27,18 @@ namespace OpenTelemetry.Instrumentation.Dependencies
/// </summary>
public class SqlClientInstrumentationOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="SqlClientInstrumentationOptions"/> class.
/// </summary>
public SqlClientInstrumentationOptions()
{
}
internal const string MicrosoftSqlServerDatabaseInstanceName = "db.mssql.instance_name";
/*
* Match...
* serverName
* serverName[ ]\\[ ]instanceName
* serverName[ ],[ ]port
* serverName[ ]\\[ ]instanceName[ ],[ ]port
* [ ] can be any number of white-space, SQL allows it for some reason.
*/
private static readonly Regex DataSourceRegex = new Regex("^(.*?)\\s*(?:[\\\\,]|$)\\s*(.*?)\\s*(?:,|$)\\s*(.*)$", RegexOptions.Compiled);
private static readonly ConcurrentDictionary<string, SqlConnectionDetails> ConnectionDetailCache = new ConcurrentDictionary<string, SqlConnectionDetails>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/> should capture the names of <see cref="CommandType.StoredProcedure"/> commands. Default value: True.
@ -38,5 +49,110 @@ namespace OpenTelemetry.Instrumentation.Dependencies
/// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/> should capture the text of <see cref="CommandType.Text"/> commands. Default value: False.
/// </summary>
public bool CaptureTextCommandContent { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/> should parse the DataSource on a SqlConnection into server name, instance name, and/or port connection-level attributes. Default value: False.
/// </summary>
/// <remarks>
/// The default behavior is to set the SqlConnection DataSource as the peer.service tag. If enabled, SqlConnection DataSource will be parsed and the server name will be sent as the net.peer.name or net.peer.ip tag, the instance name will be sent as the db.mssql.instance_name tag, and the port will be sent as the net.peer.port tag if it is not 1433 (the default port).
/// </remarks>
public bool EnableConnectionLevelAttributes { get; set; } = false;
internal static SqlConnectionDetails ParseDataSource(string dataSource)
{
Match match = DataSourceRegex.Match(dataSource);
string serverHostName = match.Groups[1].Value;
string serverIpAddress = null;
var uriHostNameType = Uri.CheckHostName(serverHostName);
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
{
serverIpAddress = serverHostName;
serverHostName = null;
}
string instanceName;
string port;
if (match.Groups[3].Length > 0)
{
instanceName = match.Groups[2].Value;
port = match.Groups[3].Value;
if (port == "1433")
{
port = null;
}
}
else if (int.TryParse(match.Groups[2].Value, out int parsedPort))
{
port = parsedPort == 1433 ? null : match.Groups[2].Value;
instanceName = null;
}
else
{
instanceName = match.Groups[2].Value;
if (string.IsNullOrEmpty(instanceName))
{
instanceName = null;
}
port = null;
}
return new SqlConnectionDetails
{
ServerHostName = serverHostName,
ServerIpAddress = serverIpAddress,
InstanceName = instanceName,
Port = port,
};
}
internal void AddConnectionLevelDetailsToActivity(string dataSource, Activity sqlActivity)
{
if (!this.EnableConnectionLevelAttributes)
{
sqlActivity.AddTag(SpanAttributeConstants.PeerServiceKey, dataSource);
}
else
{
if (!ConnectionDetailCache.TryGetValue(dataSource, out SqlConnectionDetails connectionDetails))
{
connectionDetails = ParseDataSource(dataSource);
ConnectionDetailCache.TryAdd(dataSource, connectionDetails);
}
if (!string.IsNullOrEmpty(connectionDetails.ServerHostName))
{
sqlActivity.AddTag(SpanAttributeConstants.NetPeerName, connectionDetails.ServerHostName);
}
else
{
sqlActivity.AddTag(SpanAttributeConstants.NetPeerIp, connectionDetails.ServerIpAddress);
}
if (!string.IsNullOrEmpty(connectionDetails.InstanceName))
{
sqlActivity.AddTag(MicrosoftSqlServerDatabaseInstanceName, connectionDetails.InstanceName);
}
if (!string.IsNullOrEmpty(connectionDetails.Port))
{
sqlActivity.AddTag(SpanAttributeConstants.NetPeerPort, connectionDetails.Port);
}
}
}
internal class SqlConnectionDetails
{
public string ServerHostName { get; set; }
public string ServerIpAddress { get; set; }
public string InstanceName { get; set; }
public string Port { get; set; }
}
}
}

View File

@ -0,0 +1,46 @@
// <copyright file="SqlClientInstrumentationOptionsTests.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 Xunit;
namespace OpenTelemetry.Instrumentation.Dependencies.Tests
{
public class SqlClientInstrumentationOptionsTests
{
[Theory]
[InlineData("localhost", "localhost", null, null, null)]
[InlineData("127.0.0.1", null, "127.0.0.1", null, null)]
[InlineData("127.0.0.1,1433", null, "127.0.0.1", null, null)]
[InlineData("127.0.0.1, 1818", null, "127.0.0.1", null, "1818")]
[InlineData("127.0.0.1 \\ instanceName", null, "127.0.0.1", "instanceName", null)]
[InlineData("127.0.0.1\\instanceName, 1818", null, "127.0.0.1", "instanceName", "1818")]
public void ParseDataSourceTests(
string dataSource,
string expectedServerHostName,
string expectedServerIpAddress,
string expectedInstanceName,
string expectedPort)
{
var sqlConnectionDetails = SqlClientInstrumentationOptions.ParseDataSource(dataSource);
Assert.NotNull(sqlConnectionDetails);
Assert.Equal(expectedServerHostName, sqlConnectionDetails.ServerHostName);
Assert.Equal(expectedServerIpAddress, sqlConnectionDetails.ServerIpAddress);
Assert.Equal(expectedInstanceName, sqlConnectionDetails.InstanceName);
Assert.Equal(expectedPort, sqlConnectionDetails.Port);
}
}
}

View File

@ -93,8 +93,14 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Tests
[InlineData(CommandType.Text, "select 1/1", false)]
[InlineData(CommandType.Text, "select 1/0", false, true)]
[InlineData(CommandType.StoredProcedure, "sp_who", false)]
[InlineData(CommandType.StoredProcedure, "sp_who", true)]
public void EventSourceFakeTests(CommandType commandType, string commandText, bool captureText, bool isFailure = false, int sqlExceptionNumber = 0)
[InlineData(CommandType.StoredProcedure, "sp_who", true, false, 0, true)]
public void EventSourceFakeTests(
CommandType commandType,
string commandText,
bool captureText,
bool isFailure = false,
int sqlExceptionNumber = 0,
bool enableConnectionLevelAttributes = false)
{
using FakeBehavingSqlEventSource fakeSqlEventSource = new FakeBehavingSqlEventSource();
@ -105,6 +111,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Tests
b.AddSqlClientDependencyInstrumentation(options =>
{
options.CaptureStoredProcedureCommandName = captureText;
options.EnableConnectionLevelAttributes = enableConnectionLevelAttributes;
});
});
@ -129,7 +136,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Tests
var activity = (Activity)activityProcessor.Invocations[1].Arguments[0];
VerifyActivityData(commandType, commandText, captureText, isFailure, "127.0.0.1", activity);
VerifyActivityData(commandType, commandText, captureText, isFailure, "127.0.0.1", activity, enableConnectionLevelAttributes);
}
[Fact]
@ -168,13 +175,48 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Tests
Assert.Equal(0, activityProcessor.Invocations.Count);
}
private static void VerifyActivityData(CommandType commandType, string commandText, bool captureText, bool isFailure, string dataSource, Activity activity)
private static void VerifyActivityData(
CommandType commandType,
string commandText,
bool captureText,
bool isFailure,
string dataSource,
Activity activity,
bool enableConnectionLevelAttributes = false)
{
Assert.Equal("master", activity.DisplayName);
Assert.Equal(ActivityKind.Client, activity.Kind);
Assert.Equal("sql", activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.ComponentKey).Value);
Assert.Equal(SqlClientDiagnosticListener.MicrosoftSqlServerDatabaseSystemName, activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.DatabaseSystemKey).Value);
Assert.Equal(dataSource, activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.PeerServiceKey).Value);
if (!enableConnectionLevelAttributes)
{
Assert.Equal(dataSource, activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.PeerServiceKey).Value);
}
else
{
var connectionDetails = SqlClientInstrumentationOptions.ParseDataSource(dataSource);
if (!string.IsNullOrEmpty(connectionDetails.ServerHostName))
{
Assert.Equal(connectionDetails.ServerHostName, activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.NetPeerName).Value);
}
else
{
Assert.Equal(connectionDetails.ServerIpAddress, activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.NetPeerIp).Value);
}
if (!string.IsNullOrEmpty(connectionDetails.InstanceName))
{
Assert.Equal(connectionDetails.InstanceName, activity.Tags.FirstOrDefault(t => t.Key == SqlClientInstrumentationOptions.MicrosoftSqlServerDatabaseInstanceName).Value);
}
if (!string.IsNullOrEmpty(connectionDetails.Port))
{
Assert.Equal(connectionDetails.Port, activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.NetPeerPort).Value);
}
}
Assert.Equal("master", activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.DatabaseNameKey).Value);
Assert.Equal(commandType.ToString(), activity.Tags.FirstOrDefault(t => t.Key == SpanAttributeConstants.DatabaseStatementTypeKey).Value);
if (commandType == CommandType.StoredProcedure)