提交 51b9aff5 编写于 作者: S ShaoHans 提交者: Lemon

grpc skywalking feature optimize (#241)

* 追踪grpc服务调用

* update

* 1.fix bug:调用grpc服务,当grpcContext.Host为null时会导致peer为null,从而未能形成一条完整链路,故为grpcContext.Host设置一个默认值
2.代码优化
上级 16444d21
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
"QueueSize": 30000, "QueueSize": 30000,
"BatchSize": 3000, "BatchSize": 3000,
"gRPC": { "gRPC": {
"Servers": "172.17.168.234:11800", "Servers": "192.168.79.132:11800",
"Timeout": 10000, "Timeout": 10000,
"ConnectTimeout": 10000, "ConnectTimeout": 10000,
"ReportTimeout": 600000 "ReportTimeout": 600000
......
...@@ -14,14 +14,10 @@ namespace SkyApm.Sample.Backend.Services ...@@ -14,14 +14,10 @@ namespace SkyApm.Sample.Backend.Services
private readonly Greeter.GreeterClient _client; private readonly Greeter.GreeterClient _client;
public GreeterGrpcService(ClientDiagnosticInterceptor interceptor) public GreeterGrpcService(ClientDiagnosticInterceptor interceptor)
{ {
_client = new Greeter.GreeterClient(GetChannel(interceptor)); var target = "localhost:12345";
} var channel = new Channel(target, ChannelCredentials.Insecure);
private CallInvoker GetChannel(ClientDiagnosticInterceptor interceptor)
{
var channel = new Channel("localhost:12345", ChannelCredentials.Insecure);
var invoker = channel.Intercept(interceptor); var invoker = channel.Intercept(interceptor);
return invoker; _client = new Greeter.GreeterClient(invoker).WithHost(target);
} }
public string SayHello(string name) public string SayHello(string name)
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
"QueueSize": 30000, "QueueSize": 30000,
"BatchSize": 3000, "BatchSize": 3000,
"gRPC": { "gRPC": {
"Servers": "172.17.168.234:11800", "Servers": "192.168.79.132:11800",
"Timeout": 100000, "Timeout": 100000,
"ConnectTimeout": 100000, "ConnectTimeout": 100000,
"ReportTimeout": 600000 "ReportTimeout": 600000
......
...@@ -14,14 +14,10 @@ namespace SkyApm.Sample.Backend.Services ...@@ -14,14 +14,10 @@ namespace SkyApm.Sample.Backend.Services
private readonly Greeter.GreeterClient _client; private readonly Greeter.GreeterClient _client;
public GreeterGrpcService(ClientDiagnosticInterceptor interceptor) public GreeterGrpcService(ClientDiagnosticInterceptor interceptor)
{ {
_client = new Greeter.GreeterClient(GetChannel(interceptor)); var target = "localhost:12345";
} var channel = new Channel(target, ChannelCredentials.Insecure);
private CallInvoker GetChannel(ClientDiagnosticInterceptor interceptor)
{
var channel = new Channel("localhost:12345", ChannelCredentials.Insecure);
var invoker = channel.Intercept(interceptor); var invoker = channel.Intercept(interceptor);
return invoker; _client = new Greeter.GreeterClient(invoker).WithHost(target);
} }
public string SayHello(string name) public string SayHello(string name)
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
"QueueSize": 30000, "QueueSize": 30000,
"BatchSize": 3000, "BatchSize": 3000,
"gRPC": { "gRPC": {
"Servers": "172.17.168.234:11800", "Servers": "192.168.79.132:11800",
"Timeout": 100000, "Timeout": 100000,
"ConnectTimeout": 100000, "ConnectTimeout": 100000,
"ReportTimeout": 600000 "ReportTimeout": 600000
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
"QueueSize": 30000, "QueueSize": 30000,
"BatchSize": 3000, "BatchSize": 3000,
"gRPC": { "gRPC": {
"Servers": "172.17.168.234:11800", "Servers": "192.168.79.132:11800",
"Timeout": 100000, "Timeout": 100000,
"ConnectTimeout": 100000, "ConnectTimeout": 100000,
"ReportTimeout": 600000 "ReportTimeout": 600000
......
using Grpc.Core;
using Grpc.Core.Interceptors;
using GrpcGreeter;
using SkyApm.Diagnostics.Grpc.Server;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
namespace SkyApm.Sample.GrpcServer
{
public static class Extensions
{
public static IServiceProvider StartGrpcServer(this IServiceProvider provider)
{
var interceptor = provider.GetService<ServerDiagnosticInterceptor>();
var definition = Greeter.BindService(new GreeterImpl());
if (interceptor != null)
{
definition = definition.Intercept(interceptor);
}
int port = 12345;
Server server = new Server
{
Services = { definition },
Ports = { new ServerPort("localhost", port, ServerCredentials.Insecure) },
};
server.Start();
Console.WriteLine("Greeter server listening on port " + port);
return provider;
}
}
}
using Grpc.Core; using Grpc.Core;
using GrpcGreeter; using GrpcGreeter;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using SkyApm.Agent.GeneralHost; using SkyApm.Agent.GeneralHost;
using System; using System;
...@@ -13,7 +14,9 @@ namespace SkyApm.Sample.GrpcServer ...@@ -13,7 +14,9 @@ namespace SkyApm.Sample.GrpcServer
{ {
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
await CreateHostBuilder(args).Build().RunAsync(); var host = CreateHostBuilder(args).Build();
host.Services.StartGrpcServer();
await host.RunAsync();
} }
public static IHostBuilder CreateHostBuilder(string[] args) public static IHostBuilder CreateHostBuilder(string[] args)
...@@ -25,9 +28,7 @@ namespace SkyApm.Sample.GrpcServer ...@@ -25,9 +28,7 @@ namespace SkyApm.Sample.GrpcServer
.AddSkyAPM() .AddSkyAPM()
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
var startUp = new Startup(hostContext.Configuration); services.AddLogging();
var provider = startUp.ConfigureServices(services);
startUp.Use(provider);
}); });
} }
} }
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
"QueueSize": 30000, "QueueSize": 30000,
"BatchSize": 3000, "BatchSize": 3000,
"gRPC": { "gRPC": {
"Servers": "172.17.168.234:11800", "Servers": "192.168.79.132:11800",
"Timeout": 100000, "Timeout": 100000,
"ConnectTimeout": 100000, "ConnectTimeout": 100000,
"ReportTimeout": 600000 "ReportTimeout": 600000
......
...@@ -94,7 +94,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{ ...@@ -94,7 +94,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Core.Tests", "test\SkyApm.Core.Tests\SkyApm.Core.Tests.csproj", "{5E654407-E22F-4696-A33F-C4B372F547BD}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Core.Tests", "test\SkyApm.Core.Tests\SkyApm.Core.Tests.csproj", "{5E654407-E22F-4696-A33F-C4B372F547BD}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Diagnostics.Grpc", "src\SkyApm.Diagnostics.Grpc\SkyApm.Diagnostics.Grpc.csproj", "{8C389735-61CE-405C-972F-3790DF1E823E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Diagnostics.Grpc", "src\SkyApm.Diagnostics.Grpc\SkyApm.Diagnostics.Grpc.csproj", "{8C389735-61CE-405C-972F-3790DF1E823E}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "grpc", "grpc", "{53E9CEBA-2D11-41FD-AA60-0151A5442CC4}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "grpc", "grpc", "{53E9CEBA-2D11-41FD-AA60-0151A5442CC4}"
EndProject EndProject
......
...@@ -42,10 +42,18 @@ namespace SkyApm.Diagnostics.Grpc.Client ...@@ -42,10 +42,18 @@ namespace SkyApm.Diagnostics.Grpc.Client
var options = context.Options.WithHeaders(metadata); var options = context.Options.WithHeaders(metadata);
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options); context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options);
var response = continuation(request, context); var response = continuation(request, context);
var responseAsync = response.ResponseAsync.ContinueWith<TResponse>(r => var responseAsync = response.ResponseAsync.ContinueWith(r =>
{ {
_processor.EndRequest(); try
return r.Result; {
_processor.EndRequest();
return r.Result;
}
catch (Exception ex)
{
_processor.DiagnosticUnhandledException(ex);
throw ex;
}
}); });
return new AsyncUnaryCall<TResponse>(responseAsync, response.ResponseHeadersAsync, response.GetStatus, response.GetTrailers, response.Dispose); return new AsyncUnaryCall<TResponse>(responseAsync, response.ResponseHeadersAsync, response.GetStatus, response.GetTrailers, response.Dispose);
} }
...@@ -55,7 +63,5 @@ namespace SkyApm.Diagnostics.Grpc.Client ...@@ -55,7 +63,5 @@ namespace SkyApm.Diagnostics.Grpc.Client
throw ex; throw ex;
} }
} }
} }
} }
...@@ -23,9 +23,11 @@ namespace SkyApm.Diagnostics.Grpc.Client ...@@ -23,9 +23,11 @@ namespace SkyApm.Diagnostics.Grpc.Client
where TRequest : class where TRequest : class
where TResponse : class where TResponse : class
{ {
var host = grpcContext.Host; // 调用grpc方法时如果没有通过WithHost()方法指定grpc服务地址,则grpcContext.Host会为null,
// 但context.Span.Peer为null的时候无法形成一条完整的链路,故设置了默认值[::1]
var host = grpcContext.Host ?? "[::1]";
var carrierHeader = new GrpcCarrierHeaderCollection(grpcContext.Options.Headers); var carrierHeader = new GrpcCarrierHeaderCollection(grpcContext.Options.Headers);
var context = _tracingContext.CreateExitSegmentContext(grpcContext.Method.FullName, host, carrierHeader); var context = _tracingContext.CreateExitSegmentContext($"{host}{grpcContext.Method.FullName}", host, carrierHeader);
context.Span.SpanLayer = SpanLayer.RPC_FRAMEWORK; context.Span.SpanLayer = SpanLayer.RPC_FRAMEWORK;
context.Span.Component = Components.GRPC; context.Span.Component = Components.GRPC;
context.Span.Peer = new StringOrIntValue(host); context.Span.Peer = new StringOrIntValue(host);
......
...@@ -20,8 +20,7 @@ namespace SkyApm.Diagnostics.Grpc.Server ...@@ -20,8 +20,7 @@ namespace SkyApm.Diagnostics.Grpc.Server
public void BeginRequest(ServerCallContext grpcContext) public void BeginRequest(ServerCallContext grpcContext)
{ {
var context = _tracingContext.CreateEntrySegmentContext(grpcContext.Method, var context = _tracingContext.CreateEntrySegmentContext(grpcContext.Method, new GrpcCarrierHeaderCollection(grpcContext.RequestHeaders));
new GrpcCarrierHeaderCollection(grpcContext.RequestHeaders));
context.Span.SpanLayer = SpanLayer.RPC_FRAMEWORK; context.Span.SpanLayer = SpanLayer.RPC_FRAMEWORK;
context.Span.Component = Components.GRPC; context.Span.Component = Components.GRPC;
context.Span.Peer = new StringOrIntValue(grpcContext.Peer); context.Span.Peer = new StringOrIntValue(grpcContext.Peer);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册