Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSSQL and MySQL peer format can be changed to the same format as java by configuration #543

Merged
merged 4 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions skyapm-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Diagnostics.MassTran
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Utilities.StaticAccessor", "src\SkyApm.Utilities.StaticAccessor\SkyApm.Utilities.StaticAccessor.csproj", "{F82B5819-0CED-4C9B-9305-7E62DBFB1219}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PeerFormatters", "PeerFormatters", "{D122E6AE-6FE7-4C1A-826F-5964ABBF2C9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.PeerFormatters.SqlClient", "src\SkyApm.PeerFormatters.SqlClient\SkyApm.PeerFormatters.SqlClient.csproj", "{5DBE2053-EBAE-404F-A7B3-9CE3CD2152D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.PeerFormatters.MySqlConnector", "src\SkyApm.PeerFormatters.MySqlConnector\SkyApm.PeerFormatters.MySqlConnector.csproj", "{2A313B7E-CC41-4556-8055-D87266C398BF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -267,6 +273,14 @@ Global
{F82B5819-0CED-4C9B-9305-7E62DBFB1219}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F82B5819-0CED-4C9B-9305-7E62DBFB1219}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F82B5819-0CED-4C9B-9305-7E62DBFB1219}.Release|Any CPU.Build.0 = Release|Any CPU
{5DBE2053-EBAE-404F-A7B3-9CE3CD2152D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DBE2053-EBAE-404F-A7B3-9CE3CD2152D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DBE2053-EBAE-404F-A7B3-9CE3CD2152D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DBE2053-EBAE-404F-A7B3-9CE3CD2152D9}.Release|Any CPU.Build.0 = Release|Any CPU
{2A313B7E-CC41-4556-8055-D87266C398BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A313B7E-CC41-4556-8055-D87266C398BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A313B7E-CC41-4556-8055-D87266C398BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A313B7E-CC41-4556-8055-D87266C398BF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -317,6 +331,9 @@ Global
{136EAD07-A501-4308-9972-82E44F655735} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB}
{1C6A8B34-BB6A-44E0-A395-05ADFB094367} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{F82B5819-0CED-4C9B-9305-7E62DBFB1219} = {4BD917BC-D579-4C75-9939-41BF012A4EE2}
{D122E6AE-6FE7-4C1A-826F-5964ABBF2C9D} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{5DBE2053-EBAE-404F-A7B3-9CE3CD2152D9} = {D122E6AE-6FE7-4C1A-826F-5964ABBF2C9D}
{2A313B7E-CC41-4556-8055-D87266C398BF} = {D122E6AE-6FE7-4C1A-826F-5964ABBF2C9D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583}
Expand Down
2 changes: 2 additions & 0 deletions src/SkyApm.Abstractions/Config/TracingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ namespace SkyApm.Config
public class TracingConfig
{
public int ExceptionMaxDepth { get; set; } = 3;

public bool DbPeerSimpleFormat { get; set; } = false;
}
}
29 changes: 29 additions & 0 deletions src/SkyApm.Abstractions/Tracing/IDbPeerFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The SkyAPM 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.Data.Common;

namespace SkyApm.Tracing
{
public interface IDbPeerFormatter
{
bool Match(DbConnection connection);

string GetPeer(DbConnection connection);
}
}
30 changes: 30 additions & 0 deletions src/SkyApm.Abstractions/Tracing/IPeerFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The SkyAPM 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.Data.Common;
using System.Text;

namespace SkyApm.Tracing
{
public interface IPeerFormatter
{
string GetDbPeer(DbConnection connection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
using SkyApm;
using SkyApm.Agent.Hosting;
using SkyApm.Diagnostics.MSLogging;
using SkyApm.PeerFormatters.SqlClient;
using SkyApm.PeerFormatters.MySqlConnector;
using ILoggerFactory = SkyApm.Logging.ILoggerFactory;

namespace Microsoft.Extensions.DependencyInjection
Expand Down Expand Up @@ -69,14 +71,17 @@ private static IServiceCollection AddSkyAPMCore(this IServiceCollection services
services.AddSingleton<IHostedService, InstrumentationHostedService>();
services.AddSingleton<IEnvironmentProvider, HostingEnvironmentProvider>();
services.AddSingleton<ISkyApmLogDispatcher, AsyncQueueSkyApmLogDispatcher>();
services.AddSingleton<IPeerFormatter, PeerFormatter>();
services.AddTracing().AddSampling().AddGrpcTransport().AddSkyApmLogging();
var extensions = services.AddSkyApmExtensions()
.AddHttpClient()
.AddGrpcClient()
.AddSqlClient()
.AddGrpc()
.AddEntityFrameworkCore(c => c.AddPomeloMysql().AddNpgsql().AddSqlite())
.AddMSLogging();
.AddMSLogging()
.AddSqlClientPeerFormatter()
.AddMySqlConnectorPeerFormatter();

extensionsSetup?.Invoke(extensions);

Expand Down
2 changes: 2 additions & 0 deletions src/SkyApm.Agent.Hosting/SkyApm.Agent.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<ProjectReference Include="..\SkyApm.Diagnostics.HttpClient\SkyApm.Diagnostics.HttpClient.csproj" />
<ProjectReference Include="..\SkyApm.Diagnostics.MSLogging\SkyApm.Diagnostics.MSLogging.csproj" />
<ProjectReference Include="..\SkyApm.Diagnostics.SqlClient\SkyApm.Diagnostics.SqlClient.csproj" />
<ProjectReference Include="..\SkyApm.PeerFormatters.MySqlConnector\SkyApm.PeerFormatters.MySqlConnector.csproj" />
<ProjectReference Include="..\SkyApm.PeerFormatters.SqlClient\SkyApm.PeerFormatters.SqlClient.csproj" />
<ProjectReference Include="..\SkyApm.Transport.Grpc\SkyApm.Transport.Grpc.csproj" />
<ProjectReference Include="..\SkyApm.Utilities.Configuration\SkyApm.Utilities.Configuration.csproj" />
<ProjectReference Include="..\SkyApm.Utilities.DependencyInjection\SkyApm.Utilities.DependencyInjection.csproj" />
Expand Down
57 changes: 57 additions & 0 deletions src/SkyApm.Core/Tracing/PeerFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using SkyApm.Config;
using System.Collections.Concurrent;
using System.Collections.Generic;
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The SkyAPM 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.Data.Common;

namespace SkyApm.Tracing
{
public class PeerFormatter : IPeerFormatter
{
private readonly ConcurrentDictionary<string, string> _peerMap = new ConcurrentDictionary<string, string>();

private readonly IEnumerable<IDbPeerFormatter> _dbPeerFormatters;
private readonly TracingConfig _tracingConfig;

public PeerFormatter(IEnumerable<IDbPeerFormatter> dbPeerFormatters, IConfigAccessor configAccessor)
{
_dbPeerFormatters= dbPeerFormatters;
_tracingConfig = configAccessor.Get<TracingConfig>();
}

public string GetDbPeer(DbConnection connection)
{
if (!_tracingConfig.DbPeerSimpleFormat) return connection.DataSource;

return _peerMap.GetOrAdd($"{connection.GetType()}_{connection.DataSource}", k =>
{
foreach (var formatter in _dbPeerFormatters)
{
if (formatter.Match(connection))
{
return formatter.GetPeer(connection);
}
}

return connection.DataSource;
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@
*/

using SkyApm.Common;
using SkyApm.Tracing;
using System.Data.Common;

namespace SkyApm.Diagnostics.EntityFrameworkCore
{
public class NpgsqlEntityFrameworkCoreSpanMetadataProvider : IEntityFrameworkCoreSpanMetadataProvider
{
private readonly IPeerFormatter _peerFormatter;

public NpgsqlEntityFrameworkCoreSpanMetadataProvider(IPeerFormatter peerFormatter)
{
_peerFormatter = peerFormatter;
}

public StringOrIntValue Component { get; } = Common.Components.NPGSQL_ENTITYFRAMEWORKCORE_POSTGRESQL;

public bool Match(DbConnection connection)
Expand All @@ -32,7 +40,7 @@ public bool Match(DbConnection connection)

public string GetPeer(DbConnection connection)
{
return connection.DataSource;
return _peerFormatter.GetDbPeer(connection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@
*/

using SkyApm.Common;
using SkyApm.Tracing;
using System.Data.Common;

namespace SkyApm.Diagnostics.EntityFrameworkCore
{
public class MySqlEntityFrameworkCoreSpanMetadataProvider : IEntityFrameworkCoreSpanMetadataProvider
{
private readonly IPeerFormatter _peerFormatter;

public MySqlEntityFrameworkCoreSpanMetadataProvider(IPeerFormatter peerFormatter)
{
_peerFormatter = peerFormatter;
}

public StringOrIntValue Component { get; } = Common.Components.POMELO_ENTITYFRAMEWORKCORE_MYSQL;

public bool Match(DbConnection connection)
Expand All @@ -32,7 +40,7 @@ public bool Match(DbConnection connection)

public string GetPeer(DbConnection connection)
{
return connection.DataSource;
return _peerFormatter.GetDbPeer(connection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@
*/

using SkyApm.Common;
using SkyApm.Tracing;
using System.Data.Common;

namespace SkyApm.Diagnostics.EntityFrameworkCore
{
public class SqliteEntityFrameworkCoreSpanMetadataProvider : IEntityFrameworkCoreSpanMetadataProvider
{
private readonly IPeerFormatter _peerFormatter;

public SqliteEntityFrameworkCoreSpanMetadataProvider(IPeerFormatter peerFormatter)
{
_peerFormatter = peerFormatter;
}

public StringOrIntValue Component { get; } = Common.Components.ENTITYFRAMEWORKCORE_SQLITE;

public bool Match(DbConnection connection)
Expand All @@ -32,18 +40,13 @@ public bool Match(DbConnection connection)

public string GetPeer(DbConnection connection)
{
string dataSource;
switch (connection.DataSource)
{
case "":
dataSource = "sqlite:memory:db";
break;
return "sqlite:memory:db";
default:
dataSource = connection.DataSource;
break;
return _peerFormatter.GetDbPeer(connection);
}

return $"{dataSource}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ public class SmartSqlTracingDiagnosticProcessor : ITracingDiagnosticProcessor
private readonly ITracingContext _tracingContext;
private readonly ILocalSegmentContextAccessor _localSegmentContextAccessor;
private readonly TracingConfig _tracingConfig;
private readonly IPeerFormatter _peerFormatter;

public SmartSqlTracingDiagnosticProcessor(ITracingContext tracingContext,
ILocalSegmentContextAccessor localSegmentContextAccessor, IConfigAccessor configAccessor)
ILocalSegmentContextAccessor localSegmentContextAccessor, IConfigAccessor configAccessor,
IPeerFormatter peerFormatter)
{
_tracingContext = tracingContext;
_localSegmentContextAccessor = localSegmentContextAccessor;
_tracingConfig = configAccessor.Get<TracingConfig>();
_peerFormatter = peerFormatter;
}
private void AddConnectionTag(SegmentContext context, DbConnection dbConnection)
{
Expand All @@ -53,7 +56,7 @@ private void AddConnectionTag(SegmentContext context, DbConnection dbConnection)
}
if (dbConnection.DataSource != null)
{
context.Span.Peer = new Common.StringOrIntValue(dbConnection.DataSource);
context.Span.Peer = _peerFormatter.GetDbPeer(dbConnection);
}
if (dbConnection.Database != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ public class SqlClientTracingDiagnosticProcessor : ITracingDiagnosticProcessor
private readonly ITracingContext _tracingContext;
private readonly IExitSegmentContextAccessor _contextAccessor;
private readonly TracingConfig _tracingConfig;
private readonly IPeerFormatter _peerFormatter;

public SqlClientTracingDiagnosticProcessor(ITracingContext tracingContext,
IExitSegmentContextAccessor contextAccessor, IConfigAccessor configAccessor)
IExitSegmentContextAccessor contextAccessor, IConfigAccessor configAccessor,
IPeerFormatter peerFormatter)
{
_tracingContext = tracingContext;
_contextAccessor = contextAccessor;
_tracingConfig = configAccessor.Get<TracingConfig>();
_peerFormatter = peerFormatter;
}


Expand All @@ -52,7 +55,7 @@ private static string ResolveOperationName(DbCommand sqlCommand)
public void BeforeExecuteCommand([Property(Name = "Command")] DbCommand sqlCommand)
{
var context = _tracingContext.CreateExitSegmentContext(ResolveOperationName(sqlCommand),
sqlCommand.Connection.DataSource);
_peerFormatter.GetDbPeer(sqlCommand.Connection));
context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.SQLCLIENT;
context.Span.AddTag(Common.Tags.DB_TYPE, "sql");
Expand Down
Loading