Skip to content

Commit

Permalink
fix code smell (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyun0112 authored May 1, 2022
1 parent 85e05cd commit d967fe3
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace SkyApm.Diagnostics
{
public class AnonymousObject : ParameterBinder
public class AnonymousObjectAttribute : ParameterBinderAttribute
{
public override object Resolve(object value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

namespace SkyApm.Diagnostics
{
public class DiagnosticName :Attribute
public class DiagnosticNameAttribute :Attribute
{
public string Name { get; }

public DiagnosticName(string name)
public DiagnosticNameAttribute(string name)
{
Name = name;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SkyApm.Core/Diagnostics/ObjectAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace SkyApm.Diagnostics
{
public class ObjectAttribute : ParameterBinder
public class ObjectAttribute : ParameterBinderAttribute
{
public Type TargetType { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace SkyApm.Diagnostics
{
public abstract class ParameterBinder : Attribute, IParameterResolver
public abstract class ParameterBinderAttribute : Attribute, IParameterResolver
{
public abstract object Resolve(object value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SkyApm.Core/Diagnostics/PropertyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace SkyApm.Diagnostics
{
public class PropertyAttribute : ParameterBinder
public class PropertyAttribute : ParameterBinderAttribute
{
public string Name { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/SkyApm.Core/Diagnostics/TracingDiagnosticMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static IEnumerable<IParameterResolver> GetParameterResolvers(MethodInfo
{
foreach (var parameter in methodInfo.GetParameters())
{
var binder = parameter.GetCustomAttribute<ParameterBinder>();
var binder = parameter.GetCustomAttribute<ParameterBinderAttribute>();
if (binder != null)
{
if(binder is ObjectAttribute objectBinder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public TracingDiagnosticMethodCollection(ITracingDiagnosticProcessor tracingDiag
_methods = new List<TracingDiagnosticMethod>();
foreach (var method in tracingDiagnosticProcessor.GetType().GetMethods())
{
var diagnosticName = method.GetCustomAttribute<DiagnosticName>();
var diagnosticName = method.GetCustomAttribute<DiagnosticNameAttribute>();
if(diagnosticName==null)
continue;
_methods.Add(new TracingDiagnosticMethod(tracingDiagnosticProcessor, method, diagnosticName.Name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void SyncStructureAfter([Object] SyncStructureAfterEventArgs eventData)
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
if (string.IsNullOrEmpty(eventData.Sql) == false)
if (!string.IsNullOrEmpty(eventData.Sql))
context.Span.AddTag(Common.Tags.DB_STATEMENT, eventData.Sql);
if (eventData?.Exception != null)
context.Span.ErrorOccurred(eventData.Exception, _tracingConfig);
Expand All @@ -127,7 +127,7 @@ public void CommandAfter([Object] CommandAfterEventArgs eventData)
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
if (string.IsNullOrEmpty(eventData.Log) == false)
if (!string.IsNullOrEmpty(eventData.Log))
context.Span.AddTag(Common.Tags.DB_STATEMENT, eventData.Log);
if (eventData?.Exception != null)
context.Span.ErrorOccurred(eventData.Exception, _tracingConfig);
Expand All @@ -149,7 +149,7 @@ public void TraceAfterUnitOfWork([Object] TraceAfterEventArgs eventData)
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
if (string.IsNullOrEmpty(eventData.Remark) == false)
if (!string.IsNullOrEmpty(eventData.Remark))
context.Span.AddTag(Common.Tags.DB_STATEMENT, eventData.Remark);
if (eventData?.Exception != null)
context.Span.ErrorOccurred(eventData.Exception, _tracingConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TR
catch (Exception ex)
{
_processor.DiagnosticUnhandledException(ex);
throw ex;
throw;
}
});
return new AsyncUnaryCall<TResponse>(responseAsync, response.ResponseHeadersAsync, response.GetStatus, response.GetTrailers, response.Dispose);
Expand Down Expand Up @@ -101,7 +101,7 @@ public T Call<T, TRequest, TResponse>(ClientInterceptorContext<TRequest, TRespon
catch (Exception ex)
{
_processor.DiagnosticUnhandledException(ex);
throw ex;
throw;
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/SkyApm.Diagnostics.Grpc/Server/ServerDiagnosticInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,37 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TR
{
_processor.BeginRequest(context);

return await Handler<TRequest, TResponse>(context, async () =>
return await Handler(context, async () =>
{
return await continuation(request, context);
});
}

public override async Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream, ServerCallContext context, ClientStreamingServerMethod<TRequest, TResponse> continuation)
{
return await Handler<TRequest, TResponse>(context, async () =>
return await Handler(context, async () =>
{
return await continuation(requestStream, context);
});
}

public override async Task ServerStreamingServerHandler<TRequest, TResponse>(TRequest request, IServerStreamWriter<TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod<TRequest, TResponse> continuation)
{
await Handler<TRequest>(context, async () =>
await Handler(context, async () =>
{
await continuation(request, responseStream, context);
});
}

public override async Task DuplexStreamingServerHandler<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream, IServerStreamWriter<TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod<TRequest, TResponse> continuation)
{
await Handler<TRequest>(context, async () =>
await Handler(context, async () =>
{
await continuation(requestStream, responseStream, context);
});
}

private async Task Handler<TRequest>(ServerCallContext context, Func<Task> func)
where TRequest : class
private async Task Handler(ServerCallContext context, Func<Task> func)
{
_processor.BeginRequest(context);
try
Expand All @@ -78,12 +77,11 @@ private async Task Handler<TRequest>(ServerCallContext context, Func<Task> func)
catch (Exception ex)
{
_processor.DiagnosticUnhandledException(ex);
throw ex;
throw;
}
}

private async Task<TResponse> Handler<TRequest, TResponse>(ServerCallContext context, Func<Task<TResponse>> func)
where TRequest : class
private async Task<TResponse> Handler<TResponse>(ServerCallContext context, Func<Task<TResponse>> func)
where TResponse : class
{
_processor.BeginRequest(context);
Expand All @@ -96,7 +94,7 @@ private async Task<TResponse> Handler<TRequest, TResponse>(ServerCallContext con
catch (Exception ex)
{
_processor.DiagnosticUnhandledException(ex);
throw ex;
throw;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace SkyApm.Diagnostics.MongoDB
{
public class DiagnosticsActivityEventSubscriber : IEventSubscriber
{
public static DiagnosticSource diagnosticSource = new DiagnosticListener("MongoSourceListener");
private static DiagnosticSource diagnosticSource = new DiagnosticListener("MongoSourceListener");
internal static readonly AssemblyName AssemblyName = typeof(DiagnosticsActivityEventSubscriber).Assembly.GetName();
internal static readonly string ActivitySourceName = AssemblyName.Name;
internal static readonly Version Version = AssemblyName.Version;
Expand Down Expand Up @@ -71,10 +71,7 @@ private void Handle(CommandStartedEvent @event)
return;
}
var activity = new Activity("MongoActivity");
if (activity == null)
{
return;
}

_activityMap.TryAdd(@event.RequestId, activity);
diagnosticSource.StartActivity(activity, @event);
}
Expand Down

0 comments on commit d967fe3

Please sign in to comment.