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 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
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";
}
}
126 changes: 126 additions & 0 deletions src/SkyApm.Diagnostics.FreeRedis/FreeRedisPluginConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.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,122 @@
/*
* 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 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;
using System.Text.RegularExpressions;

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>();
}


#region Notice
[DiagnosticName(FreeRedis_Notice)]
public void Notice([Object] NoticeEventArgs eventData)
{
var ans = Analysis(eventData.Log);
if (ans == null) return;
var context = _tracingContext.CreateExitSegmentContext(ans[2], ans[0]);
context.Span.SpanLayer = SpanLayer.CACHE;
context.Span.Component = Components.Free_Redis;
context.Span.AddTag(Tags.CACHE_TYPE, "FreeRedis");
context.Span.AddTag(Tags.CACHE_OP, parseOperation(ans[2]));
context.Span.AddTag(Tags.CACHE_CMD, ans[1]);
context.Span.AddTag("result", ans[3]);
context.Span.AddTag("exec_time", ans[4]);
if (eventData?.Exception != null)
context.Span.ErrorOccurred(eventData.Exception, _tracingConfig);
_tracingContext.Release(context);
}
#endregion
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;
}
private static Regex anRex = new Regex("(.+)\\s>\\s(([A-Z]+)\\s*.*?)\\r\\n(.*?)\\r\\n\\((\\d+)ms\\)\\r\\n");
/// <summary>
/// Analysis
/// </summary>
/// <param name="cmd"></param>
/// <returns>[address,cmd,op,result,exectime]</returns>
public static string[] Analysis(string cmd)
{
if (!(cmd == null || !cmd.Contains(">")))
{
if (anRex.IsMatch(cmd))
{
var matchs = anRex.Match(cmd);
if (matchs.Success && matchs.Groups.Count > 5)
{
return new string[] {
matchs.Groups[1].Value,
matchs.Groups[2].Value,
matchs.Groups[3].Value,
matchs.Groups[4].Value,
matchs.Groups[5].Value,
};
}
}
}
return null;
}
}
}
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>
59 changes: 59 additions & 0 deletions src/SkyApm.Diagnostics.FreeRedis/SkyWalkingBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 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);
}
};
}
}
}