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

Add FreeRedis Diagnostics #547

Merged
merged 4 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 9 additions & 2 deletions skyapm-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Utilities.StaticAcce
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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.PeerFormatters.MySqlConnector", "src\SkyApm.PeerFormatters.MySqlConnector\SkyApm.PeerFormatters.MySqlConnector.csproj", "{2A313B7E-CC41-4556-8055-D87266C398BF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Diagnostics.FreeRedis", "src\SkyApm.Diagnostics.FreeRedis\SkyApm.Diagnostics.FreeRedis.csproj", "{829F7955-3138-4CA2-8AA3-3E4D72C97599}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -281,6 +283,10 @@ Global
{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
{829F7955-3138-4CA2-8AA3-3E4D72C97599}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{829F7955-3138-4CA2-8AA3-3E4D72C97599}.Debug|Any CPU.Build.0 = Debug|Any CPU
{829F7955-3138-4CA2-8AA3-3E4D72C97599}.Release|Any CPU.ActiveCfg = Release|Any CPU
{829F7955-3138-4CA2-8AA3-3E4D72C97599}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -334,6 +340,7 @@ Global
{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}
{829F7955-3138-4CA2-8AA3-3E4D72C97599} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583}
Expand Down
2 changes: 2 additions & 0 deletions src/SkyApm.Abstractions/Common/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static class Components

public static readonly StringOrIntValue Free_SQL = new StringOrIntValue(3017, "FreeSql");

public static readonly StringOrIntValue Free_Redis = new StringOrIntValue(3018, "FreeRedis");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guochen2 Please register 3018 ID in the main repository.


public static readonly StringOrIntValue GRPC = new StringOrIntValue(23, "GRPC");

public static readonly StringOrIntValue MongoDBCLIENT = new StringOrIntValue(42, "MongoDB.Driver");
Expand Down
6 changes: 6 additions & 0 deletions src/SkyApm.Abstractions/Common/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@ public static class Tags
public static readonly string GRPC_METHOD_NAME = "grpc.method";

public static readonly string GRPC_STATUS = "grpc.status";

public static readonly string CACHE_TYPE = "cache.type";

public static readonly string CACHE_OP = "cache.op";

public static readonly string CACHE_CMD = "cache.cmd";
}
}
108 changes: 108 additions & 0 deletions src/SkyApm.Diagnostics.FreeRedis/FreeRedisPluginConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All code files need to add the SkyAPM copyright header. Please refer to https://github.com/SkyAPM/SkyAPM-dotnet/blob/main/src/SkyApm.Abstractions/ITracingDiagnosticProcessor.cs#L1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All code files need to add the SkyAPM copyright header. Please refer to https://github.com/SkyAPM/SkyAPM-dotnet/blob/main/src/SkyApm.Abstractions/ITracingDiagnosticProcessor.cs#L1

ok

using System.Collections.Generic;
using System.Text;

namespace SkyApm.Diagnostics.FreeRedis
{
internal static class FreeRedisPluginConfig
{
internal static HashSet<String> OPERATION_MAPPING_WRITE = new HashSet<string>() {
"GETSET",
"SET",
"SETBIT",
"SETEX ",
"SETNX ",
"SETRANGE",
"STRLEN ",
"MSET",
"MSETNX ",
"PSETEX",
"INCR ",
"INCRBY ",
"INCRBYFLOAT",
"DECR ",
"DECRBY ",
"APPEND ",
"HMSET",
"HSET",
"HSETNX ",
"HINCRBY",
"HINCRBYFLOAT",
"HDEL",
"RPOPLPUSH",
"RPUSH",
"RPUSHX",
"LPUSH",
"LPUSHX",
"LREM",
"LTRIM",
"LSET",
"BRPOPLPUSH",
"LINSERT",
"SADD",
"SDIFF",
"SDIFFSTORE",
"SINTERSTORE",
"SISMEMBER",
"SREM",
"SUNION",
"SUNIONSTORE",
"SINTER",
"ZADD",
"ZINCRBY",
"ZINTERSTORE",
"ZRANGE",
"ZRANGEBYLEX",
"ZRANGEBYSCORE",
"ZRANK",
"ZREM",
"ZREMRANGEBYLEX",
"ZREMRANGEBYRANK",
"ZREMRANGEBYSCORE",
"ZREVRANGE",
"ZREVRANGEBYSCORE",
"ZREVRANK",
"ZUNIONSTORE",
"XADD",
"XDEL",
"DEL",
"XTRIM"
};
internal static HashSet<String> OPERATION_MAPPING_READ = new HashSet<string>(){
"GET",
"GETRANGE",
"GETBIT ",
"MGET",
"HVALS",
"HKEYS",
"HLEN",
"HEXISTS",
"HGET",
"HGETALL",
"HMGET",
"BLPOP",
"BRPOP",
"LINDEX",
"LLEN",
"LPOP",
"LRANGE",
"RPOP",
"SCARD",
"SRANDMEMBER",
"SPOP",
"SSCAN",
"SMOVE",
"ZLEXCOUNT",
"ZSCORE",
"ZSCAN",
"ZCARD",
"ZCOUNT",
"XGET",
"GET",
"XREAD",
"XLEN",
"XRANGE",
"XREVRANGE"
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using FreeRedis;
using SkyApm;
using SkyApm.Common;
using SkyApm.Config;
using SkyApm.Diagnostics;
using SkyApm.Tracing;
using SkyApm.Tracing.Segments;
using System;
using System.Linq;
using System.Text;

namespace SkyApm.Diagnostics.FreeRedis
{


/// <summary>
/// FreeRedisTracingDiagnosticProcessor
/// </summary>
public class FreeRedisTracingDiagnosticProcessor : ITracingDiagnosticProcessor
{

#region Const

public const string ComponentName = "FreeRedis";

public const string FreeRedis_Notice = "FreeRedis.Notice";

#endregion

public string ListenerName => "FreeRedisDiagnosticListener";

private readonly ITracingContext _tracingContext;
private readonly ILocalSegmentContextAccessor _localSegmentContextAccessor;
private readonly TracingConfig _tracingConfig;
public FreeRedisTracingDiagnosticProcessor(ITracingContext tracingContext,
ILocalSegmentContextAccessor localSegmentContextAccessor, IConfigAccessor configAccessor)
{
_tracingContext = tracingContext;
_localSegmentContextAccessor = localSegmentContextAccessor;
_tracingConfig = configAccessor.Get<TracingConfig>();
}

private SegmentContext CreateFreeRedisLocalSegmentContext(string operation)
{
var context = _tracingContext.CreateLocalSegmentContext(operation);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redis calls should use exit segment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redis calls should use exit segment

I don't understand what you mean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateExitSegmentContext.

context.Span.SpanLayer = SpanLayer.CACHE;
context.Span.Component = Components.Free_Redis;
context.Span.AddTag(Tags.CACHE_TYPE, "FreeRedis");
return context;
}

#region Notice
[DiagnosticName(FreeRedis_Notice)]
public void Notice([Object] NoticeEventArgs eventData)
{
var context = CreateFreeRedisLocalSegmentContext(eventData.NoticeType.ToString());
context.Span.Peer = AnalysisDomain(eventData.Log);
string cmd = AnalysisCmd(eventData.Log);
context.Span.AddTag(Tags.CACHE_OP, parseOperation(cmd));
context.Span.AddTag(Tags.CACHE_CMD, eventData.Log);
if (eventData?.Exception != null)
context.Span.ErrorOccurred(eventData.Exception, _tracingConfig);
_tracingContext.Release(context);
}
#endregion
/// <summary>
/// 解析读写
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove CN

/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
private static String parseOperation(String cmd)
{
if (FreeRedisPluginConfig.OPERATION_MAPPING_READ.Contains(cmd))
{
return "read";
}
if (FreeRedisPluginConfig.OPERATION_MAPPING_WRITE.Contains(cmd))
{
return "write";
}
return string.Empty;
}
/// <summary>
/// 解析命令
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
private static string AnalysisCmd(string cmd)
{
if (cmd == null) return string.Empty;
cmd.Replace("\r\n", " ");
char[] cmds = cmd.ToArray();
StringBuilder stringBuilder = new StringBuilder();
bool beginAn = false;
for (var i = 0; i < cmds.Length; i++)
{
if (cmds[i] == '>')
{
beginAn = true;
}
else if (beginAn && ((cmds[i] >= 'a' && cmds[i] <= 'z') || (cmds[i] >= 'A' && cmds[i] <= 'Z')))
{
stringBuilder.Append(cmds[i]);
}
else if (stringBuilder.Length > 0)
{
return stringBuilder.ToString();
}
}
return string.Empty;
}
/// <summary>
/// 解析地址
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
private static string AnalysisDomain(string cmd)
{
if (cmd == null || !cmd.Contains(">")) return string.Empty;
return cmd.Substring(0, cmd.IndexOf(">")).Trim();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<AssemblyName>$(PackagePrefix).Diagnostics.FreeRedis</AssemblyName>
<PackageId>$(PackagePrefix).Diagnostics.FreeRedis</PackageId>
<PackageTags>SkyWalking;APM;Diagnostics;FreeRedis</PackageTags>
<Description>SkyApm.Diagnostics.FreeRedis notifies of FreeRedis requests.</Description>
<RootNamespace>SkyApm.Diagnostics.FreeRedis</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FreeRedis" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SkyApm.Utilities.DependencyInjection\SkyApm.Utilities.DependencyInjection.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions src/SkyApm.Diagnostics.FreeRedis/SkyWalkingBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FreeRedis;
using Microsoft.Extensions.DependencyInjection;
using SkyApm;
using SkyApm.Utilities.DependencyInjection;
using System;
using System.Diagnostics;

namespace SkyApm.Diagnostics.FreeRedis
{

public static class SkyWalkingBuilderExtensions
{
public static readonly DiagnosticListener dl = new DiagnosticListener("FreeRedisDiagnosticListener");

public static SkyApmExtensions AddFreeRedis(this SkyApmExtensions extensions, RedisClient redisClient, bool includeAuth = false)
{
if (extensions == null)
{
throw new ArgumentNullException(nameof(extensions));
}
extensions.Services.AddSingleton<ITracingDiagnosticProcessor, FreeRedisTracingDiagnosticProcessor>();
if (redisClient != null)
{
ConfigAop(redisClient, includeAuth);
}
return extensions;
}


public static void ConfigAop(RedisClient redisClient, bool includeAuth = false)
{
redisClient.Notice += (s, e) =>
{
if (!string.IsNullOrWhiteSpace(e.Log) && (includeAuth || (!e.Log.Contains("> AUTH "))))
{
dl.Write(FreeRedisTracingDiagnosticProcessor.FreeRedis_Notice, e);
}
};
}
}
}