Skip to content

Commit

Permalink
Merge pull request #1 from SkyAPM/master
Browse files Browse the repository at this point in the history
add grpc skywalking (SkyAPM#236)
  • Loading branch information
ShaoHans committed Sep 11, 2019
2 parents 5d685bc + d8f684c commit 823a671
Show file tree
Hide file tree
Showing 41 changed files with 1,215 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sample/SkyApm.Sample.AspNet/skyapm.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"QueueSize": 30000,
"BatchSize": 3000,
"gRPC": {
"Servers": "localhost:11800",
"Servers": "172.17.168.234:11800",
"Timeout": 10000,
"ConnectTimeout": 10000,
"ReportTimeout": 600000
Expand Down
36 changes: 36 additions & 0 deletions sample/SkyApm.Sample.Backend/Controllers/GrpcController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SkyApm.Sample.Backend.Services;

namespace SkyApm.Sample.Backend.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class GrpcController : ControllerBase
{
private readonly GreeterGrpcService _greeter;

public GrpcController(GreeterGrpcService greeter)
{
_greeter = greeter;
}

// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new List<string> { "value1", "value2" };
}

[HttpGet("greeter")]
public async Task<IActionResult> SayHelloAsync(string name)
{
var reply = await _greeter.SayHelloAsync(name);
return Ok($"from backend grpc message{reply}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
},
"SkyWalking.Sample.Backend": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore"
Expand Down
51 changes: 51 additions & 0 deletions sample/SkyApm.Sample.Backend/Services/GreeterGrpcService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Grpc.Core;
using Grpc.Core.Interceptors;
using GrpcGreeter;
using SkyApm.Diagnostics.Grpc.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SkyApm.Sample.Backend.Services
{
public class GreeterGrpcService
{
private readonly Greeter.GreeterClient _client;
public GreeterGrpcService(ClientDiagnosticInterceptor interceptor)
{
_client = new Greeter.GreeterClient(GetChannel(interceptor));
}

private CallInvoker GetChannel(ClientDiagnosticInterceptor interceptor)
{
var channel = new Channel("localhost:12345", ChannelCredentials.Insecure);
var invoker = channel.Intercept(interceptor);
return invoker;
}

public string SayHello(string name)
{
var reply = _client.SayHello(new HelloRequest { Name = name });
return reply.Message;
}

public async Task<string> SayHelloAsync(string name)
{
var reply = await _client.SayHelloAsync(new HelloRequest { Name = name });
return reply.Message;
}

public string SayHelloWithException(string name)
{
var reply = _client.SayHelloWithException(new HelloRequest { Name = name });
return reply.Message;
}

public async Task<string> SayHelloWithExceptionAsync(string name)
{
var reply = await _client.SayHelloWithExceptionAsync(new HelloRequest { Name = name });
return reply.Message;
}
}
}
3 changes: 2 additions & 1 deletion sample/SkyApm.Sample.Backend/SkyApm.Sample.Backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
<ItemGroup>
<ProjectReference Include="..\..\src\SkyApm.Agent.AspNetCore\SkyApm.Agent.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\SkyApm.Diagnostics.EntityFrameworkCore.Sqlite\SkyApm.Diagnostics.EntityFrameworkCore.Sqlite.csproj" />
<ProjectReference Include="..\grpc\SkyApm.Sample.GrpcProto\SkyApm.Sample.GrpcProto.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite">
<Version>2.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.9" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
Expand Down
4 changes: 4 additions & 0 deletions sample/SkyApm.Sample.Backend/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using SkyApm.Sample.Backend.Models;
using SkyApm.Sample.Backend.Sampling;
using SkyApm.Sample.Backend.Services;
using SkyApm.Tracing;

namespace SkyApm.Sample.Backend
Expand All @@ -31,6 +32,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddEntityFrameworkSqlite().AddDbContext<SampleDbContext>(c => c.UseSqlite(sqliteConnection));

services.AddSingleton<ISamplingInterceptor, CustomSamplingInterceptor>();

// DI grpc service
services.AddSingleton<GreeterGrpcService>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion sample/SkyApm.Sample.Backend/skyapm.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"QueueSize": 30000,
"BatchSize": 3000,
"gRPC": {
"Servers": "localhost:11800",
"Servers": "172.17.168.234:11800",
"Timeout": 100000,
"ConnectTimeout": 100000,
"ReportTimeout": 600000
Expand Down
32 changes: 32 additions & 0 deletions sample/SkyApm.Sample.Frontend/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SkyApm.Sample.Backend.Services;

namespace SkyApm.Sample.Frontend.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly GreeterGrpcService _greeter;

public ValuesController(GreeterGrpcService greeter)
{
_greeter = greeter;
}

// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
Expand All @@ -25,5 +34,28 @@ public async Task<string> Get(int id)
client.GetAsync("http://localhost:5002/api/delay/200"));
return await client.GetStringAsync("http://localhost:5002/api/delay/100");
}

[HttpGet("greeter")]
public async Task<IActionResult> SayHelloAsync(string name)
{
var content = new StringBuilder();
var message = await _greeter.SayHelloAsync(name);
content.AppendLine($"from frontend grpc message:{message}");

var response = await new HttpClient().GetStringAsync("http://localhost:5002/api/values");
content.AppendLine($"from frontend httpclient message:{response}");

response = await new HttpClient().GetStringAsync($"http://localhost:5002/api/grpc/greeter?name={name}");
content.AppendLine(response);

return Ok(content);
}

[HttpGet("greeter/exception")]
public async Task<IActionResult> SayHelloWithExceptionAsync(string name)
{
var message = await _greeter.SayHelloWithExceptionAsync(name);
return Ok(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
},
"SkyWalking.Sample.Frontend": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore"
Expand Down
51 changes: 51 additions & 0 deletions sample/SkyApm.Sample.Frontend/Services/GreeterGrpcService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Grpc.Core;
using Grpc.Core.Interceptors;
using GrpcGreeter;
using SkyApm.Diagnostics.Grpc.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SkyApm.Sample.Backend.Services
{
public class GreeterGrpcService
{
private readonly Greeter.GreeterClient _client;
public GreeterGrpcService(ClientDiagnosticInterceptor interceptor)
{
_client = new Greeter.GreeterClient(GetChannel(interceptor));
}

private CallInvoker GetChannel(ClientDiagnosticInterceptor interceptor)
{
var channel = new Channel("localhost:12345", ChannelCredentials.Insecure);
var invoker = channel.Intercept(interceptor);
return invoker;
}

public string SayHello(string name)
{
var reply = _client.SayHello(new HelloRequest { Name = name });
return reply.Message;
}

public async Task<string> SayHelloAsync(string name)
{
var reply = await _client.SayHelloAsync(new HelloRequest { Name = name });
return reply.Message;
}

public string SayHelloWithException(string name)
{
var reply = _client.SayHelloWithException(new HelloRequest { Name = name });
return reply.Message;
}

public async Task<string> SayHelloWithExceptionAsync(string name)
{
var reply = await _client.SayHelloWithExceptionAsync(new HelloRequest { Name = name });
return reply.Message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\SkyApm.Agent.AspNetCore\SkyApm.Agent.AspNetCore.csproj" />
<ProjectReference Include="..\grpc\SkyApm.Sample.GrpcProto\SkyApm.Sample.GrpcProto.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="logs" />
Expand Down
4 changes: 4 additions & 0 deletions sample/SkyApm.Sample.Frontend/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SkyApm.Sample.Backend.Services;

namespace SkyApm.Sample.Frontend
{
Expand All @@ -18,6 +19,9 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

// DI grpc service
services.AddSingleton<GreeterGrpcService>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
6 changes: 3 additions & 3 deletions sample/SkyApm.Sample.Frontend/skyapm.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"QueueSize": 30000,
"BatchSize": 3000,
"gRPC": {
"Servers": "localhost:11800",
"Timeout": 10000,
"ConnectTimeout": 10000,
"Servers": "172.17.168.234:11800",
"Timeout": 100000,
"ConnectTimeout": 100000,
"ReportTimeout": 600000
}
}
Expand Down
2 changes: 1 addition & 1 deletion sample/SkyApm.Sample.GeneralHost/skyapm.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"QueueSize": 30000,
"BatchSize": 3000,
"gRPC": {
"Servers": "localhost:11800",
"Servers": "172.17.168.234:11800",
"Timeout": 100000,
"ConnectTimeout": 100000,
"ReportTimeout": 600000
Expand Down

0 comments on commit 823a671

Please sign in to comment.